mirror of
https://github.com/gabime/spdlog.git
synced 2025-11-16 09:28:56 +08:00
support for color formatting
This commit is contained in:
@@ -28,9 +28,9 @@ public:
|
||||
: target_file_(file)
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
||||
colors_[level::trace] = cyan;
|
||||
colors_[level::trace] = magenta;
|
||||
colors_[level::debug] = cyan;
|
||||
colors_[level::info] = reset;
|
||||
colors_[level::info] = green;
|
||||
colors_[level::warn] = yellow + bold;
|
||||
colors_[level::err] = red + bold;
|
||||
colors_[level::critical] = bold + on_red;
|
||||
@@ -83,17 +83,20 @@ protected:
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
if (should_do_colors_)
|
||||
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
const std::string &prefix = colors_[msg.level];
|
||||
fwrite(prefix.data(), sizeof(char), prefix.size(), target_file_);
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
||||
fwrite(reset.data(), sizeof(char), reset.size(), target_file_);
|
||||
fwrite(clear_line.data(), sizeof(char), clear_line.size(), target_file_);
|
||||
// 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
|
||||
{
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
||||
_print_range(msg, 0, msg.formatted.size());
|
||||
}
|
||||
_flush();
|
||||
}
|
||||
@@ -103,6 +106,15 @@ protected:
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
private:
|
||||
void _print_ccode(const std::string &color_code)
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
}
|
||||
void _print_range(const details::log_msg &msg, size_t start, size_t end)
|
||||
{
|
||||
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
FILE *target_file_;
|
||||
bool should_do_colors_;
|
||||
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
||||
|
||||
Reference in New Issue
Block a user