Skip to content

Update code for tprintf #4306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions src/ccutil/tprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,49 @@

namespace tesseract {

#define MAX_MSG_LEN 2048

INT_VAR(log_level, INT_MAX, "Logging level");

static STRING_VAR(debug_file, "", "File to send tprintf output to");

// Trace printf
void tprintf(const char *format, ...) {
const char *debug_file_name = debug_file.c_str();
static FILE *debugfp = nullptr; // debug file

if (debug_file_name == nullptr) {
// This should not happen.
return;
}
// File for debug output.
static FILE *debugfp;

// Set output for log messages.
// The output is written to stderr if debug_file is empty.
// Otherwise it is written to debug_file.
// It is possible to switch between stderr and debug_file output:
// tprintf("write to configured output\n");
// debug_file = "";
// tprintf("write to stderr\n");
// debug_file = "/tmp/log";
// tprintf("write to /tmp/log\n");
// debug_file = "";
// tprintf("write to stderr\n");
Comment on lines +44 to +51
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether such use cases exist. We could simplify the code further if we only allow setting the desired output once before the first use of tprintf.

static void set_debugfp() {
if (debug_file.empty()) {
// Write to stderr.
if (debugfp != stderr && debugfp != nullptr) {
fclose(debugfp);
}
debugfp = stderr;
} else if (debugfp == stderr || debugfp == nullptr) {
// Write to file.
#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);
}
if (debug_file == "/dev/null") {
// Replace /dev/null by nul for Windows.
debug_file = "nul";
}
#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;
debugfp = fopen(debug_file.c_str(), "wb");
}
}

// Trace printf.
void tprintf(const char *format, ...) {
set_debugfp();
va_list args; // variable args
va_start(args, format); // variable list
if (debugfp != nullptr) {
vfprintf(debugfp, format, args);
} else {
vfprintf(stderr, format, args);
}
vfprintf(debugfp, format, args);
va_end(args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ccutil/tprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef TESSERACT_CCUTIL_TPRINTF_H
#define TESSERACT_CCUTIL_TPRINTF_H

#include "params.h" // for BOOL_VAR_H
#include "params.h" // for INT_VAR_H
#include <tesseract/export.h> // for TESS_API

namespace tesseract {
Expand Down
Loading