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