]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
ff0532eb05c6aa48f5ab8da36c4497e6d5a869e9
[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 #ifdef __GNUG__
20 #pragma implementation "log.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 // wxWindows
31 #ifndef WX_PRECOMP
32 #include <wx/app.h>
33 #include <wx/string.h>
34 #include <wx/intl.h>
35 #include <wx/menu.h>
36
37 #include <wx/generic/msgdlgg.h>
38 #include <wx/filedlg.h>
39 #include <wx/textctrl.h>
40 #endif //WX_PRECOMP
41
42 #include <wx/file.h>
43 #include <wx/textfile.h>
44 #include <wx/utils.h>
45 #include <wx/log.h>
46
47 // other standard headers
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <time.h>
51
52 #ifdef __WXMSW__
53 #include <windows.h>
54 #else //Unix
55 #include <signal.h>
56 #endif //Win/Unix
57
58 // ----------------------------------------------------------------------------
59 // non member functions
60 // ----------------------------------------------------------------------------
61
62 // define this to enable wrapping of log messages
63 //#define LOG_PRETTY_WRAP
64
65 #ifdef LOG_PRETTY_WRAP
66 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
67 #endif
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73 // ----------------------------------------------------------------------------
74 // implementation of Log functions
75 //
76 // NB: unfortunately we need all these distinct functions, we can't make them
77 // macros and not all compilers inline vararg functions.
78 // ----------------------------------------------------------------------------
79
80 // log functions can't allocate memory (LogError("out of memory...") should
81 // work!), so we use a static buffer for all log messages
82 #define LOG_BUFFER_SIZE (4096)
83
84 // static buffer for error messages (@@@ MT-unsafe)
85 static char s_szBuf[LOG_BUFFER_SIZE];
86
87 // generic log function
88 void wxLogGeneric(wxLogLevel level, const char *szFormat, ...)
89 {
90 if ( wxLog::GetActiveTarget() != NULL ) {
91 va_list argptr;
92 va_start(argptr, szFormat);
93 vsprintf(s_szBuf, szFormat, argptr);
94 va_end(argptr);
95
96 wxLog::OnLog(level, s_szBuf);
97 }
98 }
99
100 #define IMPLEMENT_LOG_FUNCTION(level) \
101 void wxLog##level(const char *szFormat, ...) \
102 { \
103 if ( wxLog::GetActiveTarget() != NULL ) { \
104 va_list argptr; \
105 va_start(argptr, szFormat); \
106 vsprintf(s_szBuf, szFormat, argptr); \
107 va_end(argptr); \
108 \
109 wxLog::OnLog(wxLOG_##level, s_szBuf); \
110 } \
111 }
112
113 IMPLEMENT_LOG_FUNCTION(FatalError)
114 IMPLEMENT_LOG_FUNCTION(Error)
115 IMPLEMENT_LOG_FUNCTION(Warning)
116 IMPLEMENT_LOG_FUNCTION(Message)
117 IMPLEMENT_LOG_FUNCTION(Info)
118 IMPLEMENT_LOG_FUNCTION(Status)
119
120 // same as info, but only if 'verbose' mode is on
121 void wxLogVerbose(const char *szFormat, ...)
122 {
123 wxLog *pLog = wxLog::GetActiveTarget();
124 if ( pLog != NULL && pLog->GetVerbose() ) {
125 va_list argptr;
126 va_start(argptr, szFormat);
127 vsprintf(s_szBuf, szFormat, argptr);
128 va_end(argptr);
129
130 wxLog::OnLog(wxLOG_Info, s_szBuf);
131 }
132 }
133
134 // debug functions
135 #ifdef __WXDEBUG__
136 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
137 void wxLog##level(const char *szFormat, ...) \
138 { \
139 if ( wxLog::GetActiveTarget() != NULL ) { \
140 va_list argptr; \
141 va_start(argptr, szFormat); \
142 vsprintf(s_szBuf, szFormat, argptr); \
143 va_end(argptr); \
144 \
145 wxLog::OnLog(wxLOG_##level, s_szBuf); \
146 } \
147 }
148
149 void wxLogTrace(wxTraceMask mask, const char *szFormat, ...)
150 {
151 wxLog *pLog = wxLog::GetActiveTarget();
152
153 // we check that all of mask bits are set in the current mask, so
154 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
155 // if both bits are set.
156 if ( pLog != NULL && (pLog->GetTraceMask() & mask == mask) ) {
157 va_list argptr;
158 va_start(argptr, szFormat);
159 vsprintf(s_szBuf, szFormat, argptr);
160 va_end(argptr);
161
162 wxLog::OnLog(wxLOG_Trace, s_szBuf);
163 }
164 }
165
166 #else // release
167 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
168 #endif
169
170 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
171 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
172
173 // wxLogSysError: one uses the last error code, for other you must give it
174 // explicitly
175
176 // common part of both wxLogSysError
177 void wxLogSysErrorHelper(long lErrCode)
178 {
179 char szErrMsg[LOG_BUFFER_SIZE / 2];
180 sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
181 strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf));
182
183 wxLog::OnLog(wxLOG_Error, s_szBuf);
184 }
185
186 void WXDLLEXPORT wxLogSysError(const char *szFormat, ...)
187 {
188 va_list argptr;
189 va_start(argptr, szFormat);
190 vsprintf(s_szBuf, szFormat, argptr);
191 va_end(argptr);
192
193 wxLogSysErrorHelper(wxSysErrorCode());
194 }
195
196 void WXDLLEXPORT wxLogSysError(long lErrCode, const char *szFormat, ...)
197 {
198 va_list argptr;
199 va_start(argptr, szFormat);
200 vsprintf(s_szBuf, szFormat, argptr);
201 va_end(argptr);
202
203 wxLogSysErrorHelper(lErrCode);
204 }
205
206 // ----------------------------------------------------------------------------
207 // wxLog class implementation
208 // ----------------------------------------------------------------------------
209
210 wxLog::wxLog()
211 {
212 m_bHasMessages = FALSE;
213 m_bVerbose = FALSE;
214 m_szTimeFormat = "[%d/%b/%y %H:%M:%S] ";
215 }
216
217 wxLog *wxLog::GetActiveTarget()
218 {
219 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
220 // prevent infinite recursion if someone calls wxLogXXX() from
221 // wxApp::CreateLogTarget()
222 static bool s_bInGetActiveTarget = FALSE;
223 if ( !s_bInGetActiveTarget ) {
224 s_bInGetActiveTarget = TRUE;
225
226 #ifdef WX_TEST_MINIMAL
227 ms_pLogger = new wxLogStderr;
228 #else
229 // ask the application to create a log target for us
230 ms_pLogger = wxTheApp->CreateLogTarget();
231 #endif
232
233 // do nothing if it fails - what can we do?
234 }
235 }
236
237 return ms_pLogger;
238 }
239
240 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
241 {
242 // flush the old messages before changing
243 if ( ms_pLogger != NULL )
244 ms_pLogger->Flush();
245
246 wxLog *pOldLogger = ms_pLogger;
247 ms_pLogger = pLogger;
248 return pOldLogger;
249 }
250
251 void wxLog::DoLog(wxLogLevel level, const char *szString)
252 {
253 wxString str;
254
255 // prepend a timestamp if not disabled
256 if ( !IsEmpty(m_szTimeFormat) ) {
257 char szBuf[128];
258 time_t timeNow;
259 struct tm *ptmNow;
260
261 time(&timeNow);
262 ptmNow = localtime(&timeNow);
263
264 strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow);
265 str = szBuf;
266 }
267
268 switch ( level ) {
269 case wxLOG_FatalError:
270 DoLogString(str << _("Fatal error: ") << szString);
271 DoLogString(_("Program aborted."));
272 Flush();
273 abort();
274 break;
275
276 case wxLOG_Error:
277 DoLogString(str << _("Error: ") << szString);
278 break;
279
280 case wxLOG_Warning:
281 DoLogString(str << _("Warning: ") << szString);
282 break;
283
284 case wxLOG_Info:
285 if ( GetVerbose() )
286 case wxLOG_Message:
287 DoLogString(str + szString);
288 // fall through
289
290 case wxLOG_Status:
291 // nothing to do
292 break;
293
294 case wxLOG_Trace:
295 case wxLOG_Debug:
296 #ifdef __WXDEBUG__
297 #ifdef __WIN32__
298 // in addition to normal logging, also send the string to debugger
299 // (don't prepend "Debug" here: it will go to debug window anyhow)
300 ::OutputDebugString(str + szString + "\n\r");
301 #endif //Win32
302 DoLogString(str << (level == wxLOG_Trace ? _("Trace") : _("Debug"))
303 << ": " << szString);
304 #endif
305
306 break;
307
308 default:
309 wxFAIL_MSG("unknown log level in wxLog::DoLog");
310 }
311 }
312
313 void wxLog::DoLogString(const char *WXUNUSED(szString))
314 {
315 wxFAIL_MSG("DoLogString must be overrided if it's called.");
316 }
317
318 void wxLog::Flush()
319 {
320 // do nothing
321 }
322
323 // ----------------------------------------------------------------------------
324 // wxLogStderr class implementation
325 // ----------------------------------------------------------------------------
326
327 wxLogStderr::wxLogStderr(FILE *fp)
328 {
329 if ( fp == NULL )
330 m_fp = stderr;
331 else
332 m_fp = fp;
333 }
334
335 void wxLogStderr::DoLogString(const char *szString)
336 {
337 fputs(szString, m_fp);
338 fputc('\n', m_fp);
339 fflush(m_fp);
340 }
341
342 // ----------------------------------------------------------------------------
343 // wxLogStream implementation
344 // ----------------------------------------------------------------------------
345
346 wxLogStream::wxLogStream(ostream *ostr)
347 {
348 if ( ostr == NULL )
349 m_ostr = &cerr;
350 else
351 m_ostr = ostr;
352 }
353
354 void wxLogStream::DoLogString(const char *szString)
355 {
356 (*m_ostr) << szString << endl << flush;
357 }
358
359 // ----------------------------------------------------------------------------
360 // wxLogTextCtrl implementation
361 // ----------------------------------------------------------------------------
362 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
363 // @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf
364 #ifndef __WXGTK__
365 : wxLogStream(new ostream(pTextCtrl))
366 #endif //GTK
367 {
368 }
369
370 wxLogTextCtrl::~wxLogTextCtrl()
371 {
372 #ifndef __WXGTK__
373 delete m_ostr;
374 #endif //GTK
375 }
376
377 // ----------------------------------------------------------------------------
378 // wxLogGui implementation
379 // ----------------------------------------------------------------------------
380
381 #ifndef WX_TEST_MINIMAL
382
383 wxLogGui::wxLogGui()
384 {
385 m_bErrors = FALSE;
386 }
387
388 void wxLogGui::Flush()
389 {
390 if ( !m_bHasMessages )
391 return;
392
393 // @@@ ugly...
394
395 // concatenate all strings (but not too many to not overfill the msg box)
396 wxString str;
397 uint nLines = 0,
398 nMsgCount = m_aMessages.Count();
399
400 // start from the most recent message
401 for ( uint n = nMsgCount; n > 0; n-- ) {
402 // for Windows strings longer than this value are wrapped (NT 4.0)
403 const uint nMsgLineWidth = 156;
404
405 nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth;
406
407 if ( nLines > 25 ) // don't put too many lines in message box
408 break;
409
410 str << m_aMessages[n - 1] << "\n";
411 }
412
413 if ( m_bErrors ) {
414 wxMessageBox(str, _("Error"), wxOK | wxICON_EXCLAMATION);
415 }
416 else {
417 wxMessageBox(str, _("Information"), wxOK | wxICON_INFORMATION);
418 }
419
420 // no undisplayed messages whatsoever
421 m_bHasMessages =
422 m_bErrors = FALSE;
423 m_aMessages.Empty();
424 }
425
426 // the default behaviour is to discard all informational messages if there
427 // are any errors/warnings.
428 void wxLogGui::DoLog(wxLogLevel level, const char *szString)
429 {
430 switch ( level ) {
431 case wxLOG_Info:
432 if ( GetVerbose() )
433 case wxLOG_Message:
434 if ( !m_bErrors ) {
435 m_aMessages.Add(szString);
436 m_bHasMessages = TRUE;
437 }
438 break;
439
440 case wxLOG_Status:
441 {
442 // find the top window and set it's status text if it has any
443 wxWindow *pWin = wxTheApp->GetTopWindow();
444 if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) {
445 wxFrame *pFrame = (wxFrame *)pWin;
446 pFrame->SetStatusText(szString);
447 }
448 }
449 break;
450
451 case wxLOG_Trace:
452 case wxLOG_Debug:
453 #ifdef __WXDEBUG__
454 #ifdef __WIN32__
455 OutputDebugString(szString);
456 OutputDebugString("\n\r");
457 #else //!WIN32
458 // send them to stderr
459 fprintf(stderr, "%s: %s\n",
460 level == wxLOG_Trace ? _("Trace") : _("Debug"), szString);
461 fflush(stderr);
462 #endif // WIN32
463 #endif
464 break;
465
466 case wxLOG_FatalError:
467 // show this one immediately
468 wxMessageBox(szString, _("Fatal error"), wxICON_HAND);
469 break;
470
471 case wxLOG_Error:
472 case wxLOG_Warning:
473 // discard earlier informational messages if this is the 1st error
474 if ( !m_bErrors ) {
475 m_aMessages.Empty();
476 m_bHasMessages = TRUE;
477 m_bErrors = TRUE;
478 }
479
480 m_aMessages.Add(szString);
481 break;
482
483 default:
484 wxFAIL_MSG("unknown log level in wxLogGui::DoLog");
485 }
486 }
487
488 // ----------------------------------------------------------------------------
489 // wxLogWindow implementation
490 // ----------------------------------------------------------------------------
491
492 // log frame class
493 class wxLogFrame : public wxFrame
494 {
495 public:
496 // ctor
497 wxLogFrame(const char *szTitle);
498
499 // menu callbacks
500 void OnClose(wxCommandEvent& event);
501 void OnCloseWindow(wxCloseEvent& event);
502 void OnSave (wxCommandEvent& event);
503 void OnClear(wxCommandEvent& event);
504
505 // accessors
506 wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
507
508 private:
509 enum
510 {
511 Menu_Close = 100,
512 Menu_Save,
513 Menu_Clear
514 };
515
516 wxTextCtrl *m_pTextCtrl;
517
518 DECLARE_EVENT_TABLE()
519 };
520
521 BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
522 // wxLogWindow menu events
523 EVT_MENU(Menu_Close, wxLogFrame::OnClose)
524 EVT_MENU(Menu_Save, wxLogFrame::OnSave)
525 EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
526
527 EVT_CLOSE(wxLogFrame::OnCloseWindow)
528 END_EVENT_TABLE()
529
530 wxLogFrame::wxLogFrame(const char *szTitle)
531 : wxFrame(NULL, -1, szTitle)
532 {
533 // we don't want to be a top-level frame because it would prevent the
534 // application termination when all other frames are closed
535 wxTopLevelWindows.DeleteObject(this);
536
537 // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating
538 // a rich edit control instead of a normal one we want
539 m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
540 wxDefaultSize,
541 wxSIMPLE_BORDER |
542 wxTE_MULTILINE |
543 wxHSCROLL |
544 wxTE_READONLY);
545 /*
546 m_pTextCtrl->SetEditable(FALSE);
547 m_pTextCtrl->SetRichEdit(FALSE);
548 */
549
550 // create menu
551 wxMenuBar *pMenuBar = new wxMenuBar;
552 wxMenu *pMenu = new wxMenu;
553 pMenu->Append(Menu_Save, "&Save...");
554 pMenu->Append(Menu_Clear, "C&lear");
555 pMenu->AppendSeparator();
556 pMenu->Append(Menu_Close, "&Close");
557 pMenuBar->Append(pMenu, "&Log");
558 SetMenuBar(pMenuBar);
559
560 // @@ what about status bar? needed (for menu prompts)?
561 }
562
563 void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
564 {
565 // just hide the window
566 Show(FALSE);
567 }
568
569 void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
570 {
571 // just hide the window
572 Show(FALSE);
573 }
574
575 void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
576 {
577 // get the file name
578 // -----------------
579 const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt");
580 if ( szFileName == NULL ) {
581 // cancelled
582 return;
583 }
584
585 // open file
586 // ---------
587 wxFile file;
588 bool bOk = FALSE;
589 if ( wxFile::Exists(szFileName) ) {
590 bool bAppend = FALSE;
591 wxString strMsg;
592 strMsg.Printf(_("Append log to file '%s' "
593 "(choosing [No] will overwrite it)?"), szFileName);
594 switch ( wxMessageBox(strMsg, "Question", wxYES_NO | wxCANCEL) ) {
595 case wxYES:
596 bAppend = TRUE;
597 break;
598
599 case wxNO:
600 bAppend = FALSE;
601 break;
602
603 case wxCANCEL:
604 return;
605
606 default:
607 wxFAIL_MSG("invalid message box return value");
608 }
609
610 if ( bAppend ) {
611 bOk = file.Open(szFileName, wxFile::write_append);
612 }
613 else {
614 bOk = file.Create(szFileName, TRUE /* overwrite */);
615 }
616 }
617 else {
618 bOk = file.Create(szFileName);
619 }
620
621 // retrieve text and save it
622 // -------------------------
623 #ifdef __WXGTK__
624 // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet
625 wxLogError("Sorry, this function is not implemented under GTK");
626 #else
627 int nLines = m_pTextCtrl->GetNumberOfLines();
628 for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) {
629 bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL());
630 }
631 #endif //GTK
632
633 if ( bOk )
634 bOk = file.Close();
635
636 if ( !bOk ) {
637 wxLogError(_("Can't save log contents to file."));
638 return;
639 }
640 }
641
642 void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
643 {
644 m_pTextCtrl->Clear();
645 }
646
647 wxLogWindow::wxLogWindow(const char *szTitle, bool bShow)
648 {
649 m_pOldLog = wxLog::GetActiveTarget();
650 m_pLogFrame = new wxLogFrame(szTitle);
651
652 if ( bShow )
653 m_pLogFrame->Show(TRUE);
654 }
655
656 void wxLogWindow::Show(bool bShow)
657 {
658 m_pLogFrame->Show(bShow);
659 }
660
661 void wxLogWindow::DoLog(wxLogLevel level, const char *szString)
662 {
663 // first let the previous logger show it
664 if ( m_pOldLog != NULL ) {
665 // @@@ why can't we access protected wxLog method from here (we derive
666 // from wxLog)? gcc gives "DoLog is protected in this context", what
667 // does this mean? Anyhow, the cast is harmless and let's us do what
668 // we want.
669 ((wxLogWindow *)m_pOldLog)->DoLog(level, szString);
670 }
671
672 // and this will format it nicely and call our DoLogString()
673 wxLog::DoLog(level, szString);
674 }
675
676 void wxLogWindow::DoLogString(const char *szString)
677 {
678 // put the text into our window
679 wxTextCtrl *pText = m_pLogFrame->TextCtrl();
680
681 // remove selection (WriteText is in fact ReplaceSelection)
682 #ifdef __WXMSW__
683 long nLen = pText->GetLastPosition();
684 pText->SetSelection(nLen, nLen);
685 #endif // Windows
686
687 pText->WriteText(szString);
688 pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n")
689
690 // ensure that the line can be seen
691 // @@@ TODO
692 }
693
694 wxLogWindow::~wxLogWindow()
695 {
696 m_pLogFrame->Close(TRUE);
697 }
698
699 #endif //WX_TEST_MINIMAL
700
701 // ============================================================================
702 // Global functions/variables
703 // ============================================================================
704
705 // ----------------------------------------------------------------------------
706 // static variables
707 // ----------------------------------------------------------------------------
708 wxLog *wxLog::ms_pLogger = NULL;
709 bool wxLog::ms_bAutoCreate = TRUE;
710 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
711
712 // ----------------------------------------------------------------------------
713 // stdout error logging helper
714 // ----------------------------------------------------------------------------
715
716 // helper function: wraps the message and justifies it under given position
717 // (looks more pretty on the terminal). Also adds newline at the end.
718 //
719 // @@ this is now disabled until I find a portable way of determining the
720 // terminal window size (ok, I found it but does anybody really cares?)
721 #ifdef LOG_PRETTY_WRAP
722 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
723 {
724 size_t nMax = 80; // @@@@
725 size_t nStart = strlen(pszPrefix);
726 fputs(pszPrefix, f);
727
728 size_t n;
729 while ( *psz != '\0' ) {
730 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
731 putc(*psz++, f);
732
733 // wrapped?
734 if ( *psz != '\0' ) {
735 /*putc('\n', f);*/
736 for ( n = 0; n < nStart; n++ )
737 putc(' ', f);
738
739 // as we wrapped, squeeze all white space
740 while ( isspace(*psz) )
741 psz++;
742 }
743 }
744
745 putc('\n', f);
746 }
747 #endif //LOG_PRETTY_WRAP
748
749 // ----------------------------------------------------------------------------
750 // error code/error message retrieval functions
751 // ----------------------------------------------------------------------------
752
753 // get error code from syste
754 unsigned long wxSysErrorCode()
755 {
756 #ifdef __WXMSW__
757 #ifdef __WIN32__
758 return ::GetLastError();
759 #else //WIN16
760 // @@@@ what to do on Windows 3.1?
761 return 0;
762 #endif //WIN16/32
763 #else //Unix
764 return errno;
765 #endif //Win/Unix
766 }
767
768 // get error message from system
769 const char *wxSysErrorMsg(unsigned long nErrCode)
770 {
771 if ( nErrCode == 0 )
772 nErrCode = wxSysErrorCode();
773
774 #ifdef __WXMSW__
775 #ifdef __WIN32__
776 static char s_szBuf[LOG_BUFFER_SIZE / 2];
777
778 // get error message from system
779 LPVOID lpMsgBuf;
780 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
781 NULL, nErrCode,
782 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
783 (LPTSTR)&lpMsgBuf,
784 0, NULL);
785
786 // copy it to our buffer and free memory
787 strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
788 s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0';
789 LocalFree(lpMsgBuf);
790
791 // returned string is capitalized and ended with '\r\n' - bad
792 s_szBuf[0] = (char)wxToLower(s_szBuf[0]);
793 size_t len = strlen(s_szBuf);
794 if ( len > 0 ) {
795 // truncate string
796 if ( s_szBuf[len - 2] == '\r' )
797 s_szBuf[len - 2] = '\0';
798 }
799
800 return s_szBuf;
801 #else //Win16
802 // TODO @@@@
803 return NULL;
804 #endif // Win16/32
805 #else // Unix
806 return strerror(nErrCode);
807 #endif // Win/Unix
808 }
809
810 // ----------------------------------------------------------------------------
811 // debug helper
812 // ----------------------------------------------------------------------------
813
814 #ifdef __WXDEBUG__
815
816 void Trap()
817 {
818 #ifdef __WXMSW__
819 DebugBreak();
820 #else // Unix
821 raise(SIGTRAP);
822 #endif // Win/Unix
823 }
824
825 // this function is called when an assert fails
826 void wxOnAssert(const char *szFile, int nLine, const char *szMsg)
827 {
828 // this variable can be set to true to suppress "assert failure" messages
829 static bool s_bNoAsserts = FALSE;
830 static bool s_bInAssert = FALSE;
831
832 if ( s_bInAssert ) {
833 // He-e-e-e-elp!! we're trapped in endless loop
834 Trap();
835 }
836
837 s_bInAssert = TRUE;
838
839 char szBuf[LOG_BUFFER_SIZE];
840 sprintf(szBuf, _("Assert failed in file %s at line %d"), szFile, nLine);
841 if ( szMsg != NULL ) {
842 strcat(szBuf, ": ");
843 strcat(szBuf, szMsg);
844 }
845 else {
846 strcat(szBuf, ".");
847 }
848
849 if ( !s_bNoAsserts ) {
850 // send it to the normal log destination
851 wxLogDebug(szBuf);
852
853 strcat(szBuf, _("\nDo you want to stop the program?"
854 "\nYou can also choose [Cancel] to suppress "
855 "further warnings."));
856
857 switch ( wxMessageBox(szBuf, _("Debug"),
858 wxYES_NO | wxCANCEL | wxICON_STOP ) ) {
859 case wxYES:
860 Trap();
861 break;
862
863 case wxCANCEL:
864 s_bNoAsserts = TRUE;
865 break;
866
867 //case wxNO: nothing to do
868 }
869 }
870
871 s_bInAssert = FALSE;
872 }
873
874 #endif //WXDEBUG
875