/* * Copyright (c) 2025 Aldrik Ramaekers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "timer.h" #include "log.hpp" log g_log = {0}; log* get_log() { return &g_log; } void log_add(const char* fmt, ...) { va_list args; va_start(args, fmt); vsnprintf(g_log.history[g_log.write_cursor], MAX_LEN_LOG_TXT, fmt, args); va_end(args); tick_t ms_since_epoch = timer_system(); char time_buf[50]; time_t seconds = (time_t)(ms_since_epoch / 1000); int milliseconds = (int)(ms_since_epoch % 1000); // Convert to local time struct tm tm_time; #if defined(_WIN32) localtime_s(&tm_time, &seconds); #else localtime_r(&seconds, &tm_time); #endif snprintf(time_buf, 50, "%02d:%02d %02d.%03d", tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, milliseconds); char tmp[MAX_LEN_LOG_TXT]; snprintf(tmp, MAX_LEN_LOG_TXT, "[%s] %s", time_buf, g_log.history[g_log.write_cursor]); tmp[MAX_LEN_LOG_TXT-1] = 0; memcpy(g_log.history[g_log.write_cursor], tmp, MAX_LEN_LOG_TXT); g_log.write_cursor++; if (g_log.write_cursor >= MAX_LEN_LOG_HISTORY) g_log.write_cursor = 0; g_log.history_length++; if (g_log.history_length > MAX_LEN_LOG_HISTORY) g_log.history_length = MAX_LEN_LOG_HISTORY; }