RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
ConsoleSink.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <array>
6#include <atomic>
7#include <iostream>
8#include <string>
9#include <string_view>
10
11namespace RST::Log {
12
16 class ConsoleSink : public ASink {
17 public:
18 explicit ConsoleSink(std::ostream& target = std::cout);
19
21 void setColor(LogLevel level, std::string_view colorCode);
22 void setColorEnabled(bool enabled) noexcept { _colorEnabled.store(enabled, std::memory_order_relaxed); }
23 [[nodiscard]] bool isColorEnabled() const noexcept { return _colorEnabled.load(std::memory_order_relaxed); }
24
25 protected:
26 void log(const LogMessage& message) override;
27 void flushSink() override;
28
29 private:
30 std::ostream& _target;
31 std::array<std::string, kLogLevelCount> _colors;
32 std::atomic<bool> _colorEnabled;
33 };
34
35}
Base class for sinks: handles the level, the pattern and the locking.
Definition ASink.hpp:17
Writes colored lines to std::cout or another stream.
Definition ConsoleSink.hpp:16
void setColorEnabled(bool enabled) noexcept
Definition ConsoleSink.hpp:22
void log(const LogMessage &message) override
Writes one message; called with the sink mutex locked.
void flushSink() override
Flushes the output; called with the sink mutex locked.
bool isColorEnabled() const noexcept
Definition ConsoleSink.hpp:23
ConsoleSink(std::ostream &target=std::cout)
void setColor(LogLevel level, std::string_view colorCode)
Sets the ANSI color of level; see Terminal.
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
LogLevel
Severity of a message, from Trace to Fatal; Off disables logging.
Definition LogLevel.hpp:11
A log record as passed to the sinks.
Definition LogMessage.hpp:18