tesseract  5.0.0-alpha-619-ge9db
tprintf.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: tprintf.cpp
3  * Description: Trace version of printf - portable between UX and NT
4  * Author: Phil Cheatle
5  *
6  * (C) Copyright 1995, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 // Include automatically generated configuration file if running autoconf.
20 #ifdef HAVE_CONFIG_H
21 #include "config_auto.h"
22 #endif
23 
24 #include <cstdio>
25 #include <cstdarg>
26 #include "params.h"
27 #include <tesseract/strngs.h>
28 #include "tprintf.h"
29 
30 #define MAX_MSG_LEN 2048
31 
32 static STRING_VAR(debug_file, "", "File to send tprintf output to");
33 
34 // Trace printf
35 DLLSYM void tprintf(const char *format, ...)
36 {
37  const char* debug_file_name = debug_file.c_str();
38  static FILE *debugfp = nullptr; // debug file
39 
40  if (debug_file_name == nullptr) {
41  // This should not happen.
42  return;
43  }
44 
45 #ifdef _WIN32
46  // Replace /dev/null by nul for Windows.
47  if (strcmp(debug_file_name, "/dev/null") == 0) {
48  debug_file_name = "nul";
49  debug_file.set_value(debug_file_name);
50  }
51 #endif
52 
53  if (debugfp == nullptr && debug_file_name[0] != '\0') {
54  debugfp = fopen(debug_file_name, "wb");
55  } else if (debugfp != nullptr && debug_file_name[0] == '\0') {
56  fclose(debugfp);
57  debugfp = nullptr;
58  }
59 
60  va_list args; // variable args
61  va_start(args, format); // variable list
62  if (debugfp != nullptr) {
63  vfprintf(debugfp, format, args);
64  } else {
65  vfprintf(stderr, format, args);
66  }
67  va_end(args);
68 }
strngs.h
params.h
STRING_VAR
#define STRING_VAR(name, val, comment)
Definition: params.h:306
DLLSYM
#define DLLSYM
Definition: platform.h:21
tprintf.h
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34