From f917f612220b4e424ac7dfd05945460b2af5ed14 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 10 May 2013 13:16:18 +0100 Subject: [PATCH] os: Prevent app from redirecting log messages. This happens e.g., with SurgeonSimulator2013 --- common/os_posix.cpp | 12 ++++++++++-- common/trace_writer_local.cpp | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/os_posix.cpp b/common/os_posix.cpp index bd15bba..5ec8b36 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -173,8 +173,16 @@ log(const char *format, ...) #ifdef ANDROID __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap); #else - vfprintf(stderr, format, ap); - fflush(stderr); + static FILE *log = NULL; + if (!log) { + // Duplicate stderr file descriptor, to prevent applications from + // redirecting our debug messages to somewhere else. + // + // Another alternative would be to log to /dev/tty when available. + log = fdopen(dup(STDERR_FILENO), "at"); + } + vfprintf(log, format, ap); + fflush(log); #endif va_end(ap); logging = false; diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index 4d946f5..d2ff3b5 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -63,6 +63,8 @@ static void exceptionCallback(void) LocalWriter::LocalWriter() : acquired(0) { + os::log("apitrace: loaded\n"); + // Install the signal handlers as early as possible, to prevent // interfering with the application's signal handling. os::setExceptionCallback(exceptionCallback); -- 2.45.2