summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/log.c94
1 files changed, 21 insertions, 73 deletions
diff --git a/src/log.c b/src/log.c
index c4b24f0..748f0d5 100644
--- a/src/log.c
+++ b/src/log.c
@@ -11,116 +11,64 @@
#define AL_LOG_MESSAGE_MAX (AL_LOG_MESSAGE_SIZE - 1) // Space for newline.
#ifndef AL_FORCE_DISABLE_OUTPUT
-static const char *levels[] = { "info", "warn", "error", "debug" };
-static inline const char *get_color_for_level(u8 level)
+static s32 (*_al_print)(void *, u8, char *) = NULL;
+static void *_al_log_userdata = NULL;
+
+void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata)
{
-#define NORMAL "\x1B[0m"
-#define RED "\x1B[31m"
-#define GREEN "\x1B[32m"
-#define YELLOW "\x1B[33m"
-#define BLUE "\x1B[34m"
-#define MAGENTA "\x1B[35m"
-#define CYAN "\x1B[36m"
-#define WHITE "\x1B[37m"
-#define RESET "\033[0m"
- switch (level) {
- case AL_LOG_ERROR: return RED;
- case AL_LOG_WARN: return YELLOW;
- default: return NORMAL;
- }
+ _al_print = print_func;
+ _al_log_userdata = userdata;
}
-#endif
-#if !defined AL_FORCE_DISABLE_OUTPUT && !defined AL_LOG_SKIP
+static const char *levels[] = { "info", "warn", "error", "debug" };
+
+#ifndef AL_LOG_SKIP
static char *get_message_buffer(u8 level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args)
{
char *buffer = al_malloc(AL_LOG_MESSAGE_SIZE);
+ s32 prefix = 0;
#ifdef AL_LOG_USE_SECTION
- s32 prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level], section);
+ prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level], section);
#else
(void)section;
- s32 prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level]);
+ prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level]);
#endif
al_vsnprintf(buffer + prefix, AL_LOG_MESSAGE_MAX - prefix, fmt, args);
buffer[strcspn(buffer, "\r\n")] = '\0';
return buffer;
}
-
-s32 al_print_default(void *userdata, u8 level, char *message)
-{
- (void)userdata;
-#ifndef _WIN32
- s32 ret = al_printf("%s%*.*s\n", get_color_for_level(level), 0, AL_LOG_MESSAGE_SIZE, message);
-#else
- (void)level;
- s32 ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_SIZE, message);
-#endif
- al_free(message);
- return ret;
-}
-
-static s32 (*_al_print)(void *, u8, char *) = al_print_default;
-static void *_al_log_userdata = NULL;
-
-void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata)
-{
- _al_print = print_func;
- _al_log_userdata = userdata;
-}
-#else
-void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata) { (void)print_func; (void)userdata; }
#endif
s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args)
{
-#ifdef AL_FORCE_DISABLE_OUTPUT
- (void)level;
- (void)section;
- (void)name;
- (void)line;
- (void)func;
- (void)fmt;
- (void)args;
- return 0;
-#else
if (!section) section = "";
#ifdef AL_LOG_SKIP
- s32 ret = al_printf(get_color_for_level(level));
+ s32 ret = 0;
#ifdef AL_LOG_USE_SECTION
- ret += al_printf(AL_LOG_TEMPLATE, name, line, func, level, section);
+ ret += al_printf(AL_LOG_TEMPLATE, name, line, func, levels[level], section);
#else
- ret += al_printf(AL_LOG_TEMPLATE, name, line, func, level);
+ ret += al_printf(AL_LOG_TEMPLATE, name, line, func, levels[level]);
#endif
ret += al_vprintf(fmt, args);
if (strcspn(fmt, "\r\n") == al_strlen(fmt)) {
ret += al_printf("\n");
}
-#else
- char *buffer = get_message_buffer(level, section, fmt, name, line, func, args);
-#endif
-#ifdef AL_LOG_SKIP
return ret;
#else
- return _al_print(_al_log_userdata, level, buffer);
-#endif
+ return _al_print(_al_log_userdata, level, get_message_buffer(level, section, fmt, name, line, func, args));
#endif
}
s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...)
{
-#ifdef AL_FORCE_DISABLE_OUTPUT
- (void)level;
- (void)section;
- (void)name;
- (void)line;
- (void)func;
- (void)fmt;
- return 0;
-#else
va_list args;
va_start(args, fmt);
s32 ret = _al_logv(level, section, name, line, func, fmt, args);
va_end(args);
return ret;
-#endif
}
+#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; }
+#endif