Skip to content

Commit

Permalink
Optimize tprintf implementation
Browse files Browse the repository at this point in the history
It no longer uses a local buffer, so it needs less memory
and no mutex.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Jul 10, 2019
1 parent 2aebd10 commit 9259ed8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
1 change: 0 additions & 1 deletion src/ccutil/ccutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ void CCUtilMutex::Unlock() {
#endif
}

CCUtilMutex tprintfMutex; // should remain global
} // namespace tesseract
1 change: 0 additions & 1 deletion src/ccutil/ccutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
48 changes: 24 additions & 24 deletions src/ccutil/tprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include <cstdio>
#include <cstdarg>
#include "ccutil.h"
#include "params.h"
#include "strngs.h"
#include "tprintf.h"
Expand All @@ -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;
}

#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 && strlen(debug_file_name) > 0) {
debugfp = fopen(debug_file.string(), "wb");
} else if (debugfp != nullptr && debug_file_name && strlen(debug_file_name) == 0) {
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);
}

0 comments on commit 9259ed8

Please sign in to comment.