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