tesseract  4.0.0-1-g2a2b
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  * Created: Wed Jun 28 15:01:15 BST 1995
6  *
7  * (C) Copyright 1995, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 // Include automatically generated configuration file if running autoconf.
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #include <cstdio>
26 #include <cstdarg>
27 #include "ccutil.h"
28 #include "params.h"
29 #include "strngs.h"
30 #include "tprintf.h"
31 
32 #define MAX_MSG_LEN 65536
33 
34 static STRING_VAR(debug_file, "", "File to send tprintf output to");
35 
36 // Trace printf
37 DLLSYM void tprintf(const char *format, ...)
38 {
40  va_list args; // variable args
41  static FILE *debugfp = nullptr; // debug file
42  // debug window
43  int32_t offset = 0; // into message
44  static 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 (strcmp(debug_file.string(), "/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 && strlen(debug_file.string()) > 0) {
58  debugfp = fopen(debug_file.string(), "wb");
59  } else if (debugfp != nullptr && strlen(debug_file.string()) == 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:32
#define STRING_VAR(name, val, comment)
Definition: params.h:282
CCUtilMutex tprintfMutex
Definition: ccutil.cpp:59
#define DLLSYM
Definition: platform.h:23
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37