pull/194/merge
Christopher Torres 9 years ago
commit 106e5f6343

@ -1,149 +1,156 @@
// //
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
#pragma once #pragma once
// //
// Global registry functions // Global registry functions
// //
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <spdlog/details/registry.h> #include <spdlog/details/registry.h>
#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 <spdlog/sinks/ansicolor_sink.h>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
inline void spdlog::register_logger(std::shared_ptr<logger> logger) inline void spdlog::register_logger(std::shared_ptr<logger> logger)
{ {
return details::registry::instance().register_logger(logger); return details::registry::instance().register_logger(logger);
} }
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name) inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
{ {
return details::registry::instance().get(name); return details::registry::instance().get(name);
} }
inline void spdlog::drop(const std::string &name) inline void spdlog::drop(const std::string &name)
{ {
details::registry::instance().drop(name); details::registry::instance().drop(name);
} }
// Create multi/single threaded rotating file logger // Create multi/single threaded rotating file logger
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush)
{ {
return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush);
} }
inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files, bool force_flush)
{ {
return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush); return create<spdlog::sinks::rotating_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files, force_flush);
} }
// Create file logger which creates new file at midnight): // Create file logger which creates new file at midnight):
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush)
{ {
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush);
} }
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush) inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute, bool force_flush)
{ {
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush); return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush);
} }
// Create stdout/stderr loggers (with optinal color support) // Create stdout/stderr loggers (with optinal color support)
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> create_console_logger_st(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
{ {
if (color) //use color wrapper sink if (color) //use color wrapper sink
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink); sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
return spdlog::details::registry::instance().create(logger_name, sink); return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color) inline std::shared_ptr<spdlog::logger> create_console_logger_mt(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
{ {
return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color); 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, bool color) }
{
return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color); inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color)
} {
return create_console_logger_mt(logger_name, sinks::stdout_sink_mt::instance(), color);
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color) }
{
return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color); inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name, bool color)
} {
return create_console_logger_st(logger_name, sinks::stdout_sink_st::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); inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color)
} {
return create_console_logger_mt(logger_name, sinks::stderr_sink_mt::instance(), color);
#if defined(__linux__) || defined(__APPLE__) }
// Create syslog logger
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option) inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name, bool color)
{ {
return create<spdlog::sinks::syslog_sink>(logger_name, syslog_ident, syslog_option); return create_console_logger_st(logger_name, sinks::stderr_sink_st::instance(), color);
} }
#endif
#if defined(__linux__) || defined(__APPLE__)
// Create syslog logger
//Create logger with multiple sinks inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option)
{
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) return create<spdlog::sinks::syslog_sink>(logger_name, syslog_ident, syslog_option);
{ }
return details::registry::instance().create(logger_name, sinks); #endif
}
//Create logger with multiple sinks
template <typename Sink, typename... Args>
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, Args... args) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
{ {
sink_ptr sink = std::make_shared<Sink>(args...); return details::registry::instance().create(logger_name, sinks);
return details::registry::instance().create(logger_name, { sink }); }
}
template <typename Sink, typename... Args>
template<class It> inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, Args... args)
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) {
{ sink_ptr sink = std::make_shared<Sink>(args...);
return details::registry::instance().create(logger_name, sinks_begin, sinks_end); return details::registry::instance().create(logger_name, { sink });
} }
inline void spdlog::set_formatter(spdlog::formatter_ptr f)
{ template<class It>
details::registry::instance().formatter(f); inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
} {
return details::registry::instance().create(logger_name, sinks_begin, sinks_end);
inline void spdlog::set_pattern(const std::string& format_string) }
{
return details::registry::instance().set_pattern(format_string); inline void spdlog::set_formatter(spdlog::formatter_ptr f)
} {
details::registry::instance().formatter(f);
inline void spdlog::set_level(level::level_enum log_level) }
{
return details::registry::instance().set_level(log_level); inline void spdlog::set_pattern(const std::string& format_string)
} {
return details::registry::instance().set_pattern(format_string);
}
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
{ inline void spdlog::set_level(level::level_enum log_level)
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); {
} return details::registry::instance().set_level(log_level);
}
inline void spdlog::set_sync_mode()
{
details::registry::instance().set_sync_mode(); inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
} {
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms);
inline void spdlog::drop_all() }
{
details::registry::instance().drop_all(); inline void spdlog::set_sync_mode()
} {
details::registry::instance().set_sync_mode();
}
inline void spdlog::drop_all()
{
details::registry::instance().drop_all();
}

@ -1,115 +1,114 @@
// //
// Copyright(c) 2016 Kevin M. Godby (a 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)
// //
#pragma once #pragma once
#include <spdlog/sinks/base_sink.h> #include <spdlog/sinks/base_sink.h>
#include <spdlog/common.h> #include <spdlog/common.h>
#include <string> #include <string>
#include <map> #include <map>
namespace spdlog namespace spdlog
{ {
namespace sinks namespace sinks
{ {
/** /**
* @brief The ansi_color_sink is a decorator around another sink and prefixes * @brief The ansi_color_sink is a decorator around another sink and prefixes
* the output with an ANSI escape sequence color code depending on the severity * the output with an ANSI escape sequence color code depending on the severity
* of the message. * of the message.
*/ */
class ansicolor_sink : public sink class ansicolor_sink : public sink
{ {
public: 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) = delete; ansicolor_sink(const ansicolor_sink& other) = delete;
ansicolor_sink& operator=(const ansicolor_sink& other) = delete; 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);
/// 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";
const std::string underline = "\033[4m"; const std::string underline = "\033[4m";
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";
// 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";
const std::string yellow = "\033[33m"; const std::string yellow = "\033[33m";
const std::string blue = "\033[34m"; const std::string blue = "\033[34m";
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";
/// 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";
const std::string on_yellow = "\033[43m"; const std::string on_yellow = "\033[43m";
const std::string on_blue = "\033[44m"; const std::string on_blue = "\033[44m";
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: sink_ptr sink_;
sink_ptr sink_; std::map<level::level_enum, std::string> colors_;
std::map<level::level_enum, std::string> colors_; };
};
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] = cyan;
colors_[level::trace] = cyan; colors_[level::debug] = cyan;
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; colors_[level::err] = red;
colors_[level::err] = red; colors_[level::critical] = bold + red;
colors_[level::critical] = bold + red; colors_[level::alert] = bold + white + on_red;
colors_[level::alert] = bold + white + on_red; colors_[level::emerg] = bold + yellow + on_red;
colors_[level::emerg] = bold + yellow + on_red; colors_[level::off] = reset;
colors_[level::off] = reset; }
}
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); }
}
inline void ansicolor_sink::flush()
inline void ansicolor_sink::flush() {
{ sink_->flush();
sink_->flush(); }
}
inline void ansicolor_sink::set_color(level::level_enum level, const std::string& color)
inline void ansicolor_sink::set_color(level::level_enum level, const std::string& color) {
{ colors_[level] = color;
colors_[level] = color; }
}
inline ansicolor_sink::~ansicolor_sink()
inline ansicolor_sink::~ansicolor_sink() {
{ flush();
flush(); }
}
} // namespace sinks
} // namespace sinks } // namespace spdlog
} // namespace spdlog

@ -0,0 +1,151 @@
//
// Copyright(c) 2016 Christopher J. Torres (a modified verison of ansicolor_sink).
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <spdlog/sinks/base_sink.h>
#include <spdlog/common.h>
#include <string>
#include <map>
namespace spdlog
{
namespace sinks
{
/**
* @brief The wincolor_sink is a decorator around another sink and uses
* the windows api to set the color depending on the severity
* of the message.
*/
template<class Mutex>
class wincolor_sink : public base_sink<Mutex>
{
public:
wincolor_sink(std::ostream& os, bool force_flush=false);
virtual ~wincolor_sink();
wincolor_sink(const wincolor_sink& other) = delete;
wincolor_sink& operator=(const wincolor_sink& other) = delete;
virtual void flush() override;
// Formatting codes
const short reset = FOREGROUND_INTENSITY;
const short bold = FOREGROUND_INTENSITY;
const short dark = reset; // Not implemented in windows
const short underline = reset; // Not implemented in windows
const short blink = reset; // Not implemented in windows
const short reverse = FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE | BACKGROUND_RED |
BACKGROUND_GREEN | BACKGROUND_BLUE; // XOR to use this
const short concealed = reset; // Not implemented in windows
// Foreground colors
const short black = 0;
const short grey = bold;
const short red = FOREGROUND_RED;
const short green = FOREGROUND_GREEN;
const short yellow = FOREGROUND_RED | FOREGROUND_GREEN;
const short blue = FOREGROUND_BLUE;
const short magenta = FOREGROUND_RED | FOREGROUND_BLUE;
const short cyan = FOREGROUND_GREEN | FOREGROUND_BLUE;
const short white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
/// Background colors
const short on_grey = BACKGROUND_INTENSITY;
const short on_red = BACKGROUND_RED;
const short on_green = BACKGROUND_GREEN;
const short on_yellow = BACKGROUND_RED | BACKGROUND_GREEN;
const short on_blue = BACKGROUND_BLUE;
const short on_magenta = BACKGROUND_RED | BACKGROUND_BLUE;
const short on_cyan = BACKGROUND_GREEN | BACKGROUND_BLUE;
const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
void set_color( level::level_enum level, const short& color );
protected:
virtual void _sink_it(const details::log_msg& msg) override;
void SetConsoleColor( WORD* Attributes, DWORD Color );
void ResetConsoleColor( WORD Attributes );
std::map<level::level_enum, short> colors_;
std::ostream& _ostream;
bool _force_flush;
};
typedef wincolor_sink<details::null_mutex> wincolor_sink_st;
typedef wincolor_sink<std::mutex> wincolor_sink_mt;
template<class Mutex>
inline wincolor_sink<Mutex>::wincolor_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush)
{
colors_[level::trace] = cyan;
colors_[level::debug] = cyan;
colors_[level::info] = white;
colors_[level::err] = red;
colors_[level::off] = reset;
colors_[level::notice] = bold | white;
colors_[level::warn] = bold | yellow;
colors_[level::critical] = bold | red;
colors_[level::alert] = bold | white | on_red;
colors_[level::emerg] = bold | yellow | on_red;
}
template<class Mutex>
inline void wincolor_sink<Mutex>::_sink_it( const details::log_msg& msg )
{
// Wrap the originally formatted message in color codes
WORD Attributes = 0;
SetConsoleColor(&Attributes, colors_[msg.level]);
_ostream.write( msg.formatted.data(), msg.formatted.size() );
if (_force_flush)
_ostream.flush();
ResetConsoleColor(Attributes);
}
template<class Mutex>
inline void wincolor_sink<Mutex>::flush()
{
_ostream.flush();
}
template<class Mutex>
inline void wincolor_sink<Mutex>::set_color( level::level_enum level, const short& color )
{
colors_[level] = color;
}
template<class Mutex>
inline wincolor_sink<Mutex>::~wincolor_sink()
{
flush();
}
template<class Mutex>
void wincolor_sink<Mutex>::SetConsoleColor( WORD* Attributes, DWORD Color )
{
CONSOLE_SCREEN_BUFFER_INFO Info;
HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo( hStdout, &Info );
*Attributes = Info.wAttributes;
SetConsoleTextAttribute( hStdout, Color );
SetConsoleTextAttribute( GetStdHandle(STD_ERROR_HANDLE), Color );
}
template<class Mutex>
void wincolor_sink<Mutex>::ResetConsoleColor( WORD Attributes )
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), Attributes );
SetConsoleTextAttribute( GetStdHandle( STD_ERROR_HANDLE ), Attributes );
}
} // namespace sinks
} // namespace spdlog
Loading…
Cancel
Save