Skip to content

Commit

Permalink
Add prefix argument to logger callback.
Browse files Browse the repository at this point in the history
Refs #148
  • Loading branch information
ayurchen committed Dec 11, 2020
1 parent dcf3ce9 commit 515ac81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
16 changes: 11 additions & 5 deletions include/wsrep/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace wsrep
debug,
info,
warning,
error
error,
unknown
};

enum debug_level
Expand All @@ -55,8 +56,12 @@ namespace wsrep

/**
* Signature for user defined logger callback function.
*
* @param pfx optional internally defined prefix for the message
* @param msg message to log
*/
typedef void (*logger_fn_type)(level, const char*);
typedef void (*logger_fn_type)(level l,
const char* pfx, const char* msg);

static const char* to_c_string(enum level level)
{
Expand All @@ -66,11 +71,12 @@ namespace wsrep
case info: return "info";
case warning: return "warning";
case error: return "error";
case unknown: break;
};
return "unknown";
}

log(enum wsrep::log::level level, const char* prefix = "")
log(enum wsrep::log::level level, const char* prefix = "L:")
: level_(level)
, prefix_(prefix)
, oss_()
Expand All @@ -80,12 +86,12 @@ namespace wsrep
{
if (logger_fn_)
{
logger_fn_(level_, oss_.str().c_str());
logger_fn_(level_, prefix_, oss_.str().c_str());
}
else
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
os_ << prefix_ << ": " << oss_.str() << std::endl;
os_ << prefix_ << oss_.str() << std::endl;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/wsrep_provider_v26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,22 +564,25 @@ namespace

void logger_cb(wsrep_log_level_t level, const char* msg)
{
static const char* const pfx("P:"); // "provider"
wsrep::log::level ll(wsrep::log::unknown);
switch (level)
{
case WSREP_LOG_FATAL:
case WSREP_LOG_ERROR:
wsrep::log_error() << msg;
ll = wsrep::log::error;
break;
case WSREP_LOG_WARN:
wsrep::log_warning() << msg;
ll = wsrep::log::warning;
break;
case WSREP_LOG_INFO:
wsrep::log_info() << msg;
ll = wsrep::log::info;
break;
case WSREP_LOG_DEBUG:
wsrep::log_debug() << msg;
ll = wsrep::log::debug;
break;
}
wsrep::log(ll, pfx) << msg;
}

static int init_thread_service(void* dlh,
Expand Down
3 changes: 2 additions & 1 deletion test/wsrep-lib_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ static std::string debug_log_level;


static void log_fn(wsrep::log::level level,
const char* pfx,
const char* msg)
{
log_file << wsrep::log::to_c_string(level) << ": " << msg << std::endl;
log_file << wsrep::log::to_c_string(level) << " " << pfx << msg << std::endl;
}

static bool parse_arg(const std::string& arg)
Expand Down

0 comments on commit 515ac81

Please sign in to comment.