2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
2017-07-03 05:35:47 +08:00
|
|
|
* File: tprintf.cpp
|
2007-03-08 04:03:40 +08:00
|
|
|
* Description: Trace version of printf - portable between UX and NT
|
|
|
|
* Author: Phil Cheatle
|
|
|
|
* Created: Wed Jun 28 15:01:15 BST 1995
|
|
|
|
*
|
|
|
|
* (C) Copyright 1995, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2010-05-28 20:03:45 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
// Include automatically generated configuration file if running autoconf.
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2013-09-23 23:26:50 +08:00
|
|
|
#include "ccutil.h"
|
2010-11-24 02:34:14 +08:00
|
|
|
#include "params.h"
|
2013-09-23 23:26:50 +08:00
|
|
|
#include "strngs.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
#include "tprintf.h"
|
|
|
|
|
2012-02-02 11:14:43 +08:00
|
|
|
#define MAX_MSG_LEN 65536
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#define EXTERN
|
2013-09-23 23:26:50 +08:00
|
|
|
// Since tprintf is protected by a mutex, these parameters can remain global.
|
2011-08-19 00:45:01 +08:00
|
|
|
DLLSYM STRING_VAR(debug_file, "", "File to send tprintf output to");
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
DLLSYM void
|
2013-09-23 23:26:50 +08:00
|
|
|
tprintf_internal( // Trace printf
|
|
|
|
const char *format, ... // Message
|
2007-03-08 04:03:40 +08:00
|
|
|
) {
|
2009-07-11 10:50:24 +08:00
|
|
|
tesseract::tprintfMutex.Lock();
|
2013-09-23 23:26:50 +08:00
|
|
|
va_list args; // variable args
|
2016-12-13 00:20:28 +08:00
|
|
|
static FILE *debugfp = nullptr; // debug file
|
2013-09-23 23:26:50 +08:00
|
|
|
// debug window
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
int32_t offset = 0; // into message
|
2007-03-08 04:03:40 +08:00
|
|
|
static char msg[MAX_MSG_LEN + 1];
|
|
|
|
|
2013-09-23 23:26:50 +08:00
|
|
|
va_start(args, format); // variable list
|
|
|
|
// Format into msg
|
2012-02-20 05:48:45 +08:00
|
|
|
#ifdef _WIN32
|
2013-09-23 23:26:50 +08:00
|
|
|
offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
|
2012-03-03 19:48:59 +08:00
|
|
|
if (strcmp(debug_file.string(), "/dev/null") == 0)
|
2012-10-09 08:39:54 +08:00
|
|
|
debug_file.set_value("nul");
|
2007-03-08 04:03:40 +08:00
|
|
|
#else
|
2013-09-23 23:26:50 +08:00
|
|
|
offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
|
2007-03-08 04:03:40 +08:00
|
|
|
#endif
|
|
|
|
va_end(args);
|
|
|
|
|
2016-12-13 00:20:28 +08:00
|
|
|
if (debugfp == nullptr && strlen(debug_file.string()) > 0) {
|
2013-09-23 23:26:50 +08:00
|
|
|
debugfp = fopen(debug_file.string(), "wb");
|
2016-12-13 00:20:28 +08:00
|
|
|
} else if (debugfp != nullptr && strlen(debug_file.string()) == 0) {
|
2007-03-08 04:03:40 +08:00
|
|
|
fclose(debugfp);
|
2016-12-13 00:20:28 +08:00
|
|
|
debugfp = nullptr;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2016-12-13 00:20:28 +08:00
|
|
|
if (debugfp != nullptr)
|
2011-08-19 00:45:01 +08:00
|
|
|
fprintf(debugfp, "%s", msg);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s", msg);
|
2009-07-11 10:50:24 +08:00
|
|
|
tesseract::tprintfMutex.Unlock();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|