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()
{
//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;
});
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));
if (_worker_teardown_cb) _worker_teardown_cb();
}
catch (const std::exception &ex) {
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...) {
catch (...)
{
_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);
}
catch (const std::exception &ex) {
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...) {
catch (...)
{
_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;
_flush_level = level::off;
_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
@ -60,15 +63,18 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar
{
if (!should_log(lvl)) return;
try {
try
{
details::log_msg log_msg(&_name, lvl);
log_msg.raw.write(fmt, args...);
_sink_it(log_msg);
}
catch (const std::exception &ex) {
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...) {
catch (...)
{
_err_handler("Unknown exception");
}
}
@ -77,15 +83,18 @@ template <typename... Args>
inline void spdlog::logger::log(level::level_enum lvl, const char* msg)
{
if (!should_log(lvl)) return;
try {
try
{
details::log_msg log_msg(&_name, lvl);
log_msg.raw << msg;
_sink_it(log_msg);
}
catch (const std::exception &ex) {
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...) {
catch (...)
{
_err_handler("Unknown exception");
}
@ -95,15 +104,18 @@ template<typename T>
inline void spdlog::logger::log(level::level_enum lvl, const T& msg)
{
if (!should_log(lvl)) return;
try {
try
{
details::log_msg log_msg(&_name, lvl);
log_msg.raw << msg;
_sink_it(log_msg);
}
catch (const std::exception &ex) {
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...) {
catch (...)
{
_err_handler("Unknown exception");
}
}

View File

@ -337,7 +337,8 @@ namespace spdlog
{
using namespace std::chrono;
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);
_last_update = msg.time;
}
@ -458,8 +459,10 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string& patter
{
auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars;
for (auto it = pattern.begin(); it != end; ++it) {
if (*it == '%') {
for (auto it = pattern.begin(); it != end; ++it)
{
if (*it == '%')
{
if (user_chars) //append user chars found so far
_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)
{
switch (flag) {
switch (flag)
{
// logger name
case 'n':
_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
std::tm tm_time;
#endif
for (auto &f : _formatters) {
for (auto &f : _formatters)
{
f->format(msg, tm_time);
}
//write eol

View File

@ -28,7 +28,8 @@ TEST_CASE("custom_error_handler", "[errors]]")
prepare_logdir();
std::string filename = "logs/simple_log.txt";
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();
});
logger->info("Good message #1");
@ -45,7 +46,8 @@ TEST_CASE("async_error_handler", "[errors]]")
std::string filename = "logs/simple_async_log.txt";
{
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");
if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt");
ofs << err_msg;