*** empty log message ***
[midnight-commander.git] / pc / trace_nt.c
bloba2dbd8489d2ea279cc3be378a9784d8a4f7a9e88
1 /* trace_nt.c - Debugging routines
2 for Midnight Commander, under Win32
4 Written 951215 by Juan Grigera <grigera@isis.unlp.edu.ar>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
21 #ifdef HAVE_TRACE
23 #include <stdio.h>
24 #ifdef _OS_NT
25 #include <windows.h>
26 #endif
27 #include <errno.h>
28 #include "trace_nt.h"
30 /* Global variables */
31 int __win32_tracing_enabled = 1;
33 static int _win32_tracing_started = 0;
34 static FILE *__win32_trace_f = NULL;
36 /* Definitions */
37 #define TRACE_FILE "mcTrace.out"
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);
47 void _win32InitTrace()
48 This func will open file TRACE_FILE for output and add _win32EndTrace to onexit
49 list of funcs.
51 static void _win32InitTrace()
53 if (!_win32_tracing_started) {
54 _win32_tracing_started = 1;
56 __win32_trace_f = fopen(TRACE_FILE, "wt");
57 if (__win32_trace_f == NULL) {
58 printf("Midnight Commander[DEBUG]: Cannot open trace file '" TRACE_FILE "': %s \n", strerror(errno));
60 atexit (&_win32EndTrace);
65 void _win32EndTrace()
66 This func closes file TRACE_FILE if opened.
68 static void _win32EndTrace()
70 if (_win32_tracing_started) {
71 _win32_tracing_started = 0;
72 if (__win32_trace_f)
73 fclose (__win32_trace_f);
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.
84 void _win32Trace (const char *fmt, ...)
86 va_list ap;
87 char buffer[256];
88 char *vp;
91 if (!_win32_tracing_started)
92 _win32InitTrace();
94 va_start(ap, fmt);
95 vsprintf(buffer, fmt, ap);
96 va_end(ap);
97 vp = buffer;
99 #ifdef _OS_NT /* Write Output to Debug monitor also */
100 OutputDebugString (vp);
101 #if (_MSC_VER > 800) /* Don't write newline in MSVC++ 1.0, has a dammed bug in Debug Output screen */
102 OutputDebugString ("\n");
103 #endif
104 #endif
106 if(__win32_trace_f)
107 fprintf (__win32_trace_f, "%s\n", vp);
111 void SetTrace (int trace)
112 Control debug output. Turn it of or on.
113 trace: 0 = off, 1 = on.
115 void _win32SetTrace (int trace)
117 /* Prototypes - interlan funcs */
118 __win32_tracing_enabled = trace;
120 void _win32TraceOn ()
122 __win32_tracing_enabled = 1;
124 void _win32TraceOff()
126 __win32_tracing_enabled = 0;
130 #ifdef _OS_NT
132 void DebugFailedWin32APICall (const char* name, int line, const char* file)
133 Report a System call failure.
134 name - text containing the source code that called the offending API func
135 line, file - place of "name" in code
137 See Also: definition of win32APICALL macro.
139 void _win32DebugFailedWin32APICall (const char* name, int line, const char* file)
141 _win32Trace ("%s(%d): Call to Win32 API Failed. \"%s\".", file, line, name);
142 _win32Trace (" System Error (%d): %s. ", GetLastError(), GetLastErrorText());
144 #endif
147 void DebugAssertionFailed (const char* name, int line, const char* file)
148 Report a logical condition failure. (e.g. a bad argument to a func)
149 name - text containing the logical condition
150 line, file - place of "name" in code
152 See Also: definition of ASSERT macro.
154 void _win32DebugAssertionFailed (const char* name, int line, const char* file)
156 _win32Trace ("%s(%d): Assertion failed! \"%s\".", file, line, name);
160 /* const char* GetLastErrorText()
161 Retrieves the text associated with the last system error.
163 Returns pointer to static buffer. Contents valid till next call
165 static const char* GetLastErrorText()
167 #define MAX_MSG_SIZE 256
168 static char szMsgBuf[MAX_MSG_SIZE];
169 DWORD dwError, dwRes;
171 dwError = GetLastError ();
173 dwRes = FormatMessage (
174 FORMAT_MESSAGE_FROM_SYSTEM,
175 NULL,
176 dwError,
177 MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
178 szMsgBuf,
179 MAX_MSG_SIZE,
180 NULL);
181 if (0 == dwRes) {
182 sprintf (szMsgBuf, "FormatMessage failed with %d", GetLastError());
184 return szMsgBuf;
187 #endif /*HAVE_TRACE*/