spdlog/src/sinks/ansicolor_sink.cpp

138 lines
4.9 KiB
C++
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-12 01:06:17 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-25 01:26:32 +08:00
#include <spdlog/sinks/ansicolor_sink.h>
2019-05-13 05:09:00 +08:00
#include <spdlog/details/os.h>
2023-09-25 07:35:55 +08:00
#include <spdlog/pattern_formatter.h>
2019-05-10 23:48:03 +08:00
2019-06-18 17:32:51 +08:00
namespace spdlog {
namespace sinks {
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
2023-09-25 21:19:10 +08:00
: target_file_(target_file),
mutex_(ConsoleMutex::mutex()),
formatter_(std::make_unique<spdlog::pattern_formatter>())
2019-05-10 23:48:03 +08:00
{
2019-05-24 17:33:14 +08:00
set_color_mode(mode);
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(level::trace)) = to_string_(white);
colors_.at(level_to_number(level::debug)) = to_string_(cyan);
colors_.at(level_to_number(level::info)) = to_string_(green);
colors_.at(level_to_number(level::warn)) = to_string_(yellow_bold);
colors_.at(level_to_number(level::err)) = to_string_(red_bold);
colors_.at(level_to_number(level::critical)) = to_string_(bold_on_red);
colors_.at(level_to_number(level::off)) = to_string_(reset);
2019-05-10 23:48:03 +08:00
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color(level color_level, string_view_t color) {
2019-05-10 23:48:03 +08:00
std::lock_guard<mutex_t> lock(mutex_);
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(color_level)) = to_string_(color);
2019-05-10 23:48:03 +08:00
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg) {
2019-05-10 23:48:03 +08:00
// 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_);
2020-02-24 23:01:09 +08:00
msg.color_range_start = 0;
msg.color_range_end = 0;
memory_buf_t formatted;
2019-05-10 23:48:03 +08:00
formatter_->format(msg, formatted);
2023-09-25 07:35:55 +08:00
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
2019-05-10 23:48:03 +08:00
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
print_ccode_(colors_.at(level_to_number(msg.log_level)));
2019-05-10 23:48:03 +08:00
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
// after color range
print_range_(formatted, msg.color_range_end, formatted.size());
2023-09-25 07:35:55 +08:00
} else // no color
2019-05-10 23:48:03 +08:00
{
print_range_(formatted, 0, formatted.size());
}
fflush(target_file_);
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::flush() {
2019-05-10 23:48:03 +08:00
std::lock_guard<mutex_t> lock(mutex_);
fflush(target_file_);
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
2019-05-10 23:48:03 +08:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_formatter(
std::unique_ptr<spdlog::formatter> sink_formatter) {
2019-05-10 23:48:03 +08:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
bool ansicolor_sink<ConsoleMutex>::should_color() {
2019-05-10 23:48:03 +08:00
return should_do_colors_;
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
switch (mode) {
2019-06-04 05:09:16 +08:00
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();
2019-06-04 05:09:16 +08:00
return;
case color_mode::never:
should_do_colors_ = false;
return;
default:
should_do_colors_ = false;
2019-05-24 17:33:14 +08:00
}
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) {
2020-02-15 17:29:04 +08:00
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
2019-05-10 23:48:03 +08:00
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
size_t start,
size_t end) {
2019-05-10 23:48:03 +08:00
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
}
2019-06-18 17:32:51 +08:00
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) {
2023-09-23 05:27:32 +08:00
return {sv.data(), sv.size()};
2020-05-05 06:03:33 +08:00
}
2019-06-18 17:32:51 +08:00
// ansicolor_stdout_sink
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
2023-09-25 07:35:55 +08:00
: ansicolor_sink<ConsoleMutex>(stdout, mode) {}
2019-06-18 17:32:51 +08:00
// ansicolor_stderr_sink
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
2023-09-25 07:35:55 +08:00
: ansicolor_sink<ConsoleMutex>(stderr, mode) {}
2019-06-18 17:32:51 +08:00
} // namespace sinks
2019-07-04 05:01:17 +08:00
} // namespace spdlog
2023-09-25 01:26:32 +08:00
// template instantiations
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_mutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stdout_sink<spdlog::details::console_nullmutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_mutex>;
template SPDLOG_API class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_nullmutex>;