From abf01f657d068aa6b22ab962cbe01b88f3b5f7ea Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 12 Sep 2025 16:35:40 +0200 Subject: event logging --- src/log.cpp | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/log.cpp') diff --git a/src/log.cpp b/src/log.cpp index 960ee85..058b594 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,24 +1,47 @@ #include #include - +#include +#include "timer.h" #include "log.hpp" log g_log = {0}; -void log_add(double timestamp, const char* fmt, ...) +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, "[%.3f] %s", timestamp, g_log.history[g_log.write_cursor]); + 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); - printf(g_log.history[g_log.write_cursor]); - g_log.write_cursor++; if (g_log.write_cursor >= MAX_LEN_LOG_HISTORY) g_log.write_cursor = 0; -- cgit v1.2.3-70-g09d2