changeset 1792:52a47611a273

Avoid printing a bunch of junk to stdout when GDB remote debugging is enabled as this can confuse GDB
author Michael Pavone <pavone@retrodev.com>
date Wed, 20 Mar 2019 22:05:27 -0700
parents 1843823f1e9b
children c9695252eff4
files gdb_remote.c io.c render_sdl.c romdb.c util.c util.h
diffstat 6 files changed, 53 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/gdb_remote.c	Wed Mar 20 21:36:32 2019 -0700
+++ b/gdb_remote.c	Wed Mar 20 22:05:27 2019 -0700
@@ -596,5 +596,7 @@
 		fatal_error("accept returned an error while listening on GDB remote debugging socket");
 	}
 	closesocket(listen_sock);
+#else
+	disable_stdout_messages();
 #endif
 }
--- a/io.c	Wed Mar 20 21:36:32 2019 -0700
+++ b/io.c	Wed Mar 20 22:05:27 2019 -0700
@@ -335,7 +335,7 @@
 				warning("IO port %s is configured to use the sega parallel board, but no paralell_pipe is set!\n", io_name(i));
 				ports[i].device_type = IO_NONE;
 			} else {
-				printf("IO port: %s connected to device '%s' with pipe name: %s\n", io_name(i), device_type_names[ports[i].device_type], pipe_name);
+				debug_message("IO port: %s connected to device '%s' with pipe name: %s\n", io_name(i), device_type_names[ports[i].device_type], pipe_name);
 				if (!strcmp("stdin", pipe_name))
 				{
 					ports[i].device.stream.data_fd = STDIN_FILENO;
@@ -361,7 +361,7 @@
 				warning("IO port %s is configured to use generic IO, but no socket is set!\n", io_name(i));
 				ports[i].device_type = IO_NONE;
 			} else {
-				printf("IO port: %s connected to device '%s' with socket name: %s\n", io_name(i), device_type_names[ports[i].device_type], sock_name);
+				debug_message("IO port: %s connected to device '%s' with socket name: %s\n", io_name(i), device_type_names[ports[i].device_type], sock_name);
 				ports[i].device.stream.data_fd = -1;
 				ports[i].device.stream.listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
 				size_t pathlen = strlen(sock_name);
@@ -391,9 +391,9 @@
 		} else
 #endif
 		if (ports[i].device_type == IO_GAMEPAD3 || ports[i].device_type == IO_GAMEPAD6 || ports[i].device_type == IO_GAMEPAD2) {
-			printf("IO port %s connected to gamepad #%d with type '%s'\n", io_name(i), ports[i].device.pad.gamepad_num, device_type_names[ports[i].device_type]);
+			debug_message("IO port %s connected to gamepad #%d with type '%s'\n", io_name(i), ports[i].device.pad.gamepad_num, device_type_names[ports[i].device_type]);
 		} else {
-			printf("IO port %s connected to device '%s'\n", io_name(i), device_type_names[ports[i].device_type]);
+			debug_message("IO port %s connected to device '%s'\n", io_name(i), device_type_names[ports[i].device_type]);
 		}
 	}
 }
@@ -471,7 +471,7 @@
 {
 	if (port->device.stream.data_fd == -1)
 	{
-		puts("Waiting for socket connection...");
+		debug_message("Waiting for socket connection...");
 		port->device.stream.data_fd = accept(port->device.stream.listen_fd, NULL, NULL);
 		fcntl(port->device.stream.data_fd, F_SETFL, O_NONBLOCK | O_RDWR);
 	}
--- a/render_sdl.c	Wed Mar 20 21:36:32 2019 -0700
+++ b/render_sdl.c	Wed Mar 20 22:05:27 2019 -0700
@@ -112,13 +112,12 @@
 	}
 	
 	if (cur != end) {
-		printf("Underflow of %d samples, read_start: %d, read_end: %d, mask: %X\n", (int)(end-cur)/2, audio->read_start, audio->read_end, audio->mask);
+		debug_message("Underflow of %d samples, read_start: %d, read_end: %d, mask: %X\n", (int)(end-cur)/2, audio->read_start, audio->read_end, audio->mask);
 	}
 	if (!sync_to_audio) {
 		audio->read_start = i;
 	}
 	if (cur != end) {
-		//printf("Underflow of %d samples, read_start: %d, read_end: %d, mask: %X\n", (int)(end-cur)/2, audio->read_start, audio->read_end, audio->mask);
 		return (cur-end)/2;
 	} else {
 		return ((i_end - i) & audio->mask) / audio->num_channels;
@@ -158,7 +157,7 @@
 		audio->read_start = i;
 	}
 	if (cur != end) {
-		printf("Underflow of %d samples, read_start: %d, read_end: %d, mask: %X\n", (int)(end-cur)/2, audio->read_start, audio->read_end, audio->mask);
+		debug_message("Underflow of %d samples, read_start: %d, read_end: %d, mask: %X\n", (int)(end-cur)/2, audio->read_start, audio->read_end, audio->mask);
 		return (cur-end)/2;
 	} else {
 		return ((i_end - i) & audio->mask) / audio->num_channels;
@@ -922,8 +921,8 @@
 				SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
 				joystick_sdl_index[index] = event->jdevice.which;
 				if (joy) {
-					printf("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
-					printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
+					debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
+					debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
 					handle_joy_added(index);
 				}
 			}
@@ -934,9 +933,9 @@
 		if (index >= 0) {
 			SDL_JoystickClose(joysticks[index]);
 			joysticks[index] = NULL;
-			printf("Joystick %d removed\n", index);
+			debug_message("Joystick %d removed\n", index);
 		} else {
-			printf("Failed to find removed joystick with instance ID: %d\n", index);
+			debug_message("Failed to find removed joystick with instance ID: %d\n", index);
 		}
 		break;
 	}
@@ -1033,7 +1032,7 @@
    	if (!samples) {
    		samples = 512;
    	}
-    printf("config says: %d\n", samples);
+    debug_message("config says: %d\n", samples);
     desired.samples = samples*2;
 	desired.callback = sync_to_audio ? audio_callback : audio_callback_drc;
 	desired.userdata = NULL;
@@ -1044,15 +1043,15 @@
 	buffer_samples = actual.samples;
 	sample_rate = actual.freq;
 	output_channels = actual.channels;
-	printf("Initialized audio at frequency %d with a %d sample buffer, ", actual.freq, actual.samples);
+	debug_message("Initialized audio at frequency %d with a %d sample buffer, ", actual.freq, actual.samples);
 	if (actual.format == AUDIO_S16SYS) {
-		puts("signed 16-bit int format");
+		debug_message("signed 16-bit int format");
 		mix = mix_s16;
 	} else if (actual.format == AUDIO_F32SYS) {
-		puts("32-bit float format");
+		debug_message("32-bit float format");
 		mix = mix_f32;
 	} else {
-		printf("unsupported format %X\n", actual.format);
+		debug_message("unsupported format %X\n", actual.format);
 		warning("Unsupported audio sample format: %X\n", actual.format);
 		mix = mix_null;
 	}
@@ -1174,7 +1173,7 @@
 		}
 		SDL_RendererInfo rinfo;
 		SDL_GetRendererInfo(main_renderer, &rinfo);
-		printf("SDL2 Render Driver: %s\n", rinfo.name);
+		debug_message("SDL2 Render Driver: %s\n", rinfo.name);
 		main_clip.x = main_clip.y = 0;
 		main_clip.w = main_width;
 		main_clip.h = main_height;
@@ -1183,7 +1182,7 @@
 #endif
 
 	SDL_GetWindowSize(main_window, &main_width, &main_height);
-	printf("Window created with size: %d x %d\n", main_width, main_height);
+	debug_message("Window created with size: %d x %d\n", main_width, main_height);
 	update_aspect();
 	render_alloc_surfaces();
 	def.ptrval = "off";
@@ -1200,7 +1199,7 @@
 		float aspect = config_aspect() > 0.0f ? config_aspect() : 4.0f/3.0f;
 		height = ((float)width / aspect) + 0.5f;
 	}
-	printf("width: %d, height: %d\n", width, height);
+	debug_message("width: %d, height: %d\n", width, height);
 	windowed_width = width;
 	windowed_height = height;
 	
@@ -1233,7 +1232,7 @@
 	if (db_data) {
 		int added = SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem(db_data, db_size), 1);
 		free(db_data);
-		printf("Added %d game controller mappings from gamecontrollerdb.txt\n", added);
+		debug_message("Added %d game controller mappings from gamecontrollerdb.txt\n", added);
 	}
 	
 	controller_add_mappings();
@@ -1386,7 +1385,7 @@
 	max_repeat++;
 	min_buffered = (((float)max_repeat * (float)sample_rate/(float)source_hz)/* / (float)buffer_samples*/);// + 0.9999;
 	//min_buffered *= buffer_samples;
-	printf("Min samples buffered before audio start: %d\n", min_buffered);
+	debug_message("Min samples buffered before audio start: %d\n", min_buffered);
 	max_adjust = BASE_MAX_ADJUST / source_hz;
 }
 
--- a/romdb.c	Wed Mar 20 21:36:32 2019 -0700
+++ b/romdb.c	Wed Mar 20 22:05:27 2019 -0700
@@ -560,7 +560,7 @@
 	if (bits_write) {
 		tern_foreach(bits_write, eeprom_write_fun, eep_map);
 	}
-	printf("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask);
+	debug_message("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask);
 	state->info->num_eeprom++;
 }
 
@@ -847,18 +847,18 @@
 		product_id[i] = rom[GAME_ID_OFF + i];
 
 	}
-	printf("Product ID: %s\n", product_id);
+	debug_message("Product ID: %s\n", product_id);
 	uint8_t raw_hash[20];
 	sha1(vrom, rom_size, raw_hash);
 	uint8_t hex_hash[41];
 	bin_to_hex(hex_hash, raw_hash, 20);
-	printf("SHA1: %s\n", hex_hash);
+	debug_message("SHA1: %s\n", hex_hash);
 	tern_node * entry = tern_find_node(rom_db, hex_hash);
 	if (!entry) {
 		entry = tern_find_node(rom_db, product_id);
 	}
 	if (!entry) {
-		puts("Not found in ROM DB, examining header\n");
+		debug_message("Not found in ROM DB, examining header\n");
 		if (xband_detect(rom, rom_size)) {
 			return xband_configure_rom(rom_db, rom, rom_size, lock_on, lock_on_size, base_map, base_chunks);
 		}
@@ -871,7 +871,7 @@
 	info.mapper_type = MAPPER_NONE;
 	info.name = tern_find_ptr(entry, "name");
 	if (info.name) {
-		printf("Found name: %s\n", info.name);
+		debug_message("Found name: %s\n", info.name);
 		info.name = strdup(info.name);
 	} else {
 		info.name = get_header_name(rom);
--- a/util.c	Wed Mar 20 21:36:32 2019 -0700
+++ b/util.c	Wed Mar 20 22:05:27 2019 -0700
@@ -477,13 +477,16 @@
 	va_end(args);
 }
 
+static uint8_t output_enabled = 1;
 void info_message(char *format, ...)
 {
 	va_list args;
 	va_start(args, format);
 #ifndef _WIN32
 	if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
-		info_printf(format, args);
+		if (output_enabled) {
+			info_printf(format, args);
+		}
 	} else {
 #endif
 		int32_t size = strlen(format) * 2;
@@ -503,7 +506,9 @@
 			va_start(args, format);
 			vsnprintf(buf, actual, format, args);
 		}
-		info_puts(buf);
+		if (output_enabled) {
+			info_puts(buf);
+		}
 		render_infobox("BlastEm Info", buf);
 		free(buf);
 #ifndef _WIN32
@@ -512,6 +517,20 @@
 	va_end(args);
 }
 
+void debug_message(char *format, ...)
+{
+	va_list args;
+	va_start(args, format);
+	if (output_enabled) {
+		info_printf(format, args);
+	}
+}
+
+void disable_stdout_messages(void)
+{
+	output_enabled = 0;
+}
+
 #ifdef _WIN32
 #include <windows.h>
 #include <shlobj.h>
--- a/util.h	Wed Mar 20 21:36:32 2019 -0700
+++ b/util.h	Wed Mar 20 22:05:27 2019 -0700
@@ -84,5 +84,9 @@
 void info_message(char *format, ...);
 //Prints an information message to stderr and to a message box if not in headless mode and not attached to a console
 void warning(char *format, ...);
+//Prints a debug message to stdout
+void debug_message(char *format, ...);
+//Disables output of info and debug messages to stdout
+void disable_stdout_messages(void);
 
 #endif //UTIL_H_