diff --git a/renderdoc/common/common.cpp b/renderdoc/common/common.cpp index d2b1ec7b1..33f0753ed 100644 --- a/renderdoc/common/common.cpp +++ b/renderdoc/common/common.cpp @@ -335,7 +335,16 @@ void rdclogprint_int(LogType type, const char *fullMsg, const char *msg) } const int rdclog_outBufSize = 4 * 1024; -static char rdclog_outputBuffer[rdclog_outBufSize + 1]; +static char rdclog_outputBuffer[rdclog_outBufSize + 3]; + +static void write_newline(char *output) +{ +#if ENABLED(RDOC_WIN32) + *(output++) = '\r'; +#endif + *(output++) = '\n'; + *output = 0; +} void rdclog_int(LogType type, const char *project, const char *file, unsigned int line, const char *fmt, ...) @@ -413,7 +422,7 @@ void rdclog_int(LogType type, const char *project, const char *file, unsigned in if(totalWritten > rdclog_outBufSize) { available = totalWritten + 3; - oversizedBuffer = output = new char[available]; + oversizedBuffer = output = new char[available + 3]; base = output; numWritten = StringFormat::snprintf(output, available, "% 4s %06u: %s%s%s - ", project, @@ -432,32 +441,33 @@ void rdclog_int(LogType type, const char *project, const char *file, unsigned in va_end(args2); } - *output = 0; - - const char newline[] = -#if ENABLED(RDOC_WIN32) - "\r\n"; -#else - "\n"; -#endif - // likely path - string contains no newlines char *nl = strchr(base, '\n'); if(nl == NULL) { - rdclogprint_int(type, base, noPrefixOutput); + // append newline + write_newline(output); - // print a newline automatically - rdclogprint_int(type, newline, newline); + rdclogprint_int(type, base, noPrefixOutput); } else { + char backup[2]; + // otherwise, print the string in sections to ensure newlines are in native format while(nl) { - *nl = 0; + // backup the two characters after the \n, to allow for DOS newlines ('\r' '\n' '\0') + backup[0] = nl[1]; + backup[1] = nl[2]; + + write_newline(nl); + rdclogprint_int(type, base, noPrefixOutput); - rdclogprint_int(type, newline, newline); + + // restore the characters + nl[1] = backup[0]; + nl[2] = backup[1]; base = nl + 1; noPrefixOutput = nl + 1; @@ -465,10 +475,10 @@ void rdclog_int(LogType type, const char *project, const char *file, unsigned in nl = strchr(base, '\n'); } - // there will be the remainder up to the final non-trailing newline, print it + // append final newline and write the last line + write_newline(output); rdclogprint_int(type, base, noPrefixOutput); - rdclogprint_int(type, newline, newline); } SAFE_DELETE_ARRAY(oversizedBuffer);