Commit e7debaac authored by gabime's avatar gabime

astyle

parent bdbe9086
...@@ -134,7 +134,8 @@ void user_defined_example() ...@@ -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);
......
...@@ -261,10 +261,12 @@ inline void spdlog::details::async_log_helper::worker_loop() ...@@ -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");
} }
} }
......
...@@ -77,10 +77,12 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg) ...@@ -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");
} }
} }
...@@ -23,7 +23,10 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c ...@@ -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 ...@@ -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 ...@@ -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> ...@@ -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> ...@@ -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");
} }
} }
......
...@@ -210,7 +210,7 @@ inline size_t filesize(FILE *f) ...@@ -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;
......
...@@ -22,13 +22,14 @@ TEST_CASE("default_error_handler", "[errors]]") ...@@ -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]]") ...@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment