global mutex stdout stderr sinks

This commit is contained in:
gabime 2018-04-16 01:07:22 +03:00
parent c50ba69689
commit 9bffa921ae
2 changed files with 79 additions and 37 deletions

View File

@ -17,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <thread> #include <thread>
#include <mutex>
#ifdef _WIN32 #ifdef _WIN32
@ -474,6 +475,19 @@ inline bool in_terminal(FILE *file)
return isatty(fileno(file)) != 0; return isatty(fileno(file)) != 0;
#endif #endif
} }
// stdout/stderr global mutexes
inline std::mutex& stdout_mutex()
{
static std::mutex &mutex = std::mutex{};
return mutex;
}
inline std::mutex& stderr_mutex()
{
static std::mutex &mutex = std::mutex{};
return mutex;
}
} // namespace os } // namespace os
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@ -10,6 +10,7 @@
#include "base_sink.h" #include "base_sink.h"
#include <mutex> #include <mutex>
#include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <wincon.h> #include <wincon.h>
@ -20,7 +21,7 @@ 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 Mutex>
class wincolor_sink : public base_sink<Mutex> class wincolor_sink : public sink
{ {
public: public:
const WORD BOLD = FOREGROUND_INTENSITY; const WORD BOLD = FOREGROUND_INTENSITY;
@ -30,8 +31,8 @@ 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) wincolor_sink(HANDLE std_handle, Mutex& stdout_mutex)
: out_handle_(std_handle) : out_handle_(std_handle), _mutex(stdout_mutex)
{ {
colors_[level::trace] = WHITE; colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN; colors_[level::debug] = CYAN;
@ -42,6 +43,7 @@ public:
colors_[level::off] = 0; colors_[level::off] = 0;
} }
~wincolor_sink() override ~wincolor_sink() override
{ {
this->flush(); this->flush();
@ -57,55 +59,58 @@ public:
colors_[level] = color; colors_[level] = color;
} }
protected: void log(const details::log_msg &msg) SPDLOG_FINAL override
void _sink_it(const details::log_msg &msg) override {
{ std::lock_guard<Mutex> lock(_mutex);
if (msg.color_range_end > msg.color_range_start)
{
// before color range
_print_range(msg, 0, msg.color_range_start);
// in color range if (msg.color_range_end > msg.color_range_start)
auto orig_attribs = set_console_attribs(colors_[msg.level]); {
_print_range(msg, msg.color_range_start, msg.color_range_end); // before color range
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors _print_range(msg, 0, msg.color_range_start);
// after color range
_print_range(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
}
}
void _flush() override // in color range
{ auto orig_attribs = set_console_attribs(colors_[msg.level]);
// windows console always flushed? _print_range(msg, msg.color_range_start, msg.color_range_end);
} ::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
// after color range
_print_range(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
}
}
private: void flush() SPDLOG_FINAL override
HANDLE out_handle_; {
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; // windows console always flushed?
}
private:
// 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)
{ {
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
WORD back_color = orig_buffer_info.wAttributes; WORD back_color = orig_buffer_info.wAttributes;
// retrieve the current background color // retrieve the current background color
back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)); back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
// keep the background color unchanged // keep the background color unchanged
SetConsoleTextAttribute(out_handle_, attribs | back_color); ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
return orig_buffer_info.wAttributes; // return orig attribs return orig_buffer_info.wAttributes; // return orig attribs
} }
// print a range of formatted message to console // print a range of formatted message to console
void _print_range(const details::log_msg &msg, size_t start, size_t end) void _print_range(const details::log_msg &msg, size_t start, size_t end)
{ {
DWORD size = static_cast<DWORD>(end - start); auto size = static_cast<DWORD>(end - start);
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr); ::WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
} }
HANDLE out_handle_;
Mutex& _mutex;
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
}; };
// //
@ -113,14 +118,26 @@ private:
// //
template<class Mutex> template<class Mutex>
class wincolor_stdout_sink : public wincolor_sink<Mutex> class wincolor_stdout_sink : public wincolor_sink<Mutex>
{ {
public: public:
wincolor_stdout_sink() wincolor_stdout_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE)) : 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_mt = wincolor_stdout_sink<std::mutex>;
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>; using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
@ -132,11 +149,22 @@ class wincolor_stderr_sink : public wincolor_sink<Mutex>
{ {
public: public:
wincolor_stderr_sink() wincolor_stderr_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE)) : 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 wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>; using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;