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