From 7ddfb2b877a0c431c647001d6b1db6fff3426628 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 9 Jul 2016 00:46:00 +0300 Subject: [PATCH] fixed macros and other stuff for the no-streams branch --- example/example.cpp | 6 ++-- include/spdlog/common.h | 32 ++++++++++------- include/spdlog/details/log_msg.h | 4 +-- include/spdlog/details/logger_impl.h | 49 +++------------------------ include/spdlog/logger.h | 14 +++----- include/spdlog/sinks/ansicolor_sink.h | 15 ++++---- include/spdlog/sinks/syslog_sink.h | 5 +-- include/spdlog/spdlog.h | 5 +-- 8 files changed, 44 insertions(+), 86 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c4e21429..9947e647 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -22,11 +22,11 @@ int main(int, char*[]) // Multithreaded color console auto console = spd::stdout_logger_mt("console", true); console->info("Welcome to spdlog!"); - console->info("An info message example {}..", 1); + console->error("An info message example {}..", 1); // Formatting examples - console->info("Easy padding in numbers like {:08d}", 12); - console->info("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); + console->warn("Easy padding in numbers like {:08d}", 12); + console->critical("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"); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 992b3008..004c0af4 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -56,26 +56,32 @@ using level_t = details::null_atomic_int; using level_t = std::atomic_int; #endif + +#define SPDLOG_LEVEL_TRACE 0 +#define SPDLOG_LEVEL_DEBUG 1 +#define SPDLOG_LEVEL_INFO 2 +#define SPDLOG_LEVEL_WARN 3 +#define SPDLOG_LEVEL_ERR 4 +#define SPDLOG_LEVEL_CRIT 5 +#define SPDLOG_LEVEL_OFF 6 + //Log level enum 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 = SPDLOG_LEVEL_TRACE, + debug = SPDLOG_LEVEL_DEBUG, + info = SPDLOG_LEVEL_INFO, + warn = SPDLOG_LEVEL_WARN, + err = SPDLOG_LEVEL_ERR, + critical = SPDLOG_LEVEL_CRIT, + off = SPDLOG_LEVEL_OFF } level_enum; -static const char* level_names[] { "trace", "debug", "info", "notice", "warning", "error", "critical", "alert", "emerg", "off"}; +static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "off"}; -static const char* short_level_names[] { "T", "D", "I", "N", "W", "E", "C", "A", "M", "O"}; +static const char* short_level_names[] { "T", "D", "I", "W", "E", "C", "O"}; inline const char* to_str(spdlog::level::level_enum l) { @@ -124,5 +130,7 @@ using filename_t = std::wstring; using filename_t = std::string; #endif +#define SDLOG_STR_HELPER(x) #x +#define SPDLOG_STR(x) SDLOG_STR_HELPER(x) } //spdlog diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index 1991ff89..52bd956a 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -19,7 +19,7 @@ namespace details struct log_msg { log_msg() = default; - log_msg(std::string *loggers_name, level::level_enum lvl) : logger_name(loggers_name), level(lvl) + log_msg(const std::string *loggers_name, level::level_enum lvl) : logger_name(loggers_name), level(lvl) { #ifndef SPDLOG_NO_DATETIME time = os::now(); @@ -35,7 +35,7 @@ struct log_msg log_msg(log_msg&& other) = delete; - std::string *logger_name; + const std::string *logger_name; level::level_enum level; log_clock::time_point time; size_t thread_id; diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index a9a0d41d..4874e939 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -31,8 +31,7 @@ inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list si // ctor with single sink inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) : - logger(logger_name, -{ + logger(logger_name, { single_sink }) {} @@ -54,8 +53,7 @@ inline void spdlog::logger::set_pattern(const std::string& pattern) template inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Args&... args) { - if (!should_log(lvl)) - return; + if (!should_log(lvl)) return; details::log_msg log_msg(&_name, lvl); try @@ -75,9 +73,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar template inline void spdlog::logger::log(level::level_enum lvl, const char* msg) { - - if (!should_log(lvl)) - return; + if (!should_log(lvl)) return; details::log_msg log_msg(&_name, lvl); log_msg.raw << msg; @@ -89,10 +85,9 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* msg) template inline void spdlog::logger::log(level::level_enum lvl, const T& msg) { - if (!should_log(lvl)) - return; - details::log_msg log_msg(&_name, lvl); + if (!should_log(lvl)) return; + details::log_msg log_msg(&_name, lvl); log_msg.raw << msg; _formatter->format(log_msg); _sink_it(log_msg); @@ -118,11 +113,6 @@ inline void spdlog::logger::info(const char* fmt, const Args&... args) log(level::info, fmt, args...); } -template -inline void spdlog::logger::notice(const char* fmt, const Args&... args) -{ - log(level::notice, fmt, args...); -} template inline void spdlog::logger::warn(const char* fmt, const Args&... args) @@ -142,19 +132,6 @@ inline void spdlog::logger::critical(const char* fmt, const Args&... args) log(level::critical, fmt, args...); } -template -inline void spdlog::logger::alert(const char* fmt, const Args&... args) -{ - log(level::alert, fmt, args...); -} - -template -inline void spdlog::logger::emerg(const char* fmt, const Args&... args) -{ - log(level::emerg, fmt, args...); -} - - template inline void spdlog::logger::trace(const T& msg) @@ -175,11 +152,6 @@ inline void spdlog::logger::info(const T& msg) log(level::info, msg); } -template -inline void spdlog::logger::notice(const T& msg) -{ - log(level::notice, msg); -} template inline void spdlog::logger::warn(const T& msg) @@ -199,17 +171,6 @@ inline void spdlog::logger::critical(const T& msg) log(level::critical, msg); } -template -inline void spdlog::logger::alert(const T& msg) -{ - log(level::alert, msg); -} - -template -inline void spdlog::logger::emerg(const T& msg) -{ - log(level::emerg, msg); -} diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index cff114b3..63c6598b 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -35,30 +35,24 @@ public: logger(const logger&) = delete; logger& operator=(const logger&) = delete; + template void log(level::level_enum lvl, const char* fmt, const Args&... args); template void log(level::level_enum lvl, const char* msg); - - template void log(level::level_enum lvl, const T&); template void trace(const char* fmt, const Args&... args); template void debug(const char* fmt, const Args&... args); template void info(const char* fmt, const Args&... args); - template void notice(const char* fmt, const Args&... args); template void warn(const char* fmt, const Args&... args); template void error(const char* fmt, const Args&... args); template void critical(const char* fmt, const Args&... args); - template void alert(const char* fmt, const Args&... args); - template void emerg(const char* fmt, const Args&... args); - + template void log(level::level_enum lvl, const T&); template void trace(const T&); template void debug(const T&); template void info(const T&); - template void notice(const T&); template void warn(const T&); template void error(const T&); template void critical(const T&); - template void alert(const T&); - template void emerg(const T&); + bool should_log(level::level_enum) const; void set_level(level::level_enum); @@ -76,7 +70,7 @@ protected: virtual void _set_pattern(const std::string&); virtual void _set_formatter(formatter_ptr); - std::string _name; + const std::string _name; std::vector _sinks; formatter_ptr _formatter; spdlog::level_t _level; diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 71b0bb9b..a3b4292d 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -72,15 +72,12 @@ protected: inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink) { - colors_[level::trace] = cyan; - colors_[level::debug] = cyan; - colors_[level::info] = white; - colors_[level::notice] = bold + white; - colors_[level::warn] = bold + yellow; - colors_[level::err] = red; - colors_[level::critical] = bold + red; - colors_[level::alert] = bold + white + on_red; - colors_[level::emerg] = bold + yellow + on_red; + colors_[level::trace] = cyan; + colors_[level::debug] = cyan; + colors_[level::info] = bold; + colors_[level::warn] = yellow + bold; + colors_[level::err] = red + bold; + colors_[level::critical] = bold + on_red; colors_[level::off] = reset; } diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 5d7ccf87..6eea7431 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -35,12 +35,9 @@ public: _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 @@ -65,7 +62,7 @@ public: private: - std::array _priorities; + std::array _priorities; //must store the ident because the man says openlog might use the pointer as is and not a string copy const std::string _ident; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index dc2f4181..4cb63a4c 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -134,14 +134,15 @@ void drop_all(); // SPDLOG_DEBUG(my_logger, "Some debug message {} {}", 1, 3.2); /////////////////////////////////////////////////////////////////////////////// + #ifdef SPDLOG_TRACE_ON -#define SPDLOG_TRACE(logger, ...) logger->trace(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; +#define SPDLOG_TRACE(logger, ...) logger->trace(__FILE__ ## " line " ## SPDLOG_STR(__LINE__) ## ": " ## __VA_ARGS__); #else #define SPDLOG_TRACE(logger, ...) #endif #ifdef SPDLOG_DEBUG_ON -#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")"; +#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__) #else #define SPDLOG_DEBUG(logger, ...) #endif