tesseract  4.1.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 <cstdio>
25 #include <cstdarg>
26 #include "ccutil.h"
27 #include "params.h"
28 #include "strngs.h"
29 #include "tprintf.h"
30 
31 #define MAX_MSG_LEN 2048
32 
33 static STRING_VAR(debug_file, "", "File to send tprintf output to");
34 
35 // Trace printf
36 DLLSYM void tprintf(const char *format, ...)
37 {
39  va_list args; // variable args
40  const char* debug_file_name = debug_file.string();
41  static FILE *debugfp = nullptr; // debug file
42  // debug window
43  int32_t offset = 0; // into message
44  char msg[MAX_MSG_LEN + 1];
45 
46  va_start(args, format); // variable list
47  // Format into msg
48  #ifdef _WIN32
49  offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
50  if (debug_file_name && strcmp(debug_file_name, "/dev/null") == 0)
51  debug_file.set_value("nul");
52  #else
53  offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
54  #endif
55  va_end(args);
56 
57  if (debugfp == nullptr && debug_file_name && strlen(debug_file_name) > 0) {
58  debugfp = fopen(debug_file.string(), "wb");
59  } else if (debugfp != nullptr && debug_file_name && strlen(debug_file_name) == 0) {
60  fclose(debugfp);
61  debugfp = nullptr;
62  }
63  if (debugfp != nullptr)
64  fprintf(debugfp, "%s", msg);
65  else
66  fprintf(stderr, "%s", msg);
68 }
#define MAX_MSG_LEN
Definition: tprintf.cpp:31
CCUtilMutex tprintfMutex
Definition: ccutil.cpp:55
#define STRING_VAR(name, val, comment)
Definition: params.h:309
#define DLLSYM
Definition: platform.h:21
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36