send filename, line number and function name to PrintConsole and FatalError for verbose debug output

This commit is contained in:
Sebastian Krzyszkowiak 2018-08-08 22:19:50 +02:00
parent 942fd043fd
commit 6684847424
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
4 changed files with 13 additions and 6 deletions

View file

@ -95,6 +95,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
game->config.fx = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"), NULL, 10); game->config.fx = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"), NULL, 10);
game->config.mute = strtol(GetConfigOptionDefault(game, "SuperDerpy", "mute", "0"), NULL, 10); game->config.mute = strtol(GetConfigOptionDefault(game, "SuperDerpy", "mute", "0"), NULL, 10);
game->config.debug = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10); game->config.debug = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10);
game->config.verbose = strtol(GetConfigOptionDefault(game, "SuperDerpy", "verbose", "0"), NULL, 10);
game->config.width = strtol(GetConfigOptionDefault(game, "SuperDerpy", "width", "1280"), NULL, 10); game->config.width = strtol(GetConfigOptionDefault(game, "SuperDerpy", "width", "1280"), NULL, 10);
if (game->config.width < 320) { game->config.width = 320; } if (game->config.width < 320) { game->config.width = 320; }
game->config.height = strtol(GetConfigOptionDefault(game, "SuperDerpy", "height", "720"), NULL, 10); game->config.height = strtol(GetConfigOptionDefault(game, "SuperDerpy", "height", "720"), NULL, 10);

View file

@ -86,6 +86,7 @@ struct Game {
bool mute; /*!< Whether audio should be muted globally. */ bool mute; /*!< Whether audio should be muted globally. */
bool fullscreen; /*!< Fullscreen toggle. */ bool fullscreen; /*!< Fullscreen toggle. */
bool debug; /*!< Toggles debug mode. */ bool debug; /*!< Toggles debug mode. */
bool verbose; /*!< Toggles verbose debug. */
int width; /*!< Width of window as being set in configuration. */ int width; /*!< Width of window as being set in configuration. */
int height; /*!< Height of window as being set in configuration. */ int height; /*!< Height of window as being set in configuration. */
} config; } config;

View file

@ -197,7 +197,7 @@ SYMBOL_EXPORT ALLEGRO_BITMAP* LoadScaledBitmap(struct Game* game, char* filename
return target; return target;
} }
__attribute__((__format__(__printf__, 3, 0))) SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { __attribute__((__format__(__printf__, 6, 0))) SYMBOL_EXPORT void FatalErrorWithContext(struct Game* game, int line, const char* file, const char* func, bool exit, char* format, ...) {
// TODO: interrupt and take over loading thread when it happens // TODO: interrupt and take over loading thread when it happens
char text[1024] = {0}; char text[1024] = {0};
PrintConsole(game, "Fatal Error, displaying Blue Screen of Derp..."); PrintConsole(game, "Fatal Error, displaying Blue Screen of Derp...");
@ -205,7 +205,7 @@ __attribute__((__format__(__printf__, 3, 0))) SYMBOL_EXPORT void FatalError(stru
va_start(vl, format); va_start(vl, format);
vsnprintf(text, 1024, format, vl); vsnprintf(text, 1024, format, vl);
va_end(vl); va_end(vl);
fprintf(stderr, "%s\n", text); fprintf(stderr, "%s:%d [%s]\n%s\n", file, line, func, text);
// TODO: synchronize with loading thread // TODO: synchronize with loading thread
@ -386,7 +386,7 @@ SYMBOL_EXPORT char* GetDataFilePath(struct Game* game, const char* filename) {
ALLEGRO_DEBUG_CHANNEL("libsuperderpy") ALLEGRO_DEBUG_CHANNEL("libsuperderpy")
__attribute__((__format__(__printf__, 2, 0))) SYMBOL_EXPORT void PrintConsole(struct Game* game, char* format, ...) { __attribute__((__format__(__printf__, 5, 0))) SYMBOL_EXPORT void PrintConsoleWithContext(struct Game* game, int line, const char* file, const char* func, char* format, ...) {
va_list vl; va_list vl;
va_start(vl, format); va_start(vl, format);
char* text = game->_priv.console[game->_priv.console_pos]; char* text = game->_priv.console[game->_priv.console_pos];
@ -401,7 +401,10 @@ __attribute__((__format__(__printf__, 2, 0))) SYMBOL_EXPORT void PrintConsole(st
if (game->config.debug) if (game->config.debug)
#endif #endif
{ {
printf("%s\n", text); if (game->config.verbose) {
printf("%s:%d ", file, line);
}
printf("[%s] %s\n", func, text);
fflush(stdout); fflush(stdout);
} }
game->_priv.console_pos++; game->_priv.console_pos++;

View file

@ -67,15 +67,17 @@ ALLEGRO_BITMAP* LoadScaledBitmap(struct Game* game, char* filename, int width, i
/*! \brief Finds path for data file. */ /*! \brief Finds path for data file. */
char* GetDataFilePath(struct Game* game, const char* filename); char* GetDataFilePath(struct Game* game, const char* filename);
void PrintConsoleWithContext(struct Game* game, int line, const char* file, const char* func, char* format, ...);
/*! \brief Print some message on game console. /*! \brief Print some message on game console.
* *
* Draws message on console bitmap, so it'll be displayed when calling DrawConsole. * Draws message on console bitmap, so it'll be displayed when calling DrawConsole.
* If game->debug is true, then it also prints given message on stdout. * If game->debug is true, then it also prints given message on stdout.
* It needs to be called in printf style. * It needs to be called in printf style.
*/ */
void PrintConsole(struct Game* game, char* format, ...); #define PrintConsole(game, format, ...) PrintConsoleWithContext(game, __LINE__, __FILE__, __func__, format, ##__VA_ARGS__)
void FatalError(struct Game* game, bool exit, char* format, ...); void FatalErrorWithContext(struct Game* game, int line, const char* file, const char* func, bool exit, char* format, ...);
#define FatalError(game, exit, format, ...) FatalErrorWithContext(game, __LINE__, __FILE__, __func__, exit, format, ##__VA_ARGS__)
void SetupViewport(struct Game* game, struct Viewport config); void SetupViewport(struct Game* game, struct Viewport config);