Clone
2
log/Home
Abdelrahman edited this page 2026-06-26 15:13:35 +01:00

log

The log package provides a lightweight, structured logging framework with configurable log levels, named loggers, and automatic timestamping.

#include "wapp_log.h"

Dependencies: common, base, os


📋 Log Levels

Six severity levels are available, ordered from most to least severe:

Level Description
WP_LOG_LEVEL_FATAL Fatal error — unrecoverable
WP_LOG_LEVEL_CRITICAL Critical condition
WP_LOG_LEVEL_ERROR Error condition
WP_LOG_LEVEL_WARNING Warning condition
WP_LOG_LEVEL_INFO Informational message
WP_LOG_LEVEL_DEBUG Debug message

Messages below the configured log level are silently discarded.


⚙️ Configuration

Global Log Level

void wpLogSetLevel(WpLogLevel level);

Sets the minimum log level. Messages below this threshold are ignored.

Output Streams

void wpLogConfigure(WpFile *outlog, WpFile *errlog, WpLogLevel level);

Configures output streams for informational and error output, and sets the log level in one call.

  • outlog — destination for debug, info, and warning messages (defaults to stdout)
  • errlog — destination for error, critical, and fatal messages (defaults to stderr)
  • level — minimum log level

🏷️ Logger

A named logger identifies the source of each log message.

typedef struct {
    WpStr8 name;
} WpLogger;

WpLogger wpLogMakeLogger(WpStr8 name);

The logger name appears in every log line, making it easy to filter messages by component.


📝 Logging Functions

Each severity level has a corresponding function:

void wpLogFatal(const WpLogger *logger, WpStr8 msg);
void wpLogCritical(const WpLogger *logger, WpStr8 msg);
void wpLogError(const WpLogger *logger, WpStr8 msg);
void wpLogWarning(const WpLogger *logger, WpStr8 msg);
void wpLogInfo(const WpLogger *logger, WpStr8 msg);
void wpLogDebug(const WpLogger *logger, WpStr8 msg);

All functions accept a WpLogger pointer and a WpStr8 message.


📄 Log Format

Each log line follows this format:

2024-01-15T10:30:00Z [debug    ] message text                           [logger_name]
  • ISO 8601 UTC timestamp
  • Log level in brackets (padded to 10 characters)
  • Message text (padded to a minimum width)
  • Logger name in brackets

Example

WpAllocator arena = wpMemArenaAllocatorInit(KiB(16));

WpLogger main_logger = wpLogMakeLogger(wpStr8Lit("main"));

wpLogInfo(&main_logger, wpStr8Lit("Application started"));
wpLogDebug(&main_logger, wpStr8Lit("Initialising subsystems..."));

// Output:
// 2024-01-15T10:30:00Z [info     ] Application started                  [main]
// 2024-01-15T10:30:00Z [debug    ] Initialising subsystems...           [main]

wpLogSetLevel(WP_LOG_LEVEL_ERROR);
wpLogDebug(&main_logger, wpStr8Lit("This will not be printed"));