Skip to content

Commit

Permalink
add colour_mode to ConsoleSink
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Oct 23, 2024
1 parent 30c06f4 commit c2af97a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@
`RTTI` ([#604](https://github.com/odygrd/quill/issues/604))
- Fixed an incorrectly triggered assertion in debug builds when `BackendOptions::log_timestamp_ordering_grace_period` is
set to 0 ([#605](https://github.com/odygrd/quill/issues/605))
- Fixed a compile-time error in `CsvWriter` that occurred when passing a custom `FrontendOptions` type as a template parameter. ([#609](https://github.com/odygrd/quill/issues/609))
- Fixed a compile-time error in `CsvWriter` that occurred when passing a custom `FrontendOptions` type as a template
parameter. ([#609](https://github.com/odygrd/quill/issues/609))
- Added `ConsoleColours::ColourMode` to `ConsoleSink`, allowing colors to be explicitly forced or conditionally enabled
based on the environment. Previously, colors were only conditionally
enabled. ([#611](https://github.com/odygrd/quill/issues/611)).

For example:
```cpp
quill::Frontend::create_or_get_sink<quill::ConsoleSink>(
"sink_id_1", quill::ConsoleColours::ColourMode::Automatic);

## v7.3.0

Expand Down
4 changes: 3 additions & 1 deletion examples/console_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ int main()
quill::Backend::start(backend_options);

// Frontend
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1");
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>(
"sink_id_1", quill::ConsoleColours::ColourMode::Automatic);

quill::Logger* logger = quill::Frontend::create_or_get_logger("root", std::move(console_sink));

// Change the LogLevel to print everything
Expand Down
91 changes: 83 additions & 8 deletions include/quill/sinks/ConsoleSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ class ConsoleColours
using WORD = unsigned short;

public:
ConsoleColours() { _colours.fill(white); }
enum class ColourMode
{
Always,
Automatic,
Never
};

ConsoleColours()
{
// by default _using_colours is false
_colours.fill(white);
}

~ConsoleColours() = default;

/**
Expand Down Expand Up @@ -145,7 +157,21 @@ class ConsoleColours
return is_atty && is_console;
}

void _set_can_use_colours(FILE* file) noexcept { _can_use_colours = _is_in_terminal(file); }
void _set_can_use_colours(FILE* file, ColourMode colour_mode) noexcept
{
if (colour_mode == ColourMode::Always)
{
_can_use_colours = true;
}
else if (colour_mode == ColourMode::Automatic)
{
_can_use_colours = _is_in_terminal(file);
}
else
{
_can_use_colours = false;
}
}

private:
std::array<WORD, 10> _colours = {0}; /**< Colours per log level */
Expand All @@ -159,7 +185,18 @@ class ConsoleColours
class ConsoleColours
{
public:
ConsoleColours() { _colours.fill(white); }
enum class ColourMode
{
Always,
Automatic,
Never
};

ConsoleColours()
{
// by default _using_colours is false
_colours.fill(white);
}

~ConsoleColours() = default;

Expand Down Expand Up @@ -293,9 +330,21 @@ class ConsoleColours
return ::isatty(fileno(file)) != 0;
}

void _set_can_use_colours(FILE* file) noexcept
/***/
void _set_can_use_colours(FILE* file, ColourMode colour_mode) noexcept
{
_can_use_colours = _is_in_terminal(file) && _is_colour_terminal();
if (colour_mode == ColourMode::Always)
{
_can_use_colours = true;
}
else if (colour_mode == ColourMode::Automatic)
{
_can_use_colours = _is_in_terminal(file) && _is_colour_terminal();
}
else
{
_can_use_colours = false;
}
}

private:
Expand All @@ -313,14 +362,19 @@ class ConsoleSink : public StreamSink
* @brief Constructor
* @param console_colours console colours instance
* @param stream stream name can only be "stdout" or "stderr"
* @param colour_mode Determines when console colours are enabled.
* - Always: Colours are always enabled.
* - Automatic: Colours are enabled automatically based on the environment (e.g., terminal support).
* - Never: Colours are never enabled.
*/
explicit ConsoleSink(ConsoleColours const& console_colours, std::string const& stream = "stdout")
explicit ConsoleSink(ConsoleColours const& console_colours, std::string const& stream = "stdout",
ConsoleColours::ColourMode colour_mode = ConsoleColours::ColourMode::Automatic)
: StreamSink{stream, nullptr}, _console_colours(console_colours)
{
assert((stream == "stdout") || (stream == "stderr"));

// In this ctor we take a full copy of console_colours and in our instance we modify it
_console_colours._set_can_use_colours(_file);
_console_colours._set_can_use_colours(_file, colour_mode);
}

/**
Expand All @@ -335,7 +389,28 @@ class ConsoleSink : public StreamSink

if (enable_colours)
{
_console_colours._set_can_use_colours(_file);
_console_colours._set_can_use_colours(_file, ConsoleColours::ColourMode::Automatic);
_console_colours.set_default_colours();
}
}

/**
* @brief Constructor
* @param colour_mode Determines when console colours are enabled.
* - Always: Colours are always enabled.
* - Automatic: Colours are enabled automatically based on the environment (e.g., terminal support).
* - Never: Colours are never enabled.
* @param stream stream name can only be "stdout" or "stderr"
*/
explicit ConsoleSink(ConsoleColours::ColourMode colour_mode, std::string const& stream = "stdout")
: StreamSink{stream, nullptr}
{
assert((stream == "stdout") || (stream == "stderr"));

_console_colours._set_can_use_colours(_file, colour_mode);

if (colour_mode != ConsoleColours::ColourMode::Never)
{
_console_colours.set_default_colours();
}
}
Expand Down

0 comments on commit c2af97a

Please sign in to comment.