9 #include "quill/core/Attributes.h" 10 #include "quill/core/LogLevel.h" 11 #include "quill/sinks/StreamSink.h" 19 #include <string_view> 24 #if !defined(WIN32_LEAN_AND_MEAN) 25 #define WIN32_LEAN_AND_MEAN 28 #if !defined(NOMINMAX) 61 Colours() { apply_default_colours(); }
70 assign_colour_to_log_level(LogLevel::TraceL3, white);
71 assign_colour_to_log_level(LogLevel::TraceL2, white);
72 assign_colour_to_log_level(LogLevel::TraceL1, white);
73 assign_colour_to_log_level(LogLevel::Debug, cyan);
74 assign_colour_to_log_level(LogLevel::Info, green);
75 assign_colour_to_log_level(LogLevel::Notice, white_bold);
76 assign_colour_to_log_level(LogLevel::Warning, yellow_bold);
77 assign_colour_to_log_level(LogLevel::Error, red_bold);
78 assign_colour_to_log_level(LogLevel::Critical, bold_on_red);
79 assign_colour_to_log_level(LogLevel::Backtrace, magenta);
89 auto const log_lvl =
static_cast<uint32_t
>(log_level);
90 _log_level_colours[log_lvl] = colour;
91 _colours_enabled =
true;
94 QUILL_ATTRIBUTE_COLD
void set_colours_enabled(
bool value) { _colours_enabled = value; }
101 return _colour_output_supported && _colours_enabled;
111 auto const log_lvl =
static_cast<uint32_t
>(log_level);
112 return _log_level_colours[log_lvl];
116 static constexpr std::string_view reset{
"\033[0m"};
117 static constexpr std::string_view bold{
"\033[1m"};
118 static constexpr std::string_view dark{
"\033[2m"};
119 static constexpr std::string_view underline{
"\033[4m"};
120 static constexpr std::string_view blink{
"\033[5m"};
121 static constexpr std::string_view reverse{
"\033[7m"};
122 static constexpr std::string_view concealed{
"\033[8m"};
123 static constexpr std::string_view clear_line{
"\033[K"};
126 static constexpr std::string_view black{
"\033[30m"};
127 static constexpr std::string_view red{
"\033[31m"};
128 static constexpr std::string_view green{
"\033[32m"};
129 static constexpr std::string_view yellow{
"\033[33m"};
130 static constexpr std::string_view blue{
"\033[34m"};
131 static constexpr std::string_view magenta{
"\033[35m"};
132 static constexpr std::string_view cyan{
"\033[36m"};
133 static constexpr std::string_view white{
"\033[37m"};
136 static constexpr std::string_view on_black{
"\033[40m"};
137 static constexpr std::string_view on_red{
"\033[41m"};
138 static constexpr std::string_view on_green{
"\033[42m"};
139 static constexpr std::string_view on_yellow{
"\033[43m"};
140 static constexpr std::string_view on_blue{
"\033[44m"};
141 static constexpr std::string_view on_magenta{
"\033[45m"};
142 static constexpr std::string_view on_cyan{
"\033[46m"};
143 static constexpr std::string_view on_white{
"\033[47m"};
146 static constexpr std::string_view white_bold{
"\033[97m\033[1m"};
147 static constexpr std::string_view yellow_bold{
"\033[33m\033[1m"};
148 static constexpr std::string_view red_bold{
"\033[31m\033[1m"};
149 static constexpr std::string_view bold_on_red{
"\033[1m\033[41m"};
155 QUILL_NODISCARD QUILL_ATTRIBUTE_COLD
static bool _supports_colour_output() noexcept
162 auto* env_p = std::getenv(
"TERM");
164 if (env_p ==
nullptr)
169 static constexpr
char const* terms[] = {
170 "ansi",
"color",
"console",
"cygwin",
"gnome",
171 "konsole",
"kterm",
"linux",
"msys",
"putty",
172 "rxvt",
"screen",
"vt100",
"xterm",
"tmux",
173 "terminator",
"alacritty",
"gnome-terminal",
"xfce4-terminal",
"lxterminal",
174 "mate-terminal",
"uxterm",
"eterm",
"tilix",
"rxvt-unicode",
178 for (
char const* term : terms)
180 if (std::strstr(env_p, term) !=
nullptr)
193 QUILL_NODISCARD QUILL_ATTRIBUTE_COLD
static bool _is_terminal_output(FILE* file) noexcept
196 return _isatty(_fileno(file)) != 0;
198 return ::isatty(fileno(file)) != 0;
204 QUILL_ATTRIBUTE_COLD
void _activate_ansi_support(FILE* file)
const 206 if (!_colour_output_supported)
212 auto const out_handle =
reinterpret_cast<HANDLE
>(_get_osfhandle(_fileno(file)));
213 if (out_handle == INVALID_HANDLE_VALUE)
219 if (!GetConsoleMode(out_handle, &dw_mode))
224 dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
225 dw_mode |= ENABLE_PROCESSED_OUTPUT;
227 SetConsoleMode(out_handle, dw_mode);
232 void _configure_colour_support(FILE* file, ColourMode colour_mode) noexcept
234 if (colour_mode == ColourMode::Always)
236 _colour_output_supported =
true;
238 else if (colour_mode == ColourMode::Automatic)
240 _colour_output_supported = _is_terminal_output(file) && _supports_colour_output();
244 _colour_output_supported =
false;
249 _activate_ansi_support(file);
254 std::array<std::string_view, 10> _log_level_colours;
255 bool _colours_enabled{
true};
256 bool _colour_output_supported{
false};
283 QUILL_ATTRIBUTE_COLD
void set_colour_mode(ColourMode colour_mode) { _colour_mode = colour_mode; }
297 QUILL_ATTRIBUTE_COLD
void set_stream(std::string
const& stream) { _stream = stream; }
310 _override_pattern_formatter_options = options;
315 QUILL_NODISCARD ColourMode colour_mode()
const noexcept {
return _colour_mode; }
316 QUILL_NODISCARD std::string
const& stream()
const noexcept {
return _stream; }
317 QUILL_NODISCARD std::optional<PatternFormatterOptions>
const& override_pattern_formatter_options()
const noexcept
319 return _override_pattern_formatter_options;
325 std::optional<PatternFormatterOptions> _override_pattern_formatter_options;
326 std::string _stream =
"stdout";
328 ColourMode _colour_mode{ColourMode::Automatic};
340 :
StreamSink{config.stream(),
nullptr, config.override_pattern_formatter_options()}, _config(config)
342 assert((_config.stream() ==
"stdout") || (config.stream() ==
"stderr"));
344 if (_config.colour_mode() == ConsoleSinkConfig::ColourMode::Never)
346 _config._colours.set_colours_enabled(
false);
350 _config._colours._configure_colour_support(_file, _config.colour_mode());
351 _config._colours.set_colours_enabled(
true);
373 std::string_view thread_id, std::string_view thread_name,
374 std::string
const& process_id, std::string_view logger_name,
375 LogLevel log_level, std::string_view log_level_description,
376 std::string_view log_level_short_code,
377 std::vector<std::pair<std::string, std::string>>
const* named_args,
378 std::string_view log_message, std::string_view log_statement)
override 380 if (_config.colours().colours_enabled())
383 std::string_view
const colour_code = _config.colours().log_level_colour(log_level);
384 safe_fwrite(colour_code.data(),
sizeof(char), colour_code.size(), _file);
388 logger_name, log_level, log_level_description, log_level_short_code,
389 named_args, log_message, log_statement);
392 safe_fwrite(ConsoleSinkConfig::Colours::reset.data(),
sizeof(
char),
393 ConsoleSinkConfig::Colours::reset.size(), _file);
399 logger_name, log_level, log_level_description, log_level_short_code,
400 named_args, log_message, log_statement);
QUILL_ATTRIBUTE_HOT void write_log(MacroMetadata const *, uint64_t, std::string_view, std::string_view, std::string const &, std::string_view, LogLevel, std::string_view, std::string_view, std::vector< std::pair< std::string, std::string >> const *, std::string_view, std::string_view log_statement) override
Writes a formatted log message to the stream.
Definition: StreamSink.h:133
QUILL_ATTRIBUTE_COLD void set_stream(std::string const &stream)
Sets the output stream for console logging.
Definition: ConsoleSink.h:297
Definition: ConsoleSink.h:332
QUILL_ATTRIBUTE_COLD void set_colour_mode(ColourMode colour_mode)
Sets the colour mode for console output.
Definition: ConsoleSink.h:283
void assign_colour_to_log_level(LogLevel log_level, std::string_view colour) noexcept
Sets a custom colour per log level.
Definition: ConsoleSink.h:87
QUILL_ATTRIBUTE_COLD void set_override_pattern_formatter_options(std::optional< PatternFormatterOptions > const &options)
Sets custom pattern formatter options for this sink.
Definition: ConsoleSink.h:308
Definition: ConsoleSink.h:45
void apply_default_colours() noexcept
Sets some default colours for terminal.
Definition: ConsoleSink.h:68
QUILL_NODISCARD bool colours_enabled() const noexcept
Definition: ConsoleSink.h:99
ConsoleSink(ConsoleSinkConfig const &config=ConsoleSinkConfig{})
Constructor with custom ConsoleColours config.
Definition: ConsoleSink.h:339
QUILL_NODISCARD std::string_view log_level_colour(LogLevel log_level) const noexcept
The colour for the given log level.
Definition: ConsoleSink.h:109
QUILL_ATTRIBUTE_HOT void write_log(MacroMetadata const *log_metadata, uint64_t log_timestamp, std::string_view thread_id, std::string_view thread_name, std::string const &process_id, std::string_view logger_name, LogLevel log_level, std::string_view log_level_description, std::string_view log_level_short_code, std::vector< std::pair< std::string, std::string >> const *named_args, std::string_view log_message, std::string_view log_statement) override
Write a formatted log message to the stream.
Definition: ConsoleSink.h:372
Represents console colours.
Definition: ConsoleSink.h:58
QUILL_ATTRIBUTE_COLD void set_colours(Colours colours)
Sets custom colours for each log level.
Definition: ConsoleSink.h:267
StreamSink class for handling log messages.
Definition: StreamSink.h:48
QUILL_NODISCARD Colours const & colours() noexcept
Getters.
Definition: ConsoleSink.h:314