HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <cstdarg>
17#include <cstring>
18#include <memory>
19#include <atomic>
20
21// Forward declarations
22class BaseLogger;
23
27enum class LogStyle : uint8_t {
28 NORMAL = 0,
29 BOLD = 1,
30 ITALIC = 2,
31 UNDERLINE = 3,
32 STRIKETHROUGH = 4,
34};
35
39enum class LogColor : uint8_t {
40 DEFAULT = 0,
41 BLACK = 30,
42 RED = 31,
43 GREEN = 32,
44 YELLOW = 33,
45 BLUE = 34,
46 MAGENTA = 35,
47 CYAN = 36,
48 WHITE = 37,
49 BRIGHT_BLACK = 90,
50 BRIGHT_RED = 91,
51 BRIGHT_GREEN = 92,
52 BRIGHT_YELLOW = 93,
53 BRIGHT_BLUE = 94,
54 BRIGHT_MAGENTA = 95,
55 BRIGHT_CYAN = 96,
56 BRIGHT_WHITE = 97
57};
58
62enum class LogBackground : uint8_t {
63 DEFAULT = 0,
64 BLACK = 40,
65 RED = 41,
66 GREEN = 42,
67 YELLOW = 43,
68 BLUE = 44,
69 MAGENTA = 45,
70 CYAN = 46,
71 WHITE = 47,
72 BRIGHT_BLACK = 100,
73 BRIGHT_RED = 101,
74 BRIGHT_GREEN = 102,
75 BRIGHT_YELLOW = 103,
76 BRIGHT_BLUE = 104,
77 BRIGHT_MAGENTA = 105,
78 BRIGHT_CYAN = 106,
79 BRIGHT_WHITE = 107
80};
81
85enum class LogLevel : uint8_t {
86 ERROR = 0,
87 WARN = 1,
88 INFO = 2,
89 DEBUG = 3,
90 VERBOSE = 4
91};
92
111
125
158class Logger {
159public:
164 static Logger& GetInstance() noexcept;
165
171 bool Initialize(const LogConfig& config = LogConfig{}) noexcept;
172
176 void Deinitialize() noexcept;
177
182 bool IsInitialized() const noexcept;
183
189 void SetLogLevel(const char* tag, LogLevel level) noexcept;
190
196 LogLevel GetLogLevel(const char* tag) const noexcept;
197
198 //==============================================================================
199 // BASIC LOGGING METHODS
200 //==============================================================================
201
208 void Error(const char* tag, const char* format, ...) noexcept;
209
216 void Warn(const char* tag, const char* format, ...) noexcept;
217
224 void Info(const char* tag, const char* format, ...) noexcept;
225
232 void Debug(const char* tag, const char* format, ...) noexcept;
233
240 void Verbose(const char* tag, const char* format, ...) noexcept;
241
242 //==============================================================================
243 // FORMATTED LOGGING METHODS
244 //==============================================================================
245
254 void Error(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
255
264 void Warn(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
265
274 void Info(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
275
284 void Debug(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
285
294 void Verbose(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
295
296 //==============================================================================
297 // ASCII ART LOGGING METHODS
298 //==============================================================================
299
306 void LogAsciiArt(const char* tag, const char* ascii_art,
307 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
308
316 void LogAsciiArt(LogLevel level, const char* tag, const char* ascii_art,
317 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
318
325 void LogBanner(const char* tag, const char* ascii_art,
326 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
327
328 //==============================================================================
329 // UTILITY METHODS
330 //==============================================================================
331
336 void SetConfig(const LogConfig& config) noexcept;
337
342 LogConfig GetConfig() const noexcept;
343
348 void EnableColors(bool enable) noexcept;
349
354 void EnableEffects(bool enable) noexcept;
355
360 void EnableAsciiArt(bool enable) noexcept;
361
365 void Flush() noexcept;
366
367private:
368 //==============================================================================
369 // PRIVATE MEMBERS
370 //==============================================================================
371
372 std::atomic<bool> initialized_;
374
376 static constexpr size_t kMaxTagLevels = 16;
377 static constexpr size_t kMaxTagLength = 32;
384
385 std::unique_ptr<BaseLogger> base_logger_;
386
387 //==============================================================================
388 // PRIVATE METHODS
389 //==============================================================================
390
394 Logger() noexcept;
395
399 ~Logger() noexcept;
400
404 Logger(const Logger&) = delete;
405
409 Logger& operator=(const Logger&) = delete;
410
414 Logger(Logger&&) = delete;
415
419 Logger& operator=(Logger&&) = delete;
420
430 void LogInternal(LogLevel level, const char* tag, LogColor color, LogStyle style,
431 const char* format, va_list args) noexcept;
432
442 size_t WriteColorPrefix(char* buf, size_t buf_size, LogColor color,
443 LogBackground background, LogStyle style) const noexcept;
444
451 static size_t WriteResetSequence(char* buf, size_t buf_size) noexcept;
452
460 void FormatAndLogAsciiArt(const char* tag, LogLevel level,
461 const char* ascii_art,
462 const AsciiArtFormat& format) noexcept;
463
470 bool IsLevelEnabled(LogLevel level, const char* tag) const noexcept;
471
476 std::unique_ptr<BaseLogger> CreateBaseLogger() noexcept;
477
482 void DumpStatistics() const noexcept;
483};
484
485//==============================================================================
486// CONVENIENCE MACROS
487//==============================================================================
488
489#define LOG_ERROR(tag, ...) Logger::GetInstance().Error(tag, __VA_ARGS__)
490#define LOG_WARN(tag, ...) Logger::GetInstance().Warn(tag, __VA_ARGS__)
491#define LOG_INFO(tag, ...) Logger::GetInstance().Info(tag, __VA_ARGS__)
492#define LOG_DEBUG(tag, ...) Logger::GetInstance().Debug(tag, __VA_ARGS__)
493#define LOG_VERBOSE(tag, ...) Logger::GetInstance().Verbose(tag, __VA_ARGS__)
494
495#define LOG_ERROR_FORMATTED(tag, color, style, ...) Logger::GetInstance().Error(tag, color, style, __VA_ARGS__)
496#define LOG_WARN_FORMATTED(tag, color, style, ...) Logger::GetInstance().Warn(tag, color, style, __VA_ARGS__)
497#define LOG_INFO_FORMATTED(tag, color, style, ...) Logger::GetInstance().Info(tag, color, style, __VA_ARGS__)
498#define LOG_DEBUG_FORMATTED(tag, color, style, ...) Logger::GetInstance().Debug(tag, color, style, __VA_ARGS__)
499#define LOG_VERBOSE_FORMATTED(tag, color, style, ...) Logger::GetInstance().Verbose(tag, color, style, __VA_ARGS__)
500
501#define LOG_ASCII_ART(tag, art, format) Logger::GetInstance().LogAsciiArt(tag, art, format)
502#define LOG_BANNER(tag, art, format) Logger::GetInstance().LogBanner(tag, art, format)
LogLevel
Log levels.
Definition Logger.h:85
@ WARN
Warning level.
@ INFO
Info level.
@ ERROR
Error level.
@ DEBUG
Debug level.
@ VERBOSE
Verbose level.
LogBackground
Background colors (ANSI color codes)
Definition Logger.h:62
@ DEFAULT
Default background.
LogColor
Text colors (ANSI color codes)
Definition Logger.h:39
@ BRIGHT_RED
Bright red.
@ BLACK
Black.
@ BLUE
Blue.
@ BRIGHT_YELLOW
Bright yellow.
@ CYAN
Cyan.
@ BRIGHT_WHITE
Bright white.
@ DEFAULT
Default color.
@ BRIGHT_GREEN
Bright green.
@ YELLOW
Yellow.
@ GREEN
Green.
@ RED
Red.
@ BRIGHT_CYAN
Bright cyan.
@ BRIGHT_BLACK
Bright black.
@ WHITE
White.
@ MAGENTA
Magenta.
@ BRIGHT_MAGENTA
Bright magenta.
@ BRIGHT_BLUE
Bright blue.
LogStyle
Text formatting styles.
Definition Logger.h:27
@ UNDERLINE
Underlined text.
@ NORMAL
Normal text.
@ ITALIC
Italic text.
@ BOLD
Bold text.
@ DOUBLE_UNDERLINE
Double underlined text.
@ STRIKETHROUGH
Strikethrough text.
Advanced logging system with formatting capabilities.
Definition Logger.h:158
void SetLogLevel(const char *tag, LogLevel level) noexcept
Set log level for a tag.
Definition Logger.cpp:119
LogConfig config_
Definition Logger.h:373
void EnableColors(bool enable) noexcept
Enable/disable colors.
Definition Logger.cpp:352
static constexpr size_t kMaxTagLevels
Fixed-size tag-level storage (avoids std::map heap allocation)
Definition Logger.h:376
std::unique_ptr< BaseLogger > CreateBaseLogger() noexcept
Create base logger instance.
Definition Logger.cpp:586
void EnableEffects(bool enable) noexcept
Enable/disable effects.
Definition Logger.cpp:362
void LogBanner(const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
Log ASCII art banner.
Definition Logger.cpp:306
LogConfig GetConfig() const noexcept
Get current configuration.
Definition Logger.cpp:348
void Flush() noexcept
Flush any buffered output.
Definition Logger.cpp:376
static constexpr size_t kMaxTagLength
Definition Logger.h:377
void Info(const char *tag, const char *format,...) noexcept
Log info message.
Definition Logger.cpp:192
size_t WriteColorPrefix(char *buf, size_t buf_size, LogColor color, LogBackground background, LogStyle style) const noexcept
Write ANSI color/style prefix into a buffer.
Definition Logger.cpp:416
static Logger & GetInstance() noexcept
Get singleton instance.
Definition Logger.cpp:61
std::atomic< bool > initialized_
Definition Logger.h:372
void Deinitialize() noexcept
Deinitialize the logger.
Definition Logger.cpp:96
void LogInternal(LogLevel level, const char *tag, LogColor color, LogStyle style, const char *format, va_list args) noexcept
Internal logging method.
Definition Logger.cpp:386
std::unique_ptr< BaseLogger > base_logger_
Definition Logger.h:385
static size_t WriteResetSequence(char *buf, size_t buf_size) noexcept
Write ANSI reset sequence into a buffer.
Definition Logger.cpp:449
void FormatAndLogAsciiArt(const char *tag, LogLevel level, const char *ascii_art, const AsciiArtFormat &format) noexcept
Format and log ASCII art line-by-line without heap allocation.
Definition Logger.cpp:460
void LogAsciiArt(const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
Log ASCII art.
Definition Logger.cpp:288
TagLevel tag_levels_[kMaxTagLevels]
Definition Logger.h:383
void Warn(const char *tag, const char *format,...) noexcept
Log warning message.
Definition Logger.cpp:181
void EnableAsciiArt(bool enable) noexcept
Enable/disable ASCII art support.
Definition Logger.cpp:372
bool IsInitialized() const noexcept
Check if logger is initialized.
Definition Logger.cpp:111
void Debug(const char *tag, const char *format,...) noexcept
Log debug message.
Definition Logger.cpp:203
void SetConfig(const LogConfig &config) noexcept
Set default configuration.
Definition Logger.cpp:336
void Verbose(const char *tag, const char *format,...) noexcept
Log verbose message.
Definition Logger.cpp:214
bool Initialize(const LogConfig &config=LogConfig{}) noexcept
Initialize the logger.
Definition Logger.cpp:70
void Error(const char *tag, const char *format,...) noexcept
Log error message.
Definition Logger.cpp:170
void DumpStatistics() const noexcept
Dump comprehensive logger statistics to log as INFO level. Logs internal logger statistics,...
Definition Logger.cpp:595
LogLevel GetLogLevel(const char *tag) const noexcept
Get log level for a tag.
Definition Logger.cpp:152
Logger() noexcept
Constructor.
Definition Logger.cpp:31
bool IsLevelEnabled(LogLevel level, const char *tag) const noexcept
Check if log level is enabled for tag.
Definition Logger.cpp:577
ASCII art formatting options.
Definition Logger.h:115
bool add_border
Add border around ASCII art.
Definition Logger.h:120
uint32_t border_padding
Border padding.
Definition Logger.h:122
LogColor color
ASCII art color.
Definition Logger.h:116
LogBackground background
ASCII art background.
Definition Logger.h:117
uint32_t max_width
Maximum width for centering.
Definition Logger.h:123
LogStyle style
ASCII art style.
Definition Logger.h:118
char border_char
Border character.
Definition Logger.h:121
bool center_art
Center the ASCII art.
Definition Logger.h:119
Logging configuration.
Definition Logger.h:96
uint32_t max_width
Maximum output width.
Definition Logger.h:103
uint32_t border_padding
Border padding.
Definition Logger.h:107
LogLevel level
Default log level.
Definition Logger.h:97
bool enable_colors
Enable ANSI colors.
Definition Logger.h:101
LogBackground background
Default background.
Definition Logger.h:99
bool enable_ascii_art
Enable ASCII art support.
Definition Logger.h:108
LogColor color
Default text color.
Definition Logger.h:98
char border_char
Border character.
Definition Logger.h:106
bool format_ascii_art
Apply formatting to ASCII art.
Definition Logger.h:109
bool center_text
Center the text.
Definition Logger.h:104
bool add_border
Add border around text.
Definition Logger.h:105
bool enable_effects
Enable special effects.
Definition Logger.h:102
LogStyle style
Default text style.
Definition Logger.h:100
Definition Logger.h:378
char tag[kMaxTagLength]
Definition Logger.h:379
bool in_use
Definition Logger.h:381
LogLevel level
Definition Logger.h:380