|
|
@ -28,9 +28,9 @@ public:
|
|
|
|
: target_file_(file)
|
|
|
|
: target_file_(file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
|
|
|
|
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::debug] = cyan;
|
|
|
|
colors_[level::info] = reset;
|
|
|
|
colors_[level::info] = green;
|
|
|
|
colors_[level::warn] = yellow + bold;
|
|
|
|
colors_[level::warn] = yellow + bold;
|
|
|
|
colors_[level::err] = red + bold;
|
|
|
|
colors_[level::err] = red + bold;
|
|
|
|
colors_[level::critical] = bold + on_red;
|
|
|
|
colors_[level::critical] = bold + on_red;
|
|
|
@ -83,17 +83,20 @@ protected:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Wrap the originally formatted message in color codes.
|
|
|
|
// Wrap the originally formatted message in color codes.
|
|
|
|
// If color is not supported in the terminal, log as is instead.
|
|
|
|
// 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];
|
|
|
|
// before color range
|
|
|
|
fwrite(prefix.data(), sizeof(char), prefix.size(), target_file_);
|
|
|
|
_print_range(msg, 0, msg.color_range_start);
|
|
|
|
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
|
|
|
// in color range
|
|
|
|
fwrite(reset.data(), sizeof(char), reset.size(), target_file_);
|
|
|
|
_print_ccode(colors_[msg.level]);
|
|
|
|
fwrite(clear_line.data(), sizeof(char), clear_line.size(), target_file_);
|
|
|
|
_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
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_);
|
|
|
|
_print_range(msg, 0, msg.formatted.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_flush();
|
|
|
|
_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -103,6 +106,15 @@ protected:
|
|
|
|
fflush(target_file_);
|
|
|
|
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_;
|
|
|
|
FILE *target_file_;
|
|
|
|
bool should_do_colors_;
|
|
|
|
bool should_do_colors_;
|
|
|
|
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
|
|
|
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
|
|
|
|