From 9259ed8f267337f54b29c87f445b004e3ca52de0 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 10 Jul 2019 20:59:07 +0200 Subject: [PATCH] Optimize tprintf implementation It no longer uses a local buffer, so it needs less memory and no mutex. Signed-off-by: Stefan Weil --- src/ccutil/ccutil.cpp | 1 - src/ccutil/ccutil.h | 1 - src/ccutil/tprintf.cpp | 48 +++++++++++++++++++++--------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/ccutil/ccutil.cpp b/src/ccutil/ccutil.cpp index 79ea6623..bf07a2a4 100644 --- a/src/ccutil/ccutil.cpp +++ b/src/ccutil/ccutil.cpp @@ -52,5 +52,4 @@ void CCUtilMutex::Unlock() { #endif } -CCUtilMutex tprintfMutex; // should remain global } // namespace tesseract diff --git a/src/ccutil/ccutil.h b/src/ccutil/ccutil.h index 71e89c60..3dd63711 100644 --- a/src/ccutil/ccutil.h +++ b/src/ccutil/ccutil.h @@ -87,7 +87,6 @@ class CCUtil { "Use ambigs for deciding whether to adapt to a character"); }; -extern CCUtilMutex tprintfMutex; // should remain global } // namespace tesseract #endif // TESSERACT_CCUTIL_CCUTIL_H_ diff --git a/src/ccutil/tprintf.cpp b/src/ccutil/tprintf.cpp index 44cd29c9..89bea92e 100644 --- a/src/ccutil/tprintf.cpp +++ b/src/ccutil/tprintf.cpp @@ -23,7 +23,6 @@ #include #include -#include "ccutil.h" #include "params.h" #include "strngs.h" #include "tprintf.h" @@ -35,34 +34,35 @@ static STRING_VAR(debug_file, "", "File to send tprintf output to"); // Trace printf DLLSYM void tprintf(const char *format, ...) { - tesseract::tprintfMutex.Lock(); - va_list args; // variable args const char* debug_file_name = debug_file.string(); static FILE *debugfp = nullptr; // debug file - // debug window - int32_t offset = 0; // into message - char msg[MAX_MSG_LEN + 1]; - va_start(args, format); // variable list - // Format into msg - #ifdef _WIN32 - offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args); - if (debug_file_name && strcmp(debug_file_name, "/dev/null") == 0) - debug_file.set_value("nul"); - #else - offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args); - #endif - va_end(args); + if (debug_file_name == nullptr) { + // This should not happen. + return; + } - if (debugfp == nullptr && debug_file_name && strlen(debug_file_name) > 0) { - debugfp = fopen(debug_file.string(), "wb"); - } else if (debugfp != nullptr && debug_file_name && strlen(debug_file_name) == 0) { +#ifdef _WIN32 + // Replace /dev/null by nul for Windows. + if (strcmp(debug_file_name, "/dev/null") == 0) { + debug_file_name = "nul"; + debug_file.set_value(debug_file_name); + } +#endif + + if (debugfp == nullptr && debug_file_name[0] != '\0') { + debugfp = fopen(debug_file_name, "wb"); + } else if (debugfp != nullptr && debug_file_name[0] == '\0') { fclose(debugfp); debugfp = nullptr; } - if (debugfp != nullptr) - fprintf(debugfp, "%s", msg); - else - fprintf(stderr, "%s", msg); - tesseract::tprintfMutex.Unlock(); + + va_list args; // variable args + va_start(args, format); // variable list + if (debugfp != nullptr) { + vfprintf(debugfp, format, args); + } else { + vfprintf(stderr, format, args); + } + va_end(args); }