mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-24 14:42:13 +08:00
Merge v1.x
This commit is contained in:
commit
7a3a560c44
@ -156,6 +156,16 @@ spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
|
|||||||
using level_hasher = std::hash<int>;
|
using level_hasher = std::hash<int>;
|
||||||
} // namespace level
|
} // namespace level
|
||||||
|
|
||||||
|
//
|
||||||
|
// Color mode used by sinks with color support.
|
||||||
|
//
|
||||||
|
enum class color_mode
|
||||||
|
{
|
||||||
|
always,
|
||||||
|
automatic,
|
||||||
|
never
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// Pattern time - specific time getting to use for pattern_formatter.
|
// Pattern time - specific time getting to use for pattern_formatter.
|
||||||
// local time by default
|
// local time by default
|
||||||
|
@ -10,12 +10,12 @@
|
|||||||
#include "spdlog/details/os.h"
|
#include "spdlog/details/os.h"
|
||||||
|
|
||||||
template<typename TargetStream, typename ConsoleMutex>
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
SPDLOG_INLINE spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::ansicolor_sink()
|
SPDLOG_INLINE spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::ansicolor_sink(color_mode mode)
|
||||||
: target_file_(TargetStream::stream())
|
: target_file_(TargetStream::stream())
|
||||||
, mutex_(ConsoleMutex::mutex())
|
, mutex_(ConsoleMutex::mutex())
|
||||||
|
|
||||||
{
|
{
|
||||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
set_color_mode(mode);
|
||||||
colors_[level::trace] = white;
|
colors_[level::trace] = white;
|
||||||
colors_[level::debug] = cyan;
|
colors_[level::debug] = cyan;
|
||||||
colors_[level::info] = green;
|
colors_[level::info] = green;
|
||||||
@ -88,6 +88,23 @@ SPDLOG_INLINE bool spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::sh
|
|||||||
return should_do_colors_;
|
return should_do_colors_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
|
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_color_mode(color_mode mode)
|
||||||
|
{
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case color_mode::always:
|
||||||
|
should_do_colors_ = true;
|
||||||
|
return;
|
||||||
|
case color_mode::automatic:
|
||||||
|
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||||
|
return;
|
||||||
|
case color_mode::never:
|
||||||
|
should_do_colors_ = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename TargetStream, typename ConsoleMutex>
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_ccode_(const std::string &color_code)
|
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_ccode_(const std::string &color_code)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@ class ansicolor_sink final : public sink
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||||
ansicolor_sink();
|
ansicolor_sink(color_mode mode = color_mode::automatic);
|
||||||
~ansicolor_sink() override = default;
|
~ansicolor_sink() override = default;
|
||||||
|
|
||||||
ansicolor_sink(const ansicolor_sink &other) = delete;
|
ansicolor_sink(const ansicolor_sink &other) = delete;
|
||||||
@ -40,6 +40,7 @@ public:
|
|||||||
void set_pattern(const std::string &pattern) final;
|
void set_pattern(const std::string &pattern) final;
|
||||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
|
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
|
||||||
bool should_color();
|
bool should_color();
|
||||||
|
void set_color_mode(color_mode mode);
|
||||||
|
|
||||||
/// Formatting codes
|
/// Formatting codes
|
||||||
const std::string reset = "\033[m";
|
const std::string reset = "\033[m";
|
||||||
|
@ -13,26 +13,26 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
template<typename Factory>
|
template<typename Factory>
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name)
|
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::stdout_color_sink_mt>(logger_name);
|
return Factory::template create<sinks::stdout_color_sink_mt>(logger_name, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory>
|
template<typename Factory>
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_st(const std::string &logger_name)
|
SPDLOG_INLINE std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::stdout_color_sink_st>(logger_name);
|
return Factory::template create<sinks::stdout_color_sink_st>(logger_name, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory>
|
template<typename Factory>
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name)
|
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::stderr_color_sink_mt>(logger_name);
|
return Factory::template create<sinks::stderr_color_sink_mt>(logger_name, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory>
|
template<typename Factory>
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_st(const std::string &logger_name)
|
SPDLOG_INLINE std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::stderr_color_sink_st>(logger_name);
|
return Factory::template create<sinks::stderr_color_sink_st>(logger_name, mode);
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
@ -29,16 +29,16 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
|
|||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
|
|
||||||
template<typename Factory = default_factory>
|
template<typename Factory = default_factory>
|
||||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name);
|
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||||
|
|
||||||
template<typename Factory = default_factory>
|
template<typename Factory = default_factory>
|
||||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name);
|
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||||
|
|
||||||
template<typename Factory = default_factory>
|
template<typename Factory = default_factory>
|
||||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name);
|
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||||
|
|
||||||
template<typename Factory = default_factory>
|
template<typename Factory = default_factory>
|
||||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name);
|
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
@ -13,10 +13,11 @@ namespace spdlog {
|
|||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
template<typename TargetStream, typename ConsoleMutex>
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::wincolor_sink()
|
SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::wincolor_sink(color_mode mode)
|
||||||
: out_handle_(TargetStream::handle())
|
: out_handle_(TargetStream::handle())
|
||||||
, mutex_(ConsoleMutex::mutex())
|
, mutex_(ConsoleMutex::mutex())
|
||||||
{
|
{
|
||||||
|
set_color_mode(mode);
|
||||||
colors_[level::trace] = WHITE;
|
colors_[level::trace] = WHITE;
|
||||||
colors_[level::debug] = CYAN;
|
colors_[level::debug] = CYAN;
|
||||||
colors_[level::info] = GREEN;
|
colors_[level::info] = GREEN;
|
||||||
@ -46,7 +47,7 @@ void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::log(const details:
|
|||||||
std::lock_guard<mutex_t> lock(mutex_);
|
std::lock_guard<mutex_t> lock(mutex_);
|
||||||
fmt::memory_buffer formatted;
|
fmt::memory_buffer formatted;
|
||||||
formatter_->format(msg, formatted);
|
formatter_->format(msg, formatted);
|
||||||
if (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
|
||||||
print_range_(formatted, 0, msg.color_range_start);
|
print_range_(formatted, 0, msg.color_range_start);
|
||||||
@ -59,7 +60,7 @@ void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::log(const details:
|
|||||||
// after color range
|
// after color range
|
||||||
print_range_(formatted, msg.color_range_end, formatted.size());
|
print_range_(formatted, msg.color_range_end, formatted.size());
|
||||||
}
|
}
|
||||||
else // print without colors if color range is invalid
|
else // print without colors if color range is invalid (or color is disabled)
|
||||||
{
|
{
|
||||||
print_range_(formatted, 0, formatted.size());
|
print_range_(formatted, 0, formatted.size());
|
||||||
}
|
}
|
||||||
@ -85,6 +86,23 @@ void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_formatter(std:
|
|||||||
formatter_ = std::move(sink_formatter);
|
formatter_ = std::move(sink_formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
|
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_color_mode(color_mode mode)
|
||||||
|
{
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case color_mode::always:
|
||||||
|
case color_mode::automatic:
|
||||||
|
should_do_colors_ = true;
|
||||||
|
break;
|
||||||
|
case color_mode::never:
|
||||||
|
should_do_colors_ = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
should_do_colors_ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set color and return the orig console attributes (for resetting later)
|
// set color and return the orig console attributes (for resetting later)
|
||||||
template<typename TargetStream, typename ConsoleMutex>
|
template<typename TargetStream, typename ConsoleMutex>
|
||||||
WORD SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_console_attribs(WORD attribs)
|
WORD SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_console_attribs(WORD attribs)
|
||||||
|
@ -35,7 +35,7 @@ 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();
|
wincolor_sink(color_mode mode = color_mode::automatic);
|
||||||
~wincolor_sink() override;
|
~wincolor_sink() override;
|
||||||
|
|
||||||
wincolor_sink(const wincolor_sink &other) = delete;
|
wincolor_sink(const wincolor_sink &other) = delete;
|
||||||
@ -47,11 +47,13 @@ public:
|
|||||||
void flush() final override;
|
void flush() final override;
|
||||||
void set_pattern(const std::string &pattern) override final;
|
void set_pattern(const std::string &pattern) override final;
|
||||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final;
|
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final;
|
||||||
|
void set_color_mode(color_mode mode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||||
HANDLE out_handle_;
|
HANDLE out_handle_;
|
||||||
mutex_t &mutex_;
|
mutex_t &mutex_;
|
||||||
|
bool should_do_colors_;
|
||||||
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
||||||
|
|
||||||
// set color and return the orig console attributes (for resetting later)
|
// set color and return the orig console attributes (for resetting later)
|
||||||
|
@ -58,15 +58,15 @@ template class spdlog::sinks::ansicolor_sink<spdlog::details::console_stderr, sp
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/sinks/stdout_color_sinks-inl.h"
|
#include "spdlog/sinks/stdout_color_sinks-inl.h"
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::synchronous_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::synchronous_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::synchronous_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||||
|
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
|
||||||
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name);
|
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
|
||||||
|
|
||||||
// Slightly modified version of fmt lib's format.cc source file.
|
// Slightly modified version of fmt lib's format.cc source file.
|
||||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
// Copyright (c) 2012 - 2016, Victor Zverovich
|
||||||
|
Loading…
Reference in New Issue
Block a user