From 654d8eca70eef868455a08ab749e21b934a4f845 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 27 Jul 2026 11:29:01 -0400 Subject: log: Add path for when log has no args Signed-off-by: Andrew Opalach --- src/log.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/log.c b/src/log.c index 9d5781f..d5baaa8 100644 --- a/src/log.c +++ b/src/log.c @@ -4,7 +4,7 @@ #define AL_LOG_TEMPLATE "%s:%d %s(): %s -> " #define AL_LOG_TEMPLATE_SECTION AL_LOG_TEMPLATE"(%s) " -#define AL_LOG_MESSAGE_MAX ((size_t)(AL_LOG_MESSAGE_SIZE - 1u)) // Space for newline. +#define AL_LOG_MESSAGE_MAX ((size_t)(AL_LOG_MESSAGE_SIZE)) #ifndef AL_FORCE_DISABLE_OUTPUT static s32 (*_al_print)(void *, u8, char *) = NULL; @@ -27,7 +27,7 @@ static const char *levels[] = { #ifndef AL_LOG_SKIP static __thread char messagebuf[AL_LOG_MESSAGE_SIZE]; -static char *get_message_buffer(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args) +static char *get_message_buffer(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, va_list args) { s32 prefix = 0; if (!section) { @@ -36,13 +36,17 @@ static char *get_message_buffer(u8 level, const char *section, const char *name, prefix = al_snprintf(messagebuf, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE_SECTION, name, line, func, levels[level], section); } al_assert(prefix > 0); - al_vsnprintf(messagebuf + prefix, AL_LOG_MESSAGE_MAX - (size_t)prefix, fmt, args); + if (no_args) { + al_snprintf(messagebuf + prefix, AL_LOG_MESSAGE_MAX, "%*.*s", 0, AL_LOG_MESSAGE_LENGTH, (char *)fmt); + } else { + al_vsnprintf(messagebuf + prefix, AL_LOG_MESSAGE_MAX - (size_t)prefix, fmt, args); + } messagebuf[strcspn(messagebuf, "\r\n")] = '\0'; return messagebuf; } #endif -s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args) +s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, va_list args) { #ifdef AL_LOG_SKIP s32 ret = 0; @@ -51,27 +55,31 @@ s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, co } else { ret += al_printf(AL_LOG_TEMPLATE_SECTION, name, line, func, levels[level], section); } - ret += al_vprintf(fmt, args); - if (strcspn(fmt, "\r\n") == al_strlen(fmt)) { + if (no_args) { + ret += al_printf("%*.*s", 0, AL_LOG_MESSAGE_LENGTH, (char *)fmt); + } else { + ret += al_vprintf(fmt, args); + } + if (fmt[al_strlen(fmt) - 1] != '\n') { ret += al_printf("\n"); } return ret; #else al_assert(_al_print && "Global print method not set."); - return _al_print(_al_log_userdata, level, get_message_buffer(level, section, name, line, func, fmt, args)); + return _al_print(_al_log_userdata, level, get_message_buffer(level, section, name, line, func, no_args, fmt, args)); #endif } -s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...) +s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, ...) { va_list args; va_start(args, fmt); - s32 ret = _al_logv(level, section, name, line, func, fmt, args); + s32 ret = _al_logv(level, section, name, line, func, no_args, fmt, args); va_end(args); return ret; } #else void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata) { (void)print_func; (void)userdata; } -s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)fmt; (void)args; return 0; } -s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)fmt; return 0; } +s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, va_list args) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)no_args; (void)fmt; (void)args; return 0; } +s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, ...) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)no_args; (void)fmt; return 0; } #endif -- cgit v1.2.3-101-g0448