diff --git a/README.md b/README.md index de701532..d23a2a21 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ int main(int, char* []) namespace spd = spdlog; try { - //Create console, multithreaded logger - auto console = spd::stdout_logger_mt("console"); + //Create thread safe console logger (with colors) + auto console = spd::stdout_logger_mt("console", true); console->info("Welcome to spdlog!") ; console->info("An info message example {}..", 1); console->info() << "Streams are supported too " << 1; @@ -133,13 +133,7 @@ int main(int, char* []) auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID); syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); #endif - - //colored console sink example - auto console_out = spdlog::sinks::stderr_sink_st::instance(); - auto color_sink = std::make_shared(console_out); // wraps around another sink - auto color_logger = spd::details::registry::instance().create("Color", color_sink); - color_sink->set_color(spd::level::info, color_sink->bold + color_sink->green); - color_logger->info("Testing color logger..."); + } catch (const spd::spdlog_ex& ex) { diff --git a/example/example.cpp b/example/example.cpp index a6bcc3d1..e9843c28 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -13,20 +13,19 @@ void async_example(); void syslog_example(); -void color_example(); namespace spd = spdlog; int main(int, char*[]) { try { - //Create console, multithreaded logger - auto console = spd::stdout_logger_mt("console"); + // Multithreaded color console + auto console = spd::stdout_logger_mt("console", true); console->info("Welcome to spdlog!"); console->info("An info message example {}..", 1); console->info() << "Streams are supported too " << 1; - //Formatting examples + // 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->info("Support for floats {:03.2f}", 1.23456); @@ -69,8 +68,6 @@ int main(int, char*[]) // syslog example. linux/osx only.. syslog_example(); - // terminal color example (works under linux/osx. windows: only if ansi.sys is loaded) - color_example(); // Release and close all loggers spdlog::drop_all(); @@ -104,28 +101,6 @@ void syslog_example() #endif } -// terminal color example (works under linux/osx. windows: only if ansi.sys is loaded) -#include "spdlog/sinks/ansicolor_sink.h" -void color_example() -{ - // Create a sink to add colors to. - auto console_out = spdlog::sinks::stderr_sink_st::instance(); - auto color_sink = std::make_shared(console_out); // wraps around another sink - auto color_logger = spd::details::registry::instance().create("Color", color_sink); - color_logger->set_level(spd::level::trace); - color_sink->set_color(spd::level::info, color_sink->bold + color_sink->green); - color_logger->info("Testing color logger..."); - color_logger->trace("Trace"); - color_logger->debug("Debug"); - color_logger->info("Info"); - color_logger->notice("Notice"); - color_logger->warn("Warning"); - color_logger->error("Error"); - color_logger->critical("Critical"); - color_logger->alert("Alert"); - color_logger->emerg("Emergency"); -} - // Example of user defined class with operator<< class some_class {}; diff --git a/example/logs/.gitignore b/example/logs/.gitignore deleted file mode 100644 index 5e7d2734..00000000 --- a/example/logs/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index e3c966dc..2d67774d 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -55,26 +56,32 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string return create(logger_name, filename, "txt", hour, minute, force_flush); } - -// Create stdout/stderr loggers -inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name) +// Create stdout/stderr loggers (with optinal color support) +inline std::shared_ptr create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); + if (color) //use color wrapper sink + sink = std::make_shared(sink); + return spdlog::details::registry::instance().create(logger_name, sink); } -inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name) +inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name, bool color) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance()); + return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color); } -inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name) +inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name, bool color) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); + return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color); } -inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name) +inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name, bool color) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance()); + return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color); +} + +inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name, bool color) +{ + return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color); } #if defined(__linux__) || defined(__APPLE__) diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index a1b8f0e8..0c09a205 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -1,5 +1,5 @@ // -// Copyright(c) 2016 Kevin M. Godby (modified version by spdlog). +// Copyright(c) 2016 Kevin M. Godby (a modified version by spdlog). // Distributed under the MIT License (http://opensource.org/licenses/MIT) // @@ -24,16 +24,15 @@ public: ansicolor_sink(sink_ptr wrapped_sink); virtual ~ansicolor_sink(); - ansicolor_sink(const ansicolor_sink& other); - ansicolor_sink& operator=(const ansicolor_sink& other); + ansicolor_sink(const ansicolor_sink& other) = delete; + ansicolor_sink& operator=(const ansicolor_sink& other) = delete; virtual void log(const details::log_msg& msg) override; virtual void flush() override; void set_color(level::level_enum level, const std::string& color); - /// \name Formatting codes - //@{ + /// Formatting codes const std::string reset = "\033[00m"; const std::string bold = "\033[1m"; const std::string dark = "\033[2m"; @@ -41,10 +40,8 @@ public: const std::string blink = "\033[5m"; const std::string reverse = "\033[7m"; const std::string concealed = "\033[8m"; - //@} - - /// \name Foreground colors - //@{ + + // Foreground colors const std::string grey = "\033[30m"; const std::string red = "\033[31m"; const std::string green = "\033[32m"; @@ -53,10 +50,8 @@ public: const std::string magenta = "\033[35m"; const std::string cyan = "\033[36m"; const std::string white = "\033[37m"; - //@} - - /// \name Background colors - //@{ + + /// Background colors const std::string on_grey = "\033[40m"; const std::string on_red = "\033[41m"; const std::string on_green = "\033[42m"; @@ -65,8 +60,7 @@ public: const std::string on_magenta = "\033[45m"; const std::string on_cyan = "\033[46m"; const std::string on_white = "\033[47m"; - //@} - + protected: sink_ptr sink_; @@ -75,8 +69,8 @@ protected: inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink) { - colors_[level::trace] = white; - colors_[level::debug] = white; + colors_[level::trace] = cyan; + colors_[level::debug] = cyan; colors_[level::info] = white; colors_[level::notice] = bold + white; colors_[level::warn] = bold + yellow; @@ -87,33 +81,12 @@ inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sin colors_[level::off] = reset; } -inline ansicolor_sink::~ansicolor_sink() -{ - flush(); -} - -inline ansicolor_sink::ansicolor_sink(const ansicolor_sink& other) : sink_(other.sink_), colors_(other.colors_) -{ - // do nothing -} - - -inline ansicolor_sink& ansicolor_sink::operator=(const ansicolor_sink& other) -{ - if (this == &other) - return *this; - - sink_ = other.sink_; - colors_ = other.colors_; - return *this; -} - inline void ansicolor_sink::log(const details::log_msg& msg) { // Wrap the originally formatted message in color codes - const std::string prefix = colors_[msg.level]; - const std::string s = msg.formatted.str(); - const std::string suffix = reset; + const std::string& prefix = colors_[msg.level]; + const std::string& s = msg.formatted.str(); + const std::string& suffix = reset; details::log_msg m; m.formatted << prefix << s << suffix; sink_->log(m); @@ -129,6 +102,11 @@ inline void ansicolor_sink::set_color(level::level_enum level, const std::string colors_[level] = color; } +inline ansicolor_sink::~ansicolor_sink() +{ + flush(); +} + } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index b92a239f..bf637a15 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -74,10 +74,10 @@ std::shared_ptr daily_logger_st(const std::string& logger_name, const st // // Create and register stdout/stderr loggers // -std::shared_ptr stdout_logger_mt(const std::string& logger_name); -std::shared_ptr stdout_logger_st(const std::string& logger_name); -std::shared_ptr stderr_logger_mt(const std::string& logger_name); -std::shared_ptr stderr_logger_st(const std::string& logger_name); +std::shared_ptr stdout_logger_mt(const std::string& logger_name, bool color = false); +std::shared_ptr stdout_logger_st(const std::string& logger_name, bool color = false); +std::shared_ptr stderr_logger_mt(const std::string& logger_name, bool color = false); +std::shared_ptr stderr_logger_st(const std::string& logger_name, bool color = false); //