implementation custom flag for pattern format

pull/623/head
fegomes 8 years ago
parent 22fdd3bf0f
commit a441c4ce3d

@ -11,6 +11,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <unordered_map>
namespace spdlog namespace spdlog
{ {
@ -19,10 +20,11 @@ namespace details
struct log_msg struct log_msg
{ {
log_msg() = default; log_msg() = default;
log_msg(const std::string *loggers_name, level::level_enum lvl) : log_msg(const std::string *loggers_name, level::level_enum lvl, std::unordered_map<char, std::string>* flags) :
logger_name(loggers_name), logger_name(loggers_name),
level(lvl), level(lvl),
msg_id(0) msg_id(0),
custom_flags(flags)
{ {
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
time = os::now(); time = os::now();
@ -45,6 +47,7 @@ struct log_msg
fmt::MemoryWriter raw; fmt::MemoryWriter raw;
fmt::MemoryWriter formatted; fmt::MemoryWriter formatted;
size_t msg_id; size_t msg_id;
std::unordered_map<char, std::string>* custom_flags;
}; };
} }
} }

@ -64,7 +64,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar
try try
{ {
details::log_msg log_msg(&_name, lvl); details::log_msg log_msg(&_name, lvl, &_custom_flags);
#if defined(SPDLOG_FMT_PRINTF) #if defined(SPDLOG_FMT_PRINTF)
fmt::printf(log_msg.raw, fmt, args...); fmt::printf(log_msg.raw, fmt, args...);
@ -90,7 +90,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* msg)
if (!should_log(lvl)) return; if (!should_log(lvl)) return;
try try
{ {
details::log_msg log_msg(&_name, lvl); details::log_msg log_msg(&_name, lvl, &_custom_flags);
log_msg.raw << msg; log_msg.raw << msg;
_sink_it(log_msg); _sink_it(log_msg);
} }
@ -111,7 +111,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T& msg)
if (!should_log(lvl)) return; if (!should_log(lvl)) return;
try try
{ {
details::log_msg log_msg(&_name, lvl); details::log_msg log_msg(&_name, lvl,&_custom_flags);
log_msg.raw << msg; log_msg.raw << msg;
_sink_it(log_msg); _sink_it(log_msg);
} }
@ -279,6 +279,25 @@ inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
_level.store(log_level); _level.store(log_level);
} }
inline void spdlog::logger::set_custom_flag(char flag,const std::string& value)
{
_custom_flags[flag] = value;
}
inline const std::string& spdlog::logger::value_custom_flag(char flag)
{
auto end = _custom_flags.end();
auto it = _custom_flags.find(flag);
if (it != end)
{
return it->second;
}
else
{
return "";
}
}
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler) inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
{ {
_err_handler = err_handler; _err_handler = err_handler;

@ -414,6 +414,27 @@ private:
char _ch; char _ch;
}; };
class custom_formatter SPDLOG_FINAL:public flag_formatter
{
public:
explicit custom_formatter(char flag) : _flag(_flag)
{}
void format(details::log_msg& msg, const std::tm&) override
{
auto end = msg.custom_flags->end();
auto it = msg.custom_flags->find(_flag);
if (it != end)
{
msg.formatted << it->second;
}
else {
msg.formatted << _flag;
}
}
private:
char _flag;
};
//aggregate user chars to display as is //aggregate user chars to display as is
class aggregate_formatter SPDLOG_FINAL:public flag_formatter class aggregate_formatter SPDLOG_FINAL:public flag_formatter
@ -655,8 +676,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
break; break;
default: //Unknown flag appears as is default: //Unknown flag appears as is
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::custom_formatter(flag)));
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter(flag)));
break; break;
} }
} }

@ -155,6 +155,28 @@ public:
_level = log_level; _level = log_level;
} }
void set_custom_flag(char flag, const std::string& value)
{
std::lock_guard<Mutex> lock(_mutex);
for (auto& l : _loggers)
l.second->set_custom_flag(flag, value);
_custom_flags[flag] = value;
}
const std::string& value_custom_flag(char flag)
{
auto end = _custom_flags.end();
auto it = _custom_flags.find(flag);
if (it != end)
{
return it->second;
}
else
{
return std::move(std::string(""));
}
}
void flush_on(level::level_enum log_level) void flush_on(level::level_enum log_level)
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
@ -215,6 +237,7 @@ private:
std::function<void()> _worker_warmup_cb = nullptr; std::function<void()> _worker_warmup_cb = nullptr;
std::chrono::milliseconds _flush_interval_ms; std::chrono::milliseconds _flush_interval_ms;
std::function<void()> _worker_teardown_cb = nullptr; std::function<void()> _worker_teardown_cb = nullptr;
std::unordered_map<char, std::string> _custom_flags;
}; };
#ifdef SPDLOG_NO_REGISTRY_MUTEX #ifdef SPDLOG_NO_REGISTRY_MUTEX
typedef registry_t<spdlog::details::null_mutex> registry; typedef registry_t<spdlog::details::null_mutex> registry;

@ -236,6 +236,16 @@ inline void spdlog::set_level(level::level_enum log_level)
return details::registry::instance().set_level(log_level); return details::registry::instance().set_level(log_level);
} }
inline void spdlog::set_custom_flag(char flag, const std::string& value)
{
return details::registry::instance().set_custom_flag(flag, value);
}
inline const std::string& spdlog::value_custom_flag(char flag)
{
return details::registry::instance().value_custom_flag(flag);
}
inline void spdlog::flush_on(level::level_enum log_level) inline void spdlog::flush_on(level::level_enum log_level)
{ {
return details::registry::instance().flush_on(log_level); return details::registry::instance().flush_on(log_level);

@ -18,6 +18,7 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
namespace spdlog namespace spdlog
{ {
@ -66,6 +67,8 @@ public:
bool should_log(level::level_enum) const; bool should_log(level::level_enum) const;
void set_level(level::level_enum); void set_level(level::level_enum);
void set_custom_flag(char flag, const std::string& value);
const std::string& value_custom_flag(char flag);
level::level_enum level() const; level::level_enum level() const;
const std::string& name() const; const std::string& name() const;
void set_pattern(const std::string&, pattern_time_type = pattern_time_type::local); void set_pattern(const std::string&, pattern_time_type = pattern_time_type::local);
@ -104,6 +107,7 @@ protected:
log_err_handler _err_handler; log_err_handler _err_handler;
std::atomic<time_t> _last_err_time; std::atomic<time_t> _last_err_time;
std::atomic<size_t> _msg_counter; std::atomic<size_t> _msg_counter;
std::unordered_map<char, std::string> _custom_flags;
}; };
} }

@ -50,6 +50,15 @@ void flush_on(level::level_enum log_level);
// //
void set_error_handler(log_err_handler); void set_error_handler(log_err_handler);
//
// Set custom flag
//
void set_custom_flag(char flag, const std::string& value);
//
// value custom flag
//
const std::string& value_custom_flag(char flag);
// //
// Turn on async mode (off by default) and set the queue size for each async_logger. // Turn on async mode (off by default) and set the queue size for each async_logger.
// effective only for loggers created after this call. // effective only for loggers created after this call.

Loading…
Cancel
Save