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:
MaxOS::common::OutputStream MaxOS::common::GenericOutputStream< string > MaxOS::common::InputStreamProcessor< Type, ProcessorType > MaxOS::common::InputStreamEventHandler< Type > MaxOS::common::GenericInputStream< ProcessorType >

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)
 Removes an output stream from the logger.
 
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.
 
Loggeroperator<< (LogLevel log_level)
 Sets the log level of the logger.
 
- Public Member Functions inherited from MaxOS::common::OutputStream
 OutputStream ()
 Constructs an OutputStream object that uses strings as the underlying data type.
 
virtual void line_feed ()
 Writes a newline to the output stream.
 
virtual void carriage_return ()
 Writes a carriage return to the output stream.
 
virtual void clear ()
 Clears the output stream.
 
void write (string string_to_write) override
 Writes a string to the output stream.
 
void write (const char *string_to_write)
 Writes a string to the output stream.
 
virtual void write_int (int int_to_write)
 Writes an integer to the output stream.
 
virtual void write_hex (uint64_t hex_to_write)
 Writes a hex to the output stream.
 
OutputStreamoperator<< (string string_to_write) override
 Writes a string to the output stream.
 
OutputStreamoperator<< (const char *string_to_write)
 Writes a string to the output stream.
 
OutputStreamoperator<< (int int_to_write)
 Writes a integer to the output stream.
 
OutputStreamoperator<< (uint64_t hex_to_write)
 Writes a hexadecimal to the output stream.
 
OutputStreamoperator<< (char char_to_write)
 Writes a character to the output stream.
 
- Public Member Functions inherited from MaxOS::common::GenericOutputStream< string >
 GenericOutputStream ()
 __________________________________________Templates__________________________________________________///
 
 ~GenericOutputStream ()
 Destructor of the GenericOutputStream class.
 
void on_stream_read (string) override
 Writes the date that was read from the input stream to the output stream.
 
void on_end_of_stream (GenericInputStream< string > *) override
 Close the stream and remove it from the list of streams when the end of the stream is reached.
 
virtual void write (string)
 write an element to the stream.
 
virtual void close ()
 Close the stream.
 
virtual GenericOutputStream< string > & operator<< (string)
 Overload the << operator to write an element to the stream.
 
- Public Member Functions inherited from MaxOS::common::InputStreamProcessor< Type, ProcessorType >
 InputStreamProcessor ()
 Creates a new InputStreamProcessor.
 
 InputStreamProcessor (InputStreamEventHandler< ProcessorType > *generic_stream_event_handler)
 Creates a new InputStreamProcessor.
 
 ~InputStreamProcessor ()
 Destroys the InputStreamProcessor.
 
void on_end_of_stream (GenericInputStream< Type > *stream) override
 Called when a stream has finished. Passes the event on to the handlers and then removes the stream from the array of streams.
 
- Public Member Functions inherited from MaxOS::common::InputStreamEventHandler< Type >
 InputStreamEventHandler ()
 _______________________________________________TEMPLATES_________________________________________________________________///
 
 ~InputStreamEventHandler ()
 Destroys the InputStreamProcessor and disconnects it from all streams.
 
- Public Member Functions inherited from MaxOS::common::GenericInputStream< ProcessorType >
 GenericInputStream ()
 Creates a new GenericInputStream.
 
 GenericInputStream (InputStreamEventHandler< ProcessorType > *)
 Creates a new GenericInputStream and connects it to the handler.
 
 ~GenericInputStream ()
 Destroys the GenericInputStream and disconnects all handlers.
 
void connect_input_stream_event_handler (InputStreamEventHandler< ProcessorType > *)
 Adds a inputStreamEventHandler to the list of internetProtocolHandlers.
 
void disconnect_input_stream_event_handler (InputStreamEventHandler< ProcessorType > *)
 Removes a handler from the list of handlers.
 

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 Loggeractive_logger ()
 Gets the active logger (to be used to modify the logger)
 
static LoggerOut ()
 Gets the active logger.
 
static Logger ERROR ()
 Gets active logger set to ERROR level.
 
static Logger WARNING ()
 Gets active logger set to WARNING level.
 
static Logger HEADER ()
 Gets active logger set to task level.
 
static Logger INFO ()
 Gets active logger set to info level.
 
static Logger TEST ()
 Gets active logger set to test level.
 
static Logger DEBUG ()
 Gets active logger set to DEBUG level.
 

Additional Inherited Members

- Protected Attributes inherited from MaxOS::common::InputStreamEventHandler< Type >
common::Vector< GenericInputStream< Type > * > m_generic_input_streams
 List of streams being observed by this handler.
 
- Protected Attributes inherited from MaxOS::common::GenericInputStream< ProcessorType >
common::Vector< InputStreamEventHandler< ProcessorType > * > m_input_stream_event_handlers
 List of streams being observed by this handler.
 

Detailed Description

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

Definition at line 44 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 = 22;
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 228 of file logger.cpp.

228 {
229 return s_active_logger;
230}

Referenced by MaxOS::drivers::console::VESABootConsole::finish(), Out(), and MaxOS::drivers::console::VESABootConsole::VESABootConsole().

◆ 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}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22

References MaxOS::MAX_LOG_WRITERS.

Referenced by MaxOS::drivers::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

Definition at line 253 of file logger.cpp.

253 {
254
255 // If the condition is met then everything is ok
256 if (condition)
257 return;
258
261
262 // Hang the system
264}
static String formatted(char const *format,...)
Creates a formated string. s = string, x = hex, d = decimal.
Definition string.cpp:624
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

References MaxOS::String::formatted(), and MaxOS::system::CPU::PANIC().

◆ DEBUG()

Logger Logger::DEBUG ( )
static

Gets active logger set to DEBUG level.

Returns
The debug logger

Definition at line 193 of file logger.cpp.

193 {
194
195 s_active_logger->set_log_level(LogLevel::DEBUG);
196 return Out();
197
198}
static Logger & Out()
Gets the active logger.
Definition logger.cpp:149

References Out().

Referenced by MaxOS::processes::GlobalScheduler::add_process(), MaxOS::processes::GlobalScheduler::add_thread(), MaxOS::hardwarecommunication::AdvancedConfigurationAndPowerInterface::AdvancedConfigurationAndPowerInterface(), MaxOS::hardwarecommunication::AdvancedProgrammableInterruptController::AdvancedProgrammableInterruptController(), MaxOS::drivers::clock::Clock::calibrate(), MaxOS::system::CPU::check_nx(), MaxOS::system::Core::Core(), core_main(), MaxOS::system::GlobalDescriptorTable::GlobalDescriptorTable(), MaxOS::system::Core::init_sse(), MaxOS::system::Core::init_tss(), MaxOS::drivers::disk::IntegratedDriveElectronicsController::initialise(), MaxOS::hardwarecommunication::IOAPIC::IOAPIC(), MaxOS::system::GlobalDescriptorTable::load(), MaxOS::processes::GlobalScheduler::load_multiboot_elfs(), MaxOS::hardwarecommunication::LocalAPIC::LocalAPIC(), MaxOS::filesystem::partition::MSDOSPartition::mount_partitions(), MaxOS::system::Multiboot::Multiboot(), MaxOS::memory::PhysicalMemoryManager::PhysicalMemoryManager(), MaxOS::memory::PhysicalMemoryManager::reserve(), MaxOS::hardwarecommunication::PCIController::select_drivers(), MaxOS::drivers::video::VideoElectronicsStandardsAssociation::VideoElectronicsStandardsAssociation(), MaxOS::memory::VirtualMemoryManager::VirtualMemoryManager(), MaxOS::system::Core::wake_up(), and MaxOS::processes::Process::~Process().

◆ disable_log_writer()

void Logger::disable_log_writer ( OutputStream log_writer)

Removes an output stream from the logger.

Parameters
log_writerThe output stream to remove

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}

◆ ERROR()

Logger Logger::ERROR ( )
static

Gets active logger set to ERROR level.

Returns
The error logger

Definition at line 216 of file logger.cpp.

216 {
217
218 s_active_logger->set_log_level(LogLevel::ERROR);
219 return Out();
220
221}

References Out().

Referenced by MaxOS::filesystem::format::Fat32Volume::Fat32Volume(), MaxOS::system::SyscallManager::handle_interrupt(), MaxOS::system::CPU::PANIC(), MaxOS::system::CPU::prepare_for_panic(), MaxOS::system::CPU::print_registers(), MaxOS::tests::Test::run(), and MaxOS::system::CPU::stack_trace().

◆ HEADER()

Logger Logger::HEADER ( )
static

Gets active logger set to task level.

Returns
The task logger

Definition at line 159 of file logger.cpp.

159 {
160
161 s_active_logger->set_log_level(LogLevel::HEADER);
162 return Out();
163
164}

References Out().

Referenced by MaxOS::drivers::console::VESABootConsole::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 272 of file logger.cpp.

272 {
273
275 return *this;
276
277}
void set_log_level(LogLevel log_level)
Sets the log level of the logger.
Definition logger.cpp:83

References set_log_level().

◆ Out()

Logger & Logger::Out ( )
static

◆ printf()

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

Prints a formatted string to the logger.

Parameters
formatThe format string
...The arguments to format

Definition at line 238 of file logger.cpp.

238 {
239
240 // Create a pointer to the data
242 va_start(parameters, format);
243
245}
void write(string string_to_write) override
Writes a string to the output stream.

References MaxOS::String::formatted(), and MaxOS::common::OutputStream::write().

◆ 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 83 of file logger.cpp.

83 {
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}
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})
const char *const ANSI_COLOURS[]
ANSI Colour Codes.
Definition colour.h:19

References MaxOS::common::ANSI_COLOURS, MaxOS::processes::GlobalScheduler::print_running_header(), and MaxOS::drivers::console::VESABootConsole::update_progress_bar().

Referenced by operator<<().

◆ TEST()

Logger Logger::TEST ( )
static

Gets active logger set to test level.

Returns
The test logger

Definition at line 182 of file logger.cpp.

182 {
183
184 s_active_logger->set_log_level(LogLevel::TEST);
185 return Out();
186}

References Out().

Referenced by MaxOS::tests::Test::run(), and MaxOS::tests::TestRunner::run_all_tests().

◆ WARNING()

◆ write_char()

void Logger::write_char ( char  c)
finalvirtual

Writes a character to the logger.

Parameters
cThe character to write

Reimplemented from MaxOS::common::OutputStream.

Definition at line 131 of file logger.cpp.

131 {
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}
virtual void write_char(char char_to_write)
Writes a character to the output stream.

References MaxOS::MAX_LOG_LEVEL, and MaxOS::common::OutputStream::write_char().


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