mirror of https://github.com/gabime/spdlog.git
#1838 Factor out ANSI coloring.
Purpose to make the ANSI coloring code reusable in new sink types.pull/3112/head
parent
3d91da64b0
commit
1d7886a27a
@ -0,0 +1,57 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef SPDLOG_HEADER_ONLY
|
||||
#include <spdlog/details/ansicolors.h>
|
||||
#endif
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
SPDLOG_INLINE ansicolors::ansicolors() {
|
||||
colors_.at(level::trace) = to_string_(white);
|
||||
colors_.at(level::debug) = to_string_(cyan);
|
||||
colors_.at(level::info) = to_string_(green);
|
||||
colors_.at(level::warn) = to_string_(yellow_bold);
|
||||
colors_.at(level::err) = to_string_(red_bold);
|
||||
colors_.at(level::critical) = to_string_(bold_on_red);
|
||||
colors_.at(level::off) = to_string_(reset);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void ansicolors::set_color(level::level_enum color_level, string_view_t color) {
|
||||
colors_.at(static_cast<size_t>(color_level)) = to_string_(color);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE std::vector<string_view_t> ansicolors::ranges(
|
||||
const details::log_msg &msg, const memory_buf_t &formatted_msg) const {
|
||||
std::vector<string_view_t> result{};
|
||||
if (msg.color_range_end > msg.color_range_start) {
|
||||
// before color range
|
||||
if (msg.color_range_start > 0) {
|
||||
result.push_back(string_view_t{formatted_msg.data(), msg.color_range_start});
|
||||
}
|
||||
// in color range
|
||||
result.push_back(string_view_t{colors_.at(static_cast<size_t>(msg.level))});
|
||||
result.push_back(string_view_t{formatted_msg.data() + msg.color_range_start,
|
||||
msg.color_range_end - msg.color_range_start});
|
||||
result.push_back(reset);
|
||||
// after color range
|
||||
if (msg.color_range_end < formatted_msg.size()) {
|
||||
result.push_back(string_view_t{formatted_msg.data() + msg.color_range_end,
|
||||
formatted_msg.size() - msg.color_range_end});
|
||||
}
|
||||
} else // no color
|
||||
{
|
||||
result.push_back(string_view_t{formatted_msg.data(), formatted_msg.size()});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE std::string ansicolors::to_string_(const string_view_t &sv) {
|
||||
return std::string(sv.data(), sv.size());
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@ -0,0 +1,72 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/common.h>
|
||||
|
||||
#include <spdlog/details/log_msg.h>
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
class SPDLOG_API ansicolors {
|
||||
public:
|
||||
explicit ansicolors();
|
||||
ansicolors(const ansicolors&) = delete;
|
||||
ansicolors& operator=(const ansicolors&) = delete;
|
||||
|
||||
void set_color(level::level_enum color_level, string_view_t color);
|
||||
|
||||
std::vector<string_view_t> ranges(const details::log_msg& msg,
|
||||
const memory_buf_t& formatted_msg) const;
|
||||
|
||||
// Formatting codes
|
||||
static constexpr const char* reset = "\033[m";
|
||||
static constexpr const char* bold = "\033[1m";
|
||||
static constexpr const char* dark = "\033[2m";
|
||||
static constexpr const char* underline = "\033[4m";
|
||||
static constexpr const char* blink = "\033[5m";
|
||||
static constexpr const char* reverse = "\033[7m";
|
||||
static constexpr const char* concealed = "\033[8m";
|
||||
static constexpr const char* clear_line = "\033[K";
|
||||
|
||||
// Foreground colors
|
||||
static constexpr const char* black = "\033[30m";
|
||||
static constexpr const char* red = "\033[31m";
|
||||
static constexpr const char* green = "\033[32m";
|
||||
static constexpr const char* yellow = "\033[33m";
|
||||
static constexpr const char* blue = "\033[34m";
|
||||
static constexpr const char* magenta = "\033[35m";
|
||||
static constexpr const char* cyan = "\033[36m";
|
||||
static constexpr const char* white = "\033[37m";
|
||||
|
||||
/// Background colors
|
||||
static constexpr const char* on_black = "\033[40m";
|
||||
static constexpr const char* on_red = "\033[41m";
|
||||
static constexpr const char* on_green = "\033[42m";
|
||||
static constexpr const char* on_yellow = "\033[43m";
|
||||
static constexpr const char* on_blue = "\033[44m";
|
||||
static constexpr const char* on_magenta = "\033[45m";
|
||||
static constexpr const char* on_cyan = "\033[46m";
|
||||
static constexpr const char* on_white = "\033[47m";
|
||||
|
||||
/// Bold colors
|
||||
static constexpr const char* yellow_bold = "\033[33m\033[1m";
|
||||
static constexpr const char* red_bold = "\033[31m\033[1m";
|
||||
static constexpr const char* bold_on_red = "\033[1m\033[41m";
|
||||
|
||||
private:
|
||||
std::array<std::string, level::n_levels> colors_;
|
||||
static std::string to_string_(const string_view_t& sv);
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
||||
#include "ansicolors-inl.h"
|
||||
#endif
|
Loading…
Reference in New Issue