power_play/src/base/base_log.h
2025-11-02 19:31:49 -06:00

146 lines
3.4 KiB
C

////////////////////////////////////////////////////////////
//~ Log event types
Struct(LogEvent)
{
u64 id;
u64 level_id;
DateTime datetime;
i64 time_ns;
String msg;
i32 level;
i32 thread_id;
i32 fiber_id;
};
Struct(LogEventsArray)
{
u64 count;
LogEvent *logs;
};
////////////////////////////////////////////////////////////
//~ Logging levels
#define LogLevel(l) (l <= LogLevel_CompTime)
/* Log level configuration */
#ifndef LogLevel_CompTime
# if RtcIsEnabled || ProfilingIsEnabled
# define LogLevel_CompTime LogLevel_Debug
# else
# define LogLevel_CompTime LogLevel_Info
# endif
#endif
#define LogLevel_None -1
#define LogLevel_Critical 0
#define LogLevel_Error 1
#define LogLevel_Warning 2
#define LogLevel_Success 3
#define LogLevel_Info 4
#define LogLevel_Debug 5
#define LogLevel_Count 6
////////////////////////////////////////////////////////////
//~ Log level types
Struct(LogLevelSettings)
{
String shorthand;
};
Global Readonly LogLevelSettings log_settings[LogLevel_Count] = {
[LogLevel_Critical] = {
LitNoCast("CRITICAL"),
},
[LogLevel_Error] = {
LitNoCast("ERROR"),
},
[LogLevel_Warning] = {
LitNoCast("WARNING"),
},
[LogLevel_Success] = {
LitNoCast("SUCCESS"),
},
[LogLevel_Info] = {
LitNoCast("INFO"),
},
[LogLevel_Debug] = {
LitNoCast("DEBUG"),
}
};
////////////////////////////////////////////////////////////
//~ Compile-time logging macros
#if LogLevel(LogLevel_Critical)
# define LogCritical(msg) Log_(LogLevel_Critical, msg)
# define LogCriticalF(fmt_lit, ...) LogF_(LogLevel_Critical, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogCritical(msg)
# define LogCriticalF(...)
#endif
#if LogLevel(LogLevel_Error)
# define LogError(msg) Log_(LogLevel_Error, msg)
# define LogErrorF(fmt_lit, ...) LogF_(LogLevel_Error, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogError(msg)
# define LogErrorF(...)
#endif
#if LogLevel(LogLevel_Warning)
# define LogWarning(msg) Log_(LogLevel_Warning, msg)
# define LogWarningF(fmt_lit, ...) LogF_(LogLevel_Warning, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogWarning(msg)
# define LogWarningF(...)
#endif
#if LogLevel(LogLevel_Success)
# define LogSuccess(msg) Log_(LogLevel_Success, msg)
# define LogSuccessF(fmt_lit, ...) LogF_(LogLevel_Success, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogSuccess(msg)
# define LogSuccessF(...)
#endif
#if LogLevel(LogLevel_Info)
# define LogInfo(msg) Log_(LogLevel_Info, msg)
# define LogInfoF(fmt_lit, ...) LogF_(LogLevel_Info, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogInfo(msg)
# define LogInfoF(...)
#endif
#if LogLevel(LogLevel_Debug)
# define LogDebug(msg) Log_(LogLevel_Debug, msg)
# define LogDebugF(fmt_lit, ...) LogF_(LogLevel_Debug, Lit(fmt_lit) , ## __VA_ARGS__, FmtEnd)
#else
# define LogDebug(msg)
# define LogDebugF(...)
#endif
////////////////////////////////////////////////////////////
//~ @hookdecl Init hooks
void InitLogSystem(String logfile_path);
////////////////////////////////////////////////////////////
//~ @hookdecl Log hooks
/* NOTE: Calling these functions rather than using the logging macros may result in logs that are compiled regardless of log level. */
void LogPanic(String msg);
void LogF_(i32 level, String fmt, ...);
LogEventsArray GetLogEvents(void);