changed non-fatal error handling to use new error functions
authorHolger Schemel <info@artsoft.org>
Fri, 18 Sep 2020 23:00:23 +0000 (01:00 +0200)
committerHolger Schemel <info@artsoft.org>
Fri, 18 Sep 2020 23:00:23 +0000 (01:00 +0200)
src/game.c
src/libgame/misc.c
src/libgame/misc.h

index 47fa48ad60b63225d2e8579b86fd7c7c627d07f0..859cf24e4cfb6812f03c2017c17872fa7a82cf0c 100644 (file)
@@ -2147,8 +2147,9 @@ static void InitGameControlValues(void)
 
     if (nr != i)
     {
 
     if (nr != i)
     {
-      Error(ERR_INFO, "'game_panel_controls' structure corrupted at %d", i);
-      Error(ERR_EXIT, "this should not happen -- please debug");
+      Error("'game_panel_controls' structure corrupted at %d", i);
+
+      Fail("this should not happen -- please debug");
     }
 
     // force update of game controls after initialization
     }
 
     // force update of game controls after initialization
@@ -15424,10 +15425,11 @@ static void LoadEngineSnapshotValues_RND(void)
 
   if (game.num_random_calls != num_random_calls)
   {
 
   if (game.num_random_calls != num_random_calls)
   {
-    Error(ERR_INFO, "number of random calls out of sync");
-    Error(ERR_INFO, "number of random calls should be %d", num_random_calls);
-    Error(ERR_INFO, "number of random calls is %d", game.num_random_calls);
-    Error(ERR_EXIT, "this should not happen -- please debug");
+    Error("number of random calls out of sync");
+    Error("number of random calls should be %d", num_random_calls);
+    Error("number of random calls is %d", game.num_random_calls);
+
+    Fail("this should not happen -- please debug");
   }
 }
 
   }
 }
 
index 3946cd7337199f17c0b24ae8cb759c518a70c19b..0b70e206b6002424b9adf0b94254a77b44919ab9 100644 (file)
@@ -252,7 +252,7 @@ void PrintLineWithPrefix(char *prefix, char *line_chars, int line_length)
 
 
 // ----------------------------------------------------------------------------
 
 
 // ----------------------------------------------------------------------------
-// generic logging functions
+// generic logging and error handling functions
 // ----------------------------------------------------------------------------
 
 enum log_levels
 // ----------------------------------------------------------------------------
 
 enum log_levels
@@ -295,6 +295,7 @@ static void vLog(int log_level, char *mode, char *format, va_list ap)
 
   if (log_level == LOG_DEBUG)
   {
 
   if (log_level == LOG_DEBUG)
   {
+    // only show debug messages when in debug mode
     if (!options.debug)
       return;
 
     if (!options.debug)
       return;
 
@@ -303,6 +304,12 @@ static void vLog(int log_level, char *mode, char *format, va_list ap)
        strstr(mode, options.debug_mode) == NULL)
       return;
   }
        strstr(mode, options.debug_mode) == NULL)
       return;
   }
+  else if (log_level == LOG_WARN)
+  {
+    // only show warning messages when in verbose mode
+    if (!options.verbose)
+      return;
+  }
 
 #if defined(PLATFORM_ANDROID)
   android_log_prio = (log_level == LOG_DEBUG ? ANDROID_LOG_DEBUG :
 
 #if defined(PLATFORM_ANDROID)
   android_log_prio = (log_level == LOG_DEBUG ? ANDROID_LOG_DEBUG :
@@ -383,6 +390,15 @@ void Warn(char *format, ...)
   va_end(ap);
 }
 
   va_end(ap);
 }
 
+void Error(char *format, ...)
+{
+  va_list ap;
+
+  va_start(ap, format);
+  vLog(LOG_ERROR, NULL, format, ap);
+  va_end(ap);
+}
+
 void Fail(char *format, ...)
 {
   va_list ap;
 void Fail(char *format, ...)
 {
   va_list ap;
@@ -1328,91 +1344,6 @@ char *GetError(void)
   return internal_error;
 }
 
   return internal_error;
 }
 
-void Error(int mode, char *format, ...)
-{
-  static boolean last_line_was_separator = FALSE;
-  char *process_name = "";
-
-  if (program.log_file[LOG_ERR_ID] == NULL)
-    return;
-
-#if defined(PLATFORM_ANDROID)
-  android_log_prio = (mode & ERR_DEBUG ? ANDROID_LOG_DEBUG :
-                     mode & ERR_INFO ? ANDROID_LOG_INFO :
-                     mode & ERR_WARN ? ANDROID_LOG_WARN :
-                     mode & ERR_EXIT ? ANDROID_LOG_FATAL :
-                     ANDROID_LOG_UNKNOWN);
-#endif
-
-  // display debug messages only when running in debug mode
-  if (mode & ERR_DEBUG && !options.debug)
-    return;
-
-  // display warnings only when running in verbose mode
-  if (mode & ERR_WARN && !options.verbose)
-    return;
-
-  if (mode == ERR_INFO_LINE)
-  {
-    if (!last_line_was_separator)
-      printf_log_line(format, 79);
-
-    last_line_was_separator = TRUE;
-
-    return;
-  }
-
-  last_line_was_separator = FALSE;
-
-  if (mode & ERR_SOUND_SERVER)
-    process_name = " sound server";
-  else if (mode & ERR_NETWORK_SERVER)
-    process_name = " network server";
-  else if (mode & ERR_NETWORK_CLIENT)
-    process_name = " network client **";
-
-  if (format)
-  {
-#if !defined(PLATFORM_ANDROID)
-    printf_log_nonewline("%s%s: ", program.command_basename, process_name);
-#endif
-
-    if (mode & ERR_WARN)
-      printf_log_nonewline("warning: ");
-
-    if (mode & ERR_EXIT)
-      printf_log_nonewline("fatal error: ");
-
-    va_list ap;
-
-    va_start(ap, format);
-    vprintf_log(format, ap);
-    va_end(ap);
-
-    if ((mode & ERR_EXIT) && !(mode & ERR_FROM_SERVER))
-    {
-      va_start(ap, format);
-      program.exit_message_function(format, ap);
-      va_end(ap);
-    }
-  }
-  
-  if (mode & ERR_HELP)
-    printf_log("%s: Try option '--help' for more information.",
-              program.command_basename);
-
-  if (mode & ERR_EXIT)
-    printf_log("%s%s: aborting", program.command_basename, process_name);
-
-  if (mode & ERR_EXIT)
-  {
-    if (mode & ERR_FROM_SERVER)
-      exit(1);                         // child process: normal exit
-    else
-      program.exit_function(1);                // main process: clean up stuff
-  }
-}
-
 
 // ----------------------------------------------------------------------------
 // checked memory allocation and freeing functions
 
 // ----------------------------------------------------------------------------
 // checked memory allocation and freeing functions
@@ -3001,13 +2932,14 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
   num_file_list_entries_found = list_pos + 1;
   if (num_file_list_entries_found != num_file_list_entries)
   {
   num_file_list_entries_found = list_pos + 1;
   if (num_file_list_entries_found != num_file_list_entries)
   {
-    Error(ERR_INFO_LINE, "-");
-    Error(ERR_INFO, "inconsistant config list information:");
-    Error(ERR_INFO, "- should be:   %d (according to 'src/conf_xxx.h')",
+    Error("---");
+    Error("inconsistant config list information:");
+    Error("- should be:   %d (according to 'src/conf_xxx.h')",
          num_file_list_entries);
          num_file_list_entries);
-    Error(ERR_INFO, "- found to be: %d (according to 'src/conf_xxx.c')",
+    Error("- found to be: %d (according to 'src/conf_xxx.c')",
          num_file_list_entries_found);
          num_file_list_entries_found);
-    Error(ERR_EXIT,   "please fix");
+
+    Fail("please fix");
   }
 
   freeSetupFileHash(ignore_tokens_hash);
   }
 
   freeSetupFileHash(ignore_tokens_hash);
@@ -3618,14 +3550,14 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
     if (file_list_entry->default_is_cloned &&
        strEqual(basename, UNDEFINED_FILENAME))
     {
     if (file_list_entry->default_is_cloned &&
        strEqual(basename, UNDEFINED_FILENAME))
     {
-      int error_mode = ERR_WARN;
+      void (*error_func)(char *, ...) = Warn;
 
       // we can get away without sounds and music, but not without graphics
       if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
 
       // we can get away without sounds and music, but not without graphics
       if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
-       error_mode = ERR_EXIT;
+       error_func = Fail;
 
 
-      Error(error_mode, "token '%s' was cloned and has no default filename",
-           file_list_entry->token);
+      error_func("token '%s' was cloned and has no default filename",
+                file_list_entry->token);
 
       return;
     }
 
       return;
     }
@@ -3642,13 +3574,13 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
 
     if (filename == NULL)
     {
 
     if (filename == NULL)
     {
-      int error_mode = ERR_WARN;
+      void (*error_func)(char *, ...) = Warn;
 
       // we can get away without sounds and music, but not without graphics
       if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
 
       // we can get away without sounds and music, but not without graphics
       if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
-       error_mode = ERR_EXIT;
+       error_func = Fail;
 
 
-      Error(error_mode, "cannot find default artwork file '%s'", basename);
+      error_func("cannot find default artwork file '%s'", basename);
 
       return;
     }
 
       return;
     }
@@ -3688,13 +3620,13 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
   }
   else
   {
   }
   else
   {
-    int error_mode = ERR_WARN;
+    void (*error_func)(char *, ...) = Warn;
 
     // we can get away without sounds and music, but not without graphics
     if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
 
     // we can get away without sounds and music, but not without graphics
     if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
-      error_mode = ERR_EXIT;
+      error_func = Fail;
 
 
-    Error(error_mode, "cannot load artwork file '%s'", basename);
+    error_func("cannot load artwork file '%s'", basename);
 
     return;
   }
 
     return;
   }
index 2fb49475e4465fddd75d73c7c43a94876e2814f6..32dc36c26abc90313ce3d4ba62192aff95cade32 100644 (file)
 #define GetEngineRandom(max)           get_random_number(RANDOM_ENGINE, max)
 #define GetSimpleRandom(max)           get_random_number(RANDOM_SIMPLE, max)
 
 #define GetEngineRandom(max)           get_random_number(RANDOM_ENGINE, max)
 #define GetSimpleRandom(max)           get_random_number(RANDOM_SIMPLE, max)
 
-// values for Error()
-#define ERR_UNKNOWN                    0
-#define ERR_DEBUG                      (1 << 0)
-#define ERR_INFO                       (1 << 1)
-#define ERR_INFO_LINE                  (1 << 2)
-#define ERR_WARN                       (1 << 3)
-#define ERR_EXIT                       (1 << 4)
-#define ERR_HELP                       (1 << 5)
-#define ERR_SOUND_SERVER               (1 << 6)
-#define ERR_NETWORK_SERVER             (1 << 7)
-#define ERR_NETWORK_CLIENT             (1 << 8)
-#define ERR_FROM_SERVER                        (ERR_SOUND_SERVER | ERR_NETWORK_SERVER)
-#define ERR_EXIT_HELP                  (ERR_EXIT | ERR_HELP)
-#define ERR_EXIT_SOUND_SERVER          (ERR_EXIT | ERR_SOUND_SERVER)
-#define ERR_EXIT_NETWORK_SERVER                (ERR_EXIT | ERR_NETWORK_SERVER)
-#define ERR_EXIT_NETWORK_CLIENT                (ERR_EXIT | ERR_NETWORK_CLIENT)
-
 // values for getFile...() and putFile...()
 #define BYTE_ORDER_BIG_ENDIAN          0
 #define BYTE_ORDER_LITTLE_ENDIAN       1
 // values for getFile...() and putFile...()
 #define BYTE_ORDER_BIG_ENDIAN          0
 #define BYTE_ORDER_LITTLE_ENDIAN       1
@@ -126,6 +109,7 @@ void PrintLineWithPrefix(char *, char *, int);
 void Debug(char *, char *, ...);
 void Info(char *, ...);
 void Warn(char *, ...);
 void Debug(char *, char *, ...);
 void Info(char *, ...);
 void Warn(char *, ...);
+void Error(char *, ...);
 void Fail(char *, ...);
 void FailWithHelp(char *, ...);
 
 void Fail(char *, ...);
 void FailWithHelp(char *, ...);
 
@@ -186,7 +170,6 @@ void GetOptions(int, char **,
 
 void SetError(char *, ...);
 char *GetError(void);
 
 void SetError(char *, ...);
 char *GetError(void);
-void Error(int, char *, ...);
 
 void *checked_malloc(unsigned int);
 void *checked_calloc(unsigned int);
 
 void *checked_malloc(unsigned int);
 void *checked_calloc(unsigned int);