HardFOC Core
API reference
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
181 bool Initialize(const LogConfig& config = LogConfig{}) noexcept;
182
197 bool Initialize(const LogConfig& config,
198 std::unique_ptr<BaseLogger> backend) noexcept;
199
203 void Deinitialize() noexcept;
204
209 bool IsInitialized() const noexcept;
210
216 void SetLogLevel(const char* tag, LogLevel level) noexcept;
217
223 LogLevel GetLogLevel(const char* tag) const noexcept;
224
225 //==============================================================================
226 // BASIC LOGGING METHODS
227 //==============================================================================
228
241 void SetTagLevel(const char* tag, LogLevel level) noexcept;
242
249 void Error(const char* tag, const char* format, ...) noexcept;
250
257 void Warn(const char* tag, const char* format, ...) noexcept;
258
265 void Info(const char* tag, const char* format, ...) noexcept;
266
273 void Debug(const char* tag, const char* format, ...) noexcept;
274
281 void Verbose(const char* tag, const char* format, ...) noexcept;
282
283 //==============================================================================
284 // FORMATTED LOGGING METHODS
285 //==============================================================================
286
295 void Error(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
296
305 void Warn(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
306
315 void Info(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
316
325 void Debug(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
326
335 void Verbose(const char* tag, LogColor color, LogStyle style, const char* format, ...) noexcept;
336
337 //==============================================================================
338 // ASCII ART LOGGING METHODS
339 //==============================================================================
340
347 void LogAsciiArt(const char* tag, const char* ascii_art,
348 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
349
357 void LogAsciiArt(LogLevel level, const char* tag, const char* ascii_art,
358 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
359
366 void LogBanner(const char* tag, const char* ascii_art,
367 const AsciiArtFormat& format = AsciiArtFormat{}) noexcept;
368
369 //==============================================================================
370 // UTILITY METHODS
371 //==============================================================================
372
377 void SetConfig(const LogConfig& config) noexcept;
378
383 // A copy on purpose: other tasks may change the configuration while the
384 // caller uses the result.
385 // cppcheck-suppress returnByReference
386 LogConfig GetConfig() const noexcept;
387
392 void EnableColors(bool enable) noexcept;
393
398 void EnableEffects(bool enable) noexcept;
399
404 void EnableAsciiArt(bool enable) noexcept;
405
409 void Flush() noexcept;
410
411private:
412 //==============================================================================
413 // PRIVATE MEMBERS
414 //==============================================================================
415
416 std::atomic<bool> initialized_;
418
420 static constexpr size_t kMaxTagLevels = 16;
421 static constexpr size_t kMaxTagLength = 32;
428
429 std::unique_ptr<BaseLogger> base_logger_;
430
431 //==============================================================================
432 // PRIVATE METHODS
433 //==============================================================================
434
438 Logger() noexcept;
439
443 ~Logger() noexcept;
444
448 Logger(const Logger&) = delete;
449
453 Logger& operator=(const Logger&) = delete;
454
458 Logger(Logger&&) = delete;
459
463 Logger& operator=(Logger&&) = delete;
464
474 void LogInternal(LogLevel level, const char* tag, LogColor color, LogStyle style,
475 const char* format, va_list args) noexcept;
476
486 size_t WriteColorPrefix(char* buf, size_t buf_size, LogColor color,
487 LogBackground background, LogStyle style) const noexcept;
488
495 static size_t WriteResetSequence(char* buf, size_t buf_size) noexcept;
496
504 void FormatAndLogAsciiArt(const char* tag, LogLevel level,
505 const char* ascii_art,
506 const AsciiArtFormat& format) noexcept;
507
514 bool IsLevelEnabled(LogLevel level, const char* tag) const noexcept;
515
526 static std::unique_ptr<BaseLogger> CreateDefaultBaseLogger() noexcept;
527
532 void DumpStatistics() const noexcept;
533};
534
535//==============================================================================
536// CONVENIENCE MACROS
537//==============================================================================
538
539#define LOG_ERROR(tag, ...) Logger::GetInstance().Error(tag, __VA_ARGS__)
540#define LOG_WARN(tag, ...) Logger::GetInstance().Warn(tag, __VA_ARGS__)
541#define LOG_INFO(tag, ...) Logger::GetInstance().Info(tag, __VA_ARGS__)
542#define LOG_DEBUG(tag, ...) Logger::GetInstance().Debug(tag, __VA_ARGS__)
543#define LOG_VERBOSE(tag, ...) Logger::GetInstance().Verbose(tag, __VA_ARGS__)
544
545#define LOG_ERROR_FORMATTED(tag, color, style, ...) Logger::GetInstance().Error(tag, color, style, __VA_ARGS__)
546#define LOG_WARN_FORMATTED(tag, color, style, ...) Logger::GetInstance().Warn(tag, color, style, __VA_ARGS__)
547#define LOG_INFO_FORMATTED(tag, color, style, ...) Logger::GetInstance().Info(tag, color, style, __VA_ARGS__)
548#define LOG_DEBUG_FORMATTED(tag, color, style, ...) Logger::GetInstance().Debug(tag, color, style, __VA_ARGS__)
549#define LOG_VERBOSE_FORMATTED(tag, color, style, ...) Logger::GetInstance().Verbose(tag, color, style, __VA_ARGS__)
550
551#define LOG_ASCII_ART(tag, art, format) Logger::GetInstance().LogAsciiArt(tag, art, format)
552#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:160
LogConfig config_
Definition Logger.h:417
static std::unique_ptr< BaseLogger > CreateDefaultBaseLogger() noexcept
Resolve the platform-default BaseLogger.
Definition EspLoggerFactory.cpp:43
void EnableColors(bool enable) noexcept
Enable/disable colors.
Definition Logger.cpp:404
static constexpr size_t kMaxTagLevels
Fixed-size tag-level storage (avoids std::map heap allocation)
Definition Logger.h:420
void EnableEffects(bool enable) noexcept
Enable/disable effects.
Definition Logger.cpp:414
void LogBanner(const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
Log ASCII art banner.
Definition Logger.cpp:358
LogConfig GetConfig() const noexcept
Get current configuration.
Definition Logger.cpp:400
void Flush() noexcept
Flush any buffered output.
Definition Logger.cpp:428
static constexpr size_t kMaxTagLength
Definition Logger.h:421
void Info(const char *tag, const char *format,...) noexcept
Log info message.
Definition Logger.cpp:244
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:468
static Logger & GetInstance() noexcept
Get singleton instance.
Definition Logger.cpp:91
std::atomic< bool > initialized_
Definition Logger.h:416
void Deinitialize() noexcept
Deinitialize the logger.
Definition Logger.cpp:137
void LogInternal(LogLevel level, const char *tag, LogColor color, LogStyle style, const char *format, va_list args) noexcept
Internal logging method.
Definition Logger.cpp:438
std::unique_ptr< BaseLogger > base_logger_
Definition Logger.h:429
static size_t WriteResetSequence(char *buf, size_t buf_size) noexcept
Write ANSI reset sequence into a buffer.
Definition Logger.cpp:501
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:512
void LogAsciiArt(const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
Log ASCII art.
Definition Logger.cpp:340
TagLevel tag_levels_[kMaxTagLevels]
Definition Logger.h:427
void Warn(const char *tag, const char *format,...) noexcept
Log warning message.
Definition Logger.cpp:233
void EnableAsciiArt(bool enable) noexcept
Enable/disable ASCII art support.
Definition Logger.cpp:424
bool IsInitialized() const noexcept
Check if logger is initialized.
Definition Logger.cpp:152
void Debug(const char *tag, const char *format,...) noexcept
Log debug message.
Definition Logger.cpp:255
void SetConfig(const LogConfig &config) noexcept
Set default configuration.
Definition Logger.cpp:388
void Verbose(const char *tag, const char *format,...) noexcept
Log verbose message.
Definition Logger.cpp:266
bool Initialize(const LogConfig &config=LogConfig{}) noexcept
Initialize the logger using the platform-default BaseLogger backend (resolved via the MCU-specific lo...
Definition Logger.cpp:100
void Error(const char *tag, const char *format,...) noexcept
Log error message.
Definition Logger.cpp:222
void DumpStatistics() const noexcept
Dump comprehensive logger statistics to log as INFO level. Logs internal logger statistics,...
Definition Logger.cpp:646
LogLevel GetLogLevel(const char *tag) const noexcept
Get log level for a tag.
Definition Logger.cpp:193
Logger() noexcept
Constructor.
Definition Logger.cpp:61
bool IsLevelEnabled(LogLevel level, const char *tag) const noexcept
Check if log level is enabled for tag.
Definition Logger.cpp:629
void SetTagLevel(const char *tag, LogLevel level) noexcept
Set the minimum visible log level for messages emitted with a specific tag (e.g. throttle a noisy HAL...
Definition Logger.cpp:211
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:422
char tag[kMaxTagLength]
Definition Logger.h:423
bool in_use
Definition Logger.h:425
LogLevel level
Definition Logger.h:424