HardFOC Core
API reference
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Logger Class Reference

Advanced logging system with formatting capabilities. More...

#include <Logger.h>

Classes

struct  TagLevel
 

Public Member Functions

bool Initialize (const LogConfig &config=LogConfig{}) noexcept
 Initialize the logger using the platform-default BaseLogger backend (resolved via the MCU-specific logger factory).
 
bool Initialize (const LogConfig &config, std::unique_ptr< BaseLogger > backend) noexcept
 Initialize the logger with a caller-supplied BaseLogger.
 
void Deinitialize () noexcept
 Deinitialize the logger.
 
bool IsInitialized () const noexcept
 Check if logger is initialized.
 
void SetLogLevel (const char *tag, LogLevel level) noexcept
 Set log level for a tag.
 
LogLevel GetLogLevel (const char *tag) const noexcept
 Get log level for a tag.
 
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 handler during a boot self-test).
 
void Error (const char *tag, const char *format,...) noexcept
 Log error message.
 
void Warn (const char *tag, const char *format,...) noexcept
 Log warning message.
 
void Info (const char *tag, const char *format,...) noexcept
 Log info message.
 
void Debug (const char *tag, const char *format,...) noexcept
 Log debug message.
 
void Verbose (const char *tag, const char *format,...) noexcept
 Log verbose message.
 
void Error (const char *tag, LogColor color, LogStyle style, const char *format,...) noexcept
 Log error message with formatting.
 
void Warn (const char *tag, LogColor color, LogStyle style, const char *format,...) noexcept
 Log warning message with formatting.
 
void Info (const char *tag, LogColor color, LogStyle style, const char *format,...) noexcept
 Log info message with formatting.
 
void Debug (const char *tag, LogColor color, LogStyle style, const char *format,...) noexcept
 Log debug message with formatting.
 
void Verbose (const char *tag, LogColor color, LogStyle style, const char *format,...) noexcept
 Log verbose message with formatting.
 
void LogAsciiArt (const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
 Log ASCII art.
 
void LogAsciiArt (LogLevel level, const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
 Log ASCII art with log level.
 
void LogBanner (const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
 Log ASCII art banner.
 
void SetConfig (const LogConfig &config) noexcept
 Set default configuration.
 
LogConfig GetConfig () const noexcept
 Get current configuration.
 
void EnableColors (bool enable) noexcept
 Enable/disable colors.
 
void EnableEffects (bool enable) noexcept
 Enable/disable effects.
 
void EnableAsciiArt (bool enable) noexcept
 Enable/disable ASCII art support.
 
void Flush () noexcept
 Flush any buffered output.
 

Static Public Member Functions

static Logger & GetInstance () noexcept
 Get singleton instance.
 

Private Member Functions

 Logger () noexcept
 Constructor.
 
 ~Logger () noexcept
 Destructor.
 
 Logger (const Logger &)=delete
 Copy constructor (deleted)
 
Logger & operator= (const Logger &)=delete
 Assignment operator (deleted)
 
 Logger (Logger &&)=delete
 Move constructor (deleted)
 
Logger & operator= (Logger &&)=delete
 Move assignment operator (deleted)
 
void LogInternal (LogLevel level, const char *tag, LogColor color, LogStyle style, const char *format, va_list args) noexcept
 Internal logging method.
 
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.
 
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.
 
bool IsLevelEnabled (LogLevel level, const char *tag) const noexcept
 Check if log level is enabled for tag.
 
void DumpStatistics () const noexcept
 Dump comprehensive logger statistics to log as INFO level. Logs internal logger statistics, configuration, and performance metrics.
 

Static Private Member Functions

static size_t WriteResetSequence (char *buf, size_t buf_size) noexcept
 Write ANSI reset sequence into a buffer.
 
static std::unique_ptr< BaseLogger > CreateDefaultBaseLogger () noexcept
 Resolve the platform-default BaseLogger.
 

Private Attributes

std::atomic< bool > initialized_
 
LogConfig config_
 
TagLevel tag_levels_ [kMaxTagLevels] = {}
 
std::unique_ptr< BaseLogger > base_logger_
 

Static Private Attributes

static constexpr size_t kMaxTagLevels = 16
 Fixed-size tag-level storage (avoids std::map heap allocation)
 
static constexpr size_t kMaxTagLength = 32
 

Detailed Description

Advanced logging system with formatting capabilities.

This class provides a comprehensive logging solution that supports various formatting options including colors, styles, and effects. It can also handle ASCII art formatting and display.

Key Features:

Usage Example:

// Basic logging
logger.Info("TAG", "Hello World");
// With formatting
logger.Info("TAG", "Hello World", LogColor::RED, LogStyle::BOLD);
// ASCII art
const char* ascii_art = "..."; // ASCII art string
logger.LogAsciiArt("BANNER", ascii_art, AsciiArtFormat{});
@ RED
Red.
@ BOLD
Bold text.
Advanced logging system with formatting capabilities.
Definition Logger.h:158
void Info(const char *tag, const char *format,...) noexcept
Log info message.
Definition Logger.cpp:244
static Logger & GetInstance() noexcept
Get singleton instance.
Definition Logger.cpp:91
void LogAsciiArt(const char *tag, const char *ascii_art, const AsciiArtFormat &format=AsciiArtFormat{}) noexcept
Log ASCII art.
Definition Logger.cpp:340
ASCII art formatting options.
Definition Logger.h:115

Constructor & Destructor Documentation

◆ Logger() [1/3]

Logger::Logger ( )
privatenoexcept

Constructor.

◆ ~Logger()

Logger::~Logger ( )
privatenoexcept

Destructor.

◆ Logger() [2/3]

Logger::Logger ( const Logger &  )
privatedelete

Copy constructor (deleted)

◆ Logger() [3/3]

Logger::Logger ( Logger &&  )
privatedelete

Move constructor (deleted)

Member Function Documentation

◆ CreateDefaultBaseLogger()

std::unique_ptr< BaseLogger > Logger::CreateDefaultBaseLogger ( )
staticprivatenoexcept

Resolve the platform-default BaseLogger.

Implemented in a per-MCU factory translation unit (logger/factory/EspLoggerFactory.cpp on ESP32). Defined as a free function so Logger.cpp itself never #includes EspLogger.h / esp_log.h — keeping the handler MCU-agnostic.

Returns
Owned backend, or nullptr if no factory was linked.

◆ Debug() [1/2]

void Logger::Debug ( const char *  tag,
const char *  format,
  ... 
)
noexcept

Log debug message.

Parameters
tagLog tag
formatFormat string
...Variable arguments

◆ Debug() [2/2]

void Logger::Debug ( const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
  ... 
)
noexcept

Log debug message with formatting.

Parameters
tagLog tag
colorText color
styleText style
formatFormat string
...Variable arguments

◆ Deinitialize()

void Logger::Deinitialize ( )
noexcept

Deinitialize the logger.

◆ DumpStatistics()

void Logger::DumpStatistics ( ) const
privatenoexcept

Dump comprehensive logger statistics to log as INFO level. Logs internal logger statistics, configuration, and performance metrics.

◆ EnableAsciiArt()

void Logger::EnableAsciiArt ( bool  enable)
noexcept

Enable/disable ASCII art support.

Parameters
enabletrue to enable, false to disable

◆ EnableColors()

void Logger::EnableColors ( bool  enable)
noexcept

Enable/disable colors.

Parameters
enabletrue to enable, false to disable

◆ EnableEffects()

void Logger::EnableEffects ( bool  enable)
noexcept

Enable/disable effects.

Parameters
enabletrue to enable, false to disable

◆ Error() [1/2]

void Logger::Error ( const char *  tag,
const char *  format,
  ... 
)
noexcept

Log error message.

Parameters
tagLog tag
formatFormat string
...Variable arguments

◆ Error() [2/2]

void Logger::Error ( const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
  ... 
)
noexcept

Log error message with formatting.

Parameters
tagLog tag
colorText color
styleText style
formatFormat string
...Variable arguments

◆ Flush()

void Logger::Flush ( )
noexcept

Flush any buffered output.

◆ FormatAndLogAsciiArt()

void Logger::FormatAndLogAsciiArt ( const char *  tag,
LogLevel  level,
const char *  ascii_art,
const AsciiArtFormat &  format 
)
privatenoexcept

Format and log ASCII art line-by-line without heap allocation.

Parameters
tagLog tag
levelLog level for output
ascii_artASCII art C string
formatFormatting options

◆ GetConfig()

LogConfig Logger::GetConfig ( ) const
noexcept

Get current configuration.

Returns
Current configuration

◆ GetInstance()

Logger & Logger::GetInstance ( )
staticnoexcept

Get singleton instance.

Returns
Logger instance

◆ GetLogLevel()

LogLevel Logger::GetLogLevel ( const char *  tag) const
noexcept

Get log level for a tag.

Parameters
tagLog tag
Returns
Log level

◆ Info() [1/2]

void Logger::Info ( const char *  tag,
const char *  format,
  ... 
)
noexcept

Log info message.

Parameters
tagLog tag
formatFormat string
...Variable arguments

◆ Info() [2/2]

void Logger::Info ( const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
  ... 
)
noexcept

Log info message with formatting.

Parameters
tagLog tag
colorText color
styleText style
formatFormat string
...Variable arguments

◆ Initialize() [1/2]

bool Logger::Initialize ( const LogConfig &  config,
std::unique_ptr< BaseLogger >  backend 
)
noexcept

Initialize the logger with a caller-supplied BaseLogger.

This is the layering-clean entry point: the HAL (or a unit test) picks the backend and hands it to the logger. Logger takes ownership; Initialize calls BaseLogger::Initialize(...) on it with the matching hf_logger_config_t.

Parameters
configConfiguration options.
backendConcrete backend (e.g. std::make_unique<EspLogger>() from the HAL, or a test double). Must be non-null.
Returns
true on success; false if backend is null or its Initialize reports failure.

◆ Initialize() [2/2]

bool Logger::Initialize ( const LogConfig &  config = LogConfig{})
noexcept

Initialize the logger using the platform-default BaseLogger backend (resolved via the MCU-specific logger factory).

On ESP32 the default factory hands back an EspLogger; on other platforms it returns whatever was linked into logger/factory/<mcu>LoggerFactory.cpp. If you need to drive the logger output through a different transport (e.g. the built-in USB Serial/JTAG controller via EspUsbSerialJtag), use the injection overload below instead — Logger.cpp itself is MCU-clean and never names a concrete BaseLogger subclass.

Parameters
configConfiguration options.
Returns
true on success.

◆ IsInitialized()

bool Logger::IsInitialized ( ) const
noexcept

Check if logger is initialized.

Returns
true if initialized, false otherwise

◆ IsLevelEnabled()

bool Logger::IsLevelEnabled ( LogLevel  level,
const char *  tag 
) const
privatenoexcept

Check if log level is enabled for tag.

Parameters
levelLog level
tagLog tag
Returns
true if enabled, false otherwise

◆ LogAsciiArt() [1/2]

void Logger::LogAsciiArt ( const char *  tag,
const char *  ascii_art,
const AsciiArtFormat &  format = AsciiArtFormat{} 
)
noexcept

Log ASCII art.

Parameters
tagLog tag
ascii_artASCII art string
formatFormatting options

◆ LogAsciiArt() [2/2]

void Logger::LogAsciiArt ( LogLevel  level,
const char *  tag,
const char *  ascii_art,
const AsciiArtFormat &  format = AsciiArtFormat{} 
)
noexcept

Log ASCII art with log level.

Parameters
levelLog level
tagLog tag
ascii_artASCII art string
formatFormatting options

◆ LogBanner()

void Logger::LogBanner ( const char *  tag,
const char *  ascii_art,
const AsciiArtFormat &  format = AsciiArtFormat{} 
)
noexcept

Log ASCII art banner.

Parameters
tagLog tag
ascii_artASCII art string
formatFormatting options

◆ LogInternal()

void Logger::LogInternal ( LogLevel  level,
const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
va_list  args 
)
privatenoexcept

Internal logging method.

Parameters
levelLog level
tagLog tag
colorText color
styleText style
formatFormat string
argsVariable arguments

◆ operator=() [1/2]

Logger & Logger::operator= ( const Logger &  )
privatedelete

Assignment operator (deleted)

◆ operator=() [2/2]

Logger & Logger::operator= ( Logger &&  )
privatedelete

Move assignment operator (deleted)

◆ SetConfig()

void Logger::SetConfig ( const LogConfig &  config)
noexcept

Set default configuration.

Parameters
configConfiguration

◆ SetLogLevel()

void Logger::SetLogLevel ( const char *  tag,
LogLevel  level 
)
noexcept

Set log level for a tag.

Parameters
tagLog tag
levelLog level

◆ SetTagLevel()

void Logger::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 handler during a boot self-test).

MCU-agnostic wrapper: on ESP32 this maps to esp_log_level_set; on platforms without per-tag filtering it is a no-op (messages still pass through, the global level governs).

Parameters
tagTag string passed to Info / Warn / Error etc.
levelMinimum severity that should still print for tag.

◆ Verbose() [1/2]

void Logger::Verbose ( const char *  tag,
const char *  format,
  ... 
)
noexcept

Log verbose message.

Parameters
tagLog tag
formatFormat string
...Variable arguments

◆ Verbose() [2/2]

void Logger::Verbose ( const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
  ... 
)
noexcept

Log verbose message with formatting.

Parameters
tagLog tag
colorText color
styleText style
formatFormat string
...Variable arguments

◆ Warn() [1/2]

void Logger::Warn ( const char *  tag,
const char *  format,
  ... 
)
noexcept

Log warning message.

Parameters
tagLog tag
formatFormat string
...Variable arguments

◆ Warn() [2/2]

void Logger::Warn ( const char *  tag,
LogColor  color,
LogStyle  style,
const char *  format,
  ... 
)
noexcept

Log warning message with formatting.

Parameters
tagLog tag
colorText color
styleText style
formatFormat string
...Variable arguments

◆ WriteColorPrefix()

size_t Logger::WriteColorPrefix ( char *  buf,
size_t  buf_size,
LogColor  color,
LogBackground  background,
LogStyle  style 
) const
privatenoexcept

Write ANSI color/style prefix into a buffer.

Parameters
bufOutput buffer
buf_sizeBuffer size
colorText color
backgroundBackground color
styleText style
Returns
Number of bytes written (excluding null terminator)

◆ WriteResetSequence()

size_t Logger::WriteResetSequence ( char *  buf,
size_t  buf_size 
)
staticprivatenoexcept

Write ANSI reset sequence into a buffer.

Parameters
bufOutput buffer
buf_sizeBuffer size
Returns
Number of bytes written (excluding null terminator)

Member Data Documentation

◆ base_logger_

std::unique_ptr<BaseLogger> Logger::base_logger_
private

◆ config_

LogConfig Logger::config_
private

◆ initialized_

std::atomic<bool> Logger::initialized_
private

◆ kMaxTagLength

constexpr size_t Logger::kMaxTagLength = 32
staticconstexprprivate

◆ kMaxTagLevels

constexpr size_t Logger::kMaxTagLevels = 16
staticconstexprprivate

Fixed-size tag-level storage (avoids std::map heap allocation)

◆ tag_levels_

TagLevel Logger::tag_levels_[kMaxTagLevels] = {}
private

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