From 6b9287470d0a33033750c0e220c21b3ad4cf1cbd Mon Sep 17 00:00:00 2001 From: baldurk Date: Sat, 22 Nov 2014 23:45:44 +0000 Subject: [PATCH] Refactor log printing to use new custom printf. --- renderdoc/common/common.cpp | 58 ++++++++++++++------------- renderdoc/common/common.h | 9 +++++ renderdoc/os/os_specific.h | 4 +- renderdoc/os/win32/win32_stringio.cpp | 14 +++++++ renderdoc/os/win32_specific.h | 2 +- 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/renderdoc/common/common.cpp b/renderdoc/common/common.cpp index 02f26c1bc..28bb2a4be 100644 --- a/renderdoc/common/common.cpp +++ b/renderdoc/common/common.cpp @@ -267,6 +267,35 @@ void rdclog_flush() { } +void rdclogprint_int(const char *str) +{ + static Threading::CriticalSection lock; + + SCOPED_LOCK(lock); + +#if defined(OUTPUT_LOG_TO_DEBUG_OUT) + OSUtility::WriteOutput(OSUtility::Output_DebugMon, str); +#endif +#if defined(OUTPUT_LOG_TO_STDOUT) + OSUtility::WriteOutput(OSUtility::Output_StdOut, str); +#endif +#if defined(OUTPUT_LOG_TO_STDERR) + OSUtility::WriteOutput(OSUtility::Output_StdErr, str); +#endif +#if defined(OUTPUT_LOG_TO_DISK) + if(!logfile().empty()) + { + FILE *f = FileIO::fopen(logfile().c_str(), L"a"); + if(f) + { + // strlen used as byte length - str is UTF-8 so this is NOT number of characters + FileIO::fwrite(str, 1, strlen(str), f); + FileIO::fclose(f); + } + } +#endif +} + void rdclog_int(LogType type, const char *file, unsigned int line, const char *fmt, ...) { if(type <= RDCLog_First || type >= RDCLog_NumTypes) @@ -307,7 +336,7 @@ void rdclog_int(LogType type, const char *file, unsigned int line, const char *f char *output = outputBuffer; size_t available = outBufSize; - int numWritten = StringFormat::snprintf(output, available, "%hs %hs%hs%hs - ", name, timestamp, location, typestr[type]); + int numWritten = StringFormat::snprintf(output, available, "%s %s%s%s - ", name, timestamp, location, typestr[type]); if(numWritten < 0) { @@ -333,31 +362,6 @@ void rdclog_int(LogType type, const char *file, unsigned int line, const char *f *output = '\n'; *(output+1) = 0; - - { - static Threading::CriticalSection lock; - SCOPED_LOCK(lock); - -#if defined(OUTPUT_LOG_TO_DEBUG_OUT) - OSUtility::DebugOutputA(outputBuffer); -#endif -#if defined(OUTPUT_LOG_TO_STDOUT) - fprintf(stdout, "%hs", outputBuffer); fflush(stdout); -#endif -#if defined(OUTPUT_LOG_TO_STDERR) - fprintf(stderr, "%hs", outputBuffer); fflush(stderr); -#endif -#if defined(OUTPUT_LOG_TO_DISK) - if(!logfile().empty()) - { - FILE *f = FileIO::fopen(logfile().c_str(), L"a"); - if(f) - { - FileIO::fwrite(outputBuffer, 1, strlen(outputBuffer), f); - FileIO::fclose(f); - } - } -#endif - } + rdclogprint_int(outputBuffer); } diff --git a/renderdoc/common/common.h b/renderdoc/common/common.h index 7f0e8c79f..0b8dfd5f5 100644 --- a/renderdoc/common/common.h +++ b/renderdoc/common/common.h @@ -125,8 +125,16 @@ enum LogType #define RDCWARN(...) do { } while(0) #define RDCERR(...) do { } while(0) #define RDCFATAL(...) do { RDCDUMP(); exit(0); } while(0) +#define RDCDUMPMSG(message) do { RDCDUMP(); exit(0); } while(0) #else +// perform any operations necessary to flush the log void rdclog_flush(); + +// actual low-level print to log output streams defined (useful for if we need to print +// fatal error messages from within the more complex log function). +void rdclogprint_int(const char *str); + +// printf() style main logger function void rdclog_int(LogType type, const char *file, unsigned int line, const char *fmt, ...); #define rdclog(type, ...) rdclog_int(type, __FILE__, __LINE__, __VA_ARGS__) @@ -155,6 +163,7 @@ void rdclog_delete(); #endif #define RDCFATAL(...) do { rdclog(RDCLog_Fatal, __VA_ARGS__); rdclog_flush(); RDCDUMP(); exit(0); } while(0) +#define RDCDUMPMSG(message) do { rdclogprint_int(message); rdclog_flush(); RDCDUMP(); exit(0); } while(0) #endif // diff --git a/renderdoc/os/os_specific.h b/renderdoc/os/os_specific.h index 5b2591295..ce45135df 100644 --- a/renderdoc/os/os_specific.h +++ b/renderdoc/os/os_specific.h @@ -233,12 +233,14 @@ namespace OSUtility inline void ForceCrash(); inline void DebugBreak(); inline bool DebuggerPresent(); - inline void DebugOutputA(const char *str); + enum { Output_DebugMon, Output_StdOut, Output_StdErr }; + void WriteOutput(int channel, const char *str); }; // must #define: // __PRETTY_FUNCTION_SIGNATURE__ - undecorated function signature // GetEmbeddedResource(name_with_underscores_ext) - function/inline that returns the given file in a std::string +// OS_DEBUG_BREAK() - instruction that debugbreaks the debugger - define instead of function to preserve callstacks #ifdef RENDERDOC_PLATFORM // "win32_specific.h" (in directory os/) diff --git a/renderdoc/os/win32/win32_stringio.cpp b/renderdoc/os/win32/win32_stringio.cpp index 2c75be61a..165350cf5 100644 --- a/renderdoc/os/win32/win32_stringio.cpp +++ b/renderdoc/os/win32/win32_stringio.cpp @@ -389,3 +389,17 @@ namespace StringFormat } }; +namespace OSUtility +{ + void WriteOutput(int channel, const char *str) + { + wstring wstr = StringFormat::UTF82Wide(string(str)); + + if(channel == OSUtility::Output_DebugMon) + OutputDebugStringW(wstr.c_str()); + else if(channel == OSUtility::Output_StdOut) + fwprintf(stdout, L"%ls", wstr.c_str()); + else if(channel == OSUtility::Output_StdErr) + fwprintf(stderr, L"%ls", wstr.c_str()); + } +}; diff --git a/renderdoc/os/win32_specific.h b/renderdoc/os/win32_specific.h index 4300aa8f8..9d70d9371 100644 --- a/renderdoc/os/win32_specific.h +++ b/renderdoc/os/win32_specific.h @@ -40,7 +40,7 @@ namespace OSUtility inline void ForceCrash() { *((int *)NULL) = 0; } inline void DebugBreak() { __debugbreak(); } inline bool DebuggerPresent() { return ::IsDebuggerPresent() == TRUE; } - inline void DebugOutputA(const char *str) { OutputDebugStringA(str); } + void WriteOutput(int channel, const char *str); }; namespace Threading