From a191ed85a2472db05929562f911e29247e35aadc Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 15 Aug 2018 13:22:57 +0300 Subject: [PATCH] MXS-2014 Expose everything needed by the MaxScale log manager --- maxutils/maxbase/include/maxbase/log.h | 89 ++++++++++++++++++++------ maxutils/maxbase/src/log.cc | 75 ++++++++++++++-------- 2 files changed, 118 insertions(+), 46 deletions(-) diff --git a/maxutils/maxbase/include/maxbase/log.h b/maxutils/maxbase/include/maxbase/log.h index 808e331e1..42661825d 100644 --- a/maxutils/maxbase/include/maxbase/log.h +++ b/maxutils/maxbase/include/maxbase/log.h @@ -99,10 +99,80 @@ bool mxb_log_init(const char* ident, size_t (*get_context)(char*, size_t)); void mxb_log_finish(void); bool mxb_log_rotate(); -int mxb_log_set_priority_enabled(int priority, bool enabled); + +/** + * Get log filename + * + * @return The current filename. + * + * @attention This function can be called only after @c mxs_log_init() has + * been called and @c mxs_log_finish() has not been called. The + * returned filename stays valid only until @c mxs_log_finish() + * is called. + */ +const char* mxb_log_get_filename(); + +/** + * Enable/disable a particular syslog priority. + * + * @param priority One of the LOG_ERR etc. constants from sys/syslog.h. + * @param enabled True if the priority should be enabled, false if it should be disabled. + * + * @return True if the priority was valid, false otherwise. + */ +bool mxb_log_set_priority_enabled(int priority, bool enabled); + +/** + * Query whether a particular syslog priority is enabled. + * + * @param priority One of the LOG_ERR etc. constants from sys/syslog.h. + * + * @return True if enabled, false otherwise. + */ +bool mxs_log_is_priority_enabled(int priority); + +/** + * Enable/disable syslog logging. + * + * @param enabled True, if syslog logging should be enabled, false if it should be disabled. + */ void mxb_log_set_syslog_enabled(bool enabled); + +/** + * Is syslog logging enabled. + * + * @return True if enabled, false otherwise. + */ +bool mxb_log_is_syslog_enabled(); + +/** + * Enable/disable maxscale log logging. + * + * @param enabled True, if maxlog logging should be enabled, false if it should be disabled. + */ void mxb_log_set_maxlog_enabled(bool enabled); + +/** + * Is maxlog logging enabled. + * + * @return True if enabled, false otherwise. + */ +bool mxb_log_is_maxlog_enabled(); + +/** + * Enable/disable highprecision logging. + * + * @param enabled True, if high precision logging should be enabled, false if it should be disabled. + */ void mxb_log_set_highprecision_enabled(bool enabled); + +/** + * Is highprecision logging enabled. + * + * @return True if enabled, false otherwise. + */ +bool mxb_log_is_highprecision_enabled(); + void mxb_log_set_augmentation(int bits); void mxb_log_set_throttling(const MXB_LOG_THROTTLING* throttling); @@ -198,21 +268,4 @@ enum trailing NULL. If longer, it will be cut. */ }; -/** - * Return a thread specific pointer to a string describing the error - * code passed as argument. The string is obtained using strerror_r. - * - * @param error One of the errno error codes. - * - * @return Thread specific pointer to string describing the error code. - * - * @attention The function is thread safe, but not re-entrant. That is, - * calling it twice with different error codes between two sequence points - * will not work. E.g: - * - * printf("EINVAL = %s, EACCESS = %s", - * mxb_strerror(EINVAL), mxb_strerror(EACCESS)); - */ -const char* mxb_strerror(int error); - MXB_END_DECLS diff --git a/maxutils/maxbase/src/log.cc b/maxutils/maxbase/src/log.cc index 52574b1e6..d3ca068ed 100644 --- a/maxutils/maxbase/src/log.cc +++ b/maxutils/maxbase/src/log.cc @@ -379,7 +379,7 @@ void mxb_log_finish(void) closelog(); } -std::string get_timestamp(void) +static std::string get_timestamp(void) { time_t t = time(NULL); struct tm tm; @@ -397,7 +397,7 @@ std::string get_timestamp(void) return buf; } -std::string get_timestamp_hp(void) +static std::string get_timestamp_hp(void) { struct timeval tv; gettimeofday(&tv, NULL); @@ -458,11 +458,6 @@ void mxb_log_set_augmentation(int bits) log_config.augmentation = bits & MXB_LOG_AUGMENTATION_MASK; } -/** - * Enable/disable syslog logging. - * - * @param enabled True, if high precision logging should be enabled, false if it should be disabled. - */ void mxb_log_set_highprecision_enabled(bool enabled) { log_config.do_highprecision = enabled; @@ -470,11 +465,11 @@ void mxb_log_set_highprecision_enabled(bool enabled) MXB_NOTICE("highprecision logging is %s.", enabled ? "enabled" : "disabled"); } -/** - * Enable/disable syslog logging. - * - * @param enabled True, if syslog logging should be enabled, false if it should be disabled. - */ +bool mxb_log_is_highprecision_enabled() +{ + return log_config.do_highprecision; +} + void mxb_log_set_syslog_enabled(bool enabled) { log_config.do_syslog = enabled; @@ -482,11 +477,11 @@ void mxb_log_set_syslog_enabled(bool enabled) MXB_NOTICE("syslog logging is %s.", enabled ? "enabled" : "disabled"); } -/** - * Enable/disable maxscale log logging. - * - * @param enabled True, if syslog logging should be enabled, false if it should be disabled. - */ +bool mxb_log_is_syslog_enabled() +{ + return log_config.do_syslog; +} + void mxb_log_set_maxlog_enabled(bool enabled) { log_config.do_maxlog = enabled; @@ -494,6 +489,11 @@ void mxb_log_set_maxlog_enabled(bool enabled) MXB_NOTICE("maxlog logging is %s.", enabled ? "enabled" : "disabled"); } +bool mxb_log_is_maxlog_enabled() +{ + return log_config.do_maxlog; +} + /** * Set the log throttling parameters. * @@ -544,6 +544,11 @@ bool mxb_log_rotate() return logger->rotate(); } +const char* mxb_log_get_filename() +{ + return logger->filename(); +} + static const char* level_name(int level) { switch (level) @@ -570,17 +575,9 @@ static const char* level_name(int level) } } -/** - * Enable/disable a particular syslog priority. - * - * @param priority One of the LOG_ERR etc. constants from sys/syslog.h. - * @param enabled True if the priority should be enabled, false if it to be disabled. - * - * @return 0 if the priority was valid, -1 otherwise. - */ -int mxb_log_set_priority_enabled(int level, bool enable) +bool mxb_log_set_priority_enabled(int level, bool enable) { - int rv = -1; + bool rv = false; const char* text = (enable ? "enable" : "disable"); if ((level & ~LOG_PRIMASK) == 0) @@ -598,6 +595,7 @@ int mxb_log_set_priority_enabled(int level, bool enable) } MXB_NOTICE("The logging of %s messages has been %sd.", level_name(level), text); + rv = true; } else { @@ -607,6 +605,27 @@ int mxb_log_set_priority_enabled(int level, bool enable) return rv; } +bool mxb_log_is_priority_enabled(int level) +{ + bool rv = false; + + if ((level & ~LOG_PRIMASK) == 0) + { + int bit = (1 << level); + + if (mxb_log_enabled_priorities & bit) + { + rv = true; + } + } + else + { + MXB_ERROR("Attempt to query unknown syslog priority %d.", level); + } + + return rv; +} + typedef struct log_prefix { const char* text; // The prefix, e.g. "error: " @@ -713,7 +732,7 @@ static message_suppression_t message_status(const char* file, int line) * @param format The printf format of the following arguments. * @param ... Optional arguments according to the format. */ -int mxs_log_message(int priority, +int mxb_log_message(int priority, const char* modname, const char* file, int line, const char* function, const char* format, ...)