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