Skip to content

Commit

Permalink
em_util_print function to print time and date along with module and s…
Browse files Browse the repository at this point in the history
…everity
  • Loading branch information
behanansaju authored and Behanan committed Dec 30, 2024
1 parent 00b868a commit 34cade1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 45 deletions.
7 changes: 4 additions & 3 deletions build/agent/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
1 change: 1 addition & 0 deletions build/ctrl/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
3 changes: 2 additions & 1 deletion src/em/config/em_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
85 changes: 48 additions & 37 deletions src/utils/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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__)
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -134,3 +144,4 @@ void em_util_print(easymesh_log_level_t level, easymesh_dbg_type_t module, const
fflush(fpg);
fclose(fpg);
}

8 changes: 4 additions & 4 deletions src/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34cade1

Please sign in to comment.