diff options
| author | 2026-07-27 11:29:01 -0400 | |
|---|---|---|
| committer | 2026-07-27 11:29:01 -0400 | |
| commit | 654d8eca70eef868455a08ab749e21b934a4f845 (patch) | |
| tree | b185ce1fb7a0a0ffc054455f02bdb6cdd0fbb428 | |
| parent | 7e9e479502e6d56057aac066b61336d0013b6a7a (diff) | |
| download | libalabaster-654d8eca70eef868455a08ab749e21b934a4f845.tar.gz libalabaster-654d8eca70eef868455a08ab749e21b934a4f845.tar.bz2 libalabaster-654d8eca70eef868455a08ab749e21b934a4f845.zip | |
log: Add path for when log has no args
Signed-off-by: Andrew Opalach <andrew@akon.city>
| -rw-r--r-- | include/al/log.h | 9 | ||||
| -rw-r--r-- | src/log.c | 30 |
2 files changed, 24 insertions, 15 deletions
diff --git a/include/al/log.h b/include/al/log.h index 7fd72dd..92e6e29 100644 --- a/include/al/log.h +++ b/include/al/log.h @@ -17,6 +17,7 @@ AL_IGNORE_WARNING_END #define __LOCATION__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__ #define AL_LOG_MESSAGE_SIZE 256u +#define AL_LOG_MESSAGE_LENGTH (AL_LOG_MESSAGE_SIZE - 1u) enum { AL_LOG_INFO = 0, @@ -28,11 +29,11 @@ enum { void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata); -s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...); -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_log(u8 level, const char *section, const char *name, const s32 line, const char *func, bool no_args, const char *fmt, ...); +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); -#define al_log(level, section, fmt, ...) _al_log(level, section, __LOCATION__, fmt, ##__VA_ARGS__) -#define al_logv(level, section, fmt, args) _al_logv(level, section, __LOCATION__, fmt, args) +#define al_log(level, section, fmt, ...) _al_log(level, section, __LOCATION__, (sizeof((char[]){#__VA_ARGS__}) == 1), fmt, ##__VA_ARGS__) +#define al_logv(level, section, fmt, args) _al_logv(level, section, __LOCATION__, false, fmt, args) #define log_info(fmt, ...) al_log(AL_LOG_INFO, AL_LOG_SECTION_NAME, fmt, ##__VA_ARGS__) #define log_warn(fmt, ...) al_log(AL_LOG_WARN, AL_LOG_SECTION_NAME, fmt, ##__VA_ARGS__) @@ -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 |