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