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