/** * @file debug.c * @brief Debug and logging implementation for Baba Yaga * @author eli_oat * @version 0.0.1 * @date 2025 * * This file implements debug and logging functionality for the Baba Yaga language. */ #include #include #include #include #include #include "baba_yaga.h" /* ============================================================================ * Debug State * ============================================================================ */ static DebugLevel current_debug_level = DEBUG_NONE; /* ============================================================================ * Debug Functions * ============================================================================ */ /** * @brief Set debug level * * @param level Debug level to set */ void baba_yaga_set_debug_level(DebugLevel level) { current_debug_level = level; } /** * @brief Get current debug level * * @return Current debug level */ DebugLevel baba_yaga_get_debug_level(void) { return current_debug_level; } /** * @brief Get debug level name * * @param level Debug level * @return String representation of debug level */ static const char* debug_level_name(DebugLevel level) { switch (level) { case DEBUG_NONE: return "NONE"; case DEBUG_ERROR: return "ERROR"; case DEBUG_WARN: return "WARN"; case DEBUG_INFO: return "INFO"; case DEBUG_DEBUG: return "DEBUG"; case DEBUG_TRACE: return "TRACE"; default: return "UNKNOWN"; } } /** * @brief Get current timestamp * * @return Current timestamp as string */ static const char* get_timestamp(void) { static char timestamp[32]; time_t now = time(NULL); struct tm* tm_info = localtime(&now); strftime(timestamp, sizeof(timestamp), "%H:%M:%S", tm_info); return timestamp; } /** * @brief Debug logging function * * @param level Debug level for this message * @param file Source file name * @param line Line number * @param func Function name * @param format Format string * @param ... Variable arguments */ void baba_yaga_debug_log(DebugLevel level, const char* file, int line, const char* func, const char* format, ...) { if (level > current_debug_level) { return; } /* Get file name without path */ const char* filename = strrchr(file, '/'); if (filename == NULL) { filename = file; } else { filename++; /* Skip the '/' */ } /* Print timestamp and level */ fprintf(stderr, "[%s] %-5s ", get_timestamp(), debug_level_name(level)); /* Print location */ fprintf(stderr, "%s:%d:%s(): ", filename, line, func); /* Print message */ va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); fprintf(stderr, "\n"); fflush(stderr); }