From 6affc4572eba0e83e734c85a71f7392501b996c1 Mon Sep 17 00:00:00 2001 From: meancoot Date: Fri, 24 May 2013 18:16:44 -0400 Subject: [PATCH] (iOS) If the token HAVE_DEBUG_DIAGLOG is defined at build time all messages sent to stdout or stderr will appear in the Diagnostic Log. --- ios/RetroArch/RALogView.m | 49 +++++++++++++++++++++++++++++++++++ ios/RetroArch/main.m | 4 +++ ios/RetroArch/rarch_wrapper.h | 9 ++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/ios/RetroArch/RALogView.m b/ios/RetroArch/RALogView.m index 41597272ca..22482a555d 100644 --- a/ios/RetroArch/RALogView.m +++ b/ios/RetroArch/RALogView.m @@ -20,6 +20,7 @@ static NSMutableArray* g_messages; static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_DEBUG_DIAGLOG void ios_add_log_message(const char* format, ...) { pthread_mutex_lock(&g_lock); @@ -36,6 +37,54 @@ void ios_add_log_message(const char* format, ...) pthread_mutex_unlock(&g_lock); } +#else +static FILE* old_stdout; +static FILE* old_stderr; +static FILE* log_stream; + +static int stdout_write(void* mem_buffer, const char* data, int len) +{ + pthread_mutex_lock(&g_lock); + + g_messages = g_messages ? g_messages : [NSMutableArray array]; + + char buffer[len + 10]; + strncpy(buffer, data, len); + buffer[len] = 0; + + [g_messages addObject:[NSString stringWithFormat:@"%s", buffer]]; + + pthread_mutex_unlock(&g_lock); + + return len; +} + +void ios_log_init() +{ + if (!log_stream) + { + old_stdout = stdout; + old_stderr = stderr; + + log_stream = fwopen(0, stdout_write); + setvbuf(log_stream, 0, _IOLBF, 0); + + stdout = log_stream; + stderr = log_stream; + } +} + +void ios_log_quit() +{ + if (log_stream) + { + stdout = old_stdout; + stderr = old_stderr; + fclose(log_stream); + log_stream = 0; + } +} +#endif @implementation RALogView diff --git a/ios/RetroArch/main.m b/ios/RetroArch/main.m index 8b70fd46a8..5c592fdfea 100644 --- a/ios/RetroArch/main.m +++ b/ios/RetroArch/main.m @@ -231,6 +231,10 @@ static void event_reload_config(void* userdata) // UIApplicationDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { +#ifdef HAVE_DEBUG_DIAGLOG + ios_log_init(); +#endif + self.system_directory = [NSString stringWithFormat:@"%@/.RetroArch", kDOCSFOLDER]; self.systemConfigPath = [NSString stringWithFormat:@"%@/.RetroArch/frontend.cfg", kDOCSFOLDER]; mkdir([self.system_directory UTF8String], 0755); diff --git a/ios/RetroArch/rarch_wrapper.h b/ios/RetroArch/rarch_wrapper.h index fd26915a21..67f7f894f5 100644 --- a/ios/RetroArch/rarch_wrapper.h +++ b/ios/RetroArch/rarch_wrapper.h @@ -30,6 +30,13 @@ void ios_set_game_view_sync(unsigned interval); void ios_get_game_view_size(unsigned *width, unsigned *height); void ios_bind_game_view_fbo(); -// Thread safe +#ifndef HAVE_DEBUG_DIAGLOG void ios_add_log_message(const char* format, ...); +#else +void ios_log_init(); +void ios_log_quit(); +// Thread safe +#define ios_add_log_message(...) do { printf(__VA_ARGS__); printf("\n"); } while(0) +#endif + #endif