]>
Commit | Line | Data |
---|---|---|
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 | // ---------------------------------------------------------------------------- | |
19 | ||
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 | |
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 | |
44 | #endif //WX_PRECOMP | |
45 | ||
46 | #include "wx/file.h" | |
47 | #include "wx/textfile.h" | |
48 | #include "wx/utils.h" | |
49 | #include "wx/wxchar.h" | |
50 | #include "wx/log.h" | |
51 | #include "wx/thread.h" | |
52 | ||
53 | #if wxUSE_LOG | |
54 | ||
55 | // other standard headers | |
56 | #include <errno.h> | |
57 | #include <stdlib.h> | |
58 | #include <time.h> | |
59 | ||
60 | #ifdef __WXMSW__ | |
61 | #include "wx/msw/private.h" // includes windows.h for OutputDebugString | |
62 | #else //Unix | |
63 | #include <signal.h> | |
64 | #endif //Win/Unix | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // non member functions | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | // define this to enable wrapping of log messages | |
71 | //#define LOG_PRETTY_WRAP | |
72 | ||
73 | #ifdef LOG_PRETTY_WRAP | |
74 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); | |
75 | #endif | |
76 | ||
77 | // ============================================================================ | |
78 | // implementation | |
79 | // ============================================================================ | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // globals | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | // log functions can't allocate memory (LogError("out of memory...") should | |
86 | // work!), so we use a static buffer for all log messages | |
87 | #define LOG_BUFFER_SIZE (4096) | |
88 | ||
89 | // static buffer for error messages | |
90 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; | |
91 | ||
92 | #if wxUSE_THREADS | |
93 | ||
94 | // the critical section protecting the static buffer | |
95 | static wxCriticalSection gs_csLogBuf; | |
96 | ||
97 | #endif // wxUSE_THREADS | |
98 | ||
99 | // return true if we have a non NULL non disabled log target | |
100 | static inline bool IsLoggingEnabled() | |
101 | { | |
102 | return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL); | |
103 | } | |
104 | ||
105 | // ---------------------------------------------------------------------------- | |
106 | // implementation of Log functions | |
107 | // | |
108 | // NB: unfortunately we need all these distinct functions, we can't make them | |
109 | // macros and not all compilers inline vararg functions. | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
112 | // generic log function | |
113 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) | |
114 | { | |
115 | if ( IsLoggingEnabled() ) { | |
116 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
117 | ||
118 | va_list argptr; | |
119 | va_start(argptr, szFormat); | |
120 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
121 | va_end(argptr); | |
122 | ||
123 | wxLog::OnLog(level, s_szBuf, time(NULL)); | |
124 | } | |
125 | } | |
126 | ||
127 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
128 | void wxLog##level(const wxChar *szFormat, ...) \ | |
129 | { \ | |
130 | if ( IsLoggingEnabled() ) { \ | |
131 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ | |
132 | \ | |
133 | va_list argptr; \ | |
134 | va_start(argptr, szFormat); \ | |
135 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ | |
136 | va_end(argptr); \ | |
137 | \ | |
138 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ | |
139 | } \ | |
140 | } | |
141 | ||
142 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
143 | IMPLEMENT_LOG_FUNCTION(Error) | |
144 | IMPLEMENT_LOG_FUNCTION(Warning) | |
145 | IMPLEMENT_LOG_FUNCTION(Message) | |
146 | IMPLEMENT_LOG_FUNCTION(Info) | |
147 | IMPLEMENT_LOG_FUNCTION(Status) | |
148 | ||
149 | // same as info, but only if 'verbose' mode is on | |
150 | void wxLogVerbose(const wxChar *szFormat, ...) | |
151 | { | |
152 | if ( IsLoggingEnabled() ) { | |
153 | wxLog *pLog = wxLog::GetActiveTarget(); | |
154 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
155 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
156 | ||
157 | va_list argptr; | |
158 | va_start(argptr, szFormat); | |
159 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
160 | va_end(argptr); | |
161 | ||
162 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | // debug functions | |
168 | #ifdef __WXDEBUG__ | |
169 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ | |
170 | void wxLog##level(const wxChar *szFormat, ...) \ | |
171 | { \ | |
172 | if ( IsLoggingEnabled() ) { \ | |
173 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ | |
174 | \ | |
175 | va_list argptr; \ | |
176 | va_start(argptr, szFormat); \ | |
177 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ | |
178 | va_end(argptr); \ | |
179 | \ | |
180 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ | |
181 | } \ | |
182 | } | |
183 | ||
184 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) | |
185 | { | |
186 | if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) { | |
187 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
188 | ||
189 | wxChar *p = s_szBuf; | |
190 | size_t len = WXSIZEOF(s_szBuf); | |
191 | wxStrncpy(s_szBuf, _T("("), len); | |
192 | len -= 1; // strlen("(") | |
193 | p += 1; | |
194 | wxStrncat(p, mask, len); | |
195 | size_t lenMask = wxStrlen(mask); | |
196 | len -= lenMask; | |
197 | p += lenMask; | |
198 | ||
199 | wxStrncat(p, _T(") "), len); | |
200 | len -= 2; | |
201 | p += 2; | |
202 | ||
203 | va_list argptr; | |
204 | va_start(argptr, szFormat); | |
205 | wxVsnprintf(p, len, szFormat, argptr); | |
206 | va_end(argptr); | |
207 | ||
208 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
209 | } | |
210 | } | |
211 | ||
212 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) | |
213 | { | |
214 | // we check that all of mask bits are set in the current mask, so | |
215 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
216 | // if both bits are set. | |
217 | if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) { | |
218 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
219 | ||
220 | va_list argptr; | |
221 | va_start(argptr, szFormat); | |
222 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
223 | va_end(argptr); | |
224 | ||
225 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
226 | } | |
227 | } | |
228 | ||
229 | #else // release | |
230 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
231 | #endif | |
232 | ||
233 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
234 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
235 | ||
236 | // wxLogSysError: one uses the last error code, for other you must give it | |
237 | // explicitly | |
238 | ||
239 | // common part of both wxLogSysError | |
240 | void wxLogSysErrorHelper(long lErrCode) | |
241 | { | |
242 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; | |
243 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), | |
244 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
245 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); | |
246 | ||
247 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); | |
248 | } | |
249 | ||
250 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) | |
251 | { | |
252 | if ( IsLoggingEnabled() ) { | |
253 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
254 | ||
255 | va_list argptr; | |
256 | va_start(argptr, szFormat); | |
257 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
258 | va_end(argptr); | |
259 | ||
260 | wxLogSysErrorHelper(wxSysErrorCode()); | |
261 | } | |
262 | } | |
263 | ||
264 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) | |
265 | { | |
266 | if ( IsLoggingEnabled() ) { | |
267 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
268 | ||
269 | va_list argptr; | |
270 | va_start(argptr, szFormat); | |
271 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
272 | va_end(argptr); | |
273 | ||
274 | wxLogSysErrorHelper(lErrCode); | |
275 | } | |
276 | } | |
277 | ||
278 | // ---------------------------------------------------------------------------- | |
279 | // wxLog class implementation | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
282 | wxLog::wxLog() | |
283 | { | |
284 | m_bHasMessages = FALSE; | |
285 | m_bVerbose = FALSE; | |
286 | } | |
287 | ||
288 | wxLog *wxLog::GetActiveTarget() | |
289 | { | |
290 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { | |
291 | // prevent infinite recursion if someone calls wxLogXXX() from | |
292 | // wxApp::CreateLogTarget() | |
293 | static bool s_bInGetActiveTarget = FALSE; | |
294 | if ( !s_bInGetActiveTarget ) { | |
295 | s_bInGetActiveTarget = TRUE; | |
296 | ||
297 | // ask the application to create a log target for us | |
298 | if ( wxTheApp != NULL ) | |
299 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
300 | else | |
301 | ms_pLogger = new wxLogStderr; | |
302 | ||
303 | s_bInGetActiveTarget = FALSE; | |
304 | ||
305 | // do nothing if it fails - what can we do? | |
306 | } | |
307 | } | |
308 | ||
309 | return ms_pLogger; | |
310 | } | |
311 | ||
312 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
313 | { | |
314 | if ( ms_pLogger != NULL ) { | |
315 | // flush the old messages before changing because otherwise they might | |
316 | // get lost later if this target is not restored | |
317 | ms_pLogger->Flush(); | |
318 | } | |
319 | ||
320 | wxLog *pOldLogger = ms_pLogger; | |
321 | ms_pLogger = pLogger; | |
322 | ||
323 | return pOldLogger; | |
324 | } | |
325 | ||
326 | void wxLog::RemoveTraceMask(const wxString& str) | |
327 | { | |
328 | int index = ms_aTraceMasks.Index(str); | |
329 | if ( index != wxNOT_FOUND ) | |
330 | ms_aTraceMasks.Remove((size_t)index); | |
331 | } | |
332 | ||
333 | void wxLog::TimeStamp(wxString *str) | |
334 | { | |
335 | if ( ms_timestamp ) | |
336 | { | |
337 | wxChar buf[256]; | |
338 | time_t timeNow; | |
339 | (void)time(&timeNow); | |
340 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); | |
341 | ||
342 | str->Empty(); | |
343 | *str << buf << wxT(": "); | |
344 | } | |
345 | } | |
346 | ||
347 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
348 | { | |
349 | switch ( level ) { | |
350 | case wxLOG_FatalError: | |
351 | DoLogString(wxString(_("Fatal error: ")) + szString, t); | |
352 | DoLogString(_("Program aborted."), t); | |
353 | Flush(); | |
354 | abort(); | |
355 | break; | |
356 | ||
357 | case wxLOG_Error: | |
358 | DoLogString(wxString(_("Error: ")) + szString, t); | |
359 | break; | |
360 | ||
361 | case wxLOG_Warning: | |
362 | DoLogString(wxString(_("Warning: ")) + szString, t); | |
363 | break; | |
364 | ||
365 | case wxLOG_Info: | |
366 | if ( GetVerbose() ) | |
367 | case wxLOG_Message: | |
368 | case wxLOG_Status: | |
369 | default: // log unknown log levels too | |
370 | DoLogString(szString, t); | |
371 | break; | |
372 | ||
373 | case wxLOG_Trace: | |
374 | case wxLOG_Debug: | |
375 | #ifdef __WXDEBUG__ | |
376 | { | |
377 | wxString msg = level == wxLOG_Trace ? wxT("Trace: ") | |
378 | : wxT("Debug: "); | |
379 | msg << szString; | |
380 | DoLogString(msg, t); | |
381 | } | |
382 | #endif // Debug | |
383 | break; | |
384 | } | |
385 | } | |
386 | ||
387 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) | |
388 | { | |
389 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); | |
390 | } | |
391 | ||
392 | void wxLog::Flush() | |
393 | { | |
394 | // do nothing | |
395 | } | |
396 | ||
397 | // ---------------------------------------------------------------------------- | |
398 | // wxLogStderr class implementation | |
399 | // ---------------------------------------------------------------------------- | |
400 | ||
401 | wxLogStderr::wxLogStderr(FILE *fp) | |
402 | { | |
403 | if ( fp == NULL ) | |
404 | m_fp = stderr; | |
405 | else | |
406 | m_fp = fp; | |
407 | } | |
408 | ||
409 | #if defined(__WXMAC__) && !defined(__UNIX__) | |
410 | #define kDebuggerSignature 'MWDB' | |
411 | ||
412 | static Boolean FindProcessBySignature(OSType signature, ProcessInfoRec* info) | |
413 | { | |
414 | OSErr err; | |
415 | ProcessSerialNumber psn; | |
416 | Boolean found = false; | |
417 | psn.highLongOfPSN = 0; | |
418 | psn.lowLongOfPSN = kNoProcess; | |
419 | ||
420 | if (!info) return false; | |
421 | ||
422 | info->processInfoLength = sizeof(ProcessInfoRec); | |
423 | info->processName = NULL; | |
424 | info->processAppSpec = NULL; | |
425 | ||
426 | err = noErr; | |
427 | while (!found && err == noErr) | |
428 | { | |
429 | err = GetNextProcess(&psn); | |
430 | if (err == noErr) | |
431 | { | |
432 | err = GetProcessInformation(&psn, info); | |
433 | found = err == noErr && info->processSignature == signature; | |
434 | } | |
435 | } | |
436 | return found; | |
437 | } | |
438 | ||
439 | pascal Boolean MWDebuggerIsRunning(void) | |
440 | { | |
441 | ProcessInfoRec info; | |
442 | return FindProcessBySignature(kDebuggerSignature, &info); | |
443 | } | |
444 | ||
445 | pascal OSErr AmIBeingMWDebugged(Boolean* result) | |
446 | { | |
447 | OSErr err; | |
448 | ProcessSerialNumber psn; | |
449 | OSType sig = kDebuggerSignature; | |
450 | AppleEvent theAE = {typeNull, NULL}; | |
451 | AppleEvent theReply = {typeNull, NULL}; | |
452 | AEAddressDesc addr = {typeNull, NULL}; | |
453 | DescType actualType; | |
454 | Size actualSize; | |
455 | ||
456 | if (!result) return paramErr; | |
457 | ||
458 | err = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &addr); | |
459 | if (err != noErr) goto exit; | |
460 | ||
461 | err = AECreateAppleEvent('MWDB', 'Dbg?', &addr, | |
462 | kAutoGenerateReturnID, kAnyTransactionID, &theAE); | |
463 | if (err != noErr) goto exit; | |
464 | ||
465 | GetCurrentProcess(&psn); | |
466 | err = AEPutParamPtr(&theAE, keyDirectObject, typeProcessSerialNumber, | |
467 | &psn, sizeof(psn)); | |
468 | if (err != noErr) goto exit; | |
469 | ||
470 | err = AESend(&theAE, &theReply, kAEWaitReply, kAENormalPriority, | |
471 | kAEDefaultTimeout, NULL, NULL); | |
472 | if (err != noErr) goto exit; | |
473 | ||
474 | err = AEGetParamPtr(&theReply, keyAEResult, typeBoolean, &actualType, result, | |
475 | sizeof(Boolean), &actualSize); | |
476 | ||
477 | exit: | |
478 | if (addr.dataHandle) | |
479 | AEDisposeDesc(&addr); | |
480 | if (theAE.dataHandle) | |
481 | AEDisposeDesc(&theAE); | |
482 | if (theReply.dataHandle) | |
483 | AEDisposeDesc(&theReply); | |
484 | ||
485 | return err; | |
486 | } | |
487 | #endif | |
488 | ||
489 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
490 | { | |
491 | wxString str; | |
492 | TimeStamp(&str); | |
493 | str << szString; | |
494 | ||
495 | fputs(str.mb_str(), m_fp); | |
496 | fputc(_T('\n'), m_fp); | |
497 | fflush(m_fp); | |
498 | ||
499 | // under Windows, programs usually don't have stderr at all, so show the | |
500 | // messages also under debugger - unless it's a console program | |
501 | #if defined(__WXMSW__) && wxUSE_GUI | |
502 | str += wxT("\r\n") ; | |
503 | OutputDebugString(str.c_str()); | |
504 | #endif // MSW | |
505 | #if defined(__WXMAC__) && !defined(__WXMAC_X__) && wxUSE_GUI | |
506 | Str255 pstr ; | |
507 | strcpy( (char*) pstr , str.c_str() ) ; | |
508 | strcat( (char*) pstr , ";g" ) ; | |
509 | c2pstr( (char*) pstr ) ; | |
510 | #if __WXDEBUG__ | |
511 | Boolean running = false ; | |
512 | ||
513 | /* | |
514 | if ( MWDebuggerIsRunning() ) | |
515 | { | |
516 | AmIBeingMWDebugged( &running ) ; | |
517 | } | |
518 | */ | |
519 | if (running) | |
520 | { | |
521 | #ifdef __powerc | |
522 | DebugStr(pstr); | |
523 | #else | |
524 | SysBreakStr(pstr); | |
525 | #endif | |
526 | } | |
527 | else | |
528 | #endif | |
529 | { | |
530 | #ifdef __powerc | |
531 | DebugStr(pstr); | |
532 | #else | |
533 | DebugStr(pstr); | |
534 | #endif | |
535 | } | |
536 | #endif // Mac | |
537 | } | |
538 | ||
539 | // ---------------------------------------------------------------------------- | |
540 | // wxLogStream implementation | |
541 | // ---------------------------------------------------------------------------- | |
542 | ||
543 | #if wxUSE_STD_IOSTREAM | |
544 | wxLogStream::wxLogStream(ostream *ostr) | |
545 | { | |
546 | if ( ostr == NULL ) | |
547 | m_ostr = &cerr; | |
548 | else | |
549 | m_ostr = ostr; | |
550 | } | |
551 | ||
552 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
553 | { | |
554 | wxString str; | |
555 | TimeStamp(&str); | |
556 | (*m_ostr) << str << wxConvertWX2MB(szString) << endl; | |
557 | } | |
558 | #endif // wxUSE_STD_IOSTREAM | |
559 | ||
560 | // ============================================================================ | |
561 | // Global functions/variables | |
562 | // ============================================================================ | |
563 | ||
564 | // ---------------------------------------------------------------------------- | |
565 | // static variables | |
566 | // ---------------------------------------------------------------------------- | |
567 | ||
568 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
569 | bool wxLog::ms_doLog = TRUE; | |
570 | bool wxLog::ms_bAutoCreate = TRUE; | |
571 | ||
572 | size_t wxLog::ms_suspendCount = 0; | |
573 | ||
574 | #if wxUSE_GUI | |
575 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
576 | #else | |
577 | const wxChar *wxLog::ms_timestamp = NULL; // save space | |
578 | #endif | |
579 | ||
580 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; | |
581 | wxArrayString wxLog::ms_aTraceMasks; | |
582 | ||
583 | // ---------------------------------------------------------------------------- | |
584 | // stdout error logging helper | |
585 | // ---------------------------------------------------------------------------- | |
586 | ||
587 | // helper function: wraps the message and justifies it under given position | |
588 | // (looks more pretty on the terminal). Also adds newline at the end. | |
589 | // | |
590 | // TODO this is now disabled until I find a portable way of determining the | |
591 | // terminal window size (ok, I found it but does anybody really cares?) | |
592 | #ifdef LOG_PRETTY_WRAP | |
593 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
594 | { | |
595 | size_t nMax = 80; // FIXME | |
596 | size_t nStart = strlen(pszPrefix); | |
597 | fputs(pszPrefix, f); | |
598 | ||
599 | size_t n; | |
600 | while ( *psz != '\0' ) { | |
601 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
602 | putc(*psz++, f); | |
603 | ||
604 | // wrapped? | |
605 | if ( *psz != '\0' ) { | |
606 | /*putc('\n', f);*/ | |
607 | for ( n = 0; n < nStart; n++ ) | |
608 | putc(' ', f); | |
609 | ||
610 | // as we wrapped, squeeze all white space | |
611 | while ( isspace(*psz) ) | |
612 | psz++; | |
613 | } | |
614 | } | |
615 | ||
616 | putc('\n', f); | |
617 | } | |
618 | #endif //LOG_PRETTY_WRAP | |
619 | ||
620 | // ---------------------------------------------------------------------------- | |
621 | // error code/error message retrieval functions | |
622 | // ---------------------------------------------------------------------------- | |
623 | ||
624 | // get error code from syste | |
625 | unsigned long wxSysErrorCode() | |
626 | { | |
627 | #ifdef __WXMSW__ | |
628 | #ifdef __WIN32__ | |
629 | return ::GetLastError(); | |
630 | #else //WIN16 | |
631 | // TODO what to do on Windows 3.1? | |
632 | return 0; | |
633 | #endif //WIN16/32 | |
634 | #else //Unix | |
635 | return errno; | |
636 | #endif //Win/Unix | |
637 | } | |
638 | ||
639 | // get error message from system | |
640 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) | |
641 | { | |
642 | if ( nErrCode == 0 ) | |
643 | nErrCode = wxSysErrorCode(); | |
644 | ||
645 | #ifdef __WXMSW__ | |
646 | #ifdef __WIN32__ | |
647 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
648 | ||
649 | // get error message from system | |
650 | LPVOID lpMsgBuf; | |
651 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
652 | NULL, nErrCode, | |
653 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
654 | (LPTSTR)&lpMsgBuf, | |
655 | 0, NULL); | |
656 | ||
657 | // copy it to our buffer and free memory | |
658 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
659 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); | |
660 | LocalFree(lpMsgBuf); | |
661 | ||
662 | // returned string is capitalized and ended with '\r\n' - bad | |
663 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); | |
664 | size_t len = wxStrlen(s_szBuf); | |
665 | if ( len > 0 ) { | |
666 | // truncate string | |
667 | if ( s_szBuf[len - 2] == wxT('\r') ) | |
668 | s_szBuf[len - 2] = wxT('\0'); | |
669 | } | |
670 | ||
671 | return s_szBuf; | |
672 | #else //Win16 | |
673 | // TODO | |
674 | return NULL; | |
675 | #endif // Win16/32 | |
676 | #else // Unix | |
677 | #if wxUSE_UNICODE | |
678 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
679 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); | |
680 | return s_szBuf; | |
681 | #else | |
682 | return strerror((int)nErrCode); | |
683 | #endif | |
684 | #endif // Win/Unix | |
685 | } | |
686 | ||
687 | // ---------------------------------------------------------------------------- | |
688 | // debug helper | |
689 | // ---------------------------------------------------------------------------- | |
690 | ||
691 | #ifdef __WXDEBUG__ | |
692 | ||
693 | // break into the debugger | |
694 | void Trap() | |
695 | { | |
696 | #ifdef __WXMSW__ | |
697 | DebugBreak(); | |
698 | #elif defined(__WXMAC__) | |
699 | #if __powerc | |
700 | Debugger(); | |
701 | #else | |
702 | SysBreak(); | |
703 | #endif | |
704 | #elif defined(__UNIX__) | |
705 | raise(SIGTRAP); | |
706 | #else | |
707 | // TODO | |
708 | #endif // Win/Unix | |
709 | } | |
710 | ||
711 | // this function is called when an assert fails | |
712 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
713 | { | |
714 | // this variable can be set to true to suppress "assert failure" messages | |
715 | static bool s_bNoAsserts = FALSE; | |
716 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
717 | ||
718 | if ( s_bInAssert ) { | |
719 | // He-e-e-e-elp!! we're trapped in endless loop | |
720 | Trap(); | |
721 | ||
722 | s_bInAssert = FALSE; | |
723 | ||
724 | return; | |
725 | } | |
726 | ||
727 | s_bInAssert = TRUE; | |
728 | ||
729 | wxChar szBuf[LOG_BUFFER_SIZE]; | |
730 | ||
731 | // make life easier for people using VC++ IDE: clicking on the message | |
732 | // will take us immediately to the place of the failed assert | |
733 | wxSnprintf(szBuf, WXSIZEOF(szBuf), | |
734 | #ifdef __VISUALC__ | |
735 | wxT("%s(%d): assert failed"), | |
736 | #else // !VC++ | |
737 | // make the error message more clear for all the others | |
738 | wxT("Assert failed in file %s at line %d"), | |
739 | #endif // VC/!VC | |
740 | szFile, nLine); | |
741 | ||
742 | if ( szMsg != NULL ) { | |
743 | wxStrcat(szBuf, wxT(": ")); | |
744 | wxStrcat(szBuf, szMsg); | |
745 | } | |
746 | else { | |
747 | wxStrcat(szBuf, wxT(".")); | |
748 | } | |
749 | ||
750 | if ( !s_bNoAsserts ) { | |
751 | // send it to the normal log destination | |
752 | wxLogDebug(szBuf); | |
753 | ||
754 | #if wxUSE_GUI || defined(__WXMSW__) | |
755 | // this message is intentionally not translated - it is for | |
756 | // developpers only | |
757 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings.")); | |
758 | ||
759 | // use the native message box if available: this is more robust than | |
760 | // using our own | |
761 | #ifdef __WXMSW__ | |
762 | switch ( ::MessageBox(NULL, szBuf, _T("Debug"), | |
763 | MB_YESNOCANCEL | MB_ICONSTOP ) ) { | |
764 | case IDYES: | |
765 | Trap(); | |
766 | break; | |
767 | ||
768 | case IDCANCEL: | |
769 | s_bNoAsserts = TRUE; | |
770 | break; | |
771 | ||
772 | //case IDNO: nothing to do | |
773 | } | |
774 | #else // !MSW | |
775 | switch ( wxMessageBox(szBuf, wxT("Debug"), | |
776 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
777 | case wxYES: | |
778 | Trap(); | |
779 | break; | |
780 | ||
781 | case wxCANCEL: | |
782 | s_bNoAsserts = TRUE; | |
783 | break; | |
784 | ||
785 | //case wxNO: nothing to do | |
786 | } | |
787 | #endif // GUI or MSW | |
788 | ||
789 | #else // !GUI | |
790 | Trap(); | |
791 | #endif // GUI/!GUI | |
792 | } | |
793 | ||
794 | s_bInAssert = FALSE; | |
795 | } | |
796 | ||
797 | #endif //WXDEBUG | |
798 | ||
799 | #endif //wxUSE_LOG |