tesseract
3.03
|
00001 /********************************************************************** 00002 * File: tprintf.c 00003 * Description: Trace version of printf - portable between UX and NT 00004 * Author: Phil Cheatle 00005 * Created: Wed Jun 28 15:01:15 BST 1995 00006 * 00007 * (C) Copyright 1995, Hewlett-Packard Ltd. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 // Include automatically generated configuration file if running autoconf. 00021 #ifdef HAVE_CONFIG_H 00022 #include "config_auto.h" 00023 #endif 00024 00025 #include <stdio.h> 00026 #include <stdarg.h> 00027 #include "ccutil.h" 00028 #include "params.h" 00029 #include "strngs.h" 00030 #include "tprintf.h" 00031 00032 #define MAX_MSG_LEN 65536 00033 00034 #define EXTERN 00035 // Since tprintf is protected by a mutex, these parameters can remain global. 00036 DLLSYM STRING_VAR(debug_file, "", "File to send tprintf output to"); 00037 00038 DLLSYM void 00039 tprintf_internal( // Trace printf 00040 const char *format, ... // Message 00041 ) { 00042 tesseract::tprintfMutex.Lock(); 00043 va_list args; // variable args 00044 static FILE *debugfp = NULL; // debug file 00045 // debug window 00046 inT32 offset = 0; // into message 00047 static char msg[MAX_MSG_LEN + 1]; 00048 00049 va_start(args, format); // variable list 00050 // Format into msg 00051 #ifdef _WIN32 00052 offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args); 00053 if (strcmp(debug_file.string(), "/dev/null") == 0) 00054 debug_file.set_value("nul"); 00055 #else 00056 offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args); 00057 #endif 00058 va_end(args); 00059 00060 if (debugfp == NULL && strlen(debug_file.string()) > 0) { 00061 debugfp = fopen(debug_file.string(), "wb"); 00062 } else if (debugfp != NULL && strlen(debug_file.string()) == 0) { 00063 fclose(debugfp); 00064 debugfp = NULL; 00065 } 00066 if (debugfp != NULL) 00067 fprintf(debugfp, "%s", msg); 00068 else 00069 fprintf(stderr, "%s", msg); 00070 tesseract::tprintfMutex.Unlock(); 00071 }