Max OS 0.3
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
11#ifndef MAXOS_COMMON_LOGGER_H
12#define MAXOS_COMMON_LOGGER_H
13
14#include <libcommon/outputStream.h>
15#include <libcommon/colour.h>
16#include <libcommon/spinlock.h>
17
18namespace MaxOS {
19
20
25 enum class LogLevel {
26 OUT,
27 ERROR,
28 WARNING,
29 HEADER,
30 INFO,
31 TEST,
32 DEBUG,
33 };
34
35
36 constexpr uint8_t MAX_LOG_WRITERS = 5;
37#ifdef NDEBUG
38 constexpr LogLevel MAX_LOG_LEVEL = LogLevel::INFO;
39#else
40 constexpr LogLevel MAX_LOG_LEVEL = LogLevel::DEBUG;
41#endif
42
43
44
45 class LogRecord;
46
51 class Logger : public MaxOS::common::OutputStream {
52
53 friend class LogRecord;
54
55 private:
56
57 // Cant use vector as this needs to be init before the heap
58 uint8_t m_log_writer_count = 0;
59 OutputStream* m_log_writers[MAX_LOG_WRITERS] = { nullptr, nullptr, nullptr, nullptr, nullptr };
60 bool m_log_writers_enabled[MAX_LOG_WRITERS] = { false, false, false, false, false };
61
62 // Progress bar
63 static inline uint8_t s_progress_total = 100;
64 uint8_t m_progress_current = 0;
65
66 static inline Logger* s_active_logger = nullptr;
67 inline static common::Spinlock s_loggers_lock = {};
68
69 LogLevel m_log_level = LogLevel::INFO;
70
71 public:
72 Logger();
73 ~Logger();
74
75 void add_log_writer(OutputStream* log_writer);
76 void disable_log_writer(OutputStream* log_writer);
77 void enable_log_writer(OutputStream* log_writer);
79
80 void set_log_level(LogLevel log_level);
81
82 void write_char(char c) final;
83 void printf(const char* format, ...);
84
85 static void assert(bool condition, const char* message, ...);
86
87 static Logger* active_logger();
88
89 static LogRecord Out();
90
91 static LogRecord ERROR();
92 static LogRecord WARNING();
93 static LogRecord HEADER();
94 static LogRecord INFO();
95 static LogRecord TEST();
96 static LogRecord DEBUG();
97
98 using OutputStream::operator <<;
99 Logger& operator <<(LogLevel log_level);
100 };
101
106 class LogRecord {
107
108 private:
109 Logger& m_logger;
110
111 public:
112 explicit LogRecord(Logger& logger, LogLevel level);
113 ~LogRecord();
114
122 template<typename T> LogRecord& operator << (const T& value) {
123 m_logger.write(value);
124 return *this;
125 }
126
134 m_logger.write_char(c);
135 return *this;
136 }
137 };
138}
139
140#endif // MAXOS_COMMON_LOGGER_H
A single logging event.
Definition logger.h:106
LogRecord & operator<<(const T &value)
Definition logger.h:122
A class that handles logging messages to the console and files.
Definition logger.h:51
static LogRecord TEST()
Gets the active logger for testing logs.
Definition logger.cpp:186
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:267
static LogRecord ERROR()
Gets the active logger for error logs.
Definition logger.cpp:222
void enable_log_writer(OutputStream *log_writer)
Lets an output stream from be used.
Definition logger.cpp:83
void printf(const char *format,...)
Prints a formatted string to the logger (thread safe)
Definition logger.cpp:250
static Logger * active_logger()
Gets the active logger (to be used to modify the logger)
Definition logger.cpp:240
static LogRecord DEBUG()
Gets the active logger for debug logs.
Definition logger.cpp:204
void disable_log_writer(OutputStream *log_writer)
Prevents an output stream from being used.
Definition logger.cpp:66
static LogRecord HEADER()
Gets the active logger for headers.
Definition logger.cpp:231
void add_log_writer(OutputStream *log_writer)
Adds an output stream to the logger.
Definition logger.cpp:44
static LogRecord INFO()
Gets the active logger for info logs.
Definition logger.cpp:195
static LogRecord WARNING()
Gets the active logger for warnings.
Definition logger.cpp:213
void write_char(char c) final
Writes a character to the logger.
Definition logger.cpp:158
static LogRecord Out()
Gets the active logger.
Definition logger.cpp:176
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:110
void enable_all_log_writers()
Mark all log writes as allowed to output to.
Definition logger.cpp:99
Logger & operator<<(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:286
LogLevel
Priority levels for logging messages. Different levels may be used to filter messages based on their ...
Definition logger.h:25
constexpr LogLevel MAX_LOG_LEVEL
The maximum log level for this build (messages above this level will not be logged)
Definition logger.h:40
constexpr uint8_t MAX_LOG_WRITERS
The maximum number of log writers that can be added to the logger.
Definition logger.h:36