9 #include "quill/core/Attributes.h" 10 #include "quill/core/LogLevel.h" 11 #include "quill/sinks/StreamSink.h" 18 #include <string_view> 23 #if !defined(WIN32_LEAN_AND_MEAN) 24 #define WIN32_LEAN_AND_MEAN 27 #if !defined(NOMINMAX) 60 Colours() { apply_default_colours(); }
69 assign_colour_to_log_level(LogLevel::TraceL3, white);
70 assign_colour_to_log_level(LogLevel::TraceL2, white);
71 assign_colour_to_log_level(LogLevel::TraceL1, white);
72 assign_colour_to_log_level(LogLevel::Debug, cyan);
73 assign_colour_to_log_level(LogLevel::Info, green);
74 assign_colour_to_log_level(LogLevel::Notice, white_bold);
75 assign_colour_to_log_level(LogLevel::Warning, yellow_bold);
76 assign_colour_to_log_level(LogLevel::Error, red_bold);
77 assign_colour_to_log_level(LogLevel::Critical, bold_on_red);
78 assign_colour_to_log_level(LogLevel::Backtrace, magenta);
88 auto const log_lvl =
static_cast<uint32_t
>(log_level);
89 _log_level_colours[log_lvl] = colour;
90 _colours_enabled =
true;
93 QUILL_ATTRIBUTE_COLD
void set_colours_enabled(
bool value) { _colours_enabled = value; }
100 return _colour_output_supported && _colours_enabled;
110 auto const log_lvl =
static_cast<uint32_t
>(log_level);
111 return _log_level_colours[log_lvl];
115 static constexpr std::string_view reset{
"\033[0m"};
116 static constexpr std::string_view bold{
"\033[1m"};
117 static constexpr std::string_view dark{
"\033[2m"};
118 static constexpr std::string_view underline{
"\033[4m"};
119 static constexpr std::string_view blink{
"\033[5m"};
120 static constexpr std::string_view reverse{
"\033[7m"};
121 static constexpr std::string_view concealed{
"\033[8m"};
122 static constexpr std::string_view clear_line{
"\033[K"};
125 static constexpr std::string_view black{
"\033[30m"};
126 static constexpr std::string_view red{
"\033[31m"};
127 static constexpr std::string_view green{
"\033[32m"};
128 static constexpr std::string_view yellow{
"\033[33m"};
129 static constexpr std::string_view blue{
"\033[34m"};
130 static constexpr std::string_view magenta{
"\033[35m"};
131 static constexpr std::string_view cyan{
"\033[36m"};
132 static constexpr std::string_view white{
"\033[37m"};
135 static constexpr std::string_view on_black{
"\033[40m"};
136 static constexpr std::string_view on_red{
"\033[41m"};
137 static constexpr std::string_view on_green{
"\033[42m"};
138 static constexpr std::string_view on_yellow{
"\033[43m"};
139 static constexpr std::string_view on_blue{
"\033[44m"};
140 static constexpr std::string_view on_magenta{
"\033[45m"};
141 static constexpr std::string_view on_cyan{
"\033[46m"};
142 static constexpr std::string_view on_white{
"\033[47m"};
145 static constexpr std::string_view white_bold{
"\033[97m\033[1m"};
146 static constexpr std::string_view yellow_bold{
"\033[33m\033[1m"};
147 static constexpr std::string_view red_bold{
"\033[31m\033[1m"};
148 static constexpr std::string_view bold_on_red{
"\033[1m\033[41m"};
154 QUILL_NODISCARD QUILL_ATTRIBUTE_COLD
static bool _supports_colour_output() noexcept
161 auto* env_p = std::getenv(
"TERM");
163 if (env_p ==
nullptr)
168 static constexpr
char const* terms[] = {
169 "ansi",
"color",
"console",
"cygwin",
"gnome",
170 "konsole",
"kterm",
"linux",
"msys",
"putty",
171 "rxvt",
"screen",
"vt100",
"xterm",
"tmux",
172 "terminator",
"alacritty",
"gnome-terminal",
"xfce4-terminal",
"lxterminal",
173 "mate-terminal",
"uxterm",
"eterm",
"tilix",
"rxvt-unicode",
177 for (
char const* term : terms)
179 if (std::strstr(env_p, term) !=
nullptr)
192 QUILL_NODISCARD QUILL_ATTRIBUTE_COLD
static bool _is_terminal_output(FILE* file) noexcept
195 return _isatty(_fileno(file)) != 0;
197 return ::isatty(fileno(file)) != 0;
203 QUILL_ATTRIBUTE_COLD
void _activate_ansi_support(FILE* file)
const 205 if (!_colour_output_supported)
211 auto const out_handle =
reinterpret_cast<HANDLE
>(_get_osfhandle(_fileno(file)));
212 if (out_handle == INVALID_HANDLE_VALUE)
218 if (!GetConsoleMode(out_handle, &dw_mode))
223 dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
224 dw_mode |= ENABLE_PROCESSED_OUTPUT;
226 SetConsoleMode(out_handle, dw_mode);
231 void _configure_colour_support(FILE* file, ColourMode colour_mode) noexcept
233 if (colour_mode == ColourMode::Always)
235 _colour_output_supported =
true;
237 else if (colour_mode == ColourMode::Automatic)
239 _colour_output_supported = _is_terminal_output(file) && _supports_colour_output();
243 _colour_output_supported =
false;
248 _activate_ansi_support(file);
253 std::array<std::string_view, 10> _log_level_colours;
254 bool _colours_enabled{
true};
255 bool _colour_output_supported{
false};
282 QUILL_ATTRIBUTE_COLD
void set_colour_mode(ColourMode colour_mode) { _colour_mode = colour_mode; }
296 QUILL_ATTRIBUTE_COLD
void set_stream(std::string
const& stream) { _stream = stream; }
309 _override_pattern_formatter_options = options;
314 QUILL_NODISCARD ColourMode colour_mode()
const noexcept {
return _colour_mode; }
315 QUILL_NODISCARD std::string
const& stream()
const noexcept {
return _stream; }
316 QUILL_NODISCARD std::optional<PatternFormatterOptions>
const& override_pattern_formatter_options()
const noexcept
318 return _override_pattern_formatter_options;
324 std::optional<PatternFormatterOptions> _override_pattern_formatter_options;
325 std::string _stream =
"stdout";
327 ColourMode _colour_mode{ColourMode::Automatic};
340 :
StreamSink{config.stream(),
nullptr, config.override_pattern_formatter_options(),
341 std::move(file_event_notifier)},
344 QUILL_ASSERT(_config.stream() ==
"stdout" || config.stream() ==
"stderr",
345 "Invalid stream name in ConsoleSink constructor, must be 'stdout' or 'stderr'");
347 if (_config.colour_mode() == ConsoleSinkConfig::ColourMode::Never)
349 _config._colours.set_colours_enabled(
false);
353 _config._colours._configure_colour_support(_file, _config.colour_mode());
354 _config._colours.set_colours_enabled(
true);
376 std::string_view thread_id, std::string_view thread_name,
377 std::string
const& process_id, std::string_view logger_name,
378 LogLevel log_level, std::string_view log_level_description,
379 std::string_view log_level_short_code,
380 std::vector<std::pair<std::string, std::string>>
const* named_args,
381 std::string_view log_message, std::string_view log_statement)
override 383 if (_config.colours().colours_enabled())
386 std::string_view
const colour_code = _config.colours().log_level_colour(log_level);
387 safe_fwrite(colour_code.data(),
sizeof(char), colour_code.size(), _file);
391 logger_name, log_level, log_level_description, log_level_short_code,
392 named_args, log_message, log_statement);
395 safe_fwrite(ConsoleSinkConfig::Colours::reset.data(),
sizeof(
char),
396 ConsoleSinkConfig::Colours::reset.size(), _file);
402 logger_name, log_level, log_level_description, log_level_short_code,
403 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:152
QUILL_ATTRIBUTE_COLD void set_stream(std::string const &stream)
Sets the output stream for console logging.
Definition: ConsoleSink.h:296
Definition: ConsoleSink.h:331
QUILL_ATTRIBUTE_COLD void set_colour_mode(ColourMode colour_mode)
Sets the colour mode for console output.
Definition: ConsoleSink.h:282
void assign_colour_to_log_level(LogLevel log_level, std::string_view colour) noexcept
Sets a custom colour per log level.
Definition: ConsoleSink.h:86
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:307
Definition: ConsoleSink.h:44
void apply_default_colours() noexcept
Sets some default colours for terminal.
Definition: ConsoleSink.h:67
QUILL_NODISCARD bool colours_enabled() const noexcept
Definition: ConsoleSink.h:98
QUILL_NODISCARD std::string_view log_level_colour(LogLevel log_level) const noexcept
The colour for the given log level.
Definition: ConsoleSink.h:108
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:375
Represents console colours.
Definition: ConsoleSink.h:57
QUILL_ATTRIBUTE_COLD void set_colours(Colours colours)
Sets custom colours for each log level.
Definition: ConsoleSink.h:266
Notifies on file events by calling the appropriate callback, the callback is executed on the backend ...
Definition: StreamSink.h:55
ConsoleSink(ConsoleSinkConfig const &config=ConsoleSinkConfig{}, FileEventNotifier file_event_notifier=FileEventNotifier{})
Constructor with custom ConsoleColours config.
Definition: ConsoleSink.h:338
StreamSink class for handling log messages.
Definition: StreamSink.h:67
QUILL_NODISCARD Colours const & colours() noexcept
Getters.
Definition: ConsoleSink.h:313