MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ConsoleSink.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Sink.hpp"
4
5#include "Ansi.hpp"
6
7namespace MayaFlux::Journal {
8
9class MAYAFLUX_API ConsoleSink : public Sink {
10private:
12
13public:
15 {
16 m_colors_enabled = AnsiColors::initialize_console_colors();
17 }
18
19 void write(const JournalEntry& entry) override
20 {
21 std::lock_guard lock(m_mutex);
22 write_to_stream(std::cout, entry);
23 }
24
25 void write(const RealtimeEntry& entry) override
26 {
27 std::lock_guard lock(m_mutex);
28 write_to_stream(std::cout, entry);
29 }
30
31 void flush() override
32 {
33 std::lock_guard lock(m_mutex);
34 std::cout.flush();
35 }
36
37 [[nodiscard]] bool is_available() const override
38 {
39 return std::cout.good();
40 }
41
42private:
43 template <typename Entry>
44
45 void write_to_stream(std::ostream& os, const Entry& entry)
46 {
47 if (m_colors_enabled) {
48 switch (entry.severity) {
49 case Severity::TRACE:
50 os << AnsiColors::Cyan;
51 break;
52 case Severity::DEBUG:
53 os << AnsiColors::Blue;
54 break;
55 case Severity::INFO:
56 os << AnsiColors::Green;
57 break;
58 case Severity::WARN:
59 os << AnsiColors::Yellow;
60 break;
61 case Severity::ERROR:
62 os << AnsiColors::BrightRed;
63 break;
64 case Severity::FATAL:
65 os << AnsiColors::BgRed << AnsiColors::White;
66 break;
67 case Severity::NONE:
68 os << AnsiColors::Reset;
69 break;
70 default:
71 os << AnsiColors::Reset;
72 break;
73 }
74 }
75
76 os << "[" << Utils::enum_to_string(entry.severity) << "]" << AnsiColors::Reset;
77
78 os << AnsiColors::Magenta << "[" << Utils::enum_to_string(entry.component) << "]" << AnsiColors::Reset;
79
80 os << AnsiColors::Cyan << "[" << Utils::enum_to_string(entry.context) << "]" << AnsiColors::Reset << " ";
81
82 if constexpr (std::is_same_v<Entry, JournalEntry>) {
83 os << entry.message;
84 if (entry.location.file_name() != nullptr) {
85 os << AnsiColors::BrightBlue << " (" << entry.location.file_name()
86 << ":" << entry.location.line() << ")" << AnsiColors::Reset;
87 }
88 } else {
89 os << entry.message;
90 if (entry.file_name != nullptr) {
91 os << AnsiColors::BrightBlue << " (" << entry.file_name
92 << ":" << entry.line << ")" << AnsiColors::Reset;
93 }
94 }
95
96 if (m_colors_enabled) {
97 os << AnsiColors::Reset;
98 }
99
100 os << std::endl;
101 }
102
103 std::mutex m_mutex;
104
105 void feed_severity(std::ostream& os, Severity severity) const
106 {
107 switch (severity) {
108 case Severity::TRACE:
109 os << AnsiColors::Cyan;
110 break;
111 case Severity::DEBUG:
112 os << AnsiColors::Blue;
113 break;
114 case Severity::INFO:
115 os << AnsiColors::Green;
116 break;
117 case Severity::WARN:
118 os << AnsiColors::Yellow;
119 break;
120 case Severity::ERROR:
121 os << AnsiColors::BrightRed;
122 break;
123 case Severity::FATAL:
124 os << AnsiColors::BgRed << AnsiColors::White;
125 break;
126 case Severity::NONE:
127 os << AnsiColors::Reset;
128 break;
129 default:
130 os << AnsiColors::Reset;
131 break;
132 }
133 }
134};
135
136} // namespace MayaFlux::Journal
void write(const RealtimeEntry &entry) override
Write a realtime entry to this sink.
void write_to_stream(std::ostream &os, const Entry &entry)
bool is_available() const override
Check if sink is available/healthy.
void feed_severity(std::ostream &os, Severity severity) const
void flush() override
Flush any buffered writes.
void write(const JournalEntry &entry) override
Write a journal entry to this sink.
A log entry structure to encapsulate log message details.
Lightweight entry for lock-free ring buffer.