From b84dbd8d3fcf7614c59b026108c755d62c3c69de Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 8 Sep 2015 15:02:42 +0300 Subject: [PATCH] MXS-357: Possibility to turn off log message augmentation. It's not always desireable to have the function name appended to every logged line. --- log_manager/log_manager.cc | 53 +++++++++++++++++++++++++++++++++----- log_manager/log_manager.h | 17 ++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 793e65651..c5c1f7bd6 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -103,6 +103,12 @@ static bool flushall_flag; static bool flushall_started_flag; static bool flushall_done_flag; +/** + * Default augmentation. + */ +static int default_log_augmentation = LOG_AUGMENT_WITH_FUNCTION; +static int log_augmentation = default_log_augmentation; + /** Writer thread structure */ struct filewriter_st { #if defined(SS_DEBUG) @@ -1359,6 +1365,16 @@ return_succp: return succp; } +void skygw_log_set_augmentation(int bits) +{ + log_augmentation = bits & LOG_AUGMENTATION_MASK; +} + +int skygw_log_get_augmentation() +{ + return log_augmentation; +} + /** * Helper for skygw_log_write and friends. * @@ -1386,16 +1402,41 @@ static int log_write(logfile_id_t id, { CHK_LOGMANAGER(lm); - const char format[] = "%s [%s]"; - len += sizeof(format); // A bit too much, but won't hurt. - assert(function); - len += strlen(function); + const char* format; + + if (log_augmentation == LOG_AUGMENT_WITH_FUNCTION) + { + static const char function_format[] = "%s [%s]"; + + format = function_format; + + len += sizeof(function_format); // A little bit more than needed, but won't hurt. + assert(function); + len += strlen(function); + } + else + { + static const char default_format[] = "%s"; + + format = default_format; + + len += sizeof(default_format); // A little bit more than needed, but won't hurt. + } + len += 1; // For the trailing NULL. char message[len]; - len = snprintf(message, sizeof(message), format, str, function); - assert(len > 0); + if (log_augmentation == LOG_AUGMENT_WITH_FUNCTION) + { + len = snprintf(message, sizeof(message), format, str, function); + } + else + { + len = snprintf(message, sizeof(message), format, str); + } + + assert(len >= 0); int attempts = 0; int successes = 0; diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index e2ec60650..dfc83cb8b 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -108,6 +108,15 @@ typedef struct log_info_st */ typedef enum { UNINIT = 0, INIT, RUN, DONE } flat_obj_state_t; +/** + * LOG_AUGMENT_WITH_FUNCTION Each logged line is suffixed with [function-name]. + */ +typedef enum +{ + LOG_AUGMENT_WITH_FUNCTION = 1, + LOG_AUGMENTATION_MASK = (LOG_AUGMENT_WITH_FUNCTION) +} log_augmentation_t; + EXTERN_C_BLOCK_BEGIN bool skygw_logmanager_init(int argc, char* argv[]); @@ -140,6 +149,14 @@ void logmanager_enable_maxscalelog(int); #define skygw_log_write_flush(id, format, ...)\ skygw_log_write_context_flush(id, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__) +/** + * What augmentation if any should a logged message be augmented with. + * + * Currently this is a global setting and affects all loggers. + */ +void skygw_log_set_augmentation(int bits); +int skygw_log_get_augmentation(); + EXTERN_C_BLOCK_END const char* get_trace_prefix_default(void);