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