From b30bdc21b594545c2e9ad0ac90df43b07e5ff3d7 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 26 Jun 2015 16:10:16 +0200 Subject: [PATCH] logging: cleanup output API Make SCLogMessage master of the logging. Reduces complexity of the SCLog macro's. --- src/util-debug.c | 83 ++++++++++++++++++----------------- src/util-debug.h | 111 ++++++++++++++++++----------------------------- 2 files changed, 85 insertions(+), 109 deletions(-) diff --git a/src/util-debug.c b/src/util-debug.c index 7e9726bbb5..e6c22cb585 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -194,7 +194,7 @@ static inline void SCLogPrintToSyslog(int syslog_log_level, const char *msg) * \param msg Pointer to the message that has to be logged * \param log_level The log_level of the message that has to be logged */ -void SCLogOutputBuffer(SCLogLevel log_level, char *msg) +static void SCLogOutputBuffer(SCLogLevel log_level, char *msg) { char *temp = msg; int len = strlen(msg); @@ -264,10 +264,12 @@ void SCLogOutputBuffer(SCLogLevel log_level, char *msg) * * \retval SC_OK on success; else an error code */ -SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, - unsigned line, const char *function) +SCError SCLogMessage(SCLogLevel log_level, const char *file, + unsigned line, const char *function, + SCError error_code, const char *message) { - char *temp = *msg; + char buffer[SC_LOG_MAX_LOG_MSG_LEN] = ""; + char *temp = buffer; const char *s = NULL; struct timeval tval; @@ -284,20 +286,6 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, return SC_ERR_LOG_MODULE_NOT_INIT; } - if (sc_log_fg_filters_present == 1) { - if (SCLogMatchFGFilterWL(file, function, line) != 1) { - return SC_ERR_LOG_FG_FILTER_MATCH; - } - - if (SCLogMatchFGFilterBL(file, function, line) != 1) { - return SC_ERR_LOG_FG_FILTER_MATCH; - } - } - - if (sc_log_fd_filters_present == 1 && SCLogMatchFDFilter(function) != 1) { - return SC_ERR_LOG_FG_FILTER_MATCH; - } - char *temp_fmt = strdup(sc_log_config->log_format); if (unlikely(temp_fmt == NULL)) { return SC_ERR_MEM_ALLOC; @@ -306,10 +294,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, char *substr = temp_fmt; while ( (temp_fmt = index(temp_fmt, SC_LOG_FMT_PREFIX)) ) { - if ((temp - *msg) > SC_LOG_MAX_LOG_MSG_LEN) { - printf("Warning: Log message exceeded message length limit of %d\n", - SC_LOG_MAX_LOG_MSG_LEN); - *msg = *msg + SC_LOG_MAX_LOG_MSG_LEN; + if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { if (temp_fmt_h != NULL) SCFree(temp_fmt_h); return SC_OK; @@ -322,7 +307,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, struct tm local_tm; tms = SCLocalTime(tval.tv_sec, &local_tm); - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%d/%d/%04d -- %02d:%02d:%02d", substr, tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min, @@ -337,7 +322,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, case SC_LOG_FMT_PID: temp_fmt[0] = '\0'; - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%u", substr, getpid()); if (cw < 0) goto error; @@ -349,7 +334,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, case SC_LOG_FMT_TID: temp_fmt[0] = '\0'; - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%lu", substr, SCGetThreadIdLong()); if (cw < 0) goto error; @@ -369,7 +354,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), "%s%s", substr, ((tv != NULL)? tv->name: "UNKNOWN TM")); #endif - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, "N/A"); if (cw < 0) goto error; @@ -382,10 +367,10 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, temp_fmt[0] = '\0'; s = SCMapEnumValueToName(log_level, sc_log_level_map); if (s != NULL) - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, s); else - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, "INVALID"); if (cw < 0) goto error; @@ -397,7 +382,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, case SC_LOG_FMT_FILE_NAME: temp_fmt[0] = '\0'; - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, file); if (cw < 0) goto error; @@ -409,7 +394,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, case SC_LOG_FMT_LINE: temp_fmt[0] = '\0'; - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%u", substr, line); if (cw < 0) goto error; @@ -421,7 +406,7 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, case SC_LOG_FMT_FUNCTION: temp_fmt[0] = '\0'; - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s%s", substr, function); if (cw < 0) goto error; @@ -434,31 +419,47 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, } temp_fmt++; } - if ((temp - *msg) > SC_LOG_MAX_LOG_MSG_LEN) { - printf("Warning: Log message exceeded message length limit of %d\n", - SC_LOG_MAX_LOG_MSG_LEN); - *msg = *msg + SC_LOG_MAX_LOG_MSG_LEN; + if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { if (temp_fmt_h != NULL) SCFree(temp_fmt_h); return SC_OK; } - cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - *msg), "%s", substr); + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s", substr); if (cw < 0) goto error; temp += cw; - if ((temp - *msg) > SC_LOG_MAX_LOG_MSG_LEN) { - printf("Warning: Log message exceeded message length limit of %d\n", - SC_LOG_MAX_LOG_MSG_LEN); - *msg = *msg + SC_LOG_MAX_LOG_MSG_LEN; + if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { if (temp_fmt_h != NULL) SCFree(temp_fmt_h); return SC_OK; } - *msg = temp; + if (error_code != SC_OK) { + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), + "[ERRCODE: %s(%d)] - ", SCErrorToString(error_code), error_code); + if (cw < 0) + goto error; + temp += cw; + if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { + if (temp_fmt_h != NULL) + SCFree(temp_fmt_h); + return SC_OK; + } + } + + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), "%s", message); + if (cw < 0) + goto error; + temp += cw; + if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { + if (temp_fmt_h != NULL) + SCFree(temp_fmt_h); + return SC_OK; + } SCFree(temp_fmt_h); + SCLogOutputBuffer(log_level, buffer); return SC_OK; error: diff --git a/src/util-debug.h b/src/util-debug.h index 5b0dd008ea..b73c390481 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -191,59 +191,45 @@ extern int sc_log_module_initialized; extern int sc_log_module_cleaned; -#define SCLog(x, ...) do { \ - char _sc_log_msg[SC_LOG_MAX_LOG_MSG_LEN] = ""; \ - char *_sc_log_temp = _sc_log_msg; \ - if ( !( \ - (sc_log_global_log_level >= x) && \ - SCLogMessage(x, &_sc_log_temp, \ - __FILE__, \ - __LINE__, \ - __FUNCTION__) \ - == SC_OK) ) \ - { } else { \ - snprintf(_sc_log_temp, \ - (SC_LOG_MAX_LOG_MSG_LEN - \ - (_sc_log_temp - _sc_log_msg)), \ - __VA_ARGS__); \ - SCLogOutputBuffer(x, _sc_log_msg); \ - } \ - } while(0) - -#define SCLogErr(x, err, ...) do { \ - char _sc_log_err_msg[SC_LOG_MAX_LOG_MSG_LEN] = ""; \ - char *_sc_log_err_temp = _sc_log_err_msg; \ - if ( !( \ - (sc_log_global_log_level >= x) && \ - SCLogMessage(x, &_sc_log_err_temp,\ - __FILE__, \ - __LINE__, \ - __FUNCTION__) \ - == SC_OK) ) \ - { } else { \ - _sc_log_err_temp = \ - _sc_log_err_temp + \ - snprintf(_sc_log_err_temp, \ - (SC_LOG_MAX_LOG_MSG_LEN - \ - (_sc_log_err_temp - _sc_log_err_msg)), \ - "[ERRCODE: %s(%d)] - ", \ - SCErrorToString(err), \ - err); \ - if ((_sc_log_err_temp - _sc_log_err_msg) > \ - SC_LOG_MAX_LOG_MSG_LEN) { \ - printf("Warning: Log message exceeded message length limit of %d\n",\ - SC_LOG_MAX_LOG_MSG_LEN); \ - _sc_log_err_temp = _sc_log_err_msg + \ - SC_LOG_MAX_LOG_MSG_LEN; \ - } else { \ - snprintf(_sc_log_err_temp, \ - (SC_LOG_MAX_LOG_MSG_LEN - \ - (_sc_log_err_temp - _sc_log_err_msg)), \ - __VA_ARGS__); \ - } \ - SCLogOutputBuffer(x, _sc_log_err_msg); \ - } \ - } while(0) +#define SCLog(x, ...) \ + do { \ + if (sc_log_global_log_level >= x && \ + (sc_log_fg_filters_present == 0 || \ + SCLogMatchFGFilterWL(__FILE__, __FUNCTION__, __LINE__) == 1 || \ + SCLogMatchFGFilterBL(__FILE__, __FUNCTION__, __LINE__) == 1) && \ + (sc_log_fd_filters_present == 0 || \ + SCLogMatchFDFilter(__FUNCTION__) == 1)) \ + { \ + char _sc_log_msg[SC_LOG_MAX_LOG_MSG_LEN] = ""; \ + \ + snprintf(_sc_log_msg, SC_LOG_MAX_LOG_MSG_LEN, __VA_ARGS__); \ + \ + SCLogMessage(x, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, SC_OK, _sc_log_msg); \ + } \ + } while(0) + +#define SCLogErr(x, err, ...) \ + do { \ + if (sc_log_global_log_level >= x && \ + (sc_log_fg_filters_present == 0 || \ + SCLogMatchFGFilterWL(__FILE__, __FUNCTION__, __LINE__) == 1 || \ + SCLogMatchFGFilterBL(__FILE__, __FUNCTION__, __LINE__) == 1) && \ + (sc_log_fd_filters_present == 0 || \ + SCLogMatchFDFilter(__FUNCTION__) == 1)) \ + { \ + char _sc_log_msg[SC_LOG_MAX_LOG_MSG_LEN] = ""; \ + \ + snprintf(_sc_log_msg, SC_LOG_MAX_LOG_MSG_LEN, __VA_ARGS__); \ + \ + SCLogMessage(x, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, err, _sc_log_msg); \ + } \ + } while(0) /** * \brief Macro used to log INFORMATIONAL messages. @@ -350,19 +336,10 @@ extern int sc_log_module_cleaned; * \retval f An argument can be supplied, although it is not used */ #define SCEnter(f) do { \ - char _sc_enter_msg[SC_LOG_MAX_LOG_MSG_LEN]; \ - char *_sc_enter_temp = _sc_enter_msg; \ if (sc_log_global_log_level >= SC_LOG_DEBUG &&\ - SCLogCheckFDFilterEntry(__FUNCTION__) && \ - SCLogMessage(SC_LOG_DEBUG, &_sc_enter_temp, \ - __FILE__, \ - __LINE__, \ - __FUNCTION__) == SC_OK) { \ - snprintf(_sc_enter_temp, (SC_LOG_MAX_LOG_MSG_LEN - \ - (_sc_enter_msg - _sc_enter_temp)), \ - "Entering ... >>"); \ - SCLogOutputBuffer(SC_LOG_DEBUG, \ - _sc_enter_msg); \ + SCLogCheckFDFilterEntry(__FUNCTION__)) \ + { \ + SCLogDebug("Entering ... >>"); \ } \ } while(0) @@ -536,9 +513,7 @@ void SCLogInitLogModuleIfEnvSet(void); void SCLogDeInitLogModule(void); -SCError SCLogMessage(SCLogLevel, char **, const char *, unsigned, const char *); - -void SCLogOutputBuffer(SCLogLevel, char *); +SCError SCLogMessage(SCLogLevel, const char *, unsigned, const char *, SCError, const char *message); SCLogOPBuffer *SCLogAllocLogOPBuffer(void);