Max OS 0.3
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1
9#include <common/logger.h>
10#include <stdarg.h>
12#include <common/version.h>
13#include <system/cpu.h>
14#include <processes/scheduler.h>
15
16using namespace MaxOS;
17using namespace MaxOS::common;
18using namespace MaxOS::drivers::console;
19using namespace MaxOS::processes;
20using namespace MaxOS::system;
21
26: m_log_writers()
27{
28
29 s_active_logger = this;
30
31 // The following line is generated automatically by the MaxOS build system.
32 s_progress_total = 22;
33
34}
35
36
37Logger::~Logger() = default;
38
45
46 // If the list is not empty
47 if (m_log_writer_count >= MAX_LOG_WRITERS)
48 return;
49
50 // Add the output stream to the list
51 m_log_writers[m_log_writer_count] = log_writer;
52 m_log_writers_enabled[m_log_writer_count] = true;
53 m_log_writer_count++;
54
55 // Print the setup info
56 *this << LogLevel::INFO << "Logger setup: " << m_log_writer_count << "\n";
57 *this << LogLevel::HEADER << "MaxOS v" << VERSION_STRING << " [ build " << BUILD_NUMBER << " ]\n";
58
59}
60
67
68 // If the list is empty
69 if (m_log_writer_count == 0)
70 return;
71
72 // Find the output stream in the list
73 for (int i = 0; i < m_log_writer_count; i++)
74 if (m_log_writers[i] == log_writer)
75 m_log_writers_enabled[i] = false;
76}
77
84
85 // Set the log level
86 m_log_level = log_level;
87
88 // Update the progress bar
89 if (log_level == LogLevel::INFO) {
90 VESABootConsole::update_progress_bar((m_progress_current * 100) / s_progress_total);
91 m_progress_current++;
92 }
93
94 // Print the header
95 switch (log_level) {
96
97 case LogLevel::HEADER:
98 *this << ANSI_COLOURS[ANSIColour::FG_Blue] << "[ BOOT ] ";
99 break;
100
101 case LogLevel::INFO:
102 *this << ANSI_COLOURS[ANSIColour::FG_Cyan] << "[ INFO ]" << ANSI_COLOURS[ANSIColour::FG_White] << " ";
103 break;
104
105 case LogLevel::TEST:
106 *this << ANSI_COLOURS[ANSIColour::FG_Green] << "[ TEST ]" << ANSI_COLOURS[ANSIColour::FG_White] << " ";
107 break;
108
109 case LogLevel::DEBUG:
110 *this << ANSI_COLOURS[ANSIColour::FG_Yellow] << "[ DEBUG ]" << ANSI_COLOURS[ANSIColour::Reset] << " ";
111 break;
112
113 case LogLevel::WARNING:
114 *this << ANSI_COLOURS[ANSIColour::BG_Yellow] << ANSI_COLOURS[FG_White] << "[ WARNING ]" << ANSI_COLOURS[ANSIColour::Reset] << " ";
115 break;
116
117 case LogLevel::ERROR:
118 *this << ANSI_COLOURS[ANSIColour::BG_Red] << "[ ERROR ]" << ANSI_COLOURS[ANSIColour::Reset] << " ";
119 break;
120 }
121
123}
124
125
131void Logger::write_char(char c) {
132
133 // Ensure logging at this level is enabled
134 if (m_log_level > MAX_LOG_LEVEL)
135 return;
136
137 // Write the character to all output streams
138 for (int i = 0; i < m_log_writer_count; i++)
139 if (m_log_writers_enabled[i])
140 m_log_writers[i]->write_char(c);
141
142}
143
150
151 return *active_logger();
152}
153
160
161 s_active_logger->set_log_level(LogLevel::HEADER);
162 return Out();
163
164}
165
172
173 s_active_logger->set_log_level(LogLevel::INFO);
174 return Out();
175}
176
183
184 s_active_logger->set_log_level(LogLevel::TEST);
185 return Out();
186}
187
194
195 s_active_logger->set_log_level(LogLevel::DEBUG);
196 return Out();
197
198}
199
206
207 s_active_logger->set_log_level(LogLevel::WARNING);
208 return Out();
209}
210
217
218 s_active_logger->set_log_level(LogLevel::ERROR);
219 return Out();
220
221}
222
229 return s_active_logger;
230}
231
238void Logger::printf(char const *format, ...) {
239
240 // Create a pointer to the data
242 va_start(parameters, format);
243
245}
246
253void Logger::ASSERT(bool condition, char const *message, ...) {
254
255 // If the condition is met then everything is ok
256 if (condition)
257 return;
258
261
262 // Hang the system
264}
265
273
275 return *this;
276
277}
A class that handles logging messages to the console and files.
Definition logger.h:44
static Logger TEST()
Gets active logger set to test level.
Definition logger.cpp:182
static Logger DEBUG()
Gets active logger set to DEBUG level.
Definition logger.cpp:193
static Logger & Out()
Gets the active logger.
Definition logger.cpp:149
void printf(const char *format,...)
Prints a formatted string to the logger.
Definition logger.cpp:238
static Logger * active_logger()
Gets the active logger (to be used to modify the logger)
Definition logger.cpp:228
void disable_log_writer(OutputStream *log_writer)
Removes an output stream from the logger.
Definition logger.cpp:66
static Logger WARNING()
Gets active logger set to WARNING level.
Definition logger.cpp:205
void add_log_writer(OutputStream *log_writer)
Adds an output stream to the logger.
Definition logger.cpp:44
static Logger INFO()
Gets active logger set to info level.
Definition logger.cpp:171
void write_char(char c) final
Writes a character to the logger.
Definition logger.cpp:131
static Logger ERROR()
Gets active logger set to ERROR level.
Definition logger.cpp:216
Logger()
Constructs a Logger object and sets it as the active logger.
Definition logger.cpp:25
void set_log_level(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:83
static Logger HEADER()
Gets active logger set to task level.
Definition logger.cpp:159
static void ASSERT(bool condition, const char *message,...)
Puts the system into a panic state if the condition is false, printing the message first.
Definition logger.cpp:253
Logger & operator<<(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:272
static String formatted(char const *format,...)
Creates a formated string. s = string, x = hex, d = decimal.
Definition string.cpp:624
A stream that strings can be written to.
void write(string string_to_write) override
Writes a string to the output stream.
virtual void write_char(char char_to_write)
Writes a character to the output stream.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static void update_progress_bar(uint8_t percentage)
Updates the progress bar.
Definition vesaboot.cpp:362
static void print_running_header()
Print the header for the running processes in the form ({name}:t{tid}c{core})
static void PANIC(const char *message, cpu_status_t *status=nullptr)
Puts the CPU into a panic state and halts it. Dumps the stack trace and registers.
Definition cpu.cpp:439
const char *const ANSI_COLOURS[]
ANSI Colour Codes.
Definition colour.h:19
Defines Central Processing Unit (CPU) structures and functions for managing CPU state and features.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
LogLevel
Priority levels for logging messages. Different levels may be used to filter messages based on their ...
Definition logger.h:23
constexpr LogLevel MAX_LOG_LEVEL
The maximum log level for this build (messages above this level will not be logged)
Definition logger.h:37
constexpr uint8_t MAX_LOG_WRITERS
The maximum number of log writers that can be added to the logger.
Definition logger.h:33
Defines a GlobalScheduler and Scheduler for managing processes and threads.
Defines the VESABootConsole driver for handling console output during boot using an VESA framebuffer.