fixed warnings
[reactos.git] / rosapps / mc / pc / trace_nt.c
1 /* trace_nt.c - Debugging routines
2 for Midnight Commander, under Win32
3
4 Written 951215 by Juan Grigera <grigera@isis.unlp.edu.ar>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include <config.h>
21 #ifdef HAVE_TRACE
22
23 #include <stdio.h>
24 #ifdef _OS_NT
25 #include <windows.h>
26 #endif
27 #include <errno.h>
28 #include "trace_nt.h"
29
30 /* Global variables */
31 int __win32_tracing_enabled = 1;
32
33 static int _win32_tracing_started = 0;
34 static FILE *__win32_trace_f = NULL;
35
36 /* Definitions */
37 #define TRACE_FILE "mcTrace.out"
38
39 /* Prototypes - static funcs */
40 static void _win32InitTrace (void);
41 static void _win32EndTrace (void);
42 static const char* GetLastErrorText(void);
43 static char *visbuf(const char *buf);
44
45
46 /*
47 void _win32InitTrace()
48 This func will open file TRACE_FILE for output and add _win32EndTrace to onexit
49 list of funcs.
50 */
51 static void _win32InitTrace()
52 {
53 if (!_win32_tracing_started) {
54 _win32_tracing_started = 1;
55
56 __win32_trace_f = fopen(TRACE_FILE, "wt");
57 if (__win32_trace_f == NULL) {
58 printf("Midnight Commander[DEBUG]: Can't open trace file '" TRACE_FILE "': %s \n", strerror(errno));
59 }
60 atexit (&_win32EndTrace);
61 }
62 }
63
64 /*
65 void _win32EndTrace()
66 This func closes file TRACE_FILE if opened.
67 */
68 static void _win32EndTrace()
69 {
70 if (_win32_tracing_started) {
71 _win32_tracing_started = 0;
72 if (__win32_trace_f)
73 fclose (__win32_trace_f);
74 }
75
76 }
77
78 /*
79 void _win32Trace (char *fmt, ...)
80 Format and output debug strings. They are written to TRACE_FILE.
81 Debug Output is controlled by SetTrace (see below).
82 Win32: Output is sent to Debug Output also.
83 */
84 void _win32Trace (const char *fmt, ...)
85 {
86 va_list ap;
87 char buffer[256];
88 char *vp;
89
90
91 if (!_win32_tracing_started)
92 _win32InitTrace();
93
94 va_start(ap, fmt);
95 vsprintf(buffer, fmt, ap);
96 vp = buffer;
97
98 #ifdef _OS_NT /* Write Output to Debug monitor also */
99 OutputDebugString (vp);
100 #if (_MSC_VER > 800) /* Don't write newline in MSVC++ 1.0, has a dammed bug in Debug Output screen */
101 OutputDebugString ("\n");
102 #endif
103 #endif
104
105 if(__win32_trace_f)
106 fprintf (__win32_trace_f, "%s\n", vp);
107 }
108
109 /*
110 void SetTrace (int trace)
111 Control debug output. Turn it of or on.
112 trace: 0 = off, 1 = on.
113 */
114 void _win32SetTrace (int trace)
115 {
116 /* Prototypes - interlan funcs */
117 __win32_tracing_enabled = trace;
118 }
119 void _win32TraceOn ()
120 {
121 __win32_tracing_enabled = 1;
122 }
123 void _win32TraceOff()
124 {
125 __win32_tracing_enabled = 0;
126 }
127
128
129 #ifdef _OS_NT
130 /*
131 void DebugFailedWin32APICall (const char* name, int line, const char* file)
132 Report a System call failure.
133 name - text containing the source code that called the offending API func
134 line, file - place of "name" in code
135
136 See Also: definition of win32APICALL macro.
137 */
138 void _win32DebugFailedWin32APICall (const char* name, int line, const char* file)
139 {
140 _win32Trace ("%s(%d): Call to Win32 API Failed. \"%s\".", file, line, name);
141 _win32Trace (" System Error (%d): %s. ", GetLastError(), GetLastErrorText());
142 }
143 #endif
144
145 /*
146 void DebugAssertionFailed (const char* name, int line, const char* file)
147 Report a logical condition failure. (e.g. a bad argument to a func)
148 name - text containing the logical condition
149 line, file - place of "name" in code
150
151 See Also: definition of ASSERT macro.
152 */
153 void _win32DebugAssertionFailed (const char* name, int line, const char* file)
154 {
155 _win32Trace ("%s(%d): Assertion failed! \"%s\".", file, line, name);
156 }
157
158
159 /* const char* GetLastErrorText()
160 Retrieves the text associated with the last system error.
161
162 Returns pointer to static buffer. Contents valid till next call
163 */
164 static const char* GetLastErrorText()
165 {
166 #define MAX_MSG_SIZE 256
167 static char szMsgBuf[MAX_MSG_SIZE];
168 DWORD dwError, dwRes;
169
170 dwError = GetLastError ();
171
172 dwRes = FormatMessage (
173 FORMAT_MESSAGE_FROM_SYSTEM,
174 NULL,
175 dwError,
176 MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
177 szMsgBuf,
178 MAX_MSG_SIZE,
179 NULL);
180 if (0 == dwRes) {
181 sprintf (szMsgBuf, "FormatMessage failed with %d", GetLastError());
182 }
183 return szMsgBuf;
184 }
185
186 #endif /*HAVE_TRACE*/