/// Log module - Logging utilities
///
/// This module provides structured logging with different severity levels.
/// Output format: [LEVEL] message
/// Log level constants
pub const LEVEL_DEBUG = 0
pub const LEVEL_INFO = 1
pub const LEVEL_WARN = 2
pub const LEVEL_ERROR = 3
/// Current log level (default: INFO)
/// Messages below this level are not displayed.
let current_level = LEVEL_INFO
// ═══════════════════════════════════════════════════════════
// Logging Functions
// ═══════════════════════════════════════════════════════════
/// Log a debug message
/// Only shown when log level is DEBUG.
/// Example: debug("Processing item {id}")
pub fn debug(msg str) void {
if current_level <= LEVEL_DEBUG {
_log_debug(msg)
}
}
/// Log an info message
/// Example: info("User {name} logged in")
pub fn info(msg str) void {
if current_level <= LEVEL_INFO {
_log_info(msg)
}
}
/// Log a warning message
/// Example: warn("Rate limit approaching")
pub fn warn(msg str) void {
if current_level <= LEVEL_WARN {
_log_warn(msg)
}
}
/// Log an error message
/// Always shown unless logging is disabled.
/// Example: error("Failed to connect: {reason}")
pub fn error(msg str) void {
if current_level <= LEVEL_ERROR {
_log_error(msg)
}
}
// ═══════════════════════════════════════════════════════════
// Log Level Management
// ═══════════════════════════════════════════════════════════
/// Set the current log level
/// Valid values: "debug", "info", "warn", "error"
/// Example: set_level("debug")
pub fn set_level(level str) void {
if level == "debug" {
current_level = LEVEL_DEBUG
} else if level == "info" {
current_level = LEVEL_INFO
} else if level == "warn" {
current_level = LEVEL_WARN
} else if level == "error" {
current_level = LEVEL_ERROR
}
}
/// Get the current log level as a string
pub fn get_level() str {
if current_level == LEVEL_DEBUG {
"debug"
} else if current_level == LEVEL_INFO {
"info"
} else if current_level == LEVEL_WARN {
"warn"
} else {
"error"
}
}
// ═══════════════════════════════════════════════════════════
// Formatted Logging
// ═══════════════════════════════════════════════════════════
/// Log info with format
/// Example: infof("User %s logged in", name)
pub fn infof(format str, value str) void {
let msg = str.format(format, value)
info(msg)
}
/// Log error with format
pub fn errorf(format str, value str) void {
let msg = str.format(format, value)
error(msg)
}
// ═══════════════════════════════════════════════════════════
// Internal Functions (VM implemented)
// ═══════════════════════════════════════════════════════════
/// Internal debug output
fn _log_debug(msg str) void;
/// Internal info output
fn _log_info(msg str) void;
/// Internal warn output
fn _log_warn(msg str) void;
/// Internal error output (stderr)
fn _log_error(msg str) void;