From 34cade148b54206a4f5c58ca86f323cfaac45098 Mon Sep 17 00:00:00 2001 From: behanansaju <140800890+behanansaju@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:12:05 +0000 Subject: [PATCH] em_util_print function to print time and date along with module and severity --- build/agent/makefile | 7 +-- build/ctrl/makefile | 1 + src/em/config/em_configuration.cpp | 3 +- src/utils/util.c | 85 +++++++++++++++++------------- src/utils/util.h | 8 +-- 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/build/agent/makefile b/build/agent/makefile index c652e34..62d9601 100755 --- a/build/agent/makefile +++ b/build/agent/makefile @@ -50,9 +50,10 @@ LIBDIRS = \ LIBS = -lm -lpthread -ldl -lcjson -luuid -lssl -lcrypto -lwebconfig -lhebus GENERIC_SOURCES = $(ONEWIFI_HOME)/source/utils/collection.c \ - $(ONEWIFI_EM_SRC)/util_crypto/aes_siv.c \ - $(ONEWIFI_HOME)/lib/common/util.c \ - $(ONEWIFI_HOME)/source/platform/linux/bus.c \ + $(ONEWIFI_EM_SRC)/util_crypto/aes_siv.c \ + $(ONEWIFI_EM_SRC)/utils/util.c \ + $(ONEWIFI_HOME)/lib/common/util.c \ + $(ONEWIFI_HOME)/source/platform/linux/bus.c \ AGENT_SOURCES = $(wildcard $(ONEWIFI_EM_SRC)/em/*.cpp) \ $(wildcard $(ONEWIFI_EM_SRC)/em/config/*.cpp) \ diff --git a/build/ctrl/makefile b/build/ctrl/makefile index 1e463fc..c94078b 100755 --- a/build/ctrl/makefile +++ b/build/ctrl/makefile @@ -43,6 +43,7 @@ LIBS = -lm -lpthread -ldl -luuid -lcjson -lssl -lcrypto -lmysqlcppconn GENERIC_SOURCES = $(ONEWIFI_HOME)/source/utils/collection.c \ $(ONEWIFI_EM_SRC)/util_crypto/aes_siv.c \ + $(ONEWIFI_EM_SRC)/utils/util.c \ CTRL_SOURCES = $(wildcard $(ONEWIFI_EM_SRC)/em/*.cpp) \ $(wildcard $(ONEWIFI_EM_SRC)/em/config/*.cpp) \ diff --git a/src/em/config/em_configuration.cpp b/src/em/config/em_configuration.cpp index 8734e93..a8c0cc3 100644 --- a/src/em/config/em_configuration.cpp +++ b/src/em/config/em_configuration.cpp @@ -2770,6 +2770,7 @@ int em_configuration_t::handle_autoconfig_resp(unsigned char *buff, unsigned int return -1; } + em_util_print(EM_LOG_LVL_INFO, EM_AGENT, __func__, __LINE__, "autoconfig wsc m1 send success"); //log printf("%s:%d: autoconfig wsc m1 send success\n", __func__, __LINE__); set_state(em_state_agent_wsc_m2_pending); @@ -2961,7 +2962,7 @@ void em_configuration_t::handle_state_config_none() printf("%s:%d: failed, err:%d\n", __func__, __LINE__, errno); return; } - + em_util_print(EM_LOG_LVL_INFO, EM_AGENT, __func__, __LINE__, "autoconfig_search send successful"); //log printf("%s:%d: autoconfig_search send successful\n", __func__, __LINE__); set_state(em_state_agent_autoconfig_rsp_pending); diff --git a/src/utils/util.c b/src/utils/util.c index 1898c60..768cfd8 100644 --- a/src/utils/util.c +++ b/src/utils/util.c @@ -37,7 +37,7 @@ void delay(int seconds) { } while ((current_time - start_time) < seconds); // Loop until desired delay is achieved } -char *get_formatted_time(char *time) +char *get_formatted_time_em(char *time) { struct tm *tm_info; struct timeval tv_now; @@ -46,16 +46,16 @@ char *get_formatted_time(char *time) gettimeofday(&tv_now, NULL); tm_info = (struct tm *)localtime(&tv_now.tv_sec); - strftime(tmp, 128, "%y%m%d-%T", tm_info); + strftime(tmp, 128, "%m/%d/%y - %T", tm_info); snprintf(time, 128, "%s.%06lld", tmp, (long long)tv_now.tv_usec); return time; } - -void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const char *format, ...) +void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const char *func, int line, const char *format, ...) { char buff[256] = {0}; + char time_buff[128] = {0}; va_list list; FILE *fpg = NULL; #if defined(__ENABLE_PID__) && (__ENABLE_PID__) @@ -65,38 +65,33 @@ void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const char filename_dbg_enable[64]; char module_filename[32]; char filename[100]; + const char *severity; switch (module) { - case EM_AGENT: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emAgentDbg"); - snprintf(module_filename, sizeof(module_filename), "emAgent"); - break; - } - case EM_CTRL: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emCtrlDbg"); - snprintf(module_filename, sizeof(module_filename), "emCtrl"); - break; - } - case EM_MGR: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emMgrDbg"); - snprintf(module_filename, sizeof(module_filename), "emMgr"); - break; - } - case EM_DB: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emDbDbg"); - snprintf(module_filename, sizeof(module_filename), "emDb"); - break; - } - case EM_PROV: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emProvDbg"); - snprintf(module_filename, sizeof(module_filename), "emProv"); - break; - } - case EM_CONF: { - snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emConfDbg"); - snprintf(module_filename, sizeof(module_filename), "emConf"); - break; - } + case EM_AGENT: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emAgentDbg"); + snprintf(module_filename, sizeof(module_filename), "emAgent"); + break; + case EM_CTRL: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emCtrlDbg"); + snprintf(module_filename, sizeof(module_filename), "emCtrl"); + break; + case EM_MGR: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emMgrDbg"); + snprintf(module_filename, sizeof(module_filename), "emMgr"); + break; + case EM_DB: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emDbDbg"); + snprintf(module_filename, sizeof(module_filename), "emDb"); + break; + case EM_PROV: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emProvDbg"); + snprintf(module_filename, sizeof(module_filename), "emProv"); + break; + case EM_CONF: + snprintf(filename_dbg_enable, sizeof(filename_dbg_enable), LOG_PATH_PREFIX "emConfDbg"); + snprintf(module_filename, sizeof(module_filename), "emConf"); + break; default: return; } @@ -122,10 +117,25 @@ void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const return; } } - snprintf(&buff[0], sizeof(buff), "[%s] ", __progname ? __progname : ""); - get_formatted_time(&buff[strlen(buff)]); - fprintf(fpg, "%s ", buff); + switch (level) { + case EM_LOG_LVL_INFO: + severity = "INFO"; + break; + case EM_LOG_LVL_ERROR: + severity = "ERROR"; + break; + case EM_LOG_LVL_DEBUG: + severity = "DEBUG"; + break; + default: + severity = "UNKNOWN"; + break; + } + + get_formatted_time_em(time_buff); + snprintf(buff, sizeof(buff), "\n[%s] %s %s:%s:%d: %s: ", __progname ? __progname : "", time_buff, module_filename, func, line, severity); + fprintf(fpg, "%s", buff); va_start(list, format); vfprintf(fpg, format, list); @@ -134,3 +144,4 @@ void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const fflush(fpg); fclose(fpg); } + diff --git a/src/utils/util.h b/src/utils/util.h index 8709617..6ae9197 100644 --- a/src/utils/util.h +++ b/src/utils/util.h @@ -49,12 +49,12 @@ typedef enum { EM_LOG_LVL_ERROR }easymesh_log_level_t; -void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const char *format, ...); +void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const char *func, int line, const char *format, ...); void delay(int ); -#define em_util_dbg_print(module, format, ...) em_util_print(EM_LOG_LVL_DEBUG, module, format, ##__VA_ARGS__) -#define em_util_info_print(module, format, ...) em_util_print(EM_LOG_LVL_INFO, module, format, ##__VA_ARGS__) -#define em_util_error_print(module, format, ...) em_util_print(EM_LOG_LVL_ERROR, module, format, ##__VA_ARGS__) +#define em_util_dbg_print(module, format, ...) em_util_print(EM_LOG_LVL_DEBUG, module, __func__, __LINE__, format, ##__VA_ARGS__) +#define em_util_info_print(module, format, ...) em_util_print(EM_LOG_LVL_INFO, module, __func__, __LINE__, format, ##__VA_ARGS__) +#define em_util_error_print(module, format, ...) em_util_print(EM_LOG_LVL_ERROR, module, __func__, __LINE__, format, ##__VA_ARGS__) #ifdef __cplusplus } #endif