Logger

enum class huira::LogLevel

Severity levels for log messages.

Ordered from least to most severe. The logger can be configured to filter messages below a certain level.

Values:

enumerator Debug
enumerator Info
enumerator Warning
enumerator Error
struct LogEntry

A single log entry with timestamp, level, message, and thread ID.

LogEntry represents an immutable log record that can be stored in the logger’s circular buffer and formatted for output.

Public Functions

LogEntry() = default
inline LogEntry(std::chrono::system_clock::time_point ts, LogLevel lvl, std::string msg, std::thread::id tid)
std::string to_string() const

Convert log entry to a formatted string.

Formats the log entry as: HH:MM:SS.mmm [LEVEL] [Thread id] message

Returns:

std::string Formatted log entry string

Public Members

std::chrono::system_clock::time_point timestamp
LogLevel level
std::string message
std::thread::id thread_id
class Logger

Thread-safe singleton logger for application-wide logging.

Logger provides a centralized logging system with support for:

  • Circular buffer for efficient log storage

  • Configurable severity filtering

  • Custom output sinks

  • Automatic crash handling and log dumping

  • Per-level console output configuration

  • Cross-platform crash reporting (signals and SEH)

The logger is thread-safe and uses lock-free atomic operations for performance. Log entries are stored in a circular buffer and can be dumped to a file on demand or automatically during crashes.

Public Types

using CustomSink = std::function<void(const LogEntry&)>

Public Functions

Logger(const Logger&) = delete
Logger &operator=(const Logger&) = delete
Logger(Logger&&) = delete
Logger &operator=(Logger&&) = delete
void set_level(LogLevel level)

Set the minimum log level.

Messages below this level will be filtered out and not logged.

Parameters:

level – Minimum severity level to log

LogLevel get_level() const

Get the current minimum log level.

Returns:

LogLevel Current minimum log level

void set_buffer_size(size_t size)

Resize the circular buffer.

Attempts to preserve existing log entries when resizing. If the new size is smaller, only the most recent entries are kept.

Parameters:

size – New buffer size (minimum 1)

size_t get_buffer_size() const

Get the current buffer size.

Returns:

size_t Number of log entries that can be stored

void set_custom_sink(CustomSink sink)

Set a custom output sink.

The custom sink is called for each logged message. Exceptions from the sink are caught and ignored to prevent logging from crashing the application.

Parameters:

sink – Callback function to receive log entries

void clear_custom_sink()

Clear the custom output sink.

void log(LogLevel level, const std::string &message)

Log a message at the specified level.

Adds the message to the circular buffer if it meets the minimum log level. Also calls the custom sink if one is configured.

Parameters:
  • level – Severity level of the message

  • message – Message to log

std::string dump_to_file(const std::string &filepath = "")

Dump all buffered log entries to a file.

Writes the log buffer to a file with header information including platform and compiler details. If no filepath is specified, uses a platform-appropriate default location. Falls back to current directory if the specified path fails.

Parameters:

filepath – Path to output file (empty for platform-specific default)

Returns:

std::string Actual path where log was written, or empty on failure

void enable_crash_handler(bool enable = true)

Enable or disable the crash handler.

When enabled, installs signal handlers and exception filters for automatic log dumping on crashes.

Parameters:

enable – True to enable crash handling

void enable_console_debug(bool enable = true)

Enable or disable debug-level console output.

Enforces log level hierarchy: enabling debug also enables info and warning.

Parameters:

enable – True to enable debug console output

void enable_console_info(bool enable = true)

Enable or disable info-level console output.

Enforces log level hierarchy: enabling info also enables warning; disabling info also disables debug.

Parameters:

enable – True to enable info console output

void enable_console_warning(bool enable = true)

Enable or disable warning-level console output.

Enforces log level hierarchy: disabling warning also disables info and debug.

Parameters:

enable – True to enable warning console output

bool is_console_debug_enabled() const

Check if debug console output is enabled.

Returns:

bool True if debug console output is enabled

bool is_console_info_enabled() const

Check if info console output is enabled.

Returns:

bool True if info console output is enabled

bool is_console_warning_enabled() const

Check if warning console output is enabled.

Returns:

bool True if warning console output is enabled

Public Static Functions

static inline Logger &instance()
inline void huira::set_log_level(LogLevel level)

Set the minimum log level for the global logger.

Parameters:

level – Minimum severity level to log

inline void huira::set_log_buffer_size(size_t size)

Set the circular buffer size for the global logger.

Parameters:

size – Number of log entries to retain in memory

inline void huira::set_log_sink(Logger::CustomSink sink)

Set a custom output sink for the global logger.

Parameters:

sink – Callback function to receive log entries

inline std::string huira::dump_log(const std::string &filepath = "")

Dump all buffered log entries to a file.

Parameters:

filepath – Path to output file (empty for platform-specific default)

Returns:

std::string Actual path where log was written, or empty on failure

inline void huira::enable_console_debug(bool enable = true)

Enable or disable debug-level console output.

Parameters:

enable – True to enable debug console output

inline void huira::enable_console_info(bool enable = true)

Enable or disable info-level console output.

Parameters:

enable – True to enable info console output

inline void huira::enable_console_warning(bool enable = true)

Enable or disable warning-level console output.

Parameters:

enable – True to enable warning console output

HUIRA_LOG_DEBUG(msg)

Log a debug-level message.

Evaluates the message only if debug logging is enabled. Outputs to console if console debug output is enabled.

Parameters:
  • msg – Message to log (can be a string or expression that converts to string)

HUIRA_LOG_INFO(msg)

Log an info-level message.

Evaluates the message only if info logging is enabled. Outputs to console if console info output is enabled.

Parameters:
  • msg – Message to log (can be a string or expression that converts to string)

HUIRA_LOG_WARNING(msg)

Log a warning-level message.

Evaluates the message only if warning logging is enabled. Outputs to console (in yellow) if console warning output is enabled.

Parameters:
  • msg – Message to log (can be a string or expression that converts to string)

HUIRA_LOG_ERROR(msg)

Log an error-level message.

Always outputs to console in red. Error messages are never filtered.

Parameters:
  • msg – Message to log (can be a string or expression that converts to string)

HUIRA_LOG(level, ...)

Log a variadic message with specified log level.

Accepts multiple arguments that are concatenated using the << operator.

Parameters:
  • level – LogLevel for this message

  • ... – Variadic arguments to concatenate into the log message

HUIRA_THROW_ERROR(msg)

Log an error message and throw a runtime_error with the same message.

This macro ensures the error is logged before throwing the exception, and outputs to console in red.

Parameters:
  • msg – Error message to log and throw