]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
latest changes: added word-wise movement (buggy, esp. backwards), more minor
[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:
88ac883a 508#if wxUSE_STATUSBAR
0fb67cd1
VZ
509 {
510 // find the top window and set it's status text if it has any
511 wxFrame *pFrame = gs_pFrame;
512 if ( pFrame == NULL ) {
513 wxWindow *pWin = wxTheApp->GetTopWindow();
514 if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) {
515 pFrame = (wxFrame *)pWin;
516 }
517 }
518
519 if ( pFrame != NULL )
520 pFrame->SetStatusText(szString);
521 }
88ac883a 522#endif // wxUSE_STATUSBAR
0fb67cd1
VZ
523 break;
524
525 case wxLOG_Trace:
526 case wxLOG_Debug:
527 #ifdef __WXDEBUG__
528 {
529 #ifdef __WXMSW__
530 // don't prepend debug/trace here: it goes to the
531 // debug window anyhow, but do put a timestamp
50920146 532 OutputDebugString(wxString(szString) + _T("\n\r"));
0fb67cd1
VZ
533 #else
534 // send them to stderr
84fff0b3
OK
535 wxFprintf(stderr, _T("%s: %s\n"),
536 level == wxLOG_Trace ? _T("Trace") : _T("Debug"),
537 szString);
0fb67cd1
VZ
538 fflush(stderr);
539 #endif
540 }
541 #endif // __WXDEBUG__
542
543 break;
544
545 case wxLOG_FatalError:
546 // show this one immediately
547 wxMessageBox(szString, _("Fatal error"), wxICON_HAND);
548 break;
549
550 case wxLOG_Error:
551 // discard earlier informational messages if this is the 1st
552 // error because they might not make sense any more
553 if ( !m_bErrors ) {
554 m_aMessages.Empty();
555 m_aTimes.Empty();
556 m_bHasMessages = TRUE;
557 m_bErrors = TRUE;
558 }
559 // fall through
560
561 case wxLOG_Warning:
562 if ( !m_bErrors ) {
563 // for the warning we don't discard the info messages
564 m_bWarnings = TRUE;
565 }
566
567 m_aMessages.Add(szString);
568 m_aTimes.Add((long)t);
569 break;
0fb67cd1 570 }
c801d85f
KB
571}
572
9ef3052c 573// ----------------------------------------------------------------------------
fe7b1156 574// wxLogWindow and wxLogFrame implementation
9ef3052c
VZ
575// ----------------------------------------------------------------------------
576
577// log frame class
fe7b1156 578// ---------------
9ef3052c
VZ
579class wxLogFrame : public wxFrame
580{
581public:
0fb67cd1 582 // ctor & dtor
50920146 583 wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle);
0fb67cd1 584 virtual ~wxLogFrame();
9ef3052c 585
0fb67cd1
VZ
586 // menu callbacks
587 void OnClose(wxCommandEvent& event);
588 void OnCloseWindow(wxCloseEvent& event);
88ac883a 589#if wxUSE_FILE
0fb67cd1 590 void OnSave (wxCommandEvent& event);
88ac883a 591#endif // wxUSE_FILE
0fb67cd1 592 void OnClear(wxCommandEvent& event);
9ef3052c 593
0fb67cd1 594 void OnIdle(wxIdleEvent&);
fe7b1156 595
0fb67cd1
VZ
596 // accessors
597 wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
9ef3052c
VZ
598
599private:
0fb67cd1
VZ
600 enum
601 {
602 Menu_Close = 100,
603 Menu_Save,
604 Menu_Clear
605 };
9ef3052c 606
0fb67cd1
VZ
607 // instead of closing just hide the window to be able to Show() it later
608 void DoClose() { Show(FALSE); }
fe7b1156 609
0fb67cd1
VZ
610 wxTextCtrl *m_pTextCtrl;
611 wxLogWindow *m_log;
9ef3052c 612
0fb67cd1 613 DECLARE_EVENT_TABLE()
9ef3052c
VZ
614};
615
616BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
0fb67cd1
VZ
617 // wxLogWindow menu events
618 EVT_MENU(Menu_Close, wxLogFrame::OnClose)
88ac883a 619#if wxUSE_FILE
0fb67cd1 620 EVT_MENU(Menu_Save, wxLogFrame::OnSave)
88ac883a 621#endif // wxUSE_FILE
0fb67cd1 622 EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
9ef3052c 623
0fb67cd1 624 EVT_CLOSE(wxLogFrame::OnCloseWindow)
9ec05cc9 625END_EVENT_TABLE()
9ef3052c 626
50920146 627wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle)
470b7da3 628 : wxFrame(pParent, -1, szTitle)
9ef3052c 629{
0fb67cd1
VZ
630 m_log = log;
631
632 m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
633 wxDefaultSize,
634 wxTE_MULTILINE |
635 wxHSCROLL |
636 wxTE_READONLY);
637
638 // create menu
639 wxMenuBar *pMenuBar = new wxMenuBar;
640 wxMenu *pMenu = new wxMenu;
88ac883a 641#if wxUSE_FILE
0fb67cd1 642 pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file"));
88ac883a 643#endif // wxUSE_FILE
0fb67cd1
VZ
644 pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents"));
645 pMenu->AppendSeparator();
646 pMenu->Append(Menu_Close, _("&Close"), _("Close this window"));
647 pMenuBar->Append(pMenu, _("&Log"));
648 SetMenuBar(pMenuBar);
649
88ac883a 650#if wxUSE_STATUSBAR
0fb67cd1
VZ
651 // status bar for menu prompts
652 CreateStatusBar();
88ac883a 653#endif // wxUSE_STATUSBAR
0fb67cd1
VZ
654
655 m_log->OnFrameCreate(this);
9ef3052c
VZ
656}
657
46dc76ba 658void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
9ef3052c 659{
0fb67cd1 660 DoClose();
9ef3052c
VZ
661}
662
46dc76ba 663void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
5260b1c5 664{
0fb67cd1 665 DoClose();
fe7b1156
VZ
666}
667
88ac883a 668#if wxUSE_FILE
46dc76ba 669void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
9ef3052c 670{
0fb67cd1
VZ
671 // get the file name
672 // -----------------
50920146 673 const wxChar *szFileName = wxSaveFileSelector(_T("log"), _T("txt"), _T("log.txt"));
0fb67cd1
VZ
674 if ( szFileName == NULL ) {
675 // cancelled
676 return;
677 }
9ef3052c 678
0fb67cd1
VZ
679 // open file
680 // ---------
681 wxFile file;
682 bool bOk = FALSE;
683 if ( wxFile::Exists(szFileName) ) {
684 bool bAppend = FALSE;
685 wxString strMsg;
686 strMsg.Printf(_("Append log to file '%s' "
9ef3052c 687 "(choosing [No] will overwrite it)?"), szFileName);
0fb67cd1
VZ
688 switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) {
689 case wxYES:
690 bAppend = TRUE;
691 break;
9ef3052c 692
0fb67cd1
VZ
693 case wxNO:
694 bAppend = FALSE;
695 break;
9ef3052c 696
0fb67cd1
VZ
697 case wxCANCEL:
698 return;
9ef3052c 699
0fb67cd1
VZ
700 default:
701 wxFAIL_MSG(_("invalid message box return value"));
702 }
9ef3052c 703
0fb67cd1
VZ
704 if ( bAppend ) {
705 bOk = file.Open(szFileName, wxFile::write_append);
706 }
707 else {
708 bOk = file.Create(szFileName, TRUE /* overwrite */);
709 }
9ef3052c
VZ
710 }
711 else {
0fb67cd1 712 bOk = file.Create(szFileName);
9ef3052c 713 }
9ef3052c 714
0fb67cd1
VZ
715 // retrieve text and save it
716 // -------------------------
717 int nLines = m_pTextCtrl->GetNumberOfLines();
718 for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) {
719 bOk = file.Write(m_pTextCtrl->GetLineText(nLine) +
720 // we're not going to pull in the whole wxTextFile if all we need is this...
1ccbb61a 721#if wxUSE_TEXTFILE
0fb67cd1 722 wxTextFile::GetEOL()
1ccbb61a 723#else // !wxUSE_TEXTFILE
0fb67cd1 724 '\n'
1ccbb61a 725#endif // wxUSE_TEXTFILE
0fb67cd1
VZ
726 );
727 }
9ec05cc9 728
0fb67cd1
VZ
729 if ( bOk )
730 bOk = file.Close();
9ef3052c 731
0fb67cd1
VZ
732 if ( !bOk ) {
733 wxLogError(_("Can't save log contents to file."));
734 }
735 else {
736 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName);
737 }
9ef3052c 738}
88ac883a 739#endif // wxUSE_FILE
9ef3052c 740
46dc76ba 741void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
9ef3052c 742{
0fb67cd1 743 m_pTextCtrl->Clear();
9ef3052c
VZ
744}
745
fe7b1156
VZ
746wxLogFrame::~wxLogFrame()
747{
0fb67cd1 748 m_log->OnFrameDelete(this);
fe7b1156
VZ
749}
750
751// wxLogWindow
752// -----------
470b7da3 753wxLogWindow::wxLogWindow(wxFrame *pParent,
88ac883a
VZ
754 const wxChar *szTitle,
755 bool bShow,
756 bool bDoPass)
9ef3052c 757{
0fb67cd1 758 m_bPassMessages = bDoPass;
a3622daa 759
0fb67cd1
VZ
760 m_pLogFrame = new wxLogFrame(pParent, this, szTitle);
761 m_pOldLog = wxLog::SetActiveTarget(this);
9ec05cc9 762
0fb67cd1
VZ
763 if ( bShow )
764 m_pLogFrame->Show(TRUE);
9ef3052c
VZ
765}
766
e51c4943 767void wxLogWindow::Show(bool bShow)
9ef3052c 768{
0fb67cd1 769 m_pLogFrame->Show(bShow);
9ef3052c
VZ
770}
771
fe7b1156 772void wxLogWindow::Flush()
06db8ebd 773{
0fb67cd1
VZ
774 if ( m_pOldLog != NULL )
775 m_pOldLog->Flush();
fe7b1156 776
0fb67cd1 777 m_bHasMessages = FALSE;
06db8ebd
VZ
778}
779
50920146 780void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
9ef3052c 781{
0fb67cd1
VZ
782 // first let the previous logger show it
783 if ( m_pOldLog != NULL && m_bPassMessages ) {
784 // FIXME why can't we access protected wxLog method from here (we derive
785 // from wxLog)? gcc gives "DoLog is protected in this context", what
786 // does this mean? Anyhow, the cast is harmless and let's us do what
787 // we want.
788 ((wxLogWindow *)m_pOldLog)->DoLog(level, szString, t);
789 }
9ec05cc9 790
0fb67cd1
VZ
791 if ( m_pLogFrame ) {
792 switch ( level ) {
793 case wxLOG_Status:
794 // by default, these messages are ignored by wxLog, so process
795 // them ourselves
37278984 796 if ( !wxIsEmpty(szString) )
0fb67cd1
VZ
797 {
798 wxString str;
799 str << _("Status: ") << szString;
800 DoLogString(str, t);
801 }
802 break;
803
804 // don't put trace messages in the text window for 2 reasons:
805 // 1) there are too many of them
806 // 2) they may provoke other trace messages thus sending a program
807 // into an infinite loop
808 case wxLOG_Trace:
809 break;
810
811 default:
812 // and this will format it nicely and call our DoLogString()
813 wxLog::DoLog(level, szString, t);
1c4a764c 814 }
1c4a764c 815 }
fe7b1156 816
0fb67cd1 817 m_bHasMessages = TRUE;
9ef3052c
VZ
818}
819
74e3313b 820void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
9ef3052c 821{
0fb67cd1
VZ
822 // put the text into our window
823 wxTextCtrl *pText = m_pLogFrame->TextCtrl();
9ef3052c 824
0fb67cd1
VZ
825 // remove selection (WriteText is in fact ReplaceSelection)
826#ifdef __WXMSW__
f42d2625
VZ
827 long nLen = pText->GetLastPosition();
828 pText->SetSelection(nLen, nLen);
0fb67cd1 829#endif // Windows
9ef3052c 830
0fb67cd1 831 pText->WriteText(szString);
50920146 832 pText->WriteText(_T("\n")); // "\n" ok here (_not_ "\r\n")
9ef3052c 833
0fb67cd1 834 // TODO ensure that the line can be seen
9ef3052c
VZ
835}
836
fe7b1156
VZ
837wxFrame *wxLogWindow::GetFrame() const
838{
0fb67cd1 839 return m_pLogFrame;
fe7b1156
VZ
840}
841
37cda9cb 842void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame))
fe7b1156
VZ
843{
844}
845
37cda9cb 846void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame))
fe7b1156 847{
0fb67cd1 848 m_pLogFrame = (wxLogFrame *)NULL;
fe7b1156
VZ
849}
850
9ef3052c
VZ
851wxLogWindow::~wxLogWindow()
852{
0fb67cd1 853 delete m_pOldLog;
c085e333 854
0fb67cd1
VZ
855 // may be NULL if log frame already auto destroyed itself
856 delete m_pLogFrame;
9ef3052c
VZ
857}
858
27fc802d 859#endif //wxUSE_NOGUI
c801d85f
KB
860
861// ============================================================================
862// Global functions/variables
863// ============================================================================
864
865// ----------------------------------------------------------------------------
866// static variables
867// ----------------------------------------------------------------------------
0fb67cd1
VZ
868
869wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
870bool wxLog::ms_doLog = TRUE;
871bool wxLog::ms_bAutoCreate = TRUE;
872wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
873wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
874
875// ----------------------------------------------------------------------------
876// stdout error logging helper
877// ----------------------------------------------------------------------------
878
879// helper function: wraps the message and justifies it under given position
880// (looks more pretty on the terminal). Also adds newline at the end.
881//
0fb67cd1
VZ
882// TODO this is now disabled until I find a portable way of determining the
883// terminal window size (ok, I found it but does anybody really cares?)
884#ifdef LOG_PRETTY_WRAP
c801d85f
KB
885static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
886{
0fb67cd1
VZ
887 size_t nMax = 80; // FIXME
888 size_t nStart = strlen(pszPrefix);
889 fputs(pszPrefix, f);
890
891 size_t n;
892 while ( *psz != '\0' ) {
893 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
894 putc(*psz++, f);
895
896 // wrapped?
897 if ( *psz != '\0' ) {
898 /*putc('\n', f);*/
899 for ( n = 0; n < nStart; n++ )
900 putc(' ', f);
901
902 // as we wrapped, squeeze all white space
903 while ( isspace(*psz) )
904 psz++;
905 }
c801d85f 906 }
c801d85f 907
0fb67cd1 908 putc('\n', f);
c801d85f
KB
909}
910#endif //LOG_PRETTY_WRAP
911
912// ----------------------------------------------------------------------------
913// error code/error message retrieval functions
914// ----------------------------------------------------------------------------
915
916// get error code from syste
917unsigned long wxSysErrorCode()
918{
0fb67cd1
VZ
919#ifdef __WXMSW__
920#ifdef __WIN32__
921 return ::GetLastError();
922#else //WIN16
923 // TODO what to do on Windows 3.1?
924 return 0;
925#endif //WIN16/32
926#else //Unix
c801d85f 927 return errno;
0fb67cd1 928#endif //Win/Unix
c801d85f
KB
929}
930
931// get error message from system
50920146 932const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 933{
0fb67cd1
VZ
934 if ( nErrCode == 0 )
935 nErrCode = wxSysErrorCode();
936
937#ifdef __WXMSW__
938#ifdef __WIN32__
50920146 939 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
940
941 // get error message from system
942 LPVOID lpMsgBuf;
943 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
944 NULL, nErrCode,
945 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
946 (LPTSTR)&lpMsgBuf,
947 0, NULL);
948
949 // copy it to our buffer and free memory
50920146
OK
950 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
951 s_szBuf[WXSIZEOF(s_szBuf) - 1] = _T('\0');
0fb67cd1
VZ
952 LocalFree(lpMsgBuf);
953
954 // returned string is capitalized and ended with '\r\n' - bad
50920146
OK
955 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
956 size_t len = wxStrlen(s_szBuf);
0fb67cd1 957 if ( len > 0 ) {
9ef3052c 958 // truncate string
50920146
OK
959 if ( s_szBuf[len - 2] == _T('\r') )
960 s_szBuf[len - 2] = _T('\0');
0fb67cd1
VZ
961 }
962
963 return s_szBuf;
964#else //Win16
965 // TODO
966 return NULL;
967#endif // Win16/32
968#else // Unix
50920146
OK
969#if wxUSE_UNICODE
970 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
971 wxConv_libc.MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
972 return s_szBuf;
973#else
9ef3052c 974 return strerror(nErrCode);
50920146 975#endif
0fb67cd1 976#endif // Win/Unix
c801d85f
KB
977}
978
979// ----------------------------------------------------------------------------
980// debug helper
981// ----------------------------------------------------------------------------
982
b2aef89b 983#ifdef __WXDEBUG__
c801d85f 984
0fb67cd1 985// break into the debugger
7502ba29
VZ
986void Trap()
987{
0fb67cd1 988#ifdef __WXMSW__
7502ba29 989 DebugBreak();
0fb67cd1
VZ
990#elif defined(__WXMAC__)
991#if __powerc
17dff81c 992 Debugger();
0fb67cd1 993#else
17dff81c 994 SysBreak();
0fb67cd1
VZ
995#endif
996#elif defined(__UNIX__)
7502ba29 997 raise(SIGTRAP);
0fb67cd1
VZ
998#else
999 // TODO
1000#endif // Win/Unix
7502ba29
VZ
1001}
1002
c801d85f 1003// this function is called when an assert fails
6f9eb452 1004void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
c801d85f 1005{
0fb67cd1
VZ
1006 // this variable can be set to true to suppress "assert failure" messages
1007 static bool s_bNoAsserts = FALSE;
1008 static bool s_bInAssert = FALSE; // FIXME MT-unsafe
7502ba29 1009
0fb67cd1
VZ
1010 if ( s_bInAssert ) {
1011 // He-e-e-e-elp!! we're trapped in endless loop
1012 Trap();
c085e333 1013
0fb67cd1 1014 s_bInAssert = FALSE;
1e8a4bc2 1015
0fb67cd1
VZ
1016 return;
1017 }
7502ba29 1018
0fb67cd1 1019 s_bInAssert = TRUE;
c801d85f 1020
50920146 1021 wxChar szBuf[LOG_BUFFER_SIZE];
c263077e 1022
0fb67cd1
VZ
1023 // make life easier for people using VC++ IDE: clicking on the message
1024 // will take us immediately to the place of the failed assert
3f4a0c5b 1025#ifdef __VISUALC__
84fff0b3 1026 wxSprintf(szBuf, _T("%s(%d): assert failed"), szFile, nLine);
c263077e 1027#else // !VC++
0fb67cd1 1028 // make the error message more clear for all the others
50920146 1029 wxSprintf(szBuf, _T("Assert failed in file %s at line %d"), szFile, nLine);
c263077e
VZ
1030#endif // VC/!VC
1031
0fb67cd1 1032 if ( szMsg != NULL ) {
50920146
OK
1033 wxStrcat(szBuf, _T(": "));
1034 wxStrcat(szBuf, szMsg);
0fb67cd1
VZ
1035 }
1036 else {
50920146 1037 wxStrcat(szBuf, _T("."));
0fb67cd1 1038 }
c801d85f 1039
0fb67cd1
VZ
1040 if ( !s_bNoAsserts ) {
1041 // send it to the normal log destination
1042 wxLogDebug(szBuf);
7502ba29 1043
0fb67cd1
VZ
1044#if wxUSE_NOGUI
1045 Trap();
1046#else
1047 // this message is intentionally not translated - it is for
1048 // developpers only
50920146 1049 wxStrcat(szBuf, _T("\nDo you want to stop the program?"
0fb67cd1 1050 "\nYou can also choose [Cancel] to suppress "
50920146 1051 "further warnings."));
0fb67cd1
VZ
1052
1053 switch ( wxMessageBox(szBuf, _("Debug"),
1054 wxYES_NO | wxCANCEL | wxICON_STOP ) ) {
1055 case wxYES:
1056 Trap();
1057 break;
1058
1059 case wxCANCEL:
1060 s_bNoAsserts = TRUE;
1061 break;
1062
1063 //case wxNO: nothing to do
1064 }
1065#endif // USE_NOGUI
1066 }
1067
1068 s_bInAssert = FALSE;
c801d85f
KB
1069}
1070
b2aef89b 1071#endif //WXDEBUG
c801d85f 1072