From: Holger Schemel Date: Wed, 16 Sep 2020 14:17:15 +0000 (+0200) Subject: added generic logging functions (not used yet) X-Git-Tag: 4.2.0.3~38 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=7719feee76dac64d8f6153d4c379c44db1d89f7d;p=rocksndiamonds.git added generic logging functions (not used yet) --- diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 79b1635f..17072d87 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -251,6 +251,110 @@ void PrintLineWithPrefix(char *prefix, char *line_chars, int line_length) } +// ---------------------------------------------------------------------------- +// generic logging functions +// ---------------------------------------------------------------------------- + +enum log_levels +{ + LOG_UNKNOWN = 0, + LOG_DEBUG, + LOG_INFO, + LOG_WARN, + LOG_ERROR, + LOG_FATAL +}; + +static char *log_tokens[] = +{ + "UNKNOWN", + "DEBUG", + "INFO", + "WARN", + "ERROR", + "FATAL" +}; + +static void printf_log_prefix(int log_level, char *mode) +{ + if (log_level < 0 || log_level > LOG_FATAL) + return; + + char *log_token = log_tokens[log_level]; + + if (log_level == LOG_DEBUG) + printf_log_nonewline("[%s] [%s] ", log_token, mode); + else + printf_log_nonewline("[%s] ", log_token); +} + +static void Log(int log_level, char *mode, char *format, va_list ap) +{ + if (log_level < 0 || log_level > LOG_FATAL) + return; + + static boolean last_line_was_separator = FALSE; + char *log_token = log_tokens[log_level]; + + if (strEqual(format, "===") || + strEqual(format, "---")) + { + static char *mode_last = NULL; + char line_char[2] = { format[0], '\0' }; + int line_length = 80 - strlen(log_token) - 3; + + if (log_level == LOG_DEBUG) + line_length -= strlen(mode) + 3; + + if (last_line_was_separator && strEqual(mode, mode_last)) + return; + + printf_log_prefix(log_level, mode); + printf_log_line(line_char, line_length); + + if (!strEqual(mode, mode_last)) + setString(&mode_last, mode); + + last_line_was_separator = TRUE; + + return; + } + + last_line_was_separator = FALSE; + + printf_log_prefix(log_level, mode); + + vprintf_log(format, ap); +} + +void Debug(char *mode, char *format, ...) +{ + va_list ap; + + va_start(ap, format); + Log(LOG_DEBUG, mode, format, ap); + va_end(ap); +} + +void Info(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + Log(LOG_INFO, NULL, format, ap); + va_end(ap); +} + +void Warn(char *format, ...) +{ + va_list ap; + + va_start(ap, format); + Log(LOG_WARN, NULL, format, ap); + va_end(ap); +} + + // ---------------------------------------------------------------------------- // string functions // ---------------------------------------------------------------------------- diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 15ff017c..ba82dad4 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -123,6 +123,10 @@ void PrintNoLog(char *, ...); void PrintLine(char *, int); void PrintLineWithPrefix(char *, char *, int); +void Debug(char *, char *, ...); +void Info(char *, ...); +void Warn(char *, ...); + char *int2str(int, int); char *i_to_a(unsigned int); int log_2(unsigned int);