|
|
|
@ -13,12 +13,9 @@
|
|
|
|
|
namespace spdlog {
|
|
|
|
|
namespace sinks {
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
|
|
|
|
: target_file_(target_file),
|
|
|
|
|
mutex_(ConsoleMutex::mutex()),
|
|
|
|
|
formatter_(details::make_unique<spdlog::pattern_formatter>())
|
|
|
|
|
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
|
|
|
|
: target_file_(target_file)
|
|
|
|
|
{
|
|
|
|
|
set_color_mode(mode);
|
|
|
|
|
colors_.at(level::trace) = to_string_(white);
|
|
|
|
@ -30,22 +27,21 @@ SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, co
|
|
|
|
|
colors_.at(level::off) = to_string_(reset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level,
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color(level::level_enum color_level,
|
|
|
|
|
string_view_t color) {
|
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
colors_.at(static_cast<size_t>(color_level)) = to_string_(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg) {
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
|
|
|
|
// 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_);
|
|
|
|
|
msg.color_range_start = 0;
|
|
|
|
|
msg.color_range_end = 0;
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
|
formatter_->format(msg, formatted);
|
|
|
|
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
|
|
|
|
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
|
|
|
|
|
// before color range
|
|
|
|
|
print_range_(formatted, 0, msg.color_range_start);
|
|
|
|
@ -62,74 +58,66 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
|
|
|
|
|
fflush(target_file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::flush() {
|
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::flush_() {
|
|
|
|
|
fflush(target_file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
|
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
|
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(
|
|
|
|
|
std::unique_ptr<spdlog::formatter> sink_formatter) {
|
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
|
formatter_ = std::move(sink_formatter);
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE bool ansicolor_sink<Mutex>::should_color() {
|
|
|
|
|
return should_do_colors_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE bool ansicolor_sink<ConsoleMutex>::should_color() {
|
|
|
|
|
return should_do_colors_;
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
set_color_mode_(mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::set_color_mode_(color_mode mode) {
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case color_mode::always:
|
|
|
|
|
should_do_colors_ = true;
|
|
|
|
|
return;
|
|
|
|
|
return;
|
|
|
|
|
case color_mode::automatic:
|
|
|
|
|
should_do_colors_ =
|
|
|
|
|
details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
|
|
|
|
return;
|
|
|
|
|
return;
|
|
|
|
|
case color_mode::never:
|
|
|
|
|
should_do_colors_ = false;
|
|
|
|
|
return;
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
should_do_colors_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) {
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::print_ccode_(const string_view_t &color_code) {
|
|
|
|
|
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted,
|
|
|
|
|
size_t start,
|
|
|
|
|
size_t end) {
|
|
|
|
|
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) {
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE std::string ansicolor_sink<Mutex>::to_string_(const string_view_t &sv) {
|
|
|
|
|
return std::string(sv.data(), sv.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ansicolor_stdout_sink
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
|
|
|
|
|
: ansicolor_sink<ConsoleMutex>(stdout, mode) {}
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_stdout_sink<Mutex>::ansicolor_stdout_sink(color_mode mode)
|
|
|
|
|
: ansicolor_sink<Mutex>(stdout, mode) {}
|
|
|
|
|
|
|
|
|
|
// ansicolor_stderr_sink
|
|
|
|
|
template <typename ConsoleMutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
|
|
|
|
|
: ansicolor_sink<ConsoleMutex>(stderr, mode) {}
|
|
|
|
|
template <typename Mutex>
|
|
|
|
|
SPDLOG_INLINE ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
|
|
|
|
|
: ansicolor_sink<Mutex>(stderr, mode) {}
|
|
|
|
|
|
|
|
|
|
} // namespace sinks
|
|
|
|
|
} // namespace spdlog
|
|
|
|
|