From de49192b75b9ff43a8c2ed6cb200efb4240d8d6d Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sat, 19 Sep 2020 01:00:23 +0200 Subject: [PATCH] changed non-fatal error handling to use new error functions --- src/game.c | 14 +++-- src/libgame/misc.c | 134 +++++++++++---------------------------------- src/libgame/misc.h | 19 +------ 3 files changed, 42 insertions(+), 125 deletions(-) diff --git a/src/game.c b/src/game.c index 47fa48ad..859cf24e 100644 --- a/src/game.c +++ b/src/game.c @@ -2147,8 +2147,9 @@ static void InitGameControlValues(void) 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 @@ -15424,10 +15425,11 @@ static void LoadEngineSnapshotValues_RND(void) 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"); } } diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 3946cd73..0b70e206 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -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 @@ -295,6 +295,7 @@ static void vLog(int log_level, char *mode, char *format, va_list ap) if (log_level == LOG_DEBUG) { + // only show debug messages when in debug mode 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; } + 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 : @@ -383,6 +390,15 @@ void Warn(char *format, ...) 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; @@ -1328,91 +1344,6 @@ char *GetError(void) 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 @@ -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) { - 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); - 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); - Error(ERR_EXIT, "please fix"); + + Fail("please fix"); } 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)) { - 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) - 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; } @@ -3642,13 +3574,13 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info, 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) - 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; } @@ -3688,13 +3620,13 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info, } 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) - 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; } diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 2fb49475..32dc36c2 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -34,23 +34,6 @@ #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 @@ -126,6 +109,7 @@ void PrintLineWithPrefix(char *, char *, int); void Debug(char *, char *, ...); void Info(char *, ...); void Warn(char *, ...); +void Error(char *, ...); void Fail(char *, ...); void FailWithHelp(char *, ...); @@ -186,7 +170,6 @@ void GetOptions(int, char **, void SetError(char *, ...); char *GetError(void); -void Error(int, char *, ...); void *checked_malloc(unsigned int); void *checked_calloc(unsigned int); -- 2.34.1