]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: log.cpp | |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
dd85fc6b | 19 | |
c801d85f KB |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "log.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | // wxWindows | |
32 | #ifndef WX_PRECOMP | |
e90c1d2a VZ |
33 | #include "wx/string.h" |
34 | #include "wx/intl.h" | |
35 | #include "wx/app.h" | |
36 | ||
37 | #if wxUSE_GUI | |
38 | #include "wx/window.h" | |
39 | #ifdef __WXMSW__ | |
40 | #include "wx/msw/private.h" | |
41 | #endif | |
42 | #include "wx/msgdlg.h" | |
43 | #endif | |
9ef3052c | 44 | #endif //WX_PRECOMP |
c801d85f | 45 | |
dbf3cd7a RR |
46 | #include "wx/file.h" |
47 | #include "wx/textfile.h" | |
48 | #include "wx/utils.h" | |
a324a7bc | 49 | #include "wx/wxchar.h" |
dbf3cd7a | 50 | #include "wx/log.h" |
b568d04f | 51 | #include "wx/thread.h" |
c801d85f KB |
52 | |
53 | // other standard headers | |
54 | #include <errno.h> | |
55 | #include <stdlib.h> | |
56 | #include <time.h> | |
57 | ||
2049ba38 | 58 | #ifdef __WXMSW__ |
b568d04f | 59 | #include "wx/msw/private.h" // includes windows.h for OutputDebugString |
3078c3a6 VZ |
60 | #else //Unix |
61 | #include <signal.h> | |
62 | #endif //Win/Unix | |
c801d85f KB |
63 | |
64 | // ---------------------------------------------------------------------------- | |
65 | // non member functions | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | // define this to enable wrapping of log messages | |
69 | //#define LOG_PRETTY_WRAP | |
70 | ||
9ef3052c | 71 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
72 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
73 | #endif | |
74 | ||
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
b568d04f | 80 | // globals |
c801d85f KB |
81 | // ---------------------------------------------------------------------------- |
82 | ||
83 | // log functions can't allocate memory (LogError("out of memory...") should | |
84 | // work!), so we use a static buffer for all log messages | |
85 | #define LOG_BUFFER_SIZE (4096) | |
86 | ||
b568d04f | 87 | // static buffer for error messages |
50920146 | 88 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; |
c801d85f | 89 | |
b568d04f VZ |
90 | #if wxUSE_THREADS |
91 | ||
92 | // the critical section protecting the static buffer | |
93 | static wxCriticalSection gs_csLogBuf; | |
94 | ||
95 | #endif // wxUSE_THREADS | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // implementation of Log functions | |
99 | // | |
100 | // NB: unfortunately we need all these distinct functions, we can't make them | |
101 | // macros and not all compilers inline vararg functions. | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
c801d85f | 104 | // generic log function |
50920146 | 105 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) |
c801d85f | 106 | { |
9ef3052c | 107 | if ( wxLog::GetActiveTarget() != NULL ) { |
b568d04f VZ |
108 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
109 | ||
9ef3052c | 110 | va_list argptr; |
7502ba29 | 111 | va_start(argptr, szFormat); |
378b05f7 | 112 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
113 | va_end(argptr); |
114 | ||
0fb67cd1 | 115 | wxLog::OnLog(level, s_szBuf, time(NULL)); |
9ef3052c | 116 | } |
c801d85f KB |
117 | } |
118 | ||
119 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
50920146 | 120 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
121 | { \ |
122 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
b568d04f VZ |
123 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
124 | \ | |
c801d85f | 125 | va_list argptr; \ |
7502ba29 | 126 | va_start(argptr, szFormat); \ |
378b05f7 | 127 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ |
c801d85f KB |
128 | va_end(argptr); \ |
129 | \ | |
0fb67cd1 | 130 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
131 | } \ |
132 | } | |
133 | ||
134 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
135 | IMPLEMENT_LOG_FUNCTION(Error) | |
136 | IMPLEMENT_LOG_FUNCTION(Warning) | |
137 | IMPLEMENT_LOG_FUNCTION(Message) | |
138 | IMPLEMENT_LOG_FUNCTION(Info) | |
139 | IMPLEMENT_LOG_FUNCTION(Status) | |
140 | ||
9ef3052c | 141 | // same as info, but only if 'verbose' mode is on |
50920146 | 142 | void wxLogVerbose(const wxChar *szFormat, ...) |
9ef3052c VZ |
143 | { |
144 | wxLog *pLog = wxLog::GetActiveTarget(); | |
145 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
b568d04f VZ |
146 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
147 | ||
9ef3052c | 148 | va_list argptr; |
7502ba29 | 149 | va_start(argptr, szFormat); |
378b05f7 | 150 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
151 | va_end(argptr); |
152 | ||
0fb67cd1 | 153 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); |
9ef3052c VZ |
154 | } |
155 | } | |
156 | ||
157 | // debug functions | |
b2aef89b | 158 | #ifdef __WXDEBUG__ |
9ef3052c | 159 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
50920146 | 160 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
161 | { \ |
162 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
b568d04f VZ |
163 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
164 | \ | |
c801d85f KB |
165 | va_list argptr; \ |
166 | va_start(argptr, szFormat); \ | |
378b05f7 | 167 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ |
c801d85f KB |
168 | va_end(argptr); \ |
169 | \ | |
0fb67cd1 | 170 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
171 | } \ |
172 | } | |
173 | ||
50920146 | 174 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) |
0fb67cd1 VZ |
175 | { |
176 | wxLog *pLog = wxLog::GetActiveTarget(); | |
177 | ||
178 | if ( pLog != NULL && wxLog::IsAllowedTraceMask(mask) ) { | |
b568d04f VZ |
179 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
180 | ||
00c4e897 VZ |
181 | wxChar *p = s_szBuf; |
182 | size_t len = WXSIZEOF(s_szBuf); | |
183 | strncpy(s_szBuf, _T("Trace ("), len); | |
184 | len -= 7; // strlen("Trace (") | |
185 | p += 7; | |
186 | strncat(p, mask, len); | |
187 | size_t lenMask = wxStrlen(mask); | |
188 | len -= lenMask; | |
189 | p += lenMask; | |
190 | ||
191 | strncat(p, _T("): "), len); | |
192 | len -= 3; | |
193 | p += 3; | |
194 | ||
0fb67cd1 VZ |
195 | va_list argptr; |
196 | va_start(argptr, szFormat); | |
00c4e897 | 197 | wxVsnprintf(p, len, szFormat, argptr); |
0fb67cd1 VZ |
198 | va_end(argptr); |
199 | ||
200 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
201 | } | |
202 | } | |
203 | ||
50920146 | 204 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) |
9ef3052c VZ |
205 | { |
206 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 207 | |
9ef3052c VZ |
208 | // we check that all of mask bits are set in the current mask, so |
209 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
210 | // if both bits are set. | |
d93f63db | 211 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { |
b568d04f VZ |
212 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
213 | ||
9ef3052c VZ |
214 | va_list argptr; |
215 | va_start(argptr, szFormat); | |
378b05f7 | 216 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
217 | va_end(argptr); |
218 | ||
0fb67cd1 | 219 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
9ef3052c VZ |
220 | } |
221 | } | |
222 | ||
223 | #else // release | |
224 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
225 | #endif | |
226 | ||
227 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
228 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
229 | ||
230 | // wxLogSysError: one uses the last error code, for other you must give it | |
231 | // explicitly | |
232 | ||
233 | // common part of both wxLogSysError | |
234 | void wxLogSysErrorHelper(long lErrCode) | |
c801d85f | 235 | { |
50920146 | 236 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; |
378b05f7 VZ |
237 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), |
238 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
50920146 | 239 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); |
c801d85f | 240 | |
0fb67cd1 | 241 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); |
9ef3052c | 242 | } |
c801d85f | 243 | |
50920146 | 244 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) |
9ef3052c | 245 | { |
b568d04f VZ |
246 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
247 | ||
0fb67cd1 VZ |
248 | va_list argptr; |
249 | va_start(argptr, szFormat); | |
378b05f7 | 250 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
0fb67cd1 | 251 | va_end(argptr); |
9ef3052c | 252 | |
0fb67cd1 | 253 | wxLogSysErrorHelper(wxSysErrorCode()); |
c801d85f KB |
254 | } |
255 | ||
50920146 | 256 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) |
c801d85f | 257 | { |
b568d04f VZ |
258 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
259 | ||
0fb67cd1 VZ |
260 | va_list argptr; |
261 | va_start(argptr, szFormat); | |
378b05f7 | 262 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
0fb67cd1 | 263 | va_end(argptr); |
c801d85f | 264 | |
0fb67cd1 | 265 | wxLogSysErrorHelper(lErrCode); |
c801d85f KB |
266 | } |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxLog class implementation | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | wxLog::wxLog() | |
273 | { | |
0fb67cd1 | 274 | m_bHasMessages = FALSE; |
a2ace6ff | 275 | |
0fb67cd1 | 276 | // enable verbose messages by default in the debug builds |
a2ace6ff | 277 | #ifdef __WXDEBUG__ |
0fb67cd1 | 278 | m_bVerbose = TRUE; |
a2ace6ff | 279 | #else // release |
0fb67cd1 | 280 | m_bVerbose = FALSE; |
a2ace6ff | 281 | #endif // debug/release |
c801d85f KB |
282 | } |
283 | ||
9ec05cc9 VZ |
284 | wxLog *wxLog::GetActiveTarget() |
285 | { | |
0fb67cd1 VZ |
286 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
287 | // prevent infinite recursion if someone calls wxLogXXX() from | |
288 | // wxApp::CreateLogTarget() | |
289 | static bool s_bInGetActiveTarget = FALSE; | |
290 | if ( !s_bInGetActiveTarget ) { | |
291 | s_bInGetActiveTarget = TRUE; | |
292 | ||
0fb67cd1 VZ |
293 | // ask the application to create a log target for us |
294 | if ( wxTheApp != NULL ) | |
295 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
296 | else | |
297 | ms_pLogger = new wxLogStderr; | |
0fb67cd1 VZ |
298 | |
299 | s_bInGetActiveTarget = FALSE; | |
300 | ||
301 | // do nothing if it fails - what can we do? | |
302 | } | |
275bf4c1 | 303 | } |
c801d85f | 304 | |
0fb67cd1 | 305 | return ms_pLogger; |
c801d85f KB |
306 | } |
307 | ||
c085e333 | 308 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 309 | { |
0fb67cd1 VZ |
310 | if ( ms_pLogger != NULL ) { |
311 | // flush the old messages before changing because otherwise they might | |
312 | // get lost later if this target is not restored | |
313 | ms_pLogger->Flush(); | |
314 | } | |
c801d85f | 315 | |
0fb67cd1 VZ |
316 | wxLog *pOldLogger = ms_pLogger; |
317 | ms_pLogger = pLogger; | |
c085e333 | 318 | |
0fb67cd1 | 319 | return pOldLogger; |
c801d85f KB |
320 | } |
321 | ||
0fb67cd1 | 322 | void wxLog::RemoveTraceMask(const wxString& str) |
c801d85f | 323 | { |
0fb67cd1 VZ |
324 | int index = ms_aTraceMasks.Index(str); |
325 | if ( index != wxNOT_FOUND ) | |
326 | ms_aTraceMasks.Remove((size_t)index); | |
327 | } | |
c801d85f | 328 | |
d2e1ef19 VZ |
329 | void wxLog::TimeStamp(wxString *str) |
330 | { | |
331 | if ( ms_timestamp ) | |
332 | { | |
333 | wxChar buf[256]; | |
334 | time_t timeNow; | |
335 | (void)time(&timeNow); | |
c49245f8 | 336 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); |
d2e1ef19 VZ |
337 | |
338 | str->Empty(); | |
223d09f6 | 339 | *str << buf << wxT(": "); |
d2e1ef19 VZ |
340 | } |
341 | } | |
342 | ||
50920146 | 343 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
0fb67cd1 | 344 | { |
0fb67cd1 VZ |
345 | switch ( level ) { |
346 | case wxLOG_FatalError: | |
786855a1 | 347 | DoLogString(wxString(_("Fatal error: ")) + szString, t); |
0fb67cd1 VZ |
348 | DoLogString(_("Program aborted."), t); |
349 | Flush(); | |
350 | abort(); | |
351 | break; | |
352 | ||
353 | case wxLOG_Error: | |
786855a1 | 354 | DoLogString(wxString(_("Error: ")) + szString, t); |
0fb67cd1 VZ |
355 | break; |
356 | ||
357 | case wxLOG_Warning: | |
786855a1 | 358 | DoLogString(wxString(_("Warning: ")) + szString, t); |
0fb67cd1 VZ |
359 | break; |
360 | ||
361 | case wxLOG_Info: | |
0fb67cd1 | 362 | if ( GetVerbose() ) |
37278984 | 363 | case wxLOG_Message: |
87a1e308 | 364 | case wxLOG_Status: |
786855a1 VZ |
365 | default: // log unknown log levels too |
366 | DoLogString(szString, t); | |
0fb67cd1 VZ |
367 | break; |
368 | ||
369 | case wxLOG_Trace: | |
370 | case wxLOG_Debug: | |
371 | #ifdef __WXDEBUG__ | |
372 | DoLogString(szString, t); | |
373 | #endif | |
0fb67cd1 | 374 | break; |
0fb67cd1 | 375 | } |
c801d85f KB |
376 | } |
377 | ||
74e3313b | 378 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) |
c801d85f | 379 | { |
223d09f6 | 380 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); |
c801d85f KB |
381 | } |
382 | ||
383 | void wxLog::Flush() | |
384 | { | |
0fb67cd1 | 385 | // do nothing |
c801d85f KB |
386 | } |
387 | ||
388 | // ---------------------------------------------------------------------------- | |
389 | // wxLogStderr class implementation | |
390 | // ---------------------------------------------------------------------------- | |
391 | ||
392 | wxLogStderr::wxLogStderr(FILE *fp) | |
393 | { | |
0fb67cd1 VZ |
394 | if ( fp == NULL ) |
395 | m_fp = stderr; | |
396 | else | |
397 | m_fp = fp; | |
c801d85f KB |
398 | } |
399 | ||
74e3313b | 400 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 401 | { |
d2e1ef19 VZ |
402 | wxString str; |
403 | TimeStamp(&str); | |
b568d04f | 404 | str << szString; |
1e8a4bc2 | 405 | |
50920146 | 406 | fputs(str.mb_str(), m_fp); |
b568d04f | 407 | fputc(_T('\n'), m_fp); |
0fb67cd1 | 408 | fflush(m_fp); |
1e8a4bc2 | 409 | |
378b05f7 | 410 | // under Windows, programs usually don't have stderr at all, so show the |
b568d04f VZ |
411 | // messages also under debugger - unless it's a console program |
412 | #if defined(__WXMSW__) && wxUSE_GUI | |
413 | OutputDebugString(str + wxT("\r\n")); | |
1e8a4bc2 | 414 | #endif // MSW |
37e2cb08 SC |
415 | #if defined(__WXMAC__) && wxUSE_GUI |
416 | debugstr(str + wxT("\r\n")); | |
417 | #endif // MSW | |
c801d85f KB |
418 | } |
419 | ||
420 | // ---------------------------------------------------------------------------- | |
421 | // wxLogStream implementation | |
422 | // ---------------------------------------------------------------------------- | |
423 | ||
4bf78aae | 424 | #if wxUSE_STD_IOSTREAM |
c801d85f KB |
425 | wxLogStream::wxLogStream(ostream *ostr) |
426 | { | |
0fb67cd1 VZ |
427 | if ( ostr == NULL ) |
428 | m_ostr = &cerr; | |
429 | else | |
430 | m_ostr = ostr; | |
c801d85f KB |
431 | } |
432 | ||
74e3313b | 433 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 434 | { |
e90c1d2a | 435 | (*m_ostr) << wxConvertWX2MB(szString) << endl; |
c801d85f | 436 | } |
0fb67cd1 | 437 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 438 | |
c801d85f KB |
439 | // ============================================================================ |
440 | // Global functions/variables | |
441 | // ============================================================================ | |
442 | ||
443 | // ---------------------------------------------------------------------------- | |
444 | // static variables | |
445 | // ---------------------------------------------------------------------------- | |
0fb67cd1 VZ |
446 | |
447 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
448 | bool wxLog::ms_doLog = TRUE; | |
449 | bool wxLog::ms_bAutoCreate = TRUE; | |
d2e1ef19 | 450 | |
9f83044f VZ |
451 | #if wxUSE_GUI |
452 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
453 | #else | |
454 | const wxChar *wxLog::ms_timestamp = NULL; // save space | |
455 | #endif | |
d2e1ef19 | 456 | |
0fb67cd1 VZ |
457 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
458 | wxArrayString wxLog::ms_aTraceMasks; | |
c801d85f KB |
459 | |
460 | // ---------------------------------------------------------------------------- | |
461 | // stdout error logging helper | |
462 | // ---------------------------------------------------------------------------- | |
463 | ||
464 | // helper function: wraps the message and justifies it under given position | |
465 | // (looks more pretty on the terminal). Also adds newline at the end. | |
466 | // | |
0fb67cd1 VZ |
467 | // TODO this is now disabled until I find a portable way of determining the |
468 | // terminal window size (ok, I found it but does anybody really cares?) | |
469 | #ifdef LOG_PRETTY_WRAP | |
c801d85f KB |
470 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
471 | { | |
0fb67cd1 VZ |
472 | size_t nMax = 80; // FIXME |
473 | size_t nStart = strlen(pszPrefix); | |
474 | fputs(pszPrefix, f); | |
475 | ||
476 | size_t n; | |
477 | while ( *psz != '\0' ) { | |
478 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
479 | putc(*psz++, f); | |
480 | ||
481 | // wrapped? | |
482 | if ( *psz != '\0' ) { | |
483 | /*putc('\n', f);*/ | |
484 | for ( n = 0; n < nStart; n++ ) | |
485 | putc(' ', f); | |
486 | ||
487 | // as we wrapped, squeeze all white space | |
488 | while ( isspace(*psz) ) | |
489 | psz++; | |
490 | } | |
c801d85f | 491 | } |
c801d85f | 492 | |
0fb67cd1 | 493 | putc('\n', f); |
c801d85f KB |
494 | } |
495 | #endif //LOG_PRETTY_WRAP | |
496 | ||
497 | // ---------------------------------------------------------------------------- | |
498 | // error code/error message retrieval functions | |
499 | // ---------------------------------------------------------------------------- | |
500 | ||
501 | // get error code from syste | |
502 | unsigned long wxSysErrorCode() | |
503 | { | |
0fb67cd1 VZ |
504 | #ifdef __WXMSW__ |
505 | #ifdef __WIN32__ | |
506 | return ::GetLastError(); | |
507 | #else //WIN16 | |
508 | // TODO what to do on Windows 3.1? | |
509 | return 0; | |
510 | #endif //WIN16/32 | |
511 | #else //Unix | |
c801d85f | 512 | return errno; |
0fb67cd1 | 513 | #endif //Win/Unix |
c801d85f KB |
514 | } |
515 | ||
516 | // get error message from system | |
50920146 | 517 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
c801d85f | 518 | { |
0fb67cd1 VZ |
519 | if ( nErrCode == 0 ) |
520 | nErrCode = wxSysErrorCode(); | |
521 | ||
522 | #ifdef __WXMSW__ | |
523 | #ifdef __WIN32__ | |
50920146 | 524 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
0fb67cd1 VZ |
525 | |
526 | // get error message from system | |
527 | LPVOID lpMsgBuf; | |
528 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
529 | NULL, nErrCode, | |
530 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
531 | (LPTSTR)&lpMsgBuf, | |
532 | 0, NULL); | |
533 | ||
534 | // copy it to our buffer and free memory | |
50920146 | 535 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); |
223d09f6 | 536 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); |
0fb67cd1 VZ |
537 | LocalFree(lpMsgBuf); |
538 | ||
539 | // returned string is capitalized and ended with '\r\n' - bad | |
50920146 OK |
540 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); |
541 | size_t len = wxStrlen(s_szBuf); | |
0fb67cd1 | 542 | if ( len > 0 ) { |
9ef3052c | 543 | // truncate string |
223d09f6 KB |
544 | if ( s_szBuf[len - 2] == wxT('\r') ) |
545 | s_szBuf[len - 2] = wxT('\0'); | |
0fb67cd1 VZ |
546 | } |
547 | ||
548 | return s_szBuf; | |
549 | #else //Win16 | |
550 | // TODO | |
551 | return NULL; | |
552 | #endif // Win16/32 | |
553 | #else // Unix | |
50920146 OK |
554 | #if wxUSE_UNICODE |
555 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
dcf924a3 | 556 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); |
50920146 OK |
557 | return s_szBuf; |
558 | #else | |
13111b2a | 559 | return strerror((int)nErrCode); |
50920146 | 560 | #endif |
0fb67cd1 | 561 | #endif // Win/Unix |
c801d85f KB |
562 | } |
563 | ||
564 | // ---------------------------------------------------------------------------- | |
565 | // debug helper | |
566 | // ---------------------------------------------------------------------------- | |
567 | ||
b2aef89b | 568 | #ifdef __WXDEBUG__ |
c801d85f | 569 | |
0fb67cd1 | 570 | // break into the debugger |
7502ba29 VZ |
571 | void Trap() |
572 | { | |
0fb67cd1 | 573 | #ifdef __WXMSW__ |
7502ba29 | 574 | DebugBreak(); |
0fb67cd1 VZ |
575 | #elif defined(__WXMAC__) |
576 | #if __powerc | |
17dff81c | 577 | Debugger(); |
0fb67cd1 | 578 | #else |
17dff81c | 579 | SysBreak(); |
0fb67cd1 VZ |
580 | #endif |
581 | #elif defined(__UNIX__) | |
7502ba29 | 582 | raise(SIGTRAP); |
0fb67cd1 VZ |
583 | #else |
584 | // TODO | |
585 | #endif // Win/Unix | |
7502ba29 VZ |
586 | } |
587 | ||
c801d85f | 588 | // this function is called when an assert fails |
6f9eb452 | 589 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) |
c801d85f | 590 | { |
0fb67cd1 VZ |
591 | // this variable can be set to true to suppress "assert failure" messages |
592 | static bool s_bNoAsserts = FALSE; | |
593 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
7502ba29 | 594 | |
0fb67cd1 VZ |
595 | if ( s_bInAssert ) { |
596 | // He-e-e-e-elp!! we're trapped in endless loop | |
597 | Trap(); | |
c085e333 | 598 | |
0fb67cd1 | 599 | s_bInAssert = FALSE; |
1e8a4bc2 | 600 | |
0fb67cd1 VZ |
601 | return; |
602 | } | |
7502ba29 | 603 | |
0fb67cd1 | 604 | s_bInAssert = TRUE; |
c801d85f | 605 | |
50920146 | 606 | wxChar szBuf[LOG_BUFFER_SIZE]; |
c263077e | 607 | |
0fb67cd1 VZ |
608 | // make life easier for people using VC++ IDE: clicking on the message |
609 | // will take us immediately to the place of the failed assert | |
378b05f7 | 610 | wxSnprintf(szBuf, WXSIZEOF(szBuf), |
3f4a0c5b | 611 | #ifdef __VISUALC__ |
378b05f7 | 612 | wxT("%s(%d): assert failed"), |
c263077e | 613 | #else // !VC++ |
0fb67cd1 | 614 | // make the error message more clear for all the others |
378b05f7 | 615 | wxT("Assert failed in file %s at line %d"), |
c263077e | 616 | #endif // VC/!VC |
378b05f7 | 617 | szFile, nLine); |
c263077e | 618 | |
0fb67cd1 | 619 | if ( szMsg != NULL ) { |
223d09f6 | 620 | wxStrcat(szBuf, wxT(": ")); |
50920146 | 621 | wxStrcat(szBuf, szMsg); |
0fb67cd1 VZ |
622 | } |
623 | else { | |
223d09f6 | 624 | wxStrcat(szBuf, wxT(".")); |
0fb67cd1 | 625 | } |
c801d85f | 626 | |
0fb67cd1 VZ |
627 | if ( !s_bNoAsserts ) { |
628 | // send it to the normal log destination | |
629 | wxLogDebug(szBuf); | |
7502ba29 | 630 | |
b76b015e | 631 | #if wxUSE_GUI || defined(__WXMSW__) |
0fb67cd1 VZ |
632 | // this message is intentionally not translated - it is for |
633 | // developpers only | |
223d09f6 | 634 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?" |
b76b015e VZ |
635 | "\nYou can also choose [Cancel] to suppress " |
636 | "further warnings.")); | |
0fb67cd1 | 637 | |
b76b015e VZ |
638 | #if wxUSE_GUI |
639 | switch ( wxMessageBox(szBuf, "Debug", | |
640 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
0fb67cd1 VZ |
641 | case wxYES: |
642 | Trap(); | |
643 | break; | |
644 | ||
645 | case wxCANCEL: | |
646 | s_bNoAsserts = TRUE; | |
647 | break; | |
648 | ||
b76b015e VZ |
649 | //case wxNO: nothing to do |
650 | } | |
651 | #else // !GUI, but MSW | |
652 | switch ( ::MessageBox(NULL, szBuf, "Debug", | |
653 | MB_YESNOCANCEL | MB_ICONSTOP ) ) { | |
654 | case IDYES: | |
655 | Trap(); | |
656 | break; | |
657 | ||
658 | case IDCANCEL: | |
659 | s_bNoAsserts = TRUE; | |
660 | break; | |
661 | ||
662 | //case IDNO: nothing to do | |
0fb67cd1 | 663 | } |
b76b015e VZ |
664 | #endif // GUI or MSW |
665 | ||
e90c1d2a VZ |
666 | #else // !GUI |
667 | Trap(); | |
668 | #endif // GUI/!GUI | |
0fb67cd1 VZ |
669 | } |
670 | ||
671 | s_bInAssert = FALSE; | |
c801d85f KB |
672 | } |
673 | ||
b2aef89b | 674 | #endif //WXDEBUG |
c801d85f | 675 |