185 lines
3.8 KiB
C++
185 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Log/Entry.hpp>
|
|
#include <Void/Log/Level.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
class Formatter
|
|
{
|
|
public:
|
|
|
|
virtual ~Formatter() = default;
|
|
|
|
[[nodiscard]] virtual std::string
|
|
format(const Entry& entry) const = 0;
|
|
};
|
|
|
|
enum class AnsiColor {
|
|
Default,
|
|
Black,
|
|
Red,
|
|
Green,
|
|
Yellow,
|
|
Blue,
|
|
Magenta,
|
|
Cyan,
|
|
White,
|
|
BrightBlack,
|
|
BrightRed,
|
|
BrightGreen,
|
|
BrightYellow,
|
|
BrightBlue,
|
|
BrightMagenta,
|
|
BrightCyan,
|
|
BrightWhite,
|
|
};
|
|
|
|
[[nodiscard]] AnsiColor parse_color(std::string_view name) noexcept;
|
|
[[nodiscard]] std::string_view ansi_code(AnsiColor color) noexcept;
|
|
|
|
inline constexpr std::string_view kAnsiReset = "\x1b[0m";
|
|
|
|
struct ColorPolicy {
|
|
bool enabled = false;
|
|
std::unordered_map<Level, AnsiColor> level_colors;
|
|
std::unordered_map<std::string, AnsiColor> token_colors;
|
|
|
|
[[nodiscard]] static ColorPolicy defaults();
|
|
};
|
|
|
|
struct FieldFilter {
|
|
std::vector<std::string> include{};
|
|
std::vector<std::string> exclude{};
|
|
std::vector<std::string> redact{};
|
|
std::string redaction_mask{"[REDACTED]"};
|
|
|
|
[[nodiscard]] bool allows(std::string_view key) const
|
|
{
|
|
for (const auto& e : exclude) {
|
|
if (e == key) {
|
|
return false;
|
|
}
|
|
}
|
|
if (include.empty()) {
|
|
return true;
|
|
}
|
|
for (const auto& i : include) {
|
|
if (i == key) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
[[nodiscard]] bool is_redacted(std::string_view key) const
|
|
{
|
|
for (const auto& r : redact) {
|
|
if (r == key) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
|
|
class PatternFormatter final : public Formatter
|
|
{
|
|
public:
|
|
|
|
explicit PatternFormatter(
|
|
std::string pattern = "{time} [{level}] {scopes}{message}");
|
|
|
|
[[nodiscard]] std::string format(const Entry& entry) const override;
|
|
|
|
void register_token(std::string name,
|
|
std::function<std::string(const Entry&)>
|
|
renderer);
|
|
|
|
void set_color_policy(ColorPolicy policy)
|
|
{
|
|
m_colors = std::move(policy);
|
|
}
|
|
|
|
[[nodiscard]] const ColorPolicy& color_policy() const noexcept
|
|
{
|
|
return m_colors;
|
|
}
|
|
|
|
void set_field_filter(FieldFilter f) { m_fields = std::move(f); }
|
|
|
|
private:
|
|
|
|
enum class Align { Left, Right, Center };
|
|
enum class ColorMode { None, Auto, Fixed };
|
|
|
|
struct Segment {
|
|
bool literal{true};
|
|
std::string text;
|
|
Align align{Align::Left};
|
|
std::size_t width{0};
|
|
ColorMode color_mode{ColorMode::None};
|
|
AnsiColor color{AnsiColor::Default};
|
|
};
|
|
|
|
[[nodiscard]] std::string render_token(const Segment& seg,
|
|
const Entry& entry) const;
|
|
|
|
std::vector<Segment> m_segments;
|
|
std::unordered_map<std::string,
|
|
std::function<std::string(const Entry&)>>
|
|
m_custom;
|
|
ColorPolicy m_colors;
|
|
FieldFilter m_fields;
|
|
};
|
|
|
|
class JsonLinesFormatter final : public Formatter
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] std::string format(const Entry& entry) const override;
|
|
|
|
void set_field_filter(FieldFilter f) { m_fields = std::move(f); }
|
|
|
|
private:
|
|
|
|
FieldFilter m_fields;
|
|
};
|
|
|
|
class LogfmtFormatter final : public Formatter
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] std::string format(const Entry& entry) const override;
|
|
|
|
void set_field_filter(FieldFilter f) { m_fields = std::move(f); }
|
|
|
|
private:
|
|
|
|
FieldFilter m_fields;
|
|
};
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
format_bounded(const Formatter& formatter,
|
|
const Entry& entry,
|
|
std::size_t max_bytes = 64ULL * 1024ULL,
|
|
bool truncate = false);
|
|
|
|
namespace FormatDetail
|
|
{
|
|
[[nodiscard]] std::string
|
|
iso8601(std::chrono::system_clock::time_point tp);
|
|
|
|
[[nodiscard]] std::string
|
|
scope_breadcrumb(const std::vector<std::string>& scopes);
|
|
} // namespace FormatDetail
|
|
|
|
} // namespace Void::Logging
|