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