Max OS 0.3
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_LOGGER_H
10#define MAXOS_COMMON_LOGGER_H
11
12#include <common/outputStream.h>
13#include <common/colour.h>
14#include <common/spinlock.h>
15
16namespace MaxOS {
17
18
23 enum class LogLevel {
24 ERROR,
25 WARNING,
26 HEADER,
27 INFO,
28 TEST,
29 DEBUG,
30 };
31
32
33 constexpr uint8_t MAX_LOG_WRITERS = 5;
34#ifdef NDEBUG
35 constexpr LogLevel MAX_LOG_LEVEL = LogLevel::INFO;
36#else
37 constexpr LogLevel MAX_LOG_LEVEL = LogLevel::DEBUG;
38#endif
39
45 private:
46
47 // Cant use vector as this needs to be init before the heap
48 uint8_t m_log_writer_count = 0;
49 OutputStream* m_log_writers[MAX_LOG_WRITERS] = { nullptr, nullptr, nullptr, nullptr, nullptr };
50 bool m_log_writers_enabled[MAX_LOG_WRITERS] = { false, false, false, false, false };
51
52 // Progress bar
53 static inline uint8_t s_progress_total = 100;
54 uint8_t m_progress_current = 0;
55
56 static inline Logger* s_active_logger = nullptr;
57
58 LogLevel m_log_level = LogLevel::INFO;
59
60 public:
61 Logger();
62 ~Logger();
63
64 void add_log_writer(OutputStream* log_writer);
65 void disable_log_writer(OutputStream* log_writer);
66
67 void set_log_level(LogLevel log_level);
68
69 void write_char(char c) final;
70 void printf(const char* format, ...);
71
72 static void ASSERT(bool condition, const char* message, ...);
73
74 static Logger* active_logger();
75
76 static Logger& Out();
77
78 static Logger ERROR();
79 static Logger WARNING();
80 static Logger HEADER();
81 static Logger INFO();
82 static Logger TEST();
83 static Logger DEBUG();
84
85 using OutputStream::operator <<;
86 Logger& operator <<(LogLevel log_level);
87 };
88
100#define ASSERT(condition, format, ...) Logger::ASSERT(condition, format, ##__VA_ARGS__)
101
102}
103
104#endif // MAXOS_COMMON_LOGGER_H
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
Logger & operator<<(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:272
A stream that strings can be written to.
OutputStream()
Constructs an OutputStream object that uses strings as the underlying data type.
Defines a Colour class for representing colours in RGBA format, along with ANSI and console colour en...
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
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100
constexpr uint8_t MAX_LOG_WRITERS
The maximum number of log writers that can be added to the logger.
Definition logger.h:33
Defines OutputStream and GenericOutputStream classes for writing data to streams.
Defines Spinlock and BlockingLock classes for thread synchronization.