This commit is contained in:
gabime 2016-08-05 04:43:20 +03:00
parent bdbe908693
commit e7debaacd7
11 changed files with 715 additions and 691 deletions

View File

@ -134,7 +134,8 @@ void user_defined_example()
void err_handler_example() void err_handler_example()
{ {
//can be set globaly or per logger(logger->set_error_handler(..)) //can be set globaly or per logger(logger->set_error_handler(..))
spdlog::set_error_handler([](const std::string& msg) { spdlog::set_error_handler([](const std::string& msg)
{
std::cerr << "my err handler: " << msg << std::endl; std::cerr << "my err handler: " << msg << std::endl;
}); });
spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3); spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3);

View File

@ -261,10 +261,12 @@ inline void spdlog::details::async_log_helper::worker_loop()
while(process_next_msg(last_pop, last_flush)); while(process_next_msg(last_pop, last_flush));
if (_worker_teardown_cb) _worker_teardown_cb(); if (_worker_teardown_cb) _worker_teardown_cb();
} }
catch (const std::exception &ex) { catch (const std::exception &ex)
{
_err_handler(ex.what()); _err_handler(ex.what());
} }
catch (...) { catch (...)
{
_err_handler("Unknown exception"); _err_handler("Unknown exception");
} }
} }

View File

@ -77,10 +77,12 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
{ {
_async_log_helper->log(msg); _async_log_helper->log(msg);
} }
catch (const std::exception &ex) { catch (const std::exception &ex)
{
_err_handler(ex.what()); _err_handler(ex.what());
} }
catch (...) { catch (...)
{
_err_handler("Unknown exception"); _err_handler("Unknown exception");
} }
} }

View File

@ -23,7 +23,10 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c
_level = level::info; _level = level::info;
_flush_level = level::off; _flush_level = level::off;
_last_err_time = 0; _last_err_time = 0;
_err_handler = [this](const std::string &msg) { this->_default_err_handler(msg);}; _err_handler = [this](const std::string &msg)
{
this->_default_err_handler(msg);
};
} }
// ctor with sinks as init list // ctor with sinks as init list
@ -35,9 +38,9 @@ inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list si
// ctor with single sink // ctor with single sink
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink): inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink):
logger(logger_name, logger(logger_name,
{ {
single_sink single_sink
}) })
{} {}
@ -60,15 +63,18 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar
{ {
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);
log_msg.raw.write(fmt, args...); log_msg.raw.write(fmt, args...);
_sink_it(log_msg); _sink_it(log_msg);
} }
catch (const std::exception &ex) { catch (const std::exception &ex)
{
_err_handler(ex.what()); _err_handler(ex.what());
} }
catch (...) { catch (...)
{
_err_handler("Unknown exception"); _err_handler("Unknown exception");
} }
} }
@ -77,15 +83,18 @@ template <typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char* msg) 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);
log_msg.raw << msg; log_msg.raw << msg;
_sink_it(log_msg); _sink_it(log_msg);
} }
catch (const std::exception &ex) { catch (const std::exception &ex)
{
_err_handler(ex.what()); _err_handler(ex.what());
} }
catch (...) { catch (...)
{
_err_handler("Unknown exception"); _err_handler("Unknown exception");
} }
@ -95,15 +104,18 @@ template<typename T>
inline void spdlog::logger::log(level::level_enum lvl, const T& msg) 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);
log_msg.raw << msg; log_msg.raw << msg;
_sink_it(log_msg); _sink_it(log_msg);
} }
catch (const std::exception &ex) { catch (const std::exception &ex)
{
_err_handler(ex.what()); _err_handler(ex.what());
} }
catch (...) { catch (...)
{
_err_handler("Unknown exception"); _err_handler("Unknown exception");
} }
} }

View File

@ -210,7 +210,7 @@ inline size_t filesize(FILE *f)
#else // unix #else // unix
int fd = fileno(f); int fd = fileno(f);
//64 bits(but not in osx, where fstat64 is deprecated) //64 bits(but not in osx, where fstat64 is deprecated)
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__))
struct stat64 st; struct stat64 st;
if (fstat64(fd, &st) == 0) if (fstat64(fd, &st) == 0)
return st.st_size; return st.st_size;

View File

@ -21,288 +21,288 @@
namespace spdlog namespace spdlog
{ {
namespace details namespace details
{ {
class flag_formatter class flag_formatter
{ {
public: public:
virtual ~flag_formatter() virtual ~flag_formatter()
{} {}
virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0; virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0;
}; };
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// name & level pattern appenders // name & level pattern appenders
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
namespace namespace
{ {
class name_formatter:public flag_formatter class name_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << *msg.logger_name; msg.formatted << *msg.logger_name;
} }
}; };
} }
// log level appender // log level appender
class level_formatter:public flag_formatter class level_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << level::to_str(msg.level); msg.formatted << level::to_str(msg.level);
} }
}; };
// short log level appender // short log level appender
class short_level_formatter:public flag_formatter class short_level_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << level::to_short_str(msg.level); msg.formatted << level::to_short_str(msg.level);
} }
}; };
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// Date time pattern appenders // Date time pattern appenders
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
static const char* ampm(const tm& t) static const char* ampm(const tm& t)
{ {
return t.tm_hour >= 12 ? "PM" : "AM"; return t.tm_hour >= 12 ? "PM" : "AM";
} }
static int to12h(const tm& t) static int to12h(const tm& t)
{ {
return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour;
} }
//Abbreviated weekday name //Abbreviated weekday name
static const std::string days[]{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
class a_formatter:public flag_formatter class a_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << days[tm_time.tm_wday]; msg.formatted << days[tm_time.tm_wday];
} }
}; };
//Full weekday name //Full weekday name
static const std::string full_days[]{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
class A_formatter:public flag_formatter class A_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << full_days[tm_time.tm_wday]; msg.formatted << full_days[tm_time.tm_wday];
} }
}; };
//Abbreviated month //Abbreviated month
static const std::string months[]{ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" }; static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
class b_formatter:public flag_formatter class b_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << months[tm_time.tm_mon]; msg.formatted << months[tm_time.tm_mon];
} }
}; };
//Full month name //Full month name
static const std::string full_months[]{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
class B_formatter:public flag_formatter class B_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << full_months[tm_time.tm_mon]; msg.formatted << full_months[tm_time.tm_mon];
} }
}; };
//write 2 ints seperated by sep with padding of 2 //write 2 ints seperated by sep with padding of 2
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, char sep) static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, char sep)
{ {
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0'); w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0');
return w; return w;
} }
//write 3 ints seperated by sep with padding of 2 //write 3 ints seperated by sep with padding of 2
static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, int v3, char sep) static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, int v3, char sep)
{ {
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0'); w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0');
return w; return w;
} }
//Date and time representation (Thu Aug 23 15:35:46 2014) //Date and time representation (Thu Aug 23 15:35:46 2014)
class c_formatter:public flag_formatter class c_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' '; msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900; pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
} }
}; };
// year - 2 digit // year - 2 digit
class C_formatter:public flag_formatter class C_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
} }
}; };
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01 // Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
class D_formatter:public flag_formatter class D_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/'); pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
} }
}; };
// year - 4 digit // year - 4 digit
class Y_formatter:public flag_formatter class Y_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << tm_time.tm_year + 1900; msg.formatted << tm_time.tm_year + 1900;
} }
}; };
// month 1-12 // month 1-12
class m_formatter:public flag_formatter class m_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
} }
}; };
// day of month 1-31 // day of month 1-31
class d_formatter:public flag_formatter class d_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
} }
}; };
// hours in 24 format 0-23 // hours in 24 format 0-23
class H_formatter:public flag_formatter class H_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
} }
}; };
// hours in 12 format 1-12 // hours in 12 format 1-12
class I_formatter:public flag_formatter class I_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(to12h(tm_time), 2, '0'); msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
} }
}; };
// minutes 0-59 // minutes 0-59
class M_formatter:public flag_formatter class M_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_min, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
} }
}; };
// seconds 0-59 // seconds 0-59
class S_formatter:public flag_formatter class S_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0'); msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
} }
}; };
// milliseconds // milliseconds
class e_formatter:public flag_formatter class e_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000; auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0'); msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0');
} }
}; };
// microseconds // microseconds
class f_formatter:public flag_formatter class f_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000; auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
msg.formatted << fmt::pad(static_cast<int>(micros), 6, '0'); msg.formatted << fmt::pad(static_cast<int>(micros), 6, '0');
} }
}; };
// nanoseconds // nanoseconds
class F_formatter:public flag_formatter class F_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000; auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
msg.formatted << fmt::pad(static_cast<int>(ns), 9, '0'); msg.formatted << fmt::pad(static_cast<int>(ns), 9, '0');
} }
}; };
// AM/PM // AM/PM
class p_formatter:public flag_formatter class p_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << ampm(tm_time); msg.formatted << ampm(tm_time);
} }
}; };
// 12 hour clock 02:55:02 pm // 12 hour clock 02:55:02 pm
class r_formatter:public flag_formatter class r_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time); pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
} }
}; };
// 24-hour HH:MM time, equivalent to %H:%M // 24-hour HH:MM time, equivalent to %H:%M
class R_formatter:public flag_formatter class R_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':'); pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
} }
}; };
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S // ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
class T_formatter:public flag_formatter class T_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':'); pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
} }
}; };
// ISO 8601 offset from UTC in timezone (+-HH:MM) // ISO 8601 offset from UTC in timezone (+-HH:MM)
class z_formatter:public flag_formatter class z_formatter:public flag_formatter
{ {
public: public:
const std::chrono::seconds cache_refresh = std::chrono::seconds(5); const std::chrono::seconds cache_refresh = std::chrono::seconds(5);
z_formatter():_last_update(std::chrono::seconds(0)) z_formatter():_last_update(std::chrono::seconds(0))
@ -328,7 +328,7 @@ namespace spdlog
} }
pad_n_join(msg.formatted, h, m, ':'); pad_n_join(msg.formatted, h, m, ':');
} }
private: private:
log_clock::time_point _last_update; log_clock::time_point _last_update;
int _offset_minutes; int _offset_minutes;
std::mutex _mutex; std::mutex _mutex;
@ -337,52 +337,53 @@ namespace spdlog
{ {
using namespace std::chrono; using namespace std::chrono;
std::lock_guard<std::mutex> l(_mutex); std::lock_guard<std::mutex> l(_mutex);
if (msg.time - _last_update >= cache_refresh) { if (msg.time - _last_update >= cache_refresh)
{
_offset_minutes = os::utc_minutes_offset(tm_time); _offset_minutes = os::utc_minutes_offset(tm_time);
_last_update = msg.time; _last_update = msg.time;
} }
return _offset_minutes; return _offset_minutes;
} }
}; };
//Thread id //Thread id
class t_formatter:public flag_formatter class t_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << msg.thread_id; msg.formatted << msg.thread_id;
} }
}; };
class v_formatter:public flag_formatter class v_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size()); msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
} }
}; };
class ch_formatter:public flag_formatter class ch_formatter:public flag_formatter
{ {
public: public:
explicit ch_formatter(char ch): _ch(ch) explicit ch_formatter(char ch): _ch(ch)
{} {}
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << _ch; msg.formatted << _ch;
} }
private: private:
char _ch; char _ch;
}; };
//aggregate user chars to display as is //aggregate user chars to display as is
class aggregate_formatter:public flag_formatter class aggregate_formatter:public flag_formatter
{ {
public: public:
aggregate_formatter() aggregate_formatter()
{} {}
void add_ch(char ch) void add_ch(char ch)
@ -393,14 +394,14 @@ namespace spdlog
{ {
msg.formatted << _str; msg.formatted << _str;
} }
private: private:
std::string _str; std::string _str;
}; };
// Full info formatter // Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
class full_formatter:public flag_formatter class full_formatter:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
@ -442,9 +443,9 @@ namespace spdlog
msg.formatted << '[' << level::to_str(msg.level) << "] "; msg.formatted << '[' << level::to_str(msg.level) << "] ";
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size()); msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
} }
}; };
} }
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// pattern_formatter inline impl // pattern_formatter inline impl
@ -458,8 +459,10 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string& patter
{ {
auto end = pattern.end(); auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars; std::unique_ptr<details::aggregate_formatter> user_chars;
for (auto it = pattern.begin(); it != end; ++it) { for (auto it = pattern.begin(); it != end; ++it)
if (*it == '%') { {
if (*it == '%')
{
if (user_chars) //append user chars found so far if (user_chars) //append user chars found so far
_formatters.push_back(std::move(user_chars)); _formatters.push_back(std::move(user_chars));
@ -483,7 +486,8 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string& patter
} }
inline void spdlog::pattern_formatter::handle_flag(char flag) inline void spdlog::pattern_formatter::handle_flag(char flag)
{ {
switch (flag) { switch (flag)
{
// logger name // logger name
case 'n': case 'n':
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter()));
@ -615,7 +619,8 @@ inline void spdlog::pattern_formatter::format(details::log_msg& msg)
#else #else
std::tm tm_time; std::tm tm_time;
#endif #endif
for (auto &f : _formatters) { for (auto &f : _formatters)
{
f->format(msg, tm_time); f->format(msg, tm_time);
} }
//write eol //write eol

View File

@ -22,13 +22,14 @@ TEST_CASE("default_error_handler", "[errors]]")
} }
struct custom_ex{}; struct custom_ex {};
TEST_CASE("custom_error_handler", "[errors]]") TEST_CASE("custom_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
{
throw custom_ex(); throw custom_ex();
}); });
logger->info("Good message #1"); logger->info("Good message #1");
@ -45,7 +46,8 @@ TEST_CASE("async_error_handler", "[errors]]")
std::string filename = "logs/simple_async_log.txt"; std::string filename = "logs/simple_async_log.txt";
{ {
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
{
std::ofstream ofs("logs/custom_err.txt"); std::ofstream ofs("logs/custom_err.txt");
if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt"); if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt");
ofs << err_msg; ofs << err_msg;