added option to limit debug output to specific debug mode
[rocksndiamonds.git] / src / libgame / misc.c
index 79b1635ff12adccea7b570845ac5fb432128f664..7047ba92d120b280eacbefa00c12289152982963 100644 (file)
@@ -251,6 +251,115 @@ 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;
+
+  // if optional debug mode specified, limit debug output accordingly
+  if (options.debug_mode != NULL &&
+      !strEqual(options.debug_mode, mode))
+    return;
+
+  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
 // ----------------------------------------------------------------------------
@@ -932,6 +1041,7 @@ void GetOptions(int argc, char *argv[],
 
   options.execute_command = NULL;
   options.special_flags = NULL;
+  options.debug_mode = NULL;
 
   options.mytapes = FALSE;
   options.serveronly = FALSE;
@@ -1066,6 +1176,10 @@ void GetOptions(int argc, char *argv[],
     else if (strncmp(option, "-debug", option_len) == 0)
     {
       options.debug = TRUE;
+
+      // optionally, debug output can be limited to a specific debug mode
+      if (option_arg != next_option)
+       options.debug_mode = getStringCopy(option_arg);
     }
     else if (strncmp(option, "-verbose", option_len) == 0)
     {