1 /**********************************************************************
2 * File: tprintf.c
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 #include "mfcpch.h" //precompiled headers
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include "strngs.h"
23 #include "varable.h"
24 #include "debugwin.h"
25 //#include "ipeerr.h"
26 #include "tprintf.h"
27 #include "ccutil.h"
28
29 #define MAX_MSG_LEN 1024
30
31 #define EXTERN
32 DLLSYM STRING_VAR (debug_file, "", "File to send tprintf output to");
33 DLLSYM BOOL_VAR (debug_window_on, FALSE,
34 "Send tprintf to window unless file set");
35
36 DLLSYM void
tprintf(const char * format,...)37 tprintf ( //Trace printf
38 const char *format, ... //special message
39 ) {
40 tesseract::tprintfMutex.Lock();
41 va_list args; //variable args
42 static FILE *debugfp = NULL; //debug file
43 //debug window
44 static DEBUG_WIN *debugwin = NULL;
45 inT32 offset = 0; //into message
46 static char msg[MAX_MSG_LEN + 1];
47
48 va_start(args, format); //variable list
49 #ifdef __MSW32__
50 //Format into msg
51 offset += _vsnprintf (msg + offset, MAX_MSG_LEN - offset, format, args);
52 #else
53 //Format into msg
54 offset += vsprintf (msg + offset, format, args);
55 #endif
56 va_end(args);
57
58 if (debugfp == NULL && strlen (debug_file.string ()) > 0)
59 debugfp = fopen (debug_file.string (), "w");
60 else if (debugfp != NULL && strlen (debug_file.string ()) == 0) {
61 fclose(debugfp);
62 debugfp = NULL;
63 }
64 if (debugfp != NULL)
65 fprintf (debugfp, "%s", msg);
66 else {
67
68 if (debug_window_on) {
69 if (debugwin == NULL)
70 //in pixels
71 debugwin = new DEBUG_WIN ("Debug Window", DEBUG_WIN_XPOS, DEBUG_WIN_YPOS,
72 //in pixels
73 DEBUG_WIN_XSIZE, DEBUG_WIN_YSIZE,
74 debug_lines);
75 debugwin->dprintf (msg);
76 }
77 else {
78 fprintf (stderr, "%s", msg);
79 }
80 }
81 tesseract::tprintfMutex.Unlock();
82 }
83
84
85 /*************************************************************************
86 * pause_continue()
87 * UI for a debugging pause - to see an intermediate state
88 * Returns TRUE to continue as normal to the next pause in the current mode;
89 * FALSE to quit the current pausing mode.
90 *************************************************************************/
91
92 DLLSYM BOOL8
93 //special message
pause_continue(const char * format,...)94 pause_continue (const char *format, ...
95 ) {
96 va_list args; //variable args
97 char msg[1000];
98 STRING str = STRING ("DEBUG PAUSE:\n");
99
100 va_start(args, format); //variable list
101 vsprintf(msg, format, args); //Format into msg
102 va_end(args);
103
104 #ifdef GRAPHICS_DISABLED
105 // No interaction allowed -> simply go on
106 return true;
107 #else
108
109 #ifdef __UNIX__
110 printf ("%s\n", msg);
111 printf ("Type \"c\" to cancel, anything else to continue: ");
112 char c = getchar ();
113 return (c != 'c');
114 #endif
115
116 #ifdef __MSW32__
117 str +=
118 STRING (msg) + STRING ("\nUse OK to continue, CANCEL to stop pausing");
119 // return AfxMessageBox( str.string(), MB_OKCANCEL ) == IDOK;
120 return::MessageBox (NULL, msg, "IMGAPP",
121 MB_APPLMODAL | MB_OKCANCEL) == IDOK;
122 #endif
123
124 #endif
125 }
126