From d9a928bc579904c7c07570da0886aaabc646b613 Mon Sep 17 00:00:00 2001 From: Jonathan <42983653+jonnyz32@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:36:08 -0400 Subject: [PATCH] Rework logging (#150) Co-authored-by: Sanjula Ganepola <32170854+SanjulaGanepola@users.noreply.github.com> --- docs/_sidebar.md | 1 + docs/config/logging.md | 22 +++++++++++++++ ile/src/debug.cpp | 49 +++++++++++++++++++++++++++------ ile/src/handler.cpp | 38 ++++++++++++------------- ile/src/manzan.h | 5 +++- ile/src/pub_db2.sqlc | 6 ++-- ile/src/pub_json.cpp | 12 ++++---- scripts/buildAll.sh | 0 scripts/buildAllAndTestE2e.sh | 0 scripts/buildCamel.sh | 0 scripts/buildCamelAndTestE2e.sh | 0 scripts/buildIle.sh | 0 scripts/deploy.sh | 0 scripts/testE2e.sh | 0 scripts/testIle.sh | 0 15 files changed, 96 insertions(+), 37 deletions(-) create mode 100644 docs/config/logging.md mode change 100644 => 100755 scripts/buildAll.sh mode change 100644 => 100755 scripts/buildAllAndTestE2e.sh mode change 100644 => 100755 scripts/buildCamel.sh mode change 100644 => 100755 scripts/buildCamelAndTestE2e.sh mode change 100644 => 100755 scripts/buildIle.sh mode change 100644 => 100755 scripts/deploy.sh mode change 100644 => 100755 scripts/testE2e.sh mode change 100644 => 100755 scripts/testIle.sh diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 1b6d4a8..3a451fd 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -5,6 +5,7 @@ * [Manzan](config/app.md) * [Data Sources](config/data.md) * [Destinations](config/dests.md) + * [Logging](config/logging.md) * Examples * [File](config/examples/file.md) * [Twilio](config/examples/twilio.md) diff --git a/docs/config/logging.md b/docs/config/logging.md new file mode 100644 index 0000000..537564d --- /dev/null +++ b/docs/config/logging.md @@ -0,0 +1,22 @@ +## Logging + +### Log Levels + +Manzan has three options for available logging levels which can be configured via the `MANZAN_DEBUG_LEVEL` system environment variable. + +* `MANZAN_DEBUG_LEVEL = 0`: Do not log any messages +* `MANZAN_DEBUG_LEVEL = 1`: Log only errors +* `MANZAN_DEBUG_LEVEL = 2`: Log errors and warnings +* `MANZAN_DEBUG_LEVEL = 3`: Log errors, warnings, and info + +Logs will be written to `/tmp/manzan_debug.txt`. + +### Setting the Log Level + +The `MANZAN_DEBUG_LEVEL` environment variable can be set with the `ADDENVVAR` command. For example, to log errors, warnings, and info messages use: + +```cl +ADDENVVAR ENVVAR(MANZAN_DEBUG_LEVEL) VALUE(3) LEVEL(*SYS) REPLACE(*YES) +``` + +After which you will need to run `ENDTCPSVR *SSHD` and `STRTCPSVR *SSHD`. Then restart your SSH session for the new environment variable to take effect. \ No newline at end of file diff --git a/ile/src/debug.cpp b/ile/src/debug.cpp index b5f6a85..0ee94a7 100644 --- a/ile/src/debug.cpp +++ b/ile/src/debug.cpp @@ -4,6 +4,7 @@ #include #include #include +#include FILE *debug_fd = NULL; @@ -25,16 +26,48 @@ extern "C" void ENDDBG() #endif } -extern "C" void DEBUG(const char *format, ...) -{ + + +// Helper function to handle the common logic +extern "C" void DEBUG_LOG(const char *level, const char *label, const char *format, va_list args) { +#ifdef DEBUG_ENABLED + char *env_level = getenv("MANZAN_DEBUG_LEVEL"); + if (env_level == NULL){ + env_level = "2"; // Show errors and warnings only + } + if (strcmp(env_level, level) >= 0 && debug_fd != NULL) { + fprintf(debug_fd, "%s ", label); // Prepend the log level + vfprintf(debug_fd, format, args); + fflush(debug_fd); + } +#endif +} + +// Wrapper functions for different debug levels + +extern "C" void DEBUG_INFO(const char *format, ...) { #ifdef DEBUG_ENABLED - if (NULL != debug_fd) - { va_list args; va_start(args, format); - vfprintf(debug_fd, format, args); + DEBUG_LOG("3", "[INFO]", format, args); + va_end(args); +#endif +} + +extern "C" void DEBUG_WARNING(const char *format, ...) { +#ifdef DEBUG_ENABLED + va_list args; + va_start(args, format); + DEBUG_LOG("2", "[WARNING]", format, args); va_end(args); - fflush(debug_fd); - } #endif -} \ No newline at end of file +} + +extern "C" void DEBUG_ERROR(const char *format, ...) { +#ifdef DEBUG_ENABLED + va_list args; + va_start(args, format); + DEBUG_LOG("1", "ERROR", format, args); + va_end(args); +#endif +} diff --git a/ile/src/handler.cpp b/ile/src/handler.cpp index 16b2dcd..092fd2e 100644 --- a/ile/src/handler.cpp +++ b/ile/src/handler.cpp @@ -93,18 +93,18 @@ int main(int _argc, char **argv) BUFSTRN(watch_option, argv[1], 10); BUFSTRN(session_id, argv[2], 10); - DEBUG("Watch program called. Watch option setting is '%s'\n", watch_option.c_str()); + DEBUG_INFO("Watch program called. Watch option setting is '%s'\n", watch_option.c_str()); publisher_info_set *publishers = conf_get_publisher_info(session_id.c_str()); int num_publishers = publishers->num_publishers; if (0 == num_publishers) { - DEBUG("No publishers found for watch option '%s' and session ID '%s'\n", watch_option.c_str(), session_id.c_str()); + DEBUG_ERROR("No publishers found for watch option '%s' and session ID '%s'\n", watch_option.c_str(), session_id.c_str()); ENDDBG(); return 0; } if (watch_option == "*MSGID") { - DEBUG("Handling message\n"); + DEBUG_INFO("Handling message\n"); msg_event_raw *msg_event = (msg_event_raw *)argv[4]; BUFSTR(msgid, msg_event->message_watched); BUFSTR(job_name, msg_event->job_name); @@ -113,7 +113,7 @@ int main(int _argc, char **argv) std::string job = job_number + "/" + user_name + "/" + job_name; BUFSTR(message_type, msg_event->message_type); std::string message_timestamp = get_iso8601_timestamp(msg_event->message_timestamp); - DEBUG("Timestamp is '%s'\n", message_timestamp.c_str()); + DEBUG_INFO("Timestamp is '%s'\n", message_timestamp.c_str()); int message_severity = msg_event->message_severity; BUFSTR(sending_usrprf, msg_event->sending_user_profile); BUFSTRN(sending_procedure_name, (char *)msg_event + msg_event->offset_send_procedure_name, msg_event->length_send_procedure_name); @@ -123,17 +123,17 @@ int main(int _argc, char **argv) int replacement_data_offset = msg_event->offset_replacement_data; int replacement_data_len = msg_event->length_replacement_data; - DEBUG("Replacement data offset is '%d'\n", replacement_data_offset); - DEBUG("REPLACEMENT DATA LENGTH IS '%d'\n", replacement_data_len); + DEBUG_INFO("Replacement data offset is '%d'\n", replacement_data_offset); + DEBUG_INFO("REPLACEMENT DATA LENGTH IS '%d'\n", replacement_data_len); char message_watched[8]; message_watched[7] = 0x00; memcpy(message_watched, msg_event->message_watched, 7); - DEBUG("MESSAGE WATCHED IS '%s'\n", message_watched); + DEBUG_INFO("MESSAGE WATCHED IS '%s'\n", message_watched); char qualified_msg_file[21]; qualified_msg_file[20] = 0x00; memcpy(qualified_msg_file, msg_event->message_file_name, 10); - memcpy(qualified_msg_file + 10, msg_event->message_file_library, 10); - DEBUG("MESSAGE FILE AND NAME IS '%s'\n", qualified_msg_file); + memcpy(qualified_msg_file+10, msg_event->message_file_library, 10); + DEBUG_INFO("MESSAGE FILE AND NAME IS '%s'\n", qualified_msg_file); char *replacement_data = (0 == replacement_data_len) ? (char *)"" : (((char *)msg_event) + replacement_data_offset); char *replacement_data_aligned = (char *)malloc(1 + replacement_data_len); memset(replacement_data_aligned, 0x00, 1 + replacement_data_len); @@ -144,14 +144,14 @@ int main(int _argc, char **argv) memset(msg_info_buf, 0x00, msg_info_buf_size); if (' ' == qualified_msg_file[0]) { - DEBUG("Message not from message file\n"); + DEBUG_INFO("Message not from message file\n"); strncpy(msg_info_buf->message, replacement_data_aligned, replacement_data_len); } else { char err_plc[64]; memset(err_plc, 0x00, sizeof(err_plc)); - DEBUG("About to format...\n"); + DEBUG_INFO("About to format...\n"); QMHRTVM( // 1 Message information Output Char(*) @@ -174,11 +174,11 @@ int main(int _argc, char **argv) "*NO ", // 10 Error code I/O Char(*) err_plc); - DEBUG("Done formatting \n"); - DEBUG("The full message is '%s'\n", msg_info_buf->message); + DEBUG_INFO("Done formatting \n"); + DEBUG_INFO("The full message is '%s'\n", msg_info_buf->message); } free(replacement_data_aligned); - DEBUG("About to publish...\n"); + DEBUG_INFO("About to publish...\n"); for (int i = 0; i < num_publishers; i++) { msg_publish_func func = publishers->array[i].msg_publish_func_ptr; @@ -194,15 +194,15 @@ int main(int _argc, char **argv) sending_program_name.c_str(), sending_module_name.c_str(), sending_procedure_name.c_str()); - DEBUG("Published\n"); + DEBUG_INFO("Published\n"); } free(msg_info_buf); memset(argv[3], ' ', 10); - DEBUG("DONE\n"); + DEBUG_INFO("DONE\n"); } else if (watch_option == "*LICLOG") { - DEBUG("Handling LIC log\n"); + DEBUG_INFO("Handling LIC log\n"); vlog_event_raw *lic_event = (vlog_event_raw *)argv[4]; BUFSTR(major_code, lic_event->lic_log_major_code); BUFSTR(minor_code, lic_event->lic_log_minor_code); @@ -245,7 +245,7 @@ int main(int _argc, char **argv) } else if (watch_option == "*PAL") { - DEBUG("Handling PAL Entry\n"); + DEBUG_INFO("Handling PAL Entry\n"); pal_event_raw *pal_event = (pal_event_raw *)argv[4]; BUFSTR(system_reference_code, pal_event->system_reference_code); BUFSTR(device_name, pal_event->device_name); @@ -293,7 +293,7 @@ int main(int _argc, char **argv) printf("Well, shit\n"); if (4 <= _argc) strncpy(argv[3], "*ERROR ", 10); - DEBUG("MCH exception happened!\n"); + DEBUG_ERROR("MCH exception happened!\n"); ENDDBG(); return 1; } \ No newline at end of file diff --git a/ile/src/manzan.h b/ile/src/manzan.h index 53b065b..14e0fa4 100644 --- a/ile/src/manzan.h +++ b/ile/src/manzan.h @@ -80,7 +80,10 @@ extern "C" #endif extern void STRDBG(); extern void ENDDBG(); - extern void DEBUG(const char *format, ...); + extern void DEBUG_INFO(const char *format, ...); + extern void DEBUG_WARNING(const char *format, ...); + extern void DEBUG_ERROR(const char *format, ...); + #define DEBUG_ENABLED 1 diff --git a/ile/src/pub_db2.sqlc b/ile/src/pub_db2.sqlc index 009ab82..288137d 100644 --- a/ile/src/pub_db2.sqlc +++ b/ile/src/pub_db2.sqlc @@ -16,12 +16,12 @@ void check_sql_error(int sqlcode, const char* sqlstate) { if (sqlcode != 0) { - DEBUG("SQL Code: %d\n", sqlcode); - DEBUG("SQL State: %s\n", sqlstate); + DEBUG_ERROR("SQL Code: %d\n", sqlcode); + DEBUG_ERROR("SQL State: %s\n", sqlstate); } else { - DEBUG("SQL statement executed succesfully!\n"); + DEBUG_INFO("SQL statement executed succesfully!\n"); } } diff --git a/ile/src/pub_json.cpp b/ile/src/pub_json.cpp index 4608453..5c035ec 100644 --- a/ile/src/pub_json.cpp +++ b/ile/src/pub_json.cpp @@ -29,7 +29,7 @@ int to_utf8(char *out, size_t out_len, const char *in) int rc = iconv(cd, &input, &inleft, &output, &outleft); if (rc == -1) { - DEBUG("Error in converting characters\n"); + DEBUG_ERROR("Error in converting characters\n"); return 9; } return iconv_close(cd); @@ -97,8 +97,8 @@ int json_publish(const char *_session_id, std::string &_json) char *utf8 = (char *)malloc(56 + _json.length() * 2); to_utf8(utf8, json_len, _json.c_str()); - DEBUG("Publishing JSON\n"); - DEBUG("%s\n", _json.c_str()); + DEBUG_INFO("Publishing JSON\n"); + DEBUG_INFO("%s\n", _json.c_str()); __attribute__((aligned(16))) char dtaq_key[11]; memset(dtaq_key, ' ', 11); @@ -108,16 +108,16 @@ int json_publish(const char *_session_id, std::string &_json) len2 += strlen(utf8); _DecimalT<3,0> keyLen = __D("10.0"); - DEBUG("About to call QSNDDTAQ\n"); + DEBUG_INFO("About to call QSNDDTAQ\n"); QSNDDTAQ("MANZANDTAQ", "MANZAN ", // TODO: How to properly resolve the library here? len2, utf8, keyLen, &dtaq_key); - DEBUG("About to free up stuff\n"); + DEBUG_INFO("About to free up stuff\n"); free(utf8); - DEBUG("Done publishing JSON\n"); + DEBUG_INFO("Done publishing JSON\n"); return 0; } diff --git a/scripts/buildAll.sh b/scripts/buildAll.sh old mode 100644 new mode 100755 diff --git a/scripts/buildAllAndTestE2e.sh b/scripts/buildAllAndTestE2e.sh old mode 100644 new mode 100755 diff --git a/scripts/buildCamel.sh b/scripts/buildCamel.sh old mode 100644 new mode 100755 diff --git a/scripts/buildCamelAndTestE2e.sh b/scripts/buildCamelAndTestE2e.sh old mode 100644 new mode 100755 diff --git a/scripts/buildIle.sh b/scripts/buildIle.sh old mode 100644 new mode 100755 diff --git a/scripts/deploy.sh b/scripts/deploy.sh old mode 100644 new mode 100755 diff --git a/scripts/testE2e.sh b/scripts/testE2e.sh old mode 100644 new mode 100755 diff --git a/scripts/testIle.sh b/scripts/testIle.sh old mode 100644 new mode 100755