tesseract  5.0.0
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 "tprintf.h"
25 
26 #include "params.h"
27 
28 #include <climits> // for INT_MAX
29 #include <cstdarg>
30 #include <cstdio>
31 
32 namespace tesseract {
33 
34 #define MAX_MSG_LEN 2048
35 
36 INT_VAR(log_level, INT_MAX, "Logging level");
37 
38 static STRING_VAR(debug_file, "", "File to send tprintf output to");
39 
40 // Trace printf
41 void tprintf(const char *format, ...) {
42  const char *debug_file_name = debug_file.c_str();
43  static FILE *debugfp = nullptr; // debug file
44 
45  if (debug_file_name == nullptr) {
46  // This should not happen.
47  return;
48  }
49 
50 #ifdef _WIN32
51  // Replace /dev/null by nul for Windows.
52  if (strcmp(debug_file_name, "/dev/null") == 0) {
53  debug_file_name = "nul";
54  debug_file.set_value(debug_file_name);
55  }
56 #endif
57 
58  if (debugfp == nullptr && debug_file_name[0] != '\0') {
59  debugfp = fopen(debug_file_name, "wb");
60  } else if (debugfp != nullptr && debug_file_name[0] == '\0') {
61  fclose(debugfp);
62  debugfp = nullptr;
63  }
64 
65  va_list args; // variable args
66  va_start(args, format); // variable list
67  if (debugfp != nullptr) {
68  vfprintf(debugfp, format, args);
69  } else {
70  vfprintf(stderr, format, args);
71  }
72  va_end(args);
73 }
74 
75 } // namespace tesseract
#define INT_VAR(name, val, comment)
Definition: params.h:356
#define STRING_VAR(name, val, comment)
Definition: params.h:362
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int log_level
Definition: tprintf.cpp:36