Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::Logger Class Reference

A class that handles logging messages to the console and files. More...

#include <logger.h>

Inheritance diagram for MaxOS::Logger:

Public Member Functions

 Logger ()
 Constructs a Logger object and sets it as the active logger.
 
void add_log_writer (OutputStream *log_writer)
 Adds an output stream to the logger.
 
void disable_log_writer (OutputStream *log_writer)
 Prevents an output stream from being used.
 
void enable_log_writer (OutputStream *log_writer)
 Lets an output stream from be used.
 
void enable_all_log_writers ()
 Mark all log writes as allowed to output to.
 
void set_log_level (LogLevel log_level)
 Sets the log level of the logger.
 
void write_char (char c) final
 Writes a character to the logger.
 
void printf (const char *format,...)
 Prints a formatted string to the logger (thread safe)
 
Logger & operator<< (LogLevel log_level)
 Sets the log level of the logger.
 

Static Public Member Functions

static void assert (bool condition, const char *message,...)
 Puts the system into a panic state if the condition is false, printing the message first.
 
static Logger * active_logger ()
 Gets the active logger (to be used to modify the logger)
 
static LogRecord Out ()
 Gets the active logger.
 
static LogRecord ERROR ()
 Gets the active logger for error logs.
 
static LogRecord WARNING ()
 Gets the active logger for warnings.
 
static LogRecord HEADER ()
 Gets the active logger for headers.
 
static LogRecord INFO ()
 Gets the active logger for info logs.
 
static LogRecord TEST ()
 Gets the active logger for testing logs.
 
static LogRecord DEBUG ()
 Gets the active logger for debug logs.
 

Friends

class LogRecord
 

Detailed Description

A class that handles logging messages to the console and files.

Definition at line 51 of file logger.h.

Constructor & Destructor Documentation

◆ Logger()

Logger::Logger ( )

Constructs a Logger object and sets it as the active logger.

Definition at line 25 of file logger.cpp.

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 = 15;
33
34}

Member Function Documentation

◆ active_logger()

Logger * Logger::active_logger ( )
static

Gets the active logger (to be used to modify the logger)

Returns
The active logger

Definition at line 240 of file logger.cpp.

240 {
241 return s_active_logger;
242}

Referenced by MaxOS::console::FramebufferConsole::finish(), MaxOS::console::FramebufferConsole::FramebufferConsole(), MaxOS::system::CPU::prepare_for_panic(), and MaxOS::processes::GlobalScheduler::print_running_header().

◆ add_log_writer()

void Logger::add_log_writer ( OutputStream *  log_writer)

Adds an output stream to the logger.

Parameters
log_writerThe output stream to add

Definition at line 44 of file logger.cpp.

44 {
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}

References MaxOS::MAX_LOG_WRITERS.

Referenced by MaxOS::console::FramebufferConsole::FramebufferConsole(), and MaxOS::console::SerialConsole::SerialConsole().

◆ assert()

void Logger::assert ( bool  condition,
const char *  message,
  ... 
)
static

Puts the system into a panic state if the condition is false, printing the message first.

Parameters
conditionThe condition to check if is met
messageThe message to print if the condition fails
...The arguments to format into the string

Definition at line 267 of file logger.cpp.

267 {
268
269 // If the condition is met then everything is ok
270 if (condition)
271 return;
272
273 va_list parameters;
274 va_start(parameters, message);
275
276 // Hang the system
277 CPU::PANIC(String::formatted(message, parameters).c_str());
278}
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:463

References MaxOS::system::CPU::PANIC().

◆ DEBUG()

◆ disable_log_writer()

void Logger::disable_log_writer ( OutputStream *  log_writer)

Prevents an output stream from being used.

Parameters
log_writerThe output stream to stop using

Definition at line 66 of file logger.cpp.

66 {
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}

Referenced by MaxOS::console::FramebufferConsole::finish().

◆ enable_all_log_writers()

void Logger::enable_all_log_writers ( )

Mark all log writes as allowed to output to.

Definition at line 99 of file logger.cpp.

99 {
100 for (int i = 0; i < m_log_writer_count; i++)
101 if (!m_log_writers_enabled[i])
102 m_log_writers_enabled[i] = true;
103}

◆ enable_log_writer()

void Logger::enable_log_writer ( OutputStream *  log_writer)

Lets an output stream from be used.

Parameters
log_writerThe output stream to use

Definition at line 83 of file logger.cpp.

83 {
84
85 // If the list is empty
86 if (m_log_writer_count == 0)
87 return;
88
89 // Find the output stream in the list
90 for (int i = 0; i < m_log_writer_count; i++)
91 if (m_log_writers[i] == log_writer)
92 m_log_writers_enabled[i] = true;
93
94}

◆ ERROR()

LogRecord Logger::ERROR ( )
static

Gets the active logger for error logs.

Returns
The error logger

Definition at line 222 of file logger.cpp.

222 {
223 return LogRecord(*s_active_logger, LogLevel::ERROR);
224}

Referenced by MaxOS::system::SyscallManager::handle_interrupt(), MaxOS::system::CPU::PANIC(), MaxOS::system::CPU::prepare_for_panic(), MaxOS::system::CPU::print_registers(), and MaxOS::system::CPU::stack_trace().

◆ HEADER()

LogRecord Logger::HEADER ( )
static

Gets the active logger for headers.

Returns
The header logger

Definition at line 231 of file logger.cpp.

231 {
232 return LogRecord(*s_active_logger, LogLevel::HEADER);
233}

Referenced by MaxOS::console::FramebufferConsole::finish(), and kernel_main().

◆ INFO()

◆ operator<<()

Logger & Logger::operator<< ( LogLevel  log_level)

Sets the log level of the logger.

Parameters
log_levelThe log level to set
Returns
This logger

Definition at line 286 of file logger.cpp.

286 {
287
288 set_log_level(log_level);
289 return *this;
290
291}
void set_log_level(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:110

References set_log_level().

◆ Out()

LogRecord Logger::Out ( )
static

Gets the active logger.

Returns
The active logger

Definition at line 176 of file logger.cpp.

176 {
177
178 return LogRecord(*s_active_logger, LogLevel::OUT);
179}

Referenced by MaxOS::system::CPU::PANIC(), MaxOS::system::CPU::prepare_for_panic(), and MaxOS::system::SyscallManager::syscall_klog().

◆ printf()

void Logger::printf ( const char *  format,
  ... 
)

Prints a formatted string to the logger (thread safe)

Parameters
formatThe format string
...The arguments to format

Definition at line 250 of file logger.cpp.

250 {
251
252
253 // Create a pointer to the data
254 va_list parameters;
255 va_start(parameters, format);
256
257 write(String::formatted(format, parameters));
258}

Referenced by MaxOS::processes::GlobalScheduler::print_running_header().

◆ set_log_level()

void Logger::set_log_level ( LogLevel  log_level)

Sets the log level of the logger.

Parameters
log_levelThe log level to set

Definition at line 110 of file logger.cpp.

110 {
111
112 // Set the log level
113 m_log_level = log_level;
114
115 // Update the progress bar
116 if (log_level == LogLevel::INFO) {
117 FramebufferConsole::update_progress_bar((m_progress_current * 100) / s_progress_total);
118 m_progress_current++;
119 }
120
121 // Print the header
122 switch (log_level) {
123
124 case LogLevel::HEADER:
125 *this << ANSI_COLOURS[FG_Blue] << "[ BOOT ] ";
126 break;
127
128 case LogLevel::INFO:
129 *this << ANSI_COLOURS[FG_Cyan] << "[ INFO ]" << ANSI_COLOURS[FG_White] << " ";
130 break;
131
132 case LogLevel::TEST:
133 *this << ANSI_COLOURS[FG_Green] << "[ TEST ]" << ANSI_COLOURS[FG_White] << " ";
134 break;
135
136 case LogLevel::DEBUG:
137 *this << ANSI_COLOURS[FG_Yellow] << "[ DEBUG ]" << ANSI_COLOURS[Reset] << " ";
138 break;
139
140 case LogLevel::WARNING:
141 *this << ANSI_COLOURS[BG_Yellow] << ANSI_COLOURS[FG_White] << "[ WARNING ]" << ANSI_COLOURS[Reset] << " ";
142 break;
143
144 case LogLevel::ERROR:
145 *this << ANSI_COLOURS[BG_Red] << "[ ERROR ]" << ANSI_COLOURS[Reset] << " ";
146 break;
147 }
148
150}
static void update_progress_bar(uint8_t percentage)
Updates the progress bar.
static void print_running_header()
Print the header for the running processes in the form ({name}:t{tid}c{core})

References MaxOS::processes::GlobalScheduler::print_running_header(), and MaxOS::console::FramebufferConsole::update_progress_bar().

Referenced by MaxOS::LogRecord::LogRecord(), and operator<<().

◆ TEST()

LogRecord Logger::TEST ( )
static

Gets the active logger for testing logs.

Returns
The testing logger

Definition at line 186 of file logger.cpp.

186 {
187 return LogRecord(*s_active_logger, LogLevel::TEST);
188}

◆ WARNING()

LogRecord Logger::WARNING ( )
static

Gets the active logger for warnings.

Returns
The warning logger

Definition at line 213 of file logger.cpp.

213 {
214 return LogRecord(*s_active_logger, LogLevel::WARNING);
215}

Referenced by MaxOS::hardwarecommunication::LocalAPIC::LocalAPIC(), and MaxOS::system::Core::wake_up().

◆ write_char()

void Logger::write_char ( char  c)
final

Writes a character to the logger.

Parameters
cThe character to write

Definition at line 158 of file logger.cpp.

158 {
159
160 // Ensure logging at this level is enabled
161 if (m_log_level > MAX_LOG_LEVEL)
162 return;
163
164 // Write the character to all output streams
165 for (int i = 0; i < m_log_writer_count; i++)
166 if (m_log_writers_enabled[i])
167 m_log_writers[i]->write_char(c);
168
169}

References MaxOS::MAX_LOG_LEVEL.

Referenced by MaxOS::LogRecord::operator<<().

Friends And Related Symbol Documentation

◆ LogRecord

friend class LogRecord
friend

Definition at line 53 of file logger.h.


The documentation for this class was generated from the following files: