support for color formatting

This commit is contained in:
gabime
2018-04-06 02:24:07 +03:00
parent 93d41b2c0e
commit c8610d9a86
18 changed files with 105 additions and 73 deletions

View File

@@ -40,6 +40,9 @@ struct log_msg
fmt::MemoryWriter raw;
fmt::MemoryWriter formatted;
size_t msg_id{0};
// wrap this range with color codes
size_t color_range_start{0};
size_t color_range_end{0};
};
} // namespace details
} // namespace spdlog

View File

@@ -238,7 +238,7 @@ inline size_t filesize(FILE *f)
int fd = fileno(f);
// 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
#if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
struct stat64 st;
struct stat64 st;
if (fstat64(fd, &st) == 0)
{
return static_cast<size_t>(st.st_size);

View File

@@ -421,7 +421,24 @@ private:
std::string _str;
};
// set the color range. expect it to be in the form of "%^colored text%$"
class start_color_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.color_range_start = msg.formatted.size();
}
};
class stop_color_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.color_range_end = msg.formatted.size();
}
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
class full_formatter SPDLOG_FINAL : public flag_formatter
{
@@ -462,8 +479,13 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
msg.formatted << '[' << *msg.logger_name << "] ";
#endif
msg.formatted << '[' << level::to_str(msg.level) << "] ";
const char *level_name = level::to_str(msg.level);
size_t level_name_size = strlen(level_name);
msg.formatted << '[' << fmt::StringRef(level_name, level_name_size) << "] ";
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
// wrap the level with color
msg.color_range_start = 37;
msg.color_range_end = 37 + level_name_size;
}
};
@@ -491,6 +513,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string &patter
{
_formatters.push_back(std::move(user_chars));
}
// if(
if (++it != end)
{
handle_flag(*it);
@@ -644,6 +667,12 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
case ('i'):
_formatters.emplace_back(new details::i_formatter());
break;
case ('^'):
_formatters.emplace_back(new details::start_color_formatter());
break;
case ('$'):
_formatters.emplace_back(new details::stop_color_formatter());
break;
default: // Unknown flag appears as is
_formatters.emplace_back(new details::ch_formatter('%'));

View File

@@ -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_;