diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-12 16:35:40 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-12 16:35:40 +0200 |
| commit | abf01f657d068aa6b22ab962cbe01b88f3b5f7ea (patch) | |
| tree | 2a354a6112ef0b9ef6975613f12865831f5d4a69 /src/log.cpp | |
| parent | d174d803de2296061731c3698980a6a51e6fc3ef (diff) | |
event logging
Diffstat (limited to 'src/log.cpp')
| -rw-r--r-- | src/log.cpp | 33 |
1 files changed, 28 insertions, 5 deletions
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 <stdio.h> #include <stdarg.h> - +#include <time.h> +#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; |
