Commit 924ef842 authored by gabime's avatar gabime

Refactred spdlog.h and console sinks. Added global lock for all console sinks (traits)

parent 9bffa921
...@@ -38,8 +38,6 @@ inline spdlog::async_logger::async_logger( ...@@ -38,8 +38,6 @@ inline spdlog::async_logger::async_logger(
// send the log message to the thread pool // send the log message to the thread pool
inline void spdlog::async_logger::_sink_it(details::log_msg &msg) inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
{ {
try
{
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER) #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
_incr_msg_counter(msg); _incr_msg_counter(msg);
#endif #endif
...@@ -51,15 +49,6 @@ inline void spdlog::async_logger::_sink_it(details::log_msg &msg) ...@@ -51,15 +49,6 @@ inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
{ {
throw spdlog_ex("async log: thread pool doens't exist anymore"); throw spdlog_ex("async log: thread pool doens't exist anymore");
} }
}
catch (const std::exception &ex)
{
_err_handler(ex.what());
}
catch (...)
{
_err_handler("Unknown exception in async logger " + _name);
}
} }
// send flush request to the thread pool // send flush request to the thread pool
...@@ -99,6 +88,11 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms ...@@ -99,6 +88,11 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms
{ {
_err_handler("Unknown exception in async logger " + _name); _err_handler("Unknown exception in async logger " + _name);
} }
if (_should_flush(incoming_log_msg))
{
_backend_flush();
}
} }
inline void spdlog::async_logger::_backend_flush() inline void spdlog::async_logger::_backend_flush()
......
...@@ -147,11 +147,19 @@ public: ...@@ -147,11 +147,19 @@ public:
} }
void drop_all() void drop_all()
{
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
_loggers.clear(); _loggers.clear();
} }
{
std::lock_guard<Mutex> lock(_tp_mutex);
_tp.reset();
}
}
Mutex &tp_mutex() Mutex &tp_mutex()
{ {
return _tp_mutex; return _tp_mutex;
......
#pragma once
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
namespace spdlog {
namespace details {
struct console_stdout_trait
{
static FILE* stream() {return stdout;}
#ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); }
#endif
};
struct console_stderr_trait
{
static FILE* stream() { return stdout; }
#ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_ERROR_HANDLE); }
#endif
};
struct console_mutex_trait
{
using mutex_t = std::mutex;
static mutex_t& console_mutex()
{
static auto mutex = mutex_t{};
return mutex;
}
};
struct console_null_mutex_trait
{
using mutex_t = null_mutex;
static mutex_t& console_mutex()
{
static auto mutex = mutex_t{};
return mutex;
}
};
}
}
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
#pragma once #pragma once
#include "../common.h" #include "../details/null_mutex.h"
#include "../details/os.h" #include "../details/os.h"
#include "base_sink.h" #include "../details/traits.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <memory>
#include <mutex>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
...@@ -20,12 +22,15 @@ namespace sinks { ...@@ -20,12 +22,15 @@ namespace sinks {
* of the message. * of the message.
* If no color terminal detected, omit the escape codes. * If no color terminal detected, omit the escape codes.
*/ */
template<class Mutex> template<class StdoutTrait, class ConsoleMutexTrait>
class ansicolor_sink : public base_sink<Mutex> class ansicolor_sink : public sink
{ {
public: public:
explicit ansicolor_sink(FILE *file) using mutex_t = typename ConsoleMutexTrait::mutex_t;
: target_file_(file) ansicolor_sink()
: target_file_(StdoutTrait::stream()),
_mutex(ConsoleMutexTrait::mutex())
{ {
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal(); should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
colors_[level::trace] = white; colors_[level::trace] = white;
...@@ -42,9 +47,12 @@ public: ...@@ -42,9 +47,12 @@ public:
_flush(); _flush();
} }
ansicolor_sink(const ansicolor_sink &other) = delete;
ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
void set_color(level::level_enum color_level, const std::string &color) void set_color(level::level_enum color_level, const std::string &color)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex); std::lock_guard<mutex_t> lock(_mutex);
colors_[color_level] = color; colors_[color_level] = color;
} }
...@@ -78,11 +86,12 @@ public: ...@@ -78,11 +86,12 @@ public:
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:
void _sink_it(const details::log_msg &msg) override void log(const details::log_msg &msg) SPDLOG_FINAL override
{ {
// Wrap the originally formatted message in color codes. // Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead. // If color is not supported in the terminal, log as is instead.
std::lock_guard<mutex_t> lock(_mutex);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
{ {
// before color range // before color range
...@@ -98,15 +107,18 @@ protected: ...@@ -98,15 +107,18 @@ protected:
{ {
_print_range(msg, 0, msg.formatted.size()); _print_range(msg, 0, msg.formatted.size());
} }
_flush(); fflush(target_file_);
} }
void _flush() override void flush() SPDLOG_FINAL override
{ {
std::lock_guard<mutex_t> lock(_mutex);
fflush(target_file_); fflush(target_file_);
} }
private: private:
void _print_ccode(const std::string &color_code) void _print_ccode(const std::string &color_code)
{ {
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
...@@ -115,36 +127,19 @@ private: ...@@ -115,36 +127,19 @@ private:
{ {
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_); fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
} }
FILE *target_file_; FILE *target_file_;
mutex_t & _mutex;
bool should_do_colors_; bool should_do_colors_;
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_; std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
}; };
#ifndef _WIN32
using stdout_color_mt = ansicolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using stdout_color_st = ansicolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
template<class Mutex> using stderr_color_mt = ansicolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
class ansicolor_stdout_sink : public ansicolor_sink<Mutex> using stderr_color_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
{ #endif
public:
ansicolor_stdout_sink()
: ansicolor_sink<Mutex>(stdout)
{
}
};
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<std::mutex>;
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::null_mutex>;
template<class Mutex>
class ansicolor_stderr_sink : public ansicolor_sink<Mutex>
{
public:
ansicolor_stderr_sink()
: ansicolor_sink<Mutex>(stderr)
{
}
};
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<std::mutex>;
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#pragma once #pragma once
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "base_sink.h" #include "../details/traits.h"
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
...@@ -15,53 +15,40 @@ ...@@ -15,53 +15,40 @@
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<class Mutex> template<class StdoutTrait, class ConsoleMutexTrait>
class stdout_sink SPDLOG_FINAL : public base_sink<Mutex> class stdout_sink : public sink
{ {
using MyType = stdout_sink<Mutex>;
public: public:
explicit stdout_sink() = default; using mutex_t = typename ConsoleMutexTrait::mutex_t;
stdout_sink() :
protected: _mutex(ConsoleMutexTrait::console_mutex()),
void _sink_it(const details::log_msg &msg) override _file(StdoutTrait::stream()) {}
{ ~stdout_sink() = default;
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
_flush();
}
void _flush() override
{
fflush(stdout);
}
};
using stdout_sink_mt = stdout_sink<std::mutex>;
using stdout_sink_st = stdout_sink<details::null_mutex>;
template<class Mutex> stdout_sink(const stdout_sink &other) = delete;
class stderr_sink SPDLOG_FINAL : public base_sink<Mutex> stdout_sink &operator=(const stdout_sink &other) = delete;
{
using MyType = stderr_sink<Mutex>;
public:
explicit stderr_sink() = default;
protected: void log(const details::log_msg &msg) override
void _sink_it(const details::log_msg &msg) override
{ {
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr); std::lock_guard<mutex_t> lock(_mutex);
_flush(); fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), _file);
fflush(StdoutTrait::stream());
} }
void _flush() override void flush() override
{ {
fflush(stderr); std::lock_guard<mutex_t> lock(_mutex);
fflush(StdoutTrait::stream());
} }
private:
typename mutex_t& _mutex;
FILE* _file;
}; };
using stderr_sink_mt = stderr_sink<std::mutex>; using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>;
using stderr_sink_st = stderr_sink<details::null_mutex>; using stdout_sink_st = stdout_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using stderr_sink_mt = stdout_sink<details::console_stderr_trait, details::console_mutex_trait>;
using stderr_sink_st = stdout_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "../common.h" #include "../common.h"
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "base_sink.h" #include "../details/traits.h"
#include <mutex> #include <mutex>
#include <memory> #include <memory>
...@@ -20,10 +20,11 @@ namespace sinks { ...@@ -20,10 +20,11 @@ namespace sinks {
/* /*
* Windows color console sink. Uses WriteConsoleA to write to the console with colors * Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/ */
template<class Mutex> template<class HandleTrait, class ConsoleMutexTrait>
class wincolor_sink : public sink class wincolor_sink : public sink
{ {
public: public:
const WORD BOLD = FOREGROUND_INTENSITY; const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED; const WORD RED = FOREGROUND_RED;
const WORD GREEN = FOREGROUND_GREEN; const WORD GREEN = FOREGROUND_GREEN;
...@@ -31,8 +32,9 @@ public: ...@@ -31,8 +32,9 @@ public:
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
wincolor_sink(HANDLE std_handle, Mutex& stdout_mutex) wincolor_sink()
: out_handle_(std_handle), _mutex(stdout_mutex) : out_handle_(HandleTrait::handle()),
_mutex(ConsoleMutexTrait::console_mutex())
{ {
colors_[level::trace] = WHITE; colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN; colors_[level::debug] = CYAN;
...@@ -55,13 +57,13 @@ public: ...@@ -55,13 +57,13 @@ public:
// change the color for the given level // change the color for the given level
void set_color(level::level_enum level, WORD color) void set_color(level::level_enum level, WORD color)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex); std::lock_guard<mutex_t> lock(_mutex);
colors_[level] = color; colors_[level] = color;
} }
void log(const details::log_msg &msg) SPDLOG_FINAL override void log(const details::log_msg &msg) SPDLOG_FINAL override
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<mutex_t> lock(_mutex);
if (msg.color_range_end > msg.color_range_start) if (msg.color_range_end > msg.color_range_start)
{ {
...@@ -86,8 +88,8 @@ public: ...@@ -86,8 +88,8 @@ public:
// windows console always flushed? // windows console always flushed?
} }
private: private:
using mutex_t = typename ConsoleMutexTrait::mutex_t;
// set color and return the orig console attributes (for resetting later) // set color and return the orig console attributes (for resetting later)
WORD set_console_attribs(WORD attribs) WORD set_console_attribs(WORD attribs)
{ {
...@@ -109,64 +111,18 @@ private: ...@@ -109,64 +111,18 @@ private:
} }
HANDLE out_handle_; HANDLE out_handle_;
Mutex& _mutex; mutex_t &_mutex;
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
}; };
// //
// windows color console to stdout // windows color console to stdout
// //
template<class Mutex>
class wincolor_stdout_sink : public wincolor_sink<Mutex>
{
public:
wincolor_stdout_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE), details::os::stdout_mutex())
{
}
};
template<>
class wincolor_stdout_sink<details::null_mutex> : public wincolor_sink<details::null_mutex>
{
details::null_mutex _null_mutex;
public:
wincolor_stdout_sink()
: wincolor_sink<details::null_mutex>(GetStdHandle(STD_OUTPUT_HANDLE), _null_mutex)
{
}
};
using wincolor_stdout_sink_mt = wincolor_stdout_sink<std::mutex>;
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
//
// windows color console to stderr
//
template<class Mutex>
class wincolor_stderr_sink : public wincolor_sink<Mutex>
{
public:
wincolor_stderr_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE), details::os::stderr_mutex())
{
}
};
template<>
class wincolor_stderr_sink<details::null_mutex> : public wincolor_sink<details::null_mutex>
{
details::null_mutex _null_mutex;
public:
wincolor_stderr_sink()
: wincolor_sink<details::null_mutex>(GetStdHandle(STD_ERROR_HANDLE), _null_mutex)
{
}
};
using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>; using stdout_color_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>; using stdout_color_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
using stderr_color_mt = wincolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
using stderr_color_st = wincolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog
...@@ -176,49 +176,58 @@ inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, c ...@@ -176,49 +176,58 @@ inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name, c
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute); return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute);
} }
///////////////////////////////////////////////////////////////////////////////
// stdout and stderr loggers
//
// multi threaded and colored:
// spdlog::console<stdout_color_mt>("name")
// spdlog::console<stderr_color_mt>("name")
//
// single threaded and colored:
// spdlog::console<stdout_color_st>("name")
// spdlog::console<stderr_color_st>("name")
// //
// multi threaded, no color: // color console logger
// spdlog::console<stdout_mt>("name")
// spdlog::console<stderr_mt>("name")
// //
// single threaded, no color: template<typename Factory = default_factory>
// spdlog::console<stdout_st>("name") inline std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name)
// spdlog::console<stderr_st>("name") {
/////////////////////////////////////////////////////////////////////////////// return Factory::template create<sinks::stdout_color_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_color_st(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_color_st>(logger_name);
}
#if defined _WIN32 // window color console template<typename Factory = default_factory>
using stdout_color_mt = sinks::wincolor_stdout_sink_mt; inline std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name)
using stdout_color_st = sinks::wincolor_stdout_sink_st; {
using stderr_color_mt = sinks::wincolor_stderr_sink_mt; return Factory::template create<sinks::stderr_color_mt>(logger_name);
using stderr_color_st = sinks::wincolor_stderr_sink_st; }
#else // ansi color console
using stdout_color_mt = sinks::ansicolor_stdout_sink_mt; template<typename Factory = default_factory>
using stdout_color_st = sinks::ansicolor_stdout_sink_st; inline std::shared_ptr<logger> stderr_color_st(const std::string &logger_name)
using stderr_color_mt = sinks::ansicolor_stderr_sink_mt;
using stderr_color_st = sinks::ansicolor_stderr_sink_st;
#endif
// no color console
using stdout_mt = sinks::stdout_sink_mt;
using stdout_st = sinks::stdout_sink_st;
using stderr_mt = sinks::stderr_sink_mt;
using stderr_st = sinks::stderr_sink_st;
template<typename Sink, typename Factory = default_factory>
inline std::shared_ptr<logger> console(const std::string &logger_name)
{ {
return Factory::template create<Sink>(logger_name); return Factory::template create<sinks::stderr_color_st>(logger_name);
} }
// console loggers (no colors)
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
template<typename Factory = default_factory>
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
#ifdef SPDLOG_ENABLE_SYSLOG #ifdef SPDLOG_ENABLE_SYSLOG
// Create and register a syslog logger // Create and register a syslog logger
template<typename Factory = default_factory> template<typename Factory = default_factory>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment