/src/tesseract/src/ccutil/tprintf.cpp
Line | Count | Source (jump to first uncovered line) |
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 "tesserrstream.h" |
25 | | #include "tprintf.h" |
26 | | |
27 | | #include "params.h" |
28 | | |
29 | | #include <climits> // for INT_MAX |
30 | | #include <cstdarg> |
31 | | #include <cstdio> |
32 | | |
33 | | namespace tesseract { |
34 | | |
35 | | INT_VAR(log_level, INT_MAX, "Logging level"); |
36 | | |
37 | | static STRING_VAR(debug_file, "", "File to send tprintf output to"); |
38 | | |
39 | | // File for debug output. |
40 | | FILE *debugfp; |
41 | | |
42 | | // Set output for log messages. |
43 | | // The output is written to stderr if debug_file is empty. |
44 | | // Otherwise it is written to debug_file. |
45 | | // It is possible to switch between stderr and debug_file output: |
46 | | // tprintf("write to configured output\n"); |
47 | | // debug_file = ""; |
48 | | // tprintf("write to stderr\n"); |
49 | | // debug_file = "/tmp/log"; |
50 | | // tprintf("write to /tmp/log\n"); |
51 | | // debug_file = ""; |
52 | | // tprintf("write to stderr\n"); |
53 | 5.05k | FILE *get_debugfp() { |
54 | 5.05k | if (debug_file.empty()) { |
55 | | // Write to stderr. |
56 | 0 | if (debugfp != stderr && debugfp != nullptr) { |
57 | 0 | fclose(debugfp); |
58 | 0 | } |
59 | 0 | debugfp = stderr; |
60 | 5.05k | } else if (debugfp == stderr || debugfp == nullptr) { |
61 | | // Write to file. |
62 | | #ifdef _WIN32 |
63 | | if (debug_file == "/dev/null") { |
64 | | // Replace /dev/null by nul for Windows. |
65 | | debug_file = "nul"; |
66 | | } |
67 | | #endif |
68 | 4 | debugfp = fopen(debug_file.c_str(), "wb"); |
69 | 4 | } |
70 | 5.05k | return debugfp; |
71 | 5.05k | } |
72 | | |
73 | | TessErrStream tesserr; |
74 | | |
75 | | } // namespace tesseract |