mirror of
https://github.com/gabime/spdlog.git
synced 2025-04-01 02:42:41 +08:00
API for color support in console logger
This commit is contained in:
parent
a96092ac32
commit
296623baa3
10
README.md
10
README.md
@ -65,8 +65,8 @@ int main(int, char* [])
|
|||||||
namespace spd = spdlog;
|
namespace spd = spdlog;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Create console, multithreaded logger
|
//Create thread safe console logger (with colors)
|
||||||
auto console = spd::stdout_logger_mt("console");
|
auto console = spd::stdout_logger_mt("console", true);
|
||||||
console->info("Welcome to spdlog!") ;
|
console->info("Welcome to spdlog!") ;
|
||||||
console->info("An info message example {}..", 1);
|
console->info("An info message example {}..", 1);
|
||||||
console->info() << "Streams are supported too " << 1;
|
console->info() << "Streams are supported too " << 1;
|
||||||
@ -134,12 +134,6 @@ int main(int, char* [])
|
|||||||
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
|
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//colored console sink example
|
|
||||||
auto console_out = spdlog::sinks::stderr_sink_st::instance();
|
|
||||||
auto color_sink = std::make_shared<spd::sinks::ansicolor_sink>(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)
|
catch (const spd::spdlog_ex& ex)
|
||||||
{
|
{
|
||||||
|
@ -13,20 +13,19 @@
|
|||||||
|
|
||||||
void async_example();
|
void async_example();
|
||||||
void syslog_example();
|
void syslog_example();
|
||||||
void color_example();
|
|
||||||
|
|
||||||
namespace spd = spdlog;
|
namespace spd = spdlog;
|
||||||
int main(int, char*[])
|
int main(int, char*[])
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Create console, multithreaded logger
|
// Multithreaded color console
|
||||||
auto console = spd::stdout_logger_mt("console");
|
auto console = spd::stdout_logger_mt("console", true);
|
||||||
console->info("Welcome to spdlog!");
|
console->info("Welcome to spdlog!");
|
||||||
console->info("An info message example {}..", 1);
|
console->info("An info message example {}..", 1);
|
||||||
console->info() << "Streams are supported too " << 1;
|
console->info() << "Streams are supported too " << 1;
|
||||||
|
|
||||||
//Formatting examples
|
// Formatting examples
|
||||||
console->info("Easy padding in numbers like {:08d}", 12);
|
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 int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||||||
console->info("Support for floats {:03.2f}", 1.23456);
|
console->info("Support for floats {:03.2f}", 1.23456);
|
||||||
@ -69,8 +68,6 @@ int main(int, char*[])
|
|||||||
// syslog example. linux/osx only..
|
// syslog example. linux/osx only..
|
||||||
syslog_example();
|
syslog_example();
|
||||||
|
|
||||||
// terminal color example (works under linux/osx. windows: only if ansi.sys is loaded)
|
|
||||||
color_example();
|
|
||||||
|
|
||||||
// Release and close all loggers
|
// Release and close all loggers
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
@ -104,28 +101,6 @@ void syslog_example()
|
|||||||
#endif
|
#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<spd::sinks::ansicolor_sink>(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<<
|
// Example of user defined class with operator<<
|
||||||
class some_class {};
|
class some_class {};
|
||||||
|
4
example/logs/.gitignore
vendored
4
example/logs/.gitignore
vendored
@ -1,4 +0,0 @@
|
|||||||
# Ignore everything in this directory
|
|
||||||
*
|
|
||||||
# Except this file
|
|
||||||
!.gitignore
|
|
@ -13,6 +13,7 @@
|
|||||||
#include <spdlog/sinks/file_sinks.h>
|
#include <spdlog/sinks/file_sinks.h>
|
||||||
#include <spdlog/sinks/stdout_sinks.h>
|
#include <spdlog/sinks/stdout_sinks.h>
|
||||||
#include <spdlog/sinks/syslog_sink.h>
|
#include <spdlog/sinks/syslog_sink.h>
|
||||||
|
#include <spdlog/sinks/ansicolor_sink.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -55,26 +56,32 @@ inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string
|
|||||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush);
|
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create stdout/stderr loggers (with optinal color support)
|
||||||
// Create stdout/stderr loggers
|
inline std::shared_ptr<spdlog::logger> create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
|
||||||
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
|
|
||||||
{
|
{
|
||||||
return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance());
|
if (color) //use color wrapper sink
|
||||||
|
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
|
||||||
|
return spdlog::details::registry::instance().create(logger_name, sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
|
inline std::shared_ptr<spdlog::logger> 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::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
|
inline std::shared_ptr<spdlog::logger> 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::logger> spdlog::stderr_logger_st(const std::string& logger_name)
|
inline std::shared_ptr<spdlog::logger> 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::logger> 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__)
|
#if defined(__linux__) || defined(__APPLE__)
|
||||||
|
@ -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)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
//
|
//
|
||||||
|
|
||||||
@ -24,16 +24,15 @@ public:
|
|||||||
ansicolor_sink(sink_ptr wrapped_sink);
|
ansicolor_sink(sink_ptr wrapped_sink);
|
||||||
virtual ~ansicolor_sink();
|
virtual ~ansicolor_sink();
|
||||||
|
|
||||||
ansicolor_sink(const ansicolor_sink& other);
|
ansicolor_sink(const ansicolor_sink& other) = delete;
|
||||||
ansicolor_sink& operator=(const ansicolor_sink& other);
|
ansicolor_sink& operator=(const ansicolor_sink& other) = delete;
|
||||||
|
|
||||||
virtual void log(const details::log_msg& msg) override;
|
virtual void log(const details::log_msg& msg) override;
|
||||||
virtual void flush() override;
|
virtual void flush() override;
|
||||||
|
|
||||||
void set_color(level::level_enum level, const std::string& color);
|
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 reset = "\033[00m";
|
||||||
const std::string bold = "\033[1m";
|
const std::string bold = "\033[1m";
|
||||||
const std::string dark = "\033[2m";
|
const std::string dark = "\033[2m";
|
||||||
@ -41,10 +40,8 @@ public:
|
|||||||
const std::string blink = "\033[5m";
|
const std::string blink = "\033[5m";
|
||||||
const std::string reverse = "\033[7m";
|
const std::string reverse = "\033[7m";
|
||||||
const std::string concealed = "\033[8m";
|
const std::string concealed = "\033[8m";
|
||||||
//@}
|
|
||||||
|
|
||||||
/// \name Foreground colors
|
// Foreground colors
|
||||||
//@{
|
|
||||||
const std::string grey = "\033[30m";
|
const std::string grey = "\033[30m";
|
||||||
const std::string red = "\033[31m";
|
const std::string red = "\033[31m";
|
||||||
const std::string green = "\033[32m";
|
const std::string green = "\033[32m";
|
||||||
@ -53,10 +50,8 @@ public:
|
|||||||
const std::string magenta = "\033[35m";
|
const std::string magenta = "\033[35m";
|
||||||
const std::string cyan = "\033[36m";
|
const std::string cyan = "\033[36m";
|
||||||
const std::string white = "\033[37m";
|
const std::string white = "\033[37m";
|
||||||
//@}
|
|
||||||
|
|
||||||
/// \name Background colors
|
/// Background colors
|
||||||
//@{
|
|
||||||
const std::string on_grey = "\033[40m";
|
const std::string on_grey = "\033[40m";
|
||||||
const std::string on_red = "\033[41m";
|
const std::string on_red = "\033[41m";
|
||||||
const std::string on_green = "\033[42m";
|
const std::string on_green = "\033[42m";
|
||||||
@ -65,7 +60,6 @@ public:
|
|||||||
const std::string on_magenta = "\033[45m";
|
const std::string on_magenta = "\033[45m";
|
||||||
const std::string on_cyan = "\033[46m";
|
const std::string on_cyan = "\033[46m";
|
||||||
const std::string on_white = "\033[47m";
|
const std::string on_white = "\033[47m";
|
||||||
//@}
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -75,8 +69,8 @@ protected:
|
|||||||
|
|
||||||
inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink)
|
inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink)
|
||||||
{
|
{
|
||||||
colors_[level::trace] = white;
|
colors_[level::trace] = cyan;
|
||||||
colors_[level::debug] = white;
|
colors_[level::debug] = cyan;
|
||||||
colors_[level::info] = white;
|
colors_[level::info] = white;
|
||||||
colors_[level::notice] = bold + white;
|
colors_[level::notice] = bold + white;
|
||||||
colors_[level::warn] = bold + yellow;
|
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;
|
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)
|
inline void ansicolor_sink::log(const details::log_msg& msg)
|
||||||
{
|
{
|
||||||
// Wrap the originally formatted message in color codes
|
// Wrap the originally formatted message in color codes
|
||||||
const std::string prefix = colors_[msg.level];
|
const std::string& prefix = colors_[msg.level];
|
||||||
const std::string s = msg.formatted.str();
|
const std::string& s = msg.formatted.str();
|
||||||
const std::string suffix = reset;
|
const std::string& suffix = reset;
|
||||||
details::log_msg m;
|
details::log_msg m;
|
||||||
m.formatted << prefix << s << suffix;
|
m.formatted << prefix << s << suffix;
|
||||||
sink_->log(m);
|
sink_->log(m);
|
||||||
@ -129,6 +102,11 @@ inline void ansicolor_sink::set_color(level::level_enum level, const std::string
|
|||||||
colors_[level] = color;
|
colors_[level] = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline ansicolor_sink::~ansicolor_sink()
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
@ -74,10 +74,10 @@ std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const st
|
|||||||
//
|
//
|
||||||
// Create and register stdout/stderr loggers
|
// Create and register stdout/stderr loggers
|
||||||
//
|
//
|
||||||
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
|
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name, bool color = false);
|
||||||
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
|
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name, bool color = false);
|
||||||
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
|
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name, bool color = false);
|
||||||
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
|
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name, bool color = false);
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user