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('%'));