diff options
author | 2019-04-30 17:40:53 +0530 | |
---|---|---|
committer | 2019-05-20 18:14:40 +0530 | |
commit | a826fe833d3f2a8fe2673fa05811fe1a22baf045 (patch) | |
tree | da11a17c46ca9b8a002a52a290628574fa3f5eda /src/framework/log/nstack_log.c | |
parent | 3e6bf7b64eea418c59959c18750261b815b2892c (diff) |
Feature: 19.04 part 1
Change-Id: Ibba924b8deca1f246b9dcb12d89d085b6fd33046
Signed-off-by: charan makkina <charan795m@gmail.com>
Diffstat (limited to 'src/framework/log/nstack_log.c')
-rw-r--r-- | src/framework/log/nstack_log.c | 1039 |
1 files changed, 589 insertions, 450 deletions
diff --git a/src/framework/log/nstack_log.c b/src/framework/log/nstack_log.c index 483a10a..d760648 100644 --- a/src/framework/log/nstack_log.c +++ b/src/framework/log/nstack_log.c @@ -17,8 +17,8 @@ /*==============================================* * include header files * *----------------------------------------------*/ - -#include "nstack_log.h" +#include "nstack_log_base.h" +#include "nstack_log_async.h" #include <sys/stat.h> #include <dirent.h> #include <time.h> @@ -26,6 +26,7 @@ #include <execinfo.h> #include <fcntl.h> #include "nstack_securec.h" +#include "nsfw_maintain_api.h" #include <pthread.h> /*==============================================* @@ -33,177 +34,258 @@ *----------------------------------------------*/ #define FILE_NAME_LEN 256 -#define MAX_PRE_INIT_LOG_COUNT 256 -#define MAX_LOCK_RETRY_COUNT 50000 /* changed MAX_LOCK_RETRY_COUNT to 50000 to avoid function being blocked for too long */ -/* *INDENT-OFF* */ + int g_l4_dump_enable = 0; -static int pre_init_log_count = 0; -static struct pre_init_info pre_init_log[MAX_PRE_INIT_LOG_COUNT] = {{0, ""}}; -/* *INDENT-ON* */ + __thread unsigned int nstack_log_nonreentry = 0; -int ctrl_log_switch = 0; +//ctrl log switch just for ctrl now +bool ctrl_log_switch = FALSE; /*==============================================* * project-wide global variables * *----------------------------------------------*/ struct nstack_logs g_nstack_logs[MAX_LOG_MODULE] = { {0, 0, 0, 0}, {0xFFFF, 0, 0, 0} }; /* Clear compile warning */ + struct log_init_para g_log_init_para = - { 50, 10, NSTACK_LOG_NAME, 10, 10, NSTACK_LOG_NAME }; + { 50, 10, NSTACK_LOG_NAME, 10, 10, NSTACK_LOG_NAME }; + static int g_my_pro_type = LOG_PRO_INVALID; #define DEFAULT_LOG_CTR_TIME 5 static struct log_ctrl_info g_log_ctrl_info[LOG_CTRL_ID_MAX]; +//better put in the struct for file_type +bool log_asyn_inited = FALSE; + /*==============================================* * routines' or functions' implementations * *----------------------------------------------*/ -/* change the print level, not only has err, Begin.*/ -void -save_pre_init_log (uint32_t level, char *fmt, ...) +void write_pre_init_log(bool type) { - va_list ap; - int ret = 0; - /* add pre_init_log_count rang check */ - if (level > NSLOG_DBG || pre_init_log_count >= MAX_PRE_INIT_LOG_COUNT - || pre_init_log_count < 0) + int i = 0; + int ret = -1; + int count = get_pre_init_log_count(); + size_t size = MAX_PRE_INIT_LOG_COUNT * sizeof(struct pre_init_info); + unsigned int level = 0; + struct pre_init_info *pre_buf = (struct pre_init_info *) malloc(size); /*malloc() can be used */ + if (!pre_buf) { - return; + NSPOL_LOGERR("malloc fail]count=%d", count); + return; } - pre_init_log[pre_init_log_count].log_buffer[PRE_INIT_LOG_LENGTH - 1] = '\0'; - (void) va_start (ap, fmt); - ret = - VSNPRINTF_S (pre_init_log[pre_init_log_count].log_buffer, - PRE_INIT_LOG_LENGTH, PRE_INIT_LOG_LENGTH - 1, fmt, ap); - if (-1 == ret) + + ret = memset_s(pre_buf, size, 0, size); + if (EOK != ret) { - va_end (ap); - return; + NSPOL_LOGERR("memset_s fail]ret=%d,count=%d", ret, count); + free(pre_buf); /*free() can be used */ + return; } - va_end (ap); - pre_init_log[pre_init_log_count].level = level; - pre_init_log_count++; -} -void -write_pre_init_log () -{ - int i = 0; - for (; i < pre_init_log_count; i++) + ret = get_pre_init_log_buffer(pre_buf, MAX_PRE_INIT_LOG_COUNT); + if (ret < 0) { - if (NSLOG_ERR == pre_init_log[i].level) - { - NSPOL_LOGERR ("pre init log: %s", pre_init_log[i].log_buffer); - } - else if (NSLOG_WAR == pre_init_log[i].level) + NSPOL_LOGERR("get the init log fail]ret=%d,count=%d", ret, count); + free(pre_buf); /*free() can be used */ + return; + } + + for (; i < count; i++) + { + level = pre_buf[i].level; + if (type && (level >= NSLOG_INF)) { - NSPOL_LOGWAR (NS_LOG_STACKX_ON, "pre init log: %s", - pre_init_log[i].log_buffer); + continue; } - else if (NSLOG_INF == pre_init_log[i].level) + + switch (level) { - NSPOL_LOGINF (NS_LOG_STACKX_ON, "pre init log: %s", - pre_init_log[i].log_buffer); + case NSLOG_ERR: + NSPOL_LOGERR("pre init log: %s", pre_buf[i].log_buffer); + break; + case NSLOG_WAR: + NSPOL_LOGWAR(NS_LOG_STACKPOOL_ON, "pre init log: %s", + pre_buf[i].log_buffer); + break; + case NSLOG_INF: + NSPOL_LOGINF(NS_LOG_STACKPOOL_ON, "pre init log: %s", + pre_buf[i].log_buffer); + break; + case NSLOG_DBG: + NSPOL_LOGDBG(NS_LOG_STACKPOOL_ON, "pre init log: %s", + pre_buf[i].log_buffer); + break; + default: + break; } } + + free(pre_buf); /*free() can be used */ + return; } -int -cmp_log_path (const char *path) +void get_current_time(char *buf, const int len) { - if (NULL == path) + if (NULL == buf || len <= 0) { - return 1; + return; } - /* remove struct log_info g_nstack_log_info */ - if (NULL != g_log_init_para.mon_log_path) + int retVal; + time_t cur_tick; + struct tm cur_time; + + (void) time(&cur_tick); /*time() can be used */ + if (NULL == localtime_r(&cur_tick, &cur_time)) { - if (strcmp (g_log_init_para.mon_log_path, path) == 0) - { - return 0; - } + return; + } + + // from man page of localtime_r: + // tm_year The number of years since 1900. + // tm_mon The number of months since January, in the range 0 to 11. + /* There are some unsafe function ,need to be replace with safe function */ + retVal = snprintf_s(buf, len, len - 1, "%04d%02d%02d%02d%02d%02d", + cur_time.tm_year + 1900, cur_time.tm_mon + 1, + cur_time.tm_mday, cur_time.tm_hour, cur_time.tm_min, + cur_time.tm_sec); + if (-1 == retVal) + { + return; } - return 1; + buf[len - 1] = 0; } -void -get_current_time (char *buf, const int len) +void nstack_setlog_level(int module, uint32_t level) { - int retVal; - time_t cur_tick; - struct tm cur_time; - (void) time (&cur_tick); - /* add return value check, */ - if (NULL == localtime_r (&cur_tick, &cur_time)) + if (MAX_LOG_MODULE <= module || module < 0) { - return; + return; + } + g_nstack_logs[module].level = level; + /* log suppression switched off when level set to DBG */ + g_nstack_logs[module].suppress_off = (NSLOG_DBG == level) ? 1 : 0; +} + +bool nstack_log_info_check(uint32_t module, uint32_t level) +{ + if ((MAX_LOG_MODULE <= module) || !nstack_log_level_valid(level)) + { + return FALSE; } - // from man page of localtime_r: - // tm_year The number of years since 1900. - // tm_mon The number of months since January, in the range 0 to 11. - retVal = - SNPRINTF_S (buf, len, len - 1, "%04d%02d%02d%02d%02d%02d", - cur_time.tm_year + 1900, cur_time.tm_mon + 1, - cur_time.tm_mday, cur_time.tm_hour, cur_time.tm_min, - cur_time.tm_sec); - if (-1 == retVal) + /* no need compare module ,which is done ahead */ + if ((LOG_PRO_INVALID == g_my_pro_type) || (FALSE != ctrl_log_switch)) { - return; + return FALSE; } - buf[len - 1] = 0; + + return TRUE; + } /***************************************************************************** -* Prototype : nstack_setlog_level -* Description : Set global log level -* Input : int module -* uint32_t level +* Prototype : nstack_log_method_check +* Description : log print method choice, process main, master, nStackCtrl use +* domain socket to log, app and segment error use orginal method. +* Input : uint32_t level +* ... * Output : None -* Return Value : void +* Return Value : bool * Calls : * Called By : *****************************************************************************/ -void -nstack_setlog_level (int module, uint32_t level) +bool nstack_log_method_check(uint32_t level) { - if (MAX_LOG_MODULE <= module || module < 0) + if (LOG_PRO_APP <= g_my_pro_type) + { + return FALSE; + } + + //for segment error log + if (level == NSLOG_EMG) { - return; + return FALSE; } - g_nstack_logs[module].level = level; + + if (!log_asyn_inited) + { + return FALSE; + } + + return TRUE; +} + +NSTACK_STATIC int get_file_type(uint32_t module) +{ + return g_nstack_logs[module].file_type; } /***************************************************************************** -* Prototype : nstack_log_info_check -* Description : log info check +* Prototype : nstack_log +* Description : log print * Input : uint32_t module -* uint32_t level +* const char *format * ... * Output : None -* Return Value : bool +* Return Value : None * Calls : * Called By : *****************************************************************************/ -bool -nstack_log_info_check (uint32_t module, uint32_t level) +NSTACK_FMT_CHECK(3) + void nstack_log(uint32_t module, uint32_t level, const char *format, ...) { - if (MAX_LOG_MODULE <= module) + va_list ap; + int ret; + char pre_buf[PRE_INIT_LOG_LENGTH] = { 0 }; + char buf[MAX_BUFFER_LEN] = { 0 }; + char format_buf[MAX_BUFFER_LEN]; + format_buf[sizeof(format_buf) - 1] = 0; + int file_type = 0; + + if (NULL == format) + { + return; + } + + ret = nstack_log_get_prefix(level, pre_buf, sizeof(pre_buf)); + if (ret < 0) { - return false; + return; } - /* no need compare module ,which is done ahead */ - if (LOG_PRO_INVALID == g_my_pro_type) + ret = sprintf_s(format_buf, sizeof(format_buf), "%s %s", pre_buf, format); + if (-1 == ret) { - return false; + return; } - return true; + + va_start(ap, format); /*no need to check return */ + ret = vsprintf_s(buf, sizeof(buf), format_buf, ap); + if (-1 == ret) + { + va_end(ap); + return; + } + va_end(ap); + + buf[sizeof(buf) - 1] = '\0'; + + //get the file type + if (MAX_LOG_MODULE <= module) + { + return; //something wrong, but can't print log here + } + file_type = get_file_type(module); + + // send the buf and write to client fd + (void) nstack_log_client_send(file_type, buf, ret); + + return; } -/* *INDENT-OFF* */ NSTACK_STATIC inline void init_operation_log_para() { g_nstack_logs[OPERATION].file_type = LOG_TYPE_OPERATION; @@ -213,24 +295,28 @@ NSTACK_STATIC inline void init_nstack_log_para() { int i = 0; - (void)glogLevelSet(GLOG_LEVEL_DEBUG); + /* change to glog functions,no need file type control */ + (void) glogLevelSet(GLOG_LEVEL_DEBUG); glogBufLevelSet(GLOG_LEVEL_WARNING); - for(; i<GLOG_LEVEL_BUTT; i++) - glogSetLogSymlink(i,""); + for (; i < GLOG_LEVEL_BUTT; i++) + glogSetLogSymlink(i, ""); glogDir(g_log_init_para.run_log_path); nstack_log_count_set(g_log_init_para.run_log_count); glogMaxLogSizeSet(g_log_init_para.run_log_size); - glogSetLogFilenameExtension(STACKX_LOG_NAME); + glogSetLogFilenameExtension(STACKPOOL_LOG_NAME); glogFlushLogSecsSet(FLUSH_TIME); - for (i = 0; i < MAX_LOG_MODULE ; i++ ) + /* Fix nstack_log_init file type */ + for (i = 0; i < MAX_LOG_MODULE; i++) { if (i == OPERATION) { continue; } + g_nstack_logs[i].file_type = LOG_TYPE_NSTACK; } + init_operation_log_para(); } @@ -238,36 +324,44 @@ NSTACK_STATIC inline void init_ctrl_log_para() { int i = 0; - (void)glogLevelSet(GLOG_LEVEL_DEBUG); + /* change to glog functions,no need file type control */ + /* omc_ctrl single log file should be 10M [Start] */ + (void) glogLevelSet(GLOG_LEVEL_DEBUG); glogBufLevelSet(GLOG_LEVEL_WARNING); - for(; i<GLOG_LEVEL_BUTT; i++) - glogSetLogSymlink(i,""); + for (; i < GLOG_LEVEL_BUTT; i++) + glogSetLogSymlink(i, ""); glogDir(g_log_init_para.mon_log_path); nstack_log_count_set(g_log_init_para.mon_log_count); glogMaxLogSizeSet(g_log_init_para.mon_log_size); glogSetLogFilenameExtension(OMC_CTRL_LOG_NAME); glogFlushLogSecsSet(FLUSH_TIME); + + for (i = 0; i < MAX_LOG_MODULE; i++) + { + g_nstack_logs[i].file_type = LOG_TYPE_CTRL; + } } NSTACK_STATIC inline void init_master_log_para() { int i = 0; - (void)glogLevelSet(GLOG_LEVEL_DEBUG); + /* change to glog functions,no need file type control */ + (void) glogLevelSet(GLOG_LEVEL_DEBUG); glogBufLevelSet(GLOG_LEVEL_WARNING); - for(; i<GLOG_LEVEL_BUTT; i++) - glogSetLogSymlink(i,""); + for (; i < GLOG_LEVEL_BUTT; i++) + glogSetLogSymlink(i, ""); glogDir(g_log_init_para.mon_log_path); nstack_log_count_set(g_log_init_para.mon_log_count); glogMaxLogSizeSet(g_log_init_para.mon_log_size); glogSetLogFilenameExtension(MASTER_LOG_NAME); glogFlushLogSecsSet(FLUSH_TIME); - for (i = 0; i < MAX_LOG_MODULE ; i++ ) + + for (i = 0; i < MAX_LOG_MODULE; i++) { g_nstack_logs[i].file_type = LOG_TYPE_MASTER; } } -/* *INDENT-OFF* */ /***************************************************************************** * Prototype : nstack_log_init @@ -280,83 +374,116 @@ NSTACK_STATIC inline void init_master_log_para() *****************************************************************************/ int nstack_log_init() { - char *pst_temp = NULL; - int log_level = NSLOG_INF; - pst_temp = getenv("NSTACK_LOG_ON"); - if (pst_temp) - { - if (strcmp (pst_temp, "INF") == 0) - { - log_level = NSLOG_INF; - } - else if (strcmp (pst_temp, "DBG") == 0) - { - log_level = NSLOG_DBG; - } - else if (strcmp (pst_temp, "WAR") == 0) - { - log_level = NSLOG_WAR; - } - else if (strcmp (pst_temp, "ERR") == 0) + char *pst_temp = NULL; + uint32_t log_level = NSLOG_INF; + int proc_type = g_my_pro_type; + int ret_client = 0; + int ret_server = 0; + bool type = FALSE; + + pst_temp = getenv("NSTACK_LOG_ON"); /*getenv() can be used */ + + if (pst_temp) { - log_level = NSLOG_ERR; + if (strcmp(pst_temp, "INF") == 0) + { + log_level = NSLOG_INF; + } + else if (strcmp(pst_temp, "DBG") == 0) + { + log_level = NSLOG_DBG; + } + else if (strcmp(pst_temp, "WAR") == 0) + { + log_level = NSLOG_WAR; + } + else if (strcmp(pst_temp, "ERR") == 0) + { + log_level = NSLOG_ERR; + } + else if (strcmp(pst_temp, "EMG") == 0) + { + log_level = NSLOG_EMG; + } + else + { + log_level = NSLOG_ERR; + } + } - else if (strcmp (pst_temp, "EMG") == 0) + else { - log_level = NSLOG_EMG; + log_level = NSLOG_INF; } - else if (strcmp (pst_temp, "PERF") == 0) + + int i = 0; + for (i = 0; i < MAX_LOG_MODULE; i++) { - ctrl_log_switch = 1; + nstack_setlog_level(i, log_level); } - else + + if (log_level <= NSLOG_WAR) { - log_level = NSLOG_ERR; + /*MONITR log level must set to larger than warning */ + nstack_setlog_level(MASTER, NSLOG_WAR); } - } - else + + /* monitr and nstack write the same file, it will cause synchronize problem */ + switch (proc_type) { - log_level = NSLOG_INF; + case LOG_PRO_NSTACK: + glogInit("NSTACK"); + init_nstack_log_para(); + break; + case LOG_PRO_OMC_CTRL: + glogInit("CTRL"); + init_ctrl_log_para(); + type = TRUE; + break; + case LOG_PRO_MASTER: + glogInit("MASTER"); + init_master_log_para(); + break; + default: + return 0; } - int i = 0; - for (i = 0; i < MAX_LOG_MODULE; i++) + + init_log_ctrl_info(); + + // this is for monitr to check whether log has beed inited + g_nstack_logs[NSOCKET].inited = 1; + + /* init the asyn log method for */ + ret_server = nstack_log_server_init(proc_type); + + ret_client = nstack_log_client_init(proc_type); + + /* if async log init fail, + * use synchron log method and record it.*/ + + if ((ret_client == 0) && (ret_server == 0)) { - nstack_setlog_level (i, log_level); + log_asyn_inited = TRUE; } - if (log_level <= NSLOG_WAR) + else { - /*MONITOR log level must set to larger than warning */ - nstack_setlog_level (MASTER, NSLOG_WAR); + NSPOL_LOGWAR(NS_LOG_STACKPOOL_ON, + "async log module init fail, use synchron log"); } - /* monitor and nstack write the same file, it will cause synchronize problem */ - switch (g_my_pro_type) - { - case LOG_PRO_NSTACK: - glogInit ("NSTACK"); - init_nstack_log_para (); - break; - case LOG_PRO_OMC_CTRL: - glogInit ("CTRL"); - init_ctrl_log_para (); - break; - case LOG_PRO_MASTER: - glogInit ("MASTER"); - init_master_log_para (); - break; - default: - return 0; - } + NSPOL_LOGINF(NS_LOG_STACKPOOL_ON, "daemon-stack_version=%s", + NSTACK_VERSION); + NSPOL_LOGINF(NS_LOG_STACKPOOL_ON, + "ret_client=%d,ret_server=%d,log_asyn_inited=%d", + ret_client, ret_server, log_asyn_inited); - init_log_ctrl_info(); + /* omc log restrain */ + write_pre_init_log(type); - // this is for monitr to check whether log has beed inited - g_nstack_logs[NSOCKET].inited = 1; - NSPOL_LOGERR ("nStackMain_version=%s", NSTACK_VERSION); - write_pre_init_log(); - return 0; + return 0; } +/* nStack Log print */ /***************************************************************************** * Prototype : get_str_value * Description : get int value @@ -366,27 +493,26 @@ int nstack_log_init() * Calls : * Called By : *****************************************************************************/ -int -get_str_value (const char *arg) +int get_str_value(const char *arg) { - char *parsing_end; - int iValue = 0; - int oldErrno = errno; - if (arg == NULL) + char *parsing_end; + int iValue = 0; + int oldErrno = errno; + + if (arg == NULL) { - return -1; + return -1; } - errno = 0; - iValue = (int) strtol (arg, &parsing_end, 0); - if (errno || (!parsing_end) || parsing_end[0] != 0) + errno = 0; + iValue = (int) strtol(arg, &parsing_end, 0); + if (errno || (!parsing_end) || parsing_end[0] != 0) { - iValue = -1; + iValue = -1; } errno = oldErrno; - return iValue; + return iValue; } - /***************************************************************************** * Prototype : setlog_level_value * Description : proc log level config @@ -397,61 +523,62 @@ get_str_value (const char *arg) * Calls : * Called By : *****************************************************************************/ -int -setlog_level_value (const char *param, const char *value) +int setlog_level_value(const char *param, const char *value) { - int i = 0; - int module = 0; - int logLevel = 0; - module = get_str_value (param); - if ((module < 0) || (MAX_LOG_MODULE <= module)) + int i = 0; + int module = 0; + int logLevel = 0; + module = get_str_value(param); + if ((module < 0) || (MAX_LOG_MODULE <= module)) { - NSOPR_LOGERR ("input module error]param=%s,module=%d", param, module); - return 1; + NSOPR_LOGERR("input module error]param=%s,module=%d", param, module); + return 1; } - - if (strcmp (value, LOG_LEVEL_ERR) == 0) + if (strcmp(value, LOG_LEVEL_ERR) == 0) { - logLevel = NSLOG_ERR; + logLevel = NSLOG_ERR; } - else if (strcmp (value, LOG_LEVEL_WAR) == 0) + else if (strcmp(value, LOG_LEVEL_WAR) == 0) { - logLevel = NSLOG_WAR; + logLevel = NSLOG_WAR; } - else if (strcmp (value, LOG_LEVEL_DBG) == 0) + else if (strcmp(value, LOG_LEVEL_DBG) == 0) { - logLevel = NSLOG_DBG; + logLevel = NSLOG_DBG; } - else if (strcmp (value, LOG_LEVEL_INF) == 0) + else if (strcmp(value, LOG_LEVEL_INF) == 0) { - logLevel = NSLOG_INF; + logLevel = NSLOG_INF; } - else if (strcmp (value, LOG_LEVEL_EMG) == 0) + else if (strcmp(value, LOG_LEVEL_EMG) == 0) { - logLevel = NSLOG_EMG; + logLevel = NSLOG_EMG; } - else + else { - NSOPR_LOGERR ("input log level error!"); - return 1; + NSOPR_LOGERR("input log level error!"); + return 1; } - NSOPR_LOGINF ("set module log with level]module=%d,logLevel=0x%x", - module, logLevel); - if (module > 0) + + NSOPR_LOGINF("set module log with level]module=%d,logLevel=0x%x", module, + logLevel); + + if (module > 0) { - nstack_setlog_level (module, logLevel); - return 0; + nstack_setlog_level(module, logLevel); + return 0; } - if (0 == module) + + if (0 == module) { - for ( i = 0; i < MAX_LOG_MODULE ; i++ ) + for (i = 0; i < MAX_LOG_MODULE; i++) { - nstack_setlog_level(i,logLevel); + nstack_setlog_level(i, logLevel); } } - return 0; -} + return 0; +} /***************************************************************************** * Prototype : check_log_dir_valid @@ -462,39 +589,39 @@ setlog_level_value (const char *param, const char *value) * Calls : * Called By : *****************************************************************************/ -int -check_log_dir_valid (const char *path) +int check_log_dir_valid(const char *path) { - size_t length; - struct stat statbuf; - if (NULL == path) + size_t length; + struct stat statbuf; + if (NULL == path) { - return -1; + return -1; } - length = strlen (path) + 1; - if ((length <= 1) || (length > FILE_NAME_LEN)) + + length = strlen(path) + 1; + if ((length <= 1) || (length > FILE_NAME_LEN)) { - return -1; + return -1; } - /* only write permission is allowed */ - if ((0 != access (path, W_OK))) + /* only write permission is legal */ + if ((0 != access(path, W_OK))) { - /* if path can access, use env path */ - return -1; + /* if path can access, use env path */ + return -1; } - if ((0 == lstat (path, &statbuf)) && S_ISDIR (statbuf.st_mode)) + if ((0 == lstat(path, &statbuf)) && S_ISDIR(statbuf.st_mode)) { - return 0; - } - else + return 0; + + } + else { - return -1; + return -1; } } - /***************************************************************************** * Prototype : get_app_env_log_path * Description : called by environment-specific log init function @@ -505,49 +632,48 @@ check_log_dir_valid (const char *path) * Calls : * Called By : *****************************************************************************/ -int -get_app_env_log_path (char *app_file_path, unsigned int app_file_size) +int get_app_env_log_path(char *app_file_path, unsigned int app_file_size) { - char *pst_app_log_path_flag = NULL; - char *pst_app_log_path_string = NULL; - int log_to_file = 0; - int ret = -1; - char *app_dir = NULL; + char *pst_app_log_path_flag = NULL; + char *pst_app_log_path_string = NULL; + int log_to_file = 0; + int ret = -1; + char *app_dir = NULL; - if ((NULL == app_file_path) || (0 == app_file_size)) + if ((NULL == app_file_path) || (0 == app_file_size)) { - return 0; + return 0; } - pst_app_log_path_flag = getenv ("NSTACK_LOG_FILE_FLAG"); - if (pst_app_log_path_flag && strcmp (pst_app_log_path_flag, "1") == 0) + + pst_app_log_path_flag = getenv("NSTACK_LOG_FILE_FLAG"); /* getenv() can be used */ + + if (pst_app_log_path_flag && strcmp(pst_app_log_path_flag, "1") == 0) { - /* if set environment variable to 1,then output to file*/ + /* if set enviroment variable to 1,then output to file */ log_to_file = 1; } - else + else { - /* if environment variable is not equal 1 or - don't set this environment variable ,output to STDOUT */ + /* if enviroment variable is not equal 1 or don't set this enviroment variable ,output to STDOUT */ return 0; } /* add the realpath and dir check */ /* APP LOG can be set by user */ - pst_app_log_path_string = getenv("NSTACK_APP_LOG_PATH"); - + pst_app_log_path_string = getenv("NSTACK_APP_LOG_PATH"); /* getenv() can be used */ if ((NULL == pst_app_log_path_string) - ||(strlen (pst_app_log_path_string) > FILE_NAME_LEN - 1)) + || (strlen(pst_app_log_path_string) > FILE_NAME_LEN - 1)) { - goto app_default; + goto app_default; } - app_dir = realpath (pst_app_log_path_string, NULL); - if (check_log_dir_valid (pst_app_log_path_string) < 0) + app_dir = realpath(pst_app_log_path_string, NULL); + if (check_log_dir_valid(pst_app_log_path_string) < 0) { - goto app_default; + goto app_default; } - ret = STRCPY_S (app_file_path, app_file_size, app_dir); - if(EOK != ret) + ret = strcpy_s(app_file_path, app_file_size, app_dir); + if (EOK != ret) { log_to_file = 0; } @@ -555,53 +681,28 @@ get_app_env_log_path (char *app_file_path, unsigned int app_file_size) free(app_dir); return log_to_file; -app_default: + app_default: - if ((0 == access (APP_LOG_PATH, W_OK))) + if ((0 == access(APP_LOG_PATH, W_OK))) { - ret = STRCPY_S (app_file_path, app_file_size, APP_LOG_PATH); - if (EOK != ret) + ret = strcpy_s(app_file_path, app_file_size, APP_LOG_PATH); + if (EOK != ret) { - log_to_file = 0; + log_to_file = 0; } } - else + else { - log_to_file = 0; + log_to_file = 0; } - if (NULL != app_dir) + if (NULL != app_dir) { - free (app_dir); + free(app_dir); } - return log_to_file; - -} - -/***************************************************************************** -* Prototype : nstack_get_app_logname -* Description : get the name of app's log -* Input : None -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ -int -nstack_get_app_logname (char* log_name) -{ - int pid = getpid (); - char processname[FILE_NAME_LEN] = {0}; - - if (log_name == NULL) - return 1; - - strncpy (processname, program_invocation_short_name, 10); - - snprintf (log_name, FILE_NAME_LEN, "app_%s_%d.log", processname, pid); + return log_to_file; - return 0; } /***************************************************************************** @@ -613,252 +714,290 @@ nstack_get_app_logname (char* log_name) * Calls : * Called By : *****************************************************************************/ -void -nstack_log_init_app () +void nstack_log_init_app() { - char *pc_temp = NULL; - int log_level = NSLOG_ERR; - int i = 0; - int file_flag = 0; - char app_log_path[FILE_NAME_LEN] = { 0 }; - int ret = 0; - char app_log_name[FILE_NAME_LEN] = { 0 }; + char *pc_temp = NULL; + uint32_t log_level = NSLOG_ERR; + int i = 0; + int file_flag = 0; + char app_log_path[FILE_NAME_LEN] = { 0 }; - /* log already initialized, just return */ - if (LOG_PRO_INVALID != g_my_pro_type) + /* log alread initialized, just return */ + if (LOG_PRO_INVALID != g_my_pro_type) { - return; + return; } - /* Add app log hook module init */ - nstack_log_hook_init (); - if (0 != g_nstack_logs[NSOCKET].inited) + /* Add app log hook module init */ + nstack_log_hook_init(); + + if (0 != g_nstack_logs[NSOCKET].inited) { - return; + return; } - glogInit ("APP"); - pc_temp = getenv ("NSTACK_LOG_ON"); - if (pc_temp) + glogInit("APP"); + + pc_temp = getenv("NSTACK_LOG_ON"); /*getenv() can be used */ + + if (pc_temp) { - if (strcmp (pc_temp, "INF") == 0) - { - log_level = NSLOG_INF; - } - else if (strcmp (pc_temp, "DBG") == 0) + if (strcmp(pc_temp, "INF") == 0) { - log_level = NSLOG_DBG; + log_level = NSLOG_INF; } - else if (strcmp (pc_temp, "WAR") == 0) + else if (strcmp(pc_temp, "DBG") == 0) { - log_level = NSLOG_WAR; + log_level = NSLOG_DBG; } - else if (strcmp (pc_temp, "ERR") == 0) + else if (strcmp(pc_temp, "WAR") == 0) { - log_level = NSLOG_ERR; + log_level = NSLOG_WAR; } - else if (strcmp (pc_temp, "EMG") == 0) + else if (strcmp(pc_temp, "ERR") == 0) { - log_level = NSLOG_EMG; + log_level = NSLOG_ERR; } - else if (strcmp (pc_temp, "PERF") == 0) + else if (strcmp(pc_temp, "EMG") == 0) { - ctrl_log_switch = 1; + log_level = NSLOG_EMG; } - else + else { - log_level = NSLOG_ERR; + log_level = NSLOG_ERR; } } - else + else { - log_level = NSLOG_ERR; + log_level = NSLOG_ERR; } - /* socket interface APP called include both stack-x and nstack module! */ - nstack_setlog_level (STACKX, log_level); - nstack_setlog_level (NSOCKET, log_level); - nstack_setlog_level (LOGRTE, log_level); - nstack_setlog_level (LOGDFX, log_level); - nstack_setlog_level (LOGFW, log_level); - nstack_setlog_level (LOGHAL, log_level); - nstack_setlog_level (LOGSBR, log_level); + /* socket interface APP called include both stackpool and nstack module! */ + nstack_setlog_level(STACKPOOL, log_level); + nstack_setlog_level(NSOCKET, log_level); + nstack_setlog_level(LOGRTP, log_level); + nstack_setlog_level(LOGDFX, log_level); + nstack_setlog_level(LOGFW, log_level); + nstack_setlog_level(LOGHAL, log_level); + nstack_setlog_level(LOGSBR, log_level); - /* package the app env handle function, Begin */ - file_flag = get_app_env_log_path (app_log_path, FILE_NAME_LEN); - if ((1 == file_flag) && (strlen (app_log_path) > 0)) + file_flag = get_app_env_log_path(app_log_path, FILE_NAME_LEN); + if ((1 == file_flag) && (strlen(app_log_path) > 0)) { - glogDir (app_log_path); - glogBufLevelSet (GLOG_LEVEL_WARNING); - (void) glogLevelSet (GLOG_LEVEL_DEBUG); - for (i = 0; i < GLOG_LEVEL_BUTT; i++) - glogSetLogSymlink (i, ""); - nstack_log_count_set (APP_LOG_COUNT); - glogMaxLogSizeSet (APP_LOG_SIZE); - ret = nstack_get_app_logname (app_log_name); - if (ret == 0) - { - glogSetLogFilenameExtension (app_log_name); - } - else - { - glogSetLogFilenameExtension (APP_LOG_NAME); - } - glogFlushLogSecsSet (FLUSH_TIME); + /* change to glog functions,no need file type control */ + glogDir(app_log_path); + glogBufLevelSet(GLOG_LEVEL_WARNING); + (void) glogLevelSet(GLOG_LEVEL_DEBUG); + for (i = 0; i < GLOG_LEVEL_BUTT; i++) + glogSetLogSymlink(i, ""); + nstack_log_count_set(APP_LOG_COUNT); + glogMaxLogSizeSet(APP_LOG_SIZE); + glogSetLogFilenameExtension(APP_LOG_NAME); + glogFlushLogSecsSet(FLUSH_TIME); } - else + else { - glogToStderrSet (1); + glogToStderrSet(1); } - for (i = 0; i < MAX_LOG_MODULE; i++) + for (i = 0; i < MAX_LOG_MODULE; i++) { - g_nstack_logs[i].file_type = LOG_TYPE_APP; + g_nstack_logs[i].file_type = LOG_TYPE_APP; } - init_log_ctrl_info (); - g_my_pro_type = LOG_PRO_APP; - SetGlogCtrlOpt (TRUE); - NSPOL_LOGERR ("app_nStack_version=%s", NSTACK_VERSION); - return; + + init_log_ctrl_info(); + + g_my_pro_type = LOG_PRO_APP; + SetGlogCtrlOpt(TRUE); + + NSPOL_LOGCUSINF("app_nStack_version=%s", NSTACK_VERSION); + return; } -void -nstack_segment_error (int s) +void nstack_segment_error(int s) { - #define BACKTRACE_SIZ 20 - void *array[BACKTRACE_SIZ]; - int size; - int i; - char **strings = NULL; - /*if set, flush the log immediately */ - glogFlushLogFiles (GLOG_LEVEL_DEBUG); + void *array[BACKTRACE_SIZ]; + int size; + int i; + char **strings = NULL; - size = backtrace (array, BACKTRACE_SIZ); - NSPOL_LOGEMG - ("------------------DUMP_BACKTRACE[%d]--------------------------------\n", - size); + /*if set, flush the log immediately */ + glogFlushLogFiles(GLOG_LEVEL_DEBUG); - /* easy to view signal in separate log file */ - NSPOL_LOGEMG ("Received signal s=%d", s); + size = backtrace(array, BACKTRACE_SIZ); + NSPOL_LOGEMG + ("------------------DUMP_BACKTRACE[%d]--------------------------------\n", + size); - for (i = 0; i < size; i++) + /*easy to view signal in separate log file */ + NSPOL_LOGEMG("Received signal s=%d", s); + + for (i = 0; i < size; i++) { - NSPOL_LOGEMG ("[%d]:%p\n", i, array[i]); + NSPOL_LOGEMG("[%d]:%p\n", i, array[i]); } - strings = backtrace_symbols (array, size); - if (NULL == strings) + + strings = backtrace_symbols(array, size); + if (NULL == strings) { - return; + return; } - for (i = 0; i < size; i++) + for (i = 0; i < size; i++) { - NSPOL_LOGEMG ("[%d]:%s\n", i, strings[i]); + NSPOL_LOGEMG("[%d]:%s\n", i, strings[i]); } - NSPOL_LOGEMG - ("-------------------------------------------------------------------\n"); - free (strings); + + NSPOL_LOGEMG + ("-------------------------------------------------------------------\n"); + free(strings); /*free() can be used */ + } -void -set_log_init_para (struct log_init_para *para) +void set_log_init_para(struct log_init_para *para) { - if (NULL == para) - + if (NULL == para) { - return; + return; } - if (EOK != - MEMCPY_S (&g_log_init_para, sizeof (struct log_init_para), para, - sizeof (struct log_init_para))) + if (EOK != + memcpy_s(&g_log_init_para, sizeof(struct log_init_para), para, + sizeof(struct log_init_para))) { - return; + return; } } /* control log printed counts */ -static inline void -update_log_prt_time (struct timespec *cur_time, struct timespec *log_prt_time) +static inline void update_log_prt_time(struct timespec *cur_time, + struct timespec *log_prt_time) { - log_prt_time->tv_sec = cur_time->tv_sec; - log_prt_time->tv_nsec = cur_time->tv_nsec; + log_prt_time->tv_sec = cur_time->tv_sec; + log_prt_time->tv_nsec = cur_time->tv_nsec; } -int -check_log_prt_time (int id) +bool check_log_prt_time(int id) { - struct timespec cur_time; - struct timespec *log_prt_time = NULL; - if (id >= LOG_CTRL_ID_MAX || id < 0) + struct timespec cur_time; + struct timespec *log_prt_time = NULL; + + if (id >= LOG_CTRL_ID_MAX || id < 0) + { + return FALSE; + } + + (void) clock_gettime(CLOCK_MONOTONIC, &cur_time); + log_prt_time = &g_log_ctrl_info[id].last_log_time; + if (cur_time.tv_sec - log_prt_time->tv_sec >= + g_log_ctrl_info[id].expire_time) { - return 0; + /*first log need print, Begin */ + set_log_ctrl_time(id, DEFAULT_LOG_CTR_TIME); + update_log_prt_time(&cur_time, log_prt_time); + return TRUE; } - (void) clock_gettime (CLOCK_MONOTONIC, &cur_time); - log_prt_time = &g_log_ctrl_info[id].last_log_time; - if (cur_time.tv_sec - log_prt_time->tv_sec >= - g_log_ctrl_info[id].expire_time) + + g_log_ctrl_info[id].unprint_count++; + return FALSE; +} + +/***************************************************************************** +* Prototype : check_log_restrain_valid +* Description : check if the log if valid and ctrl restrain time expire. +* Input : int id +* : uint32_t module, +* : uint32_t level +* ... +* Output : None +* Return Value : bool +* Calls : +* Called By : +*****************************************************************************/ +bool check_log_restrain_valid(int id, uint32_t module, uint32_t level) +{ + if (nstack_log_info_check(module, level) && check_log_prt_time(id)) { - /* first log need print */ - set_log_ctrl_time (id, DEFAULT_LOG_CTR_TIME); - update_log_prt_time (&cur_time, log_prt_time); - return 1; + return TRUE; } - g_log_ctrl_info[id].unprint_count++; - return 0; + + return FALSE; } -int -get_unprt_log_count (int id) +int get_unprt_log_count(int id) { - return g_log_ctrl_info[id].unprint_count; + return g_log_ctrl_info[id].unprint_count; } -void -clr_unprt_log_count (int id) +void clr_unprt_log_count(int id) { - g_log_ctrl_info[id].unprint_count = 0; + g_log_ctrl_info[id].unprint_count = 0; } -void -set_log_ctrl_time (int id, int ctrl_time) +void set_log_ctrl_time(int id, int ctrl_time) { - if (id >= LOG_CTRL_ID_MAX || id < 0) + if (id >= LOG_CTRL_ID_MAX || id < 0) { - return; + return; } - if (ctrl_time <= 0) + if (ctrl_time <= 0) + { + return; + } + + g_log_ctrl_info[id].expire_time = ctrl_time; +} + +void init_log_ctrl_info() +{ + int i = 0; + for (; i < LOG_CTRL_ID_MAX; i++) { - return; + /*first log need print */ + g_log_ctrl_info[i].expire_time = 0; + g_log_ctrl_info[i].unprint_count = 0; + g_log_ctrl_info[i].last_log_time.tv_sec = 0; + g_log_ctrl_info[i].last_log_time.tv_nsec = 0; } - g_log_ctrl_info[id].expire_time = ctrl_time; + // for every socket api, need different log id + + // for nstack inner +} + +void set_log_proc_type(int log_proc_type) +{ + g_my_pro_type = log_proc_type; } -void -init_log_ctrl_info () +int nstack_log_flush(unsigned long long timeout) { - int i = 0; - for (; i < LOG_CTRL_ID_MAX; i++) + if (LOG_PRO_APP <= g_my_pro_type) { - /* first log need print */ - g_log_ctrl_info[i].expire_time = 0; - g_log_ctrl_info[i].unprint_count = 0; - g_log_ctrl_info[i].last_log_time.tv_sec = 0; - g_log_ctrl_info[i].last_log_time.tv_nsec = 0; + glogFlushLogFiles(GLOG_LEVEL_DEBUG); + return 0; } - // for every socket api, need different log id + if (log_asyn_inited) + { + return nstack_log_server_flush(g_my_pro_type, timeout); + } + else + { + return -1; + } +} - // for nstack inner +void set_nstack_log_nonreentry(uint32_t val) +{ + nstack_log_nonreentry = val; } -void -set_log_proc_type (int log_proc_type) +unsigned int get_nstack_log_nonreentry(void) { - g_my_pro_type = log_proc_type; + return nstack_log_nonreentry; } |