MXS-2014 Additional cleanup of log.cc

This commit is contained in:
Johan Wikman
2018-08-15 16:38:19 +03:00
parent 5b70c7605c
commit 79b292dc39

View File

@ -26,34 +26,28 @@
#include <maxbase/assert.h> #include <maxbase/assert.h>
#include <maxbase/logger.hh> #include <maxbase/logger.hh>
// Number of chars needed to represent a number.
#define CALCLEN(i) ((size_t)(floor(log10(abs((int64_t)i))) + 1))
#define UINTLEN(i) (i < 10 ? 1 : (i < 100 ? 2 : (i < 1000 ? 3 : CALCLEN(i))))
/**
* Default augmentation.
*/
static int DEFAULT_LOG_AUGMENTATION = 0;
// A message that is logged 10 times in 1 second will be suppressed for 10 seconds.
static MXB_LOG_THROTTLING DEFAULT_LOG_THROTTLING = { 10, 1000, 10000 };
/** /**
* Variable holding the enabled priorities information. * Variable holding the enabled priorities information.
* Used from logging macros. * Used from logging macros.
*/ */
int mxb_log_enabled_priorities = (1 << LOG_ERR) | (1 << LOG_NOTICE) | (1 << LOG_WARNING); int mxb_log_enabled_priorities = (1 << LOG_ERR) | (1 << LOG_NOTICE) | (1 << LOG_WARNING);
/** // Number of chars needed to represent a number.
* BUFSIZ comes from the system. It equals with block size or #define CALCLEN(i) ((size_t)(floor(log10(abs((int64_t)i))) + 1))
* its multiplication. #define UINTLEN(i) (i < 10 ? 1 : (i < 100 ? 2 : (i < 1000 ? 3 : CALCLEN(i))))
*/
#define MAX_LOGSTRLEN BUFSIZ
/** namespace
* Returns the current time. {
*
* @return Current monotonic raw time in milliseconds. int DEFAULT_LOG_AUGMENTATION = 0;
*/
// A message that is logged 10 times in 1 second will be suppressed for 10 seconds.
static MXB_LOG_THROTTLING DEFAULT_LOG_THROTTLING = { 10, 1000, 10000 };
// BUFSIZ comes from the system. It equals with block size or its multiplication.
const int MAX_LOGSTRLEN = BUFSIZ;
// Current monotonic raw time in milliseconds.
static uint64_t time_monotonic_ms() static uint64_t time_monotonic_ms()
{ {
struct timespec now; struct timespec now;
@ -62,15 +56,12 @@ static uint64_t time_monotonic_ms()
return now.tv_sec * 1000 + now.tv_nsec / 1000000; return now.tv_sec * 1000 + now.tv_nsec / 1000000;
} }
namespace enum message_suppression_t
{
typedef enum message_suppression
{ {
MESSAGE_NOT_SUPPRESSED, // Message is not suppressed. MESSAGE_NOT_SUPPRESSED, // Message is not suppressed.
MESSAGE_SUPPRESSED, // Message is suppressed for the first time (for this round) MESSAGE_SUPPRESSED, // Message is suppressed for the first time (for this round)
MESSAGE_STILL_SUPPRESSED // Message is still suppressed (for this round) MESSAGE_STILL_SUPPRESSED // Message is still suppressed (for this round)
} message_suppression_t; };
class MessageRegistryKey class MessageRegistryKey
{ {
@ -279,6 +270,7 @@ struct this_unit
MXB_LOG_THROTTLING throttling; // Can change during the lifetime of log_manager. MXB_LOG_THROTTLING throttling; // Can change during the lifetime of log_manager.
std::unique_ptr<mxb::Logger> sLogger; std::unique_ptr<mxb::Logger> sLogger;
std::unique_ptr<MessageRegistry> sMessage_registry; std::unique_ptr<MessageRegistry> sMessage_registry;
size_t (*context_provider)(char* buffer, size_t len);
} this_unit = } this_unit =
{ {
DEFAULT_LOG_AUGMENTATION, // augmentation DEFAULT_LOG_AUGMENTATION, // augmentation
@ -290,8 +282,6 @@ struct this_unit
} }
static size_t (*get_context)(char* buffer, size_t len);
/** /**
* Initializes log manager * Initializes log manager
* *
@ -305,7 +295,7 @@ bool mxb_log_init(const char* ident,
const char* logdir, const char* logdir,
const char* filename, const char* filename,
mxb_log_target_t target, mxb_log_target_t target,
size_t (*gc)(char*, size_t)) size_t (*provide_context)(char*, size_t))
{ {
mxb_assert(!this_unit.sLogger && !this_unit.sMessage_registry); mxb_assert(!this_unit.sLogger && !this_unit.sMessage_registry);
@ -354,7 +344,7 @@ bool mxb_log_init(const char* ident,
break; break;
} }
get_context = gc; this_unit.context_provider = provide_context;
return this_unit.sLogger && this_unit.sMessage_registry; return this_unit.sLogger && this_unit.sMessage_registry;
} }
@ -732,9 +722,9 @@ int mxb_log_message(int priority,
char context[32]; // The documentation will guarantee a buffer of at least 32 bytes. char context[32]; // The documentation will guarantee a buffer of at least 32 bytes.
int context_len = 0; int context_len = 0;
if (get_context) if (this_unit.context_provider)
{ {
context_len = get_context(context, sizeof(context)); context_len = this_unit.context_provider(context, sizeof(context));
if (context_len != 0) if (context_len != 0)
{ {