Replace printf/fprintf with own print_info() / print_error() using SDL_log
authorAndreas Eversberg <jolly@eversberg.eu>
Thu, 22 Mar 2018 16:31:59 +0000 (17:31 +0100)
committerAndreas Eversberg <jolly@eversberg.eu>
Sun, 25 Mar 2018 12:13:12 +0000 (14:13 +0200)
12 files changed:
src/libcpu/execute.c
src/libkeyboard/keyboard.c
src/libsdl/Makefile.am
src/libsdl/opengl.c
src/libsdl/print.c [new file with mode: 0644]
src/libsdl/print.h [new file with mode: 0644]
src/libsdl/sdl.c
src/libsound/sound.c
src/libvideo/video.c
src/mercenary/main.c
src/mercenary/mercenary2.c
src/mercenary/mercenary3.c

index 0b53f23..9d3180e 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include "../libsdl/print.h"
 #include "execute.h"
 #include "m68k.h"
 #include "m68kcpu.h"
@@ -175,7 +176,7 @@ int execute_cpu(int irq, const struct cpu_stop stop_at[], int *event)
                        /* checking if game does not hit any 'stop_at' break point, give error output */
                        if (instruction_count >= 10000000) {
                                if (instruction_count == 10000000)
-                                       fprintf(stderr, "!!! games seems to got stuck in an endless loop, please fix !!!\n");
+                                       print_error("!!! games seems to got stuck in an endless loop, please fix !!!\n");
                                fprintf(stderr, "program counter at: %06x\n", REG_PC);
                                if (instruction_count == 10000020)
                                        break;
@@ -184,7 +185,7 @@ int execute_cpu(int irq, const struct cpu_stop stop_at[], int *event)
                        for (i = 0; stop_at[i].event; i++) {
                                if (REG_PC == stop_at[i].pc) {
 #ifdef DEBUG_CPU
-                                       printf("execution to address 0x%06x took %d opcodes\n", REG_PC, instruction_count);
+                                       print_error("execution to address 0x%06x took %d opcodes\n", REG_PC, instruction_count);
 #endif
                                        *event = stop_at[i].event;
                                        return cycle_count;
index 5643c31..f6d5cce 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
+#include "../libsdl/print.h"
 #include "keyboard.h"
 
 //#define DEBUG_KEYS
@@ -120,7 +121,7 @@ void set_key(const char *key, int down)
                }
        }
        if (code < 0 || code >= 0x80) {
-               fprintf(stderr, "Key code '%s' unknown, please fix!\n", key);
+               print_error("Key code '%s' unknown, please fix!\n", key);
                return;
        }
 
@@ -141,7 +142,7 @@ void set_key(const char *key, int down)
        }
 
        if (buffer_len == buffer_size) {
-               fprintf(stderr, "keyboard buffer overflow\n");
+               print_error("keyboard buffer overflow\n");
                return;
        }
 
index ac69ce8..bb2db08 100644 (file)
@@ -4,5 +4,6 @@ noinst_LIBRARIES = libsdl.a
 
 libsdl_a_SOURCES = \
        sdl.c \
-       opengl.c
+       opengl.c \
+       print.c
 
index 40cf6bb..ae3eaca 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <errno.h>
+#include "print.h"
 #include "opengl.h"
 #include <GL/glew.h>
 
@@ -43,7 +44,7 @@ int init_opengl(int _image_width, int _image_height)
                ;
        text_rgb = calloc(texture_size * texture_size * 10, 3);
        if (!text_rgb) {
-               fprintf(stderr, "Failed to allocate texture\n");
+               print_error("Failed to allocate texture\n");
                rc = -ENOMEM;
                goto error;
        }
diff --git a/src/libsdl/print.c b/src/libsdl/print.c
new file mode 100644 (file)
index 0000000..24a9f7f
--- /dev/null
@@ -0,0 +1,26 @@
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <SDL2/SDL_log.h>
+
+void __attribute__ ((format (printf, 1, 2))) _print_info(const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+
+       SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, format, args);
+
+       va_end(args);
+}
+
+void __attribute__ ((format (printf, 3, 4))) _print_error(const char *_file, int _line, const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+
+       SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Error in %s line %d:\n", _file, _line);
+       SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, format, args);
+
+       va_end(args);
+}
+
diff --git a/src/libsdl/print.h b/src/libsdl/print.h
new file mode 100644 (file)
index 0000000..f4e6ee5
--- /dev/null
@@ -0,0 +1,7 @@
+
+#define print_info(format, arg...) _print_info(format, ##arg)
+void __attribute__ ((format (printf, 1, 2))) _print_info(const char *format, ...);
+
+#define print_error(format, arg...) _print_error(__FILE__, __LINE__, format, ##arg)
+void __attribute__ ((format (printf, 3, 4))) _print_error(const char *_file, int _line, const char *format, ...);
+
index d310791..50af2a8 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <errno.h>
+#include "print.h"
 #include "sdl.h"
 #include "opengl.h"
 
@@ -52,7 +53,7 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        /* init SDL library */
        rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
        if (rc < 0) {
-               fprintf(stderr, "Failed to init SDL\n");
+               print_error("Failed to init SDL\n");
                goto error;
        }
        sdl_initialized = 1;
@@ -60,7 +61,7 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        /* open window */
        gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
        if (!gl_window) {
-               fprintf(stderr, "Failed to open SDL window: %s\n", SDL_GetError());
+               print_error("Failed to open SDL window: %s\n", SDL_GetError());
                rc = EIO;
                goto error;
        }
@@ -68,44 +69,44 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        /* create GL context */
        gl_context = SDL_GL_CreateContext(gl_window);
        if (!gl_context) {
-               fprintf(stderr, "Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
+               print_error("Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
                rc = EIO;
                goto error;
        }
 
        rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
        if (rc < 0) {
-               fprintf(stderr, "Failed to set SDL's OpenGL context profile\n");
+               print_error("Failed to set SDL's OpenGL context profile\n");
                goto error;
        }
 
        rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        if (rc < 0) {
-               fprintf(stderr, "Failed to set SDL's OpenGL major version\n");
+               print_error("Failed to set SDL's OpenGL major version\n");
                goto error;
        }
        rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
        if (rc < 0) {
-               fprintf(stderr, "Failed to set SDL's OpenGL minor version\n");
+               print_error("Failed to set SDL's OpenGL minor version\n");
                goto error;
        }
 
        rc = SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        if (rc < 0) {
-               fprintf(stderr, "Failed to set SDL's OpenGL doublebuffer\n");
+               print_error("Failed to set SDL's OpenGL doublebuffer\n");
                goto error;
        }
 
        rc = SDL_GL_SetSwapInterval(1);
        if (rc < 0) {
-               fprintf(stderr, "Failed to set SDL's OpenGL swap interval to VBLANK\n");
+               print_error("Failed to set SDL's OpenGL swap interval to VBLANK\n");
                goto error;
        }
 
 #ifndef __APPLE__
        glewExperimental = GL_TRUE;
        if (glewInit() != GLEW_OK) {
-               fprintf(stderr, "Failed to init GLEW\n");
+               print_error("Failed to init GLEW\n");
                goto error;
        }
 #endif
@@ -124,15 +125,15 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        want.callback = audio_cb;
        rc = SDL_OpenAudio(&want, &have);
        if (rc < 0) {
-               fprintf(stderr, "Failed to open audio\n");
+               print_error("Failed to open audio\n");
                goto error;
        } else if (have.format != want.format) {
-               fprintf(stderr, "Failed to open audio with desired audio format\n");
+               print_error("Failed to open audio with desired audio format\n");
                SDL_CloseAudio();
                rc = -EIO;
                goto error;
        } else if (have.freq != want.freq) {
-               fprintf(stderr, "Failed to open audio with desired sample rate\n");
+               print_error("Failed to open audio with desired sample rate\n");
                SDL_CloseAudio();
                rc = -EIO;
                goto error;
index 9a6f2b1..5b6243a 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <math.h>
+#include "../libsdl/print.h"
 #include "sound.h"
 #include "../libcpu/m68kcpu.h"
 
@@ -62,17 +63,17 @@ static int start_dma(int c, struct audio_channel *chan)
        chan->end = chan->start + chan->length;
        /* check ranges */
        if (chan->length == 0) {
-               fprintf(stderr, "Sample length of channel %d is 0, please fix!\n", c);
+               print_error("Sample length of channel %d is 0, please fix!\n", c);
                chan->dma_on = 0;
                return -1;
        }
        if (chan->pointer >= 0x80000) {
-               fprintf(stderr, "Sample pointer 0x%08x of channel %d is outside memory, please fix!\n", chan->pointer, c);
+               print_error("Sample pointer 0x%08x of channel %d is outside memory, please fix!\n", chan->pointer, c);
                chan->dma_on = 0;
                return -1;
        }
        if (chan->end > 0x80000) {
-               fprintf(stderr, "Sample end (pointer 0x%08x + length 0x%x) of channel %d is outside memory, please fix!\n", chan->pointer, chan->length, c);
+               print_error("Sample end (pointer 0x%08x + length 0x%x) of channel %d is outside memory, please fix!\n", chan->pointer, chan->length, c);
                chan->dma_on = 0;
                return -1;
        }
index 516122b..c48742f 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include "../libsdl/print.h"
 #include "video.h"
 #include "../libcpu/m68kcpu.h"
 
@@ -147,7 +148,7 @@ void emul_video(uint8_t *rgb, uint8_t *memory, uint16_t render_palette[], int wi
        /* get copper list start pointer */
        copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
        if (!copperlist) {
-               fprintf(stderr, "Copper list pointer not initialized, please fix!\n");
+               print_error("Copper list pointer not initialized, please fix!\n");
                return;
        }
 
@@ -161,7 +162,7 @@ void emul_video(uint8_t *rgb, uint8_t *memory, uint16_t render_palette[], int wi
        last_line = 0;
        while (42) {
                if (++count == 100) {
-                       fprintf(stderr, "Copper list does not seem to terminate, please fix!\n");
+                       print_error("Copper list does not seem to terminate, please fix!\n");
                        return;
                }
                c1 = m68k_read_memory_16(copperlist);
@@ -202,7 +203,7 @@ void emul_video(uint8_t *rgb, uint8_t *memory, uint16_t render_palette[], int wi
                        }
                } else {
                        if ((c2 & 1)) {
-                               fprintf(stderr, "We suppport no SKIP command in copper list, please fix!\n");
+                               print_error("We suppport no SKIP command in copper list, please fix!\n");
                                continue;
                        }
                        /* WAIT */
@@ -234,8 +235,10 @@ void emul_video(uint8_t *rgb, uint8_t *memory, uint16_t render_palette[], int wi
                                printf(" %06x", bitplane[i]);
 #endif
                                if (bitplane[i] == 0 || bitplane[i] + width * height / 8 >= 0x80000) {
+#ifdef DEBUG_COPPERLIST
                                        printf("\n");
-                                       fprintf(stderr, "Bitplane %d in copper list not set or out of range, please fix!\n", i);
+#endif
+                                       print_error("Bitplane %d in copper list not set or out of range, please fix!\n", i);
                                        return;
                                }
                                bitmem[i] = memory + bitplane[i];
index 384b821..c15f23e 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 #include "../libsdl/sdl.h"
 #include "../libsdl/opengl.h"
+#include "../libsdl/print.h"
 #include "../libcpu/m68k.h"
 #include "../libcpu/execute.h"
 #include "../libvideo/video.h"
@@ -83,20 +84,20 @@ int parse_args(int argc, char *argv[])
 
        while (argc > i) {
                if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
-                       printf("Usage: %s\n", argv[0]);
-                       printf(" -s --amiga-speed original | full\n");
-                       printf("        Set speed of rendering to original Amiga or full speed.\n");
-                       printf(" -v --video-filter on | off\n");
-                       printf("        Set video filter.\n");
-                       printf(" -a --audio-filter on | off\n");
-                       printf("        Set audio filter.\n");
+                       print_info("Usage: %s\n", argv[0]);
+                       print_info(" -s --amiga-speed original | full\n");
+                       print_info("        Set speed of rendering to original Amiga or full speed.\n");
+                       print_info(" -v --video-filter on | off\n");
+                       print_info("        Set video filter.\n");
+                       print_info(" -a --audio-filter on | off\n");
+                       print_info("        Set audio filter.\n");
                        return -1;
                } else
                if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--amiga-speed")) {
                        i++;
                        if (argc == i) {
 missing_parameter:
-                               printf("Missing parameter, use '--help'!\n");
+                               print_info("Missing parameter, use '--help'!\n");
                                return -1;
                        }
                        if (!strcmp(argv[i], "original"))
@@ -106,7 +107,7 @@ missing_parameter:
                                config_amiga_speed = 0;
                        else {
 illegal_parameter:
-                               printf("Illegal parameter, use '--help'!\n");
+                               print_info("Illegal parameter, use '--help'!\n");
                                return -1;
                        }
                } else
@@ -134,7 +135,7 @@ illegal_parameter:
                        else
                                goto illegal_parameter;
                } else {
-                       printf("Illegal option '%s', use '--help'!\n", argv[i]);
+                       print_info("Illegal option '%s', use '--help'!\n", argv[i]);
                        return -1;
                }
                i++;
@@ -284,7 +285,7 @@ static int last_track = 0;
 static void disk_read(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
 {
        if (length > sizeof(game_save[0])) {
-               fprintf(stderr, "loading game state failed, because length exceeds game save data size, please fix!\n");
+               print_error("loading game state failed, because length exceeds game save data size, please fix!\n");
                return;
        }
 
@@ -306,7 +307,7 @@ static void disk_read(int track, int __attribute__((unused)) side, uint32_t data
                fp = fopen(filename, "r");
                if (!fp) {
 fail:
-                       fprintf(stderr, "failed to load game from '%s'\n", filename);
+                       print_error("failed to load game from '%s'\n", filename);
                        goto copy;
                }
                got = fread(game_save, sizeof(game_save[0]), 2, fp);
@@ -328,14 +329,14 @@ static void disk_write(int track, int __attribute__((unused)) side, uint32_t dat
        length -= 0x200;
 
        if (length != sizeof(game_save[0])) {
-               fprintf(stderr, "saving game state failed, because length of data does not match, please fix!\n");
+               print_error("saving game state failed, because length of data does not match, please fix!\n");
                return;
        }
 
        /* don't save if last track is the same, because disk is written on both sides with the same data */
        if (track == last_track) {
                if (memcmp(memory + data, game_save[track & 1], length)) {
-                       fprintf(stderr, "saving game data on other side of the disk is different, please fix!\n");
+                       print_error("saving game data on other side of the disk is different, please fix!\n");
                }
                return;
        }
@@ -361,10 +362,10 @@ static void disk_write(int track, int __attribute__((unused)) side, uint32_t dat
                fp = fopen(filename, "w");
                if (!fp) {
 fail:
-                       fprintf(stderr, "failed to save game to '%s'\n", filename);
+                       print_error("failed to save game to '%s'\n", filename);
                        return;
                }
-               printf("Game state saved to '%s'\n", filename);
+               print_info("Game state saved to '%s'\n", filename);
                wrote = fwrite(game_save, sizeof(game_save[0]), 2, fp);
                fclose(fp);
                if (wrote != 2)
@@ -384,19 +385,19 @@ static void keyboard_sdl(int down, SDL_Keycode sym)
        case SDLK_v:
                if (down && ctrl) {
                        config_video_filter ^= 1;
-                       printf("video filter: %s\n", (config_video_filter) ? "on" : "off");
+                       print_info("video filter: %s\n", (config_video_filter) ? "on" : "off");
                }
                break;
        case SDLK_a:
                if (down && ctrl) {
                        config_audio_filter ^= 1;
-                       printf("audio filter: %s\n", (config_audio_filter) ? "on" : "off");
+                       print_info("audio filter: %s\n", (config_audio_filter) ? "on" : "off");
                }
                break;
        case SDLK_s:
                if (down && ctrl) {
                        config_amiga_speed ^= 1;
-                       printf("amiga speed: %s\n", (config_amiga_speed) ? "original" : "full");
+                       print_info("amiga speed: %s\n", (config_amiga_speed) ? "original" : "full");
                }
                break;
        case SDLK_c:
@@ -571,16 +572,16 @@ int main(int argc, char *argv[])
        /* allocate image */
        image = calloc(IMAGE_WIDTH * IMAGE_HEIGHT * ((double_size) ? 4 : 1), 3);
        if (!image) {
-               fprintf(stderr, "Failed to allocate image buffer\n");
+               print_error("Failed to allocate image buffer\n");
                goto done;
        }
 
        if ((SOUND_SAMPLERATE % IRQ_RATE)) {
-               fprintf(stderr, "Sample rate must be a multiple of IRQ rate, please fix!\n");
+               print_error("Sample rate must be a multiple of IRQ rate, please fix!\n");
                goto done;
        }
        if ((1000 % IRQ_RATE)) {
-               fprintf(stderr, "1000 (Ticks per second) rate must be a multiple of IRQ rate, please fix!\n");
+               print_error("1000 (Ticks per second) rate must be a multiple of IRQ rate, please fix!\n");
                goto done;
        }
 
@@ -596,19 +597,19 @@ int main(int argc, char *argv[])
        sound_buffer_size = SOUND_SAMPLERATE / IRQ_RATE * SOUND_CHUNKS;
        sound_buffer = calloc(sound_buffer_size, sizeof(*sound_buffer));
        if (!sound_buffer) {
-               fprintf(stderr, "Failed to allocate image buffer\n");
+               print_error("Failed to allocate image buffer\n");
                goto done;
        }
 
        /* allocate memory */
        memory = calloc(MEMORY_SIZE, 1);
        if (!memory) {
-               fprintf(stderr, "Failed to allocate cpu's memory\n");
+               print_error("Failed to allocate cpu's memory\n");
                goto done;
        }
        chipreg = calloc(IOSIZE, 1);
        if (!chipreg) {
-               fprintf(stderr, "Failed to allocate chip register\n");
+               print_error("Failed to allocate chip register\n");
                goto done;
        }
 
@@ -639,16 +640,16 @@ int main(int argc, char *argv[])
        /* start cpu */
        reset_cpu();
 
-       printf("**********************************\n");
-       printf("* Welcome to Mercenary Reloaded! *\n");
-       printf("**********************************\n\n");
-       printf("Press CTRL + cursor keys to select inventory or pickup/drop item.\n");
-       printf("Press CTRL + f to toggle full screen.\n");
-       printf("Press CTRL + s to toggle rendering speed.\n");
-       printf("Press CTRL + v to toggle video filter.\n");
-       printf("Press CTRL + a to toggle audio filter.\n");
-       printf("Press CTRL + c to exit game.\n\n");
-       printf("Use '--help' as command line option for configuration settings.\n\n");
+       print_info("**********************************\n");
+       print_info("* Welcome to Mercenary Reloaded! *\n");
+       print_info("**********************************\n\n");
+       print_info("Press CTRL + cursor keys to select inventory or pickup/drop item.\n");
+       print_info("Press CTRL + f to toggle full screen.\n");
+       print_info("Press CTRL + s to toggle rendering speed.\n");
+       print_info("Press CTRL + v to toggle video filter.\n");
+       print_info("Press CTRL + a to toggle audio filter.\n");
+       print_info("Press CTRL + c to exit game.\n\n");
+       print_info("Use '--help' as command line option for configuration settings.\n\n");
 
        /* run game */
        main_loop();
index 65e970e..37d648a 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include "../libsdl/print.h"
 #include "../libcpu/m68k.h"
 #include "../libcpu/execute.h"
 #include "mercenary.h"
@@ -64,13 +65,13 @@ void mercenary_patch(void)
 
        /* reduce loop that waits for disk stepper to move */
        if (m68k_read_memory_32(0x55398) != 0x0000091b) {
-               fprintf(stderr, "expecting loop counter of 0x0000091b here, please fix!\n");
+               print_error("expecting loop counter of 0x0000091b here, please fix!\n");
                exit(0);
        }
        m68k_write_memory_32(0x55398, 1);
        /* reduce loop that waits for disk side change */
        if (m68k_read_memory_32(0x54ffc) != 0x00000d02) {
-               fprintf(stderr, "expecting loop counter of 0x00000d02 here, please fix!\n");
+               print_error("expecting loop counter of 0x00000d02 here, please fix!\n");
                exit(0);
        }
        m68k_write_memory_32(0x54ffc, 1);
index 76cb479..3ebec3f 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include "../libsdl/print.h"
 #include "../libcpu/m68k.h"
 #include "../libcpu/execute.h"
 #include "mercenary.h"
@@ -72,13 +73,13 @@ void mercenary_patch(void)
 
        /* reduce loop that waits for disk stepper to move */
        if (m68k_read_memory_32(0x562f8) != 0x000091b0) {
-               fprintf(stderr, "expecting loop counter of 0x000091b0 here, please fix!\n");
+               print_error("expecting loop counter of 0x000091b0 here, please fix!\n");
                exit(0);
        }
        m68k_write_memory_32(0x562f8, 1);
        /* reduce loop that waits for disk side change */
        if (m68k_read_memory_32(0x55f5c) != 0x0000d020) {
-               fprintf(stderr, "expecting loop counter of 0x0000d020 here, please fix!\n");
+               print_error("expecting loop counter of 0x0000d020 here, please fix!\n");
                exit(0);
        }
        m68k_write_memory_32(0x55f5c, 1);