From b1867cfba3ef10a08c9d0bfdd95db53a7f02f137 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 21 Dec 2014 01:30:39 +0200 Subject: [PATCH] log levels now lowercase --- README.md | 6 +-- example/example.cpp | 52 +++++++++------------- include/spdlog/common.h | 20 ++++----- include/spdlog/details/async_logger_impl.h | 2 +- include/spdlog/details/log_msg.h | 2 +- include/spdlog/details/logger_impl.h | 40 ++++++++--------- include/spdlog/details/registry.h | 4 +- include/spdlog/sinks/syslog_sink.h | 20 ++++----- 8 files changed, 68 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index d0b0ae97..5168b95c 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ int main(int, char* []) try { // Set log level to all loggers to DEBUG and above - spd::set_level(spd::level::DEBUG); + spd::set_level(spd::level::debug); //Create console, multithreaded logger auto console = spd::stdout_logger_mt("console"); @@ -87,7 +87,7 @@ int main(int, char* []) //Create a file rotating logger with 5mb size max and 3 rotated files auto file_logger = spd::rotating_logger_mt("file_logger", "logs/mylogfile", 1048576 * 5, 3); - file_logger->set_level(spd::level::INFO); + file_logger->set_level(spd::level::info); for(int i = 0; i < 10; ++i) file_logger->info("{} * {} equals {:>10}", i, i, i*i); @@ -114,7 +114,7 @@ int main(int, char* []) // #ifdef __linux__ std::string ident = "my_app"; - auto syslog_logger = spd::syslog_logger("syslog", ident, spd::sinks::syslog::option::PID | spd::sinks::syslog::option::PERROR, "mail" ); + auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID | LOG_PERROR); syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); #endif } diff --git a/example/example.cpp b/example/example.cpp index cd6bfded..aea669e6 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -28,17 +28,13 @@ #include #include "spdlog/spdlog.h" - int main(int, char* []) { - namespace spd = spdlog; - try { - std::string filename = "logs/spdlog_example"; // Set log level to all loggers to DEBUG and above - spd::set_level(spd::level::DEBUG); + spd::set_level(spd::level::debug); //Create console, multithreaded logger auto console = spd::stdout_logger_mt("console"); @@ -47,55 +43,49 @@ int main(int, char* []) console->info() << "Streams are supported too " << 1; console->info("Easy padding in numbers like {:08d}", 12); - console->info("Support for int: {0:d}; hex: {0:08x}; oct: {0:o}; bin: {0:b}", 42); + console->info("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); console->info("Support for floats {:03.2f}", 1.23456); console->info("Positional args are {1} {0}..", "too", "supported"); console->info("{:<30}", "left aligned"); console->info("{:>30}", "right aligned"); console->info("{:^30}", "centered"); - + //Create a file rotating logger with 5mb size max and 3 rotated files - auto file_logger = spd::rotating_logger_mt("file_logger", filename, 1024 * 1024 * 5, 3); - - file_logger->info("Log file message number", 1); + auto file_logger = spd::rotating_logger_mt("file_logger", "logs/mylogfile", 1048576 * 5, 3); + file_logger->set_level(spd::level::info); for(int i = 0; i < 10; ++i) - file_logger->info("{} * {} equals {:>10}", i, i, i*i); + file_logger->info("{} * {} equals {:>10}", i, i, i*i); + //Customize msg format for all messages spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***"); file_logger->info("This is another message with custom format"); spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); - // Debug and trace macros to be turned on/off at compile time. - // Evaluates to empty statements if not turned on - // Define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON - before including spdlog.h SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23); - -#ifdef __linux__ - // syslog example - std::string ident = "spdlog_example"; // empty ident can be used to use current program name - auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID); - syslog_logger->set_pattern("[%l] %v"); //syslog already put timestamps so set the pattern to minimum (log level and the message) - syslog_logger->warn("This message that will end up in syslog. This is Linux only.."); -#endif - - // Asynchronous logging is easy.. + // + // Asynchronous logging is very fast.. // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. - // Note: queue size must be power of 2! - size_t q_size = 1048576; + // + size_t q_size = 1048576; //queue size must be power of 2 spdlog::set_async_mode(q_size); auto async_file= spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); - for(int i = 0; i < 100; i++) - async_file->info("This is async log message #{}.. Should be very fast.. ", i); - + async_file->info() << "This is async log.." << "Should be very fast!"; + + // + // syslog example + // +#ifdef __linux__ + std::string ident = "my_app"; + auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID | LOG_PERROR); + syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); +#endif } catch (const spd::spdlog_ex& ex) { std::cout << "Log failed: " << ex.what() << std::endl; } - return 0; } - diff --git a/include/spdlog/common.h b/include/spdlog/common.h index d10a1d3b..509a90e6 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -54,16 +54,16 @@ namespace level { typedef enum { - TRACE = 0, - DEBUG = 1, - INFO = 2, - NOTICE = 3, - WARN = 4, - ERR = 5, - CRITICAL = 6, - ALERT = 7, - EMERG = 8, - OFF = 9 + trace = 0, + debug = 1, + info = 2, + notice = 3, + warn = 4, + err = 5, + critical = 6, + alert = 7, + emerg = 8, + off = 9 } level_enum; static const char* level_names[] { "trace", "debug", "info", "notice", "warning", "error", "critical", "alert", "emerg", "off"}; diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 0b3022ec..cb9b1bc9 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -63,7 +63,7 @@ inline void spdlog::async_logger::_set_pattern(const std::string& pattern) inline void spdlog::async_logger::_stop() { - set_level(level::OFF); + set_level(level::off); } inline void spdlog::async_logger::_log_msg(details::log_msg& msg) diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index abd9bd41..fcf5ca3c 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -80,7 +80,7 @@ struct log_msg void clear() { - level = level::OFF; + level = level::off; raw.clear(); formatted.clear(); } diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 11afa1f6..3b680063 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -40,7 +40,7 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c { // no support under vs2013 for member initialization for std::atomic - _level = level::INFO; + _level = level::info; } // ctor with sinks as init list @@ -92,55 +92,55 @@ inline spdlog::details::line_logger spdlog::logger::_log_if_enabled(level::level template inline spdlog::details::line_logger spdlog::logger::trace(const char* fmt, const Args&... args) { - return _log_if_enabled(level::TRACE, fmt, args...); + return _log_if_enabled(level::trace, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::debug(const char* fmt, const Args&... args) { - return _log_if_enabled(level::DEBUG, fmt, args...); + return _log_if_enabled(level::debug, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::info(const char* fmt, const Args&... args) { - return _log_if_enabled(level::INFO, fmt, args...); + return _log_if_enabled(level::info, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::notice(const char* fmt, const Args&... args) { - return _log_if_enabled(level::NOTICE, fmt, args...); + return _log_if_enabled(level::notice, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::warn(const char* fmt, const Args&... args) { - return _log_if_enabled(level::WARN, fmt, args...); + return _log_if_enabled(level::warn, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::error(const char* fmt, const Args&... args) { - return _log_if_enabled(level::ERR, fmt, args...); + return _log_if_enabled(level::err, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::critical(const char* fmt, const Args&... args) { - return _log_if_enabled(level::CRITICAL, fmt, args...); + return _log_if_enabled(level::critical, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::alert(const char* fmt, const Args&... args) { - return _log_if_enabled(level::ALERT, fmt, args...); + return _log_if_enabled(level::alert, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::emerg(const char* fmt, const Args&... args) { - return _log_if_enabled(level::EMERG, fmt, args...); + return _log_if_enabled(level::emerg, fmt, args...); } @@ -152,48 +152,48 @@ inline spdlog::details::line_logger spdlog::logger::emerg(const char* fmt, const inline spdlog::details::line_logger spdlog::logger::trace() { - return _log_if_enabled(level::TRACE); + return _log_if_enabled(level::trace); } inline spdlog::details::line_logger spdlog::logger::debug() { - return _log_if_enabled(level::DEBUG); + return _log_if_enabled(level::debug); } inline spdlog::details::line_logger spdlog::logger::info() { - return _log_if_enabled(level::INFO); + return _log_if_enabled(level::info); } inline spdlog::details::line_logger spdlog::logger::notice() { - return _log_if_enabled(level::NOTICE); + return _log_if_enabled(level::notice); } inline spdlog::details::line_logger spdlog::logger::warn() { - return _log_if_enabled(level::WARN); + return _log_if_enabled(level::warn); } inline spdlog::details::line_logger spdlog::logger::error() { - return _log_if_enabled(level::ERR); + return _log_if_enabled(level::err); } inline spdlog::details::line_logger spdlog::logger::critical() { - return _log_if_enabled(level::CRITICAL); + return _log_if_enabled(level::critical); } inline spdlog::details::line_logger spdlog::logger::alert() { - return _log_if_enabled(level::ALERT); + return _log_if_enabled(level::alert); } inline spdlog::details::line_logger spdlog::logger::emerg() { - return _log_if_enabled(level::EMERG); + return _log_if_enabled(level::emerg); } @@ -255,7 +255,7 @@ inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter) inline void spdlog::logger::_stop() { - set_level(level::OFF); + set_level(level::off); } diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 0e24ba26..2fee86d7 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -130,7 +130,7 @@ public: void stop_all() { std::lock_guard lock(_mutex); - _level = level::OFF; + _level = level::off; for (auto& l : _loggers) l.second->stop(); } @@ -149,7 +149,7 @@ private: std::mutex _mutex; std::unordered_map > _loggers; formatter_ptr _formatter; - level::level_enum _level = level::INFO; + level::level_enum _level = level::info; bool _async_mode = false; size_t _async_q_size = 0; }; diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 0b8b36ba..9da77e5e 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -50,16 +50,16 @@ public: syslog_sink(const std::string& ident = "", int syslog_option=0, int syslog_facility=LOG_USER): _ident(ident) { - _priorities[static_cast(level::TRACE)] = LOG_DEBUG; - _priorities[static_cast(level::DEBUG)] = LOG_DEBUG; - _priorities[static_cast(level::INFO)] = LOG_INFO; - _priorities[static_cast(level::NOTICE)] = LOG_NOTICE; - _priorities[static_cast(level::WARN)] = LOG_WARNING; - _priorities[static_cast(level::ERR)] = LOG_ERR; - _priorities[static_cast(level::CRITICAL)] = LOG_CRIT; - _priorities[static_cast(level::ALERT)] = LOG_ALERT; - _priorities[static_cast(level::EMERG)] = LOG_EMERG; - _priorities[static_cast(level::OFF)] = LOG_INFO; + _priorities[static_cast(level::trace)] = LOG_DEBUG; + _priorities[static_cast(level::debug)] = LOG_DEBUG; + _priorities[static_cast(level::info)] = LOG_INFO; + _priorities[static_cast(level::notice)] = LOG_NOTICE; + _priorities[static_cast(level::warn)] = LOG_WARNING; + _priorities[static_cast(level::err)] = LOG_ERR; + _priorities[static_cast(level::critical)] = LOG_CRIT; + _priorities[static_cast(level::alert)] = LOG_ALERT; + _priorities[static_cast(level::emerg)] = LOG_EMERG; + _priorities[static_cast(level::off)] = LOG_INFO; //set ident to be program name if empty ::openlog(_ident.empty()? nullptr:_ident.c_str(), syslog_option, syslog_facility);