mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
Refactred spdlog.h and console sinks. Added global lock for all console sinks (traits)
This commit is contained in:
@@ -5,12 +5,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../common.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "../details/os.h"
|
||||
#include "base_sink.h"
|
||||
#include "../details/traits.h"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
@@ -20,12 +22,15 @@ namespace sinks {
|
||||
* of the message.
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
template<class Mutex>
|
||||
class ansicolor_sink : public base_sink<Mutex>
|
||||
{
|
||||
template<class StdoutTrait, class ConsoleMutexTrait>
|
||||
class ansicolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
explicit ansicolor_sink(FILE *file)
|
||||
: target_file_(file)
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
ansicolor_sink()
|
||||
: target_file_(StdoutTrait::stream()),
|
||||
_mutex(ConsoleMutexTrait::mutex())
|
||||
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
||||
colors_[level::trace] = white;
|
||||
@@ -42,9 +47,12 @@ public:
|
||||
_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)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
|
||||
std::lock_guard<mutex_t> lock(_mutex);
|
||||
colors_[color_level] = color;
|
||||
}
|
||||
|
||||
@@ -78,35 +86,39 @@ public:
|
||||
const std::string on_cyan = "\033[46m";
|
||||
const std::string on_white = "\033[47m";
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
// before color range
|
||||
_print_range(msg, 0, msg.color_range_start);
|
||||
// in color range
|
||||
_print_ccode(colors_[msg.level]);
|
||||
_print_range(msg, msg.color_range_start, msg.color_range_end);
|
||||
_print_ccode(reset);
|
||||
// after color range
|
||||
_print_range(msg, msg.color_range_end, msg.formatted.size());
|
||||
}
|
||||
else // no color
|
||||
{
|
||||
_print_range(msg, 0, msg.formatted.size());
|
||||
}
|
||||
_flush();
|
||||
}
|
||||
|
||||
void _flush() override
|
||||
{
|
||||
fflush(target_file_);
|
||||
}
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// 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)
|
||||
{
|
||||
// before color range
|
||||
_print_range(msg, 0, msg.color_range_start);
|
||||
// in color range
|
||||
_print_ccode(colors_[msg.level]);
|
||||
_print_range(msg, msg.color_range_start, msg.color_range_end);
|
||||
_print_ccode(reset);
|
||||
// after color range
|
||||
_print_range(msg, msg.color_range_end, msg.formatted.size());
|
||||
}
|
||||
else // no color
|
||||
{
|
||||
_print_range(msg, 0, msg.formatted.size());
|
||||
}
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
private:
|
||||
void flush() SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(_mutex);
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void _print_ccode(const std::string &color_code)
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
@@ -115,36 +127,19 @@ private:
|
||||
{
|
||||
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
FILE *target_file_;
|
||||
mutex_t & _mutex;
|
||||
|
||||
bool should_do_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>
|
||||
class ansicolor_stdout_sink : public ansicolor_sink<Mutex>
|
||||
{
|
||||
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>;
|
||||
|
||||
using stderr_color_mt = ansicolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
|
||||
using stderr_color_st = ansicolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
|
||||
#endif
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
#include "../details/traits.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
@@ -15,53 +15,40 @@
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<class Mutex>
|
||||
class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
using MyType = stdout_sink<Mutex>;
|
||||
|
||||
template<class StdoutTrait, class ConsoleMutexTrait>
|
||||
class stdout_sink : public sink
|
||||
{
|
||||
public:
|
||||
explicit stdout_sink() = default;
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
stdout_sink() :
|
||||
_mutex(ConsoleMutexTrait::console_mutex()),
|
||||
_file(StdoutTrait::stream()) {}
|
||||
~stdout_sink() = default;
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
stdout_sink(const stdout_sink &other) = delete;
|
||||
stdout_sink &operator=(const stdout_sink &other) = delete;
|
||||
|
||||
void log(const details::log_msg &msg) override
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
|
||||
_flush();
|
||||
std::lock_guard<mutex_t> lock(_mutex);
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), _file);
|
||||
fflush(StdoutTrait::stream());
|
||||
}
|
||||
|
||||
void _flush() override
|
||||
void flush() override
|
||||
{
|
||||
fflush(stdout);
|
||||
std::lock_guard<mutex_t> lock(_mutex);
|
||||
fflush(StdoutTrait::stream());
|
||||
}
|
||||
private:
|
||||
typename mutex_t& _mutex;
|
||||
FILE* _file;
|
||||
};
|
||||
|
||||
using stdout_sink_mt = stdout_sink<std::mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::null_mutex>;
|
||||
|
||||
template<class Mutex>
|
||||
class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
|
||||
{
|
||||
using MyType = stderr_sink<Mutex>;
|
||||
|
||||
public:
|
||||
explicit stderr_sink() = default;
|
||||
|
||||
protected:
|
||||
void _sink_it(const details::log_msg &msg) override
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr);
|
||||
_flush();
|
||||
}
|
||||
|
||||
void _flush() override
|
||||
{
|
||||
fflush(stderr);
|
||||
}
|
||||
};
|
||||
|
||||
using stderr_sink_mt = stderr_sink<std::mutex>;
|
||||
using stderr_sink_st = stderr_sink<details::null_mutex>;
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>;
|
||||
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 spdlog
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
#include "../details/null_mutex.h"
|
||||
#include "base_sink.h"
|
||||
#include "../details/traits.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
@@ -19,11 +19,12 @@ namespace spdlog {
|
||||
namespace sinks {
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
public:
|
||||
|
||||
const WORD BOLD = FOREGROUND_INTENSITY;
|
||||
const WORD RED = FOREGROUND_RED;
|
||||
const WORD GREEN = FOREGROUND_GREEN;
|
||||
@@ -31,8 +32,9 @@ public:
|
||||
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
|
||||
wincolor_sink(HANDLE std_handle, Mutex& stdout_mutex)
|
||||
: out_handle_(std_handle), _mutex(stdout_mutex)
|
||||
wincolor_sink()
|
||||
: out_handle_(HandleTrait::handle()),
|
||||
_mutex(ConsoleMutexTrait::console_mutex())
|
||||
{
|
||||
colors_[level::trace] = WHITE;
|
||||
colors_[level::debug] = CYAN;
|
||||
@@ -55,13 +57,13 @@ public:
|
||||
// change the color for the given level
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -85,9 +87,9 @@ public:
|
||||
{
|
||||
// windows console always flushed?
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
// set color and return the orig console attributes (for resetting later)
|
||||
WORD set_console_attribs(WORD attribs)
|
||||
{
|
||||
@@ -109,64 +111,18 @@ private:
|
||||
}
|
||||
|
||||
HANDLE out_handle_;
|
||||
Mutex& _mutex;
|
||||
mutex_t &_mutex;
|
||||
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
||||
};
|
||||
|
||||
//
|
||||
// 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 wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
|
||||
using stdout_color_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
|
||||
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 spdlog
|
||||
|
||||
Reference in New Issue
Block a user