]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
implement GetBestSize() (patch 1386199)
[wxWidgets.git] / src / common / log.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/common/log.cpp
c801d85f
KB
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>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
dd85fc6b 19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
e2478fde 24 #pragma hdrstop
c801d85f
KB
25#endif
26
e2478fde
VZ
27#if wxUSE_LOG
28
77ffb593 29// wxWidgets
c801d85f 30#ifndef WX_PRECOMP
e4db172a 31 #include "wx/log.h"
e90c1d2a 32 #include "wx/app.h"
df5168c4 33 #include "wx/arrstr.h"
e2478fde
VZ
34 #include "wx/intl.h"
35 #include "wx/string.h"
de6185e2 36 #include "wx/utils.h"
9ef3052c 37#endif //WX_PRECOMP
c801d85f 38
e2478fde 39#include "wx/apptrait.h"
7b2d1c74 40#include "wx/datetime.h"
e2478fde 41#include "wx/file.h"
e2478fde
VZ
42#include "wx/msgout.h"
43#include "wx/textfile.h"
44#include "wx/thread.h"
e2478fde 45#include "wx/wxchar.h"
f94dfb38 46
c801d85f 47// other standard headers
1c193821 48#ifndef __WXWINCE__
e2478fde 49#include <errno.h>
1c193821
JS
50#endif
51
e2478fde 52#include <stdlib.h>
1c193821
JS
53
54#ifndef __WXWINCE__
e2478fde 55#include <time.h>
1c193821
JS
56#else
57#include "wx/msw/wince/time.h"
58#endif
31907d03 59
9cce3be7
VS
60#if defined(__WINDOWS__)
61 #include "wx/msw/private.h" // includes windows.h
62#endif
63
c801d85f
KB
64// ----------------------------------------------------------------------------
65// non member functions
66// ----------------------------------------------------------------------------
67
68// define this to enable wrapping of log messages
69//#define LOG_PRETTY_WRAP
70
9ef3052c 71#ifdef LOG_PRETTY_WRAP
c801d85f
KB
72 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
73#endif
74
75// ============================================================================
76// implementation
77// ============================================================================
78
b568d04f
VZ
79// ----------------------------------------------------------------------------
80// implementation of Log functions
81//
82// NB: unfortunately we need all these distinct functions, we can't make them
83// macros and not all compilers inline vararg functions.
84// ----------------------------------------------------------------------------
85
c801d85f 86// generic log function
1d63fd6b 87void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr)
c801d85f 88{
d68d8590 89 if ( wxLog::IsEnabled() ) {
2e7f3845 90 wxLog::OnLog(level, wxString::FormatV(szFormat, argptr), time(NULL));
807a903e 91 }
c801d85f
KB
92}
93
c9f78968 94void wxDoLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
ea44a631
GD
95{
96 va_list argptr;
97 va_start(argptr, szFormat);
1d63fd6b 98 wxVLogGeneric(level, szFormat, argptr);
ea44a631
GD
99 va_end(argptr);
100}
101
807a903e 102#define IMPLEMENT_LOG_FUNCTION(level) \
1d63fd6b 103 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
807a903e 104 { \
d68d8590 105 if ( wxLog::IsEnabled() ) { \
2e7f3845
VZ
106 wxLog::OnLog(wxLOG_##level, \
107 wxString::FormatV(szFormat, argptr), time(NULL));\
807a903e 108 } \
ea44a631 109 } \
ef0dd8e5 110 \
c9f78968 111 void wxDoLog##level(const wxChar *szFormat, ...) \
ea44a631
GD
112 { \
113 va_list argptr; \
114 va_start(argptr, szFormat); \
1800689f 115 wxVLog##level(szFormat, argptr); \
ea44a631 116 va_end(argptr); \
c801d85f
KB
117 }
118
c801d85f
KB
119IMPLEMENT_LOG_FUNCTION(Error)
120IMPLEMENT_LOG_FUNCTION(Warning)
121IMPLEMENT_LOG_FUNCTION(Message)
122IMPLEMENT_LOG_FUNCTION(Info)
123IMPLEMENT_LOG_FUNCTION(Status)
124
c11d62a6
VZ
125void wxSafeShowMessage(const wxString& title, const wxString& text)
126{
127#ifdef __WINDOWS__
128 ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP);
129#else
130 wxFprintf(stderr, _T("%s: %s\n"), title.c_str(), text.c_str());
65f06384 131 fflush(stderr);
c11d62a6
VZ
132#endif
133}
134
1800689f
VZ
135// fatal errors can't be suppressed nor handled by the custom log target and
136// always terminate the program
137void wxVLogFatalError(const wxChar *szFormat, va_list argptr)
138{
2e7f3845 139 wxSafeShowMessage(_T("Fatal Error"), wxString::FormatV(szFormat, argptr));
1800689f 140
1c193821
JS
141#ifdef __WXWINCE__
142 ExitThread(3);
143#else
1800689f 144 abort();
1c193821 145#endif
1800689f
VZ
146}
147
c9f78968 148void wxDoLogFatalError(const wxChar *szFormat, ...)
1800689f
VZ
149{
150 va_list argptr;
151 va_start(argptr, szFormat);
152 wxVLogFatalError(szFormat, argptr);
5e475383
VZ
153
154 // some compilers warn about unreachable code and it shouldn't matter
155 // for the others anyhow...
156 //va_end(argptr);
1800689f
VZ
157}
158
9ef3052c 159// same as info, but only if 'verbose' mode is on
1d63fd6b 160void wxVLogVerbose(const wxChar *szFormat, va_list argptr)
9ef3052c 161{
d68d8590 162 if ( wxLog::IsEnabled() ) {
2a1f999f 163 if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) {
2e7f3845
VZ
164 wxLog::OnLog(wxLOG_Info,
165 wxString::FormatV(szFormat, argptr), time(NULL));
807a903e
VZ
166 }
167 }
9ef3052c
VZ
168}
169
c9f78968 170void wxDoLogVerbose(const wxChar *szFormat, ...)
ea44a631
GD
171{
172 va_list argptr;
173 va_start(argptr, szFormat);
1d63fd6b 174 wxVLogVerbose(szFormat, argptr);
ea44a631
GD
175 va_end(argptr);
176}
177
9ef3052c 178// debug functions
b2aef89b 179#ifdef __WXDEBUG__
807a903e 180#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
1d63fd6b 181 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
807a903e 182 { \
d68d8590 183 if ( wxLog::IsEnabled() ) { \
2e7f3845
VZ
184 wxLog::OnLog(wxLOG_##level, \
185 wxString::FormatV(szFormat, argptr), time(NULL));\
807a903e 186 } \
ea44a631 187 } \
2e7f3845 188 \
c9f78968 189 void wxDoLog##level(const wxChar *szFormat, ...) \
ea44a631
GD
190 { \
191 va_list argptr; \
192 va_start(argptr, szFormat); \
1d63fd6b 193 wxVLog##level(szFormat, argptr); \
ea44a631 194 va_end(argptr); \
c801d85f
KB
195 }
196
1d63fd6b 197 void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
0fb67cd1 198 {
d68d8590 199 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
2e7f3845
VZ
200 wxString msg;
201 msg << _T("(") << mask << _T(") ") << wxString::FormatV(szFormat, argptr);
c9f78968 202
2e7f3845 203 wxLog::OnLog(wxLOG_Trace, msg, time(NULL));
0fb67cd1
VZ
204 }
205 }
206
c9f78968 207 void wxDoLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
ea44a631
GD
208 {
209 va_list argptr;
210 va_start(argptr, szFormat);
1d63fd6b 211 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
212 va_end(argptr);
213 }
214
1d63fd6b 215 void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
9ef3052c 216 {
9ef3052c
VZ
217 // we check that all of mask bits are set in the current mask, so
218 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
219 // if both bits are set.
d68d8590 220 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
2e7f3845 221 wxLog::OnLog(wxLOG_Trace, wxString::FormatV(szFormat, argptr), time(NULL));
9ef3052c
VZ
222 }
223 }
224
c9f78968 225 void wxDoLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
ea44a631
GD
226 {
227 va_list argptr;
228 va_start(argptr, szFormat);
1d63fd6b 229 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
230 va_end(argptr);
231 }
232
9ef3052c
VZ
233#else // release
234 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
235#endif
236
237IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
238IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
239
240// wxLogSysError: one uses the last error code, for other you must give it
241// explicitly
242
2e7f3845
VZ
243// return the system error message description
244static inline wxString wxLogSysErrorHelper(long err)
c801d85f 245{
2e7f3845 246 return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
9ef3052c 247}
c801d85f 248
1d63fd6b 249void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr)
9ef3052c 250{
2e7f3845 251 wxVLogSysError(wxSysErrorCode(), szFormat, argptr);
c801d85f
KB
252}
253
c9f78968 254void WXDLLEXPORT wxDoLogSysError(const wxChar *szFormat, ...)
ea44a631
GD
255{
256 va_list argptr;
257 va_start(argptr, szFormat);
1d63fd6b 258 wxVLogSysError(szFormat, argptr);
ea44a631
GD
259 va_end(argptr);
260}
261
2e7f3845 262void WXDLLEXPORT wxVLogSysError(long err, const wxChar *fmt, va_list argptr)
c801d85f 263{
d68d8590 264 if ( wxLog::IsEnabled() ) {
2e7f3845
VZ
265 wxLog::OnLog(wxLOG_Error,
266 wxString::FormatV(fmt, argptr) + wxLogSysErrorHelper(err),
267 time(NULL));
807a903e 268 }
c801d85f
KB
269}
270
c9f78968 271void WXDLLEXPORT wxDoLogSysError(long lErrCode, const wxChar *szFormat, ...)
ea44a631
GD
272{
273 va_list argptr;
274 va_start(argptr, szFormat);
1d63fd6b 275 wxVLogSysError(lErrCode, szFormat, argptr);
ea44a631
GD
276 va_end(argptr);
277}
278
c801d85f
KB
279// ----------------------------------------------------------------------------
280// wxLog class implementation
281// ----------------------------------------------------------------------------
282
f9837791
VZ
283/* static */
284unsigned wxLog::DoLogNumberOfRepeats()
285{
286 long retval = ms_prevCounter;
287 wxLog *pLogger = GetActiveTarget();
288 if ( pLogger && ms_prevCounter > 0 )
289 {
290 wxString msg;
459b97df 291#if wxUSE_INTL
f9837791
VZ
292 msg.Printf(wxPLURAL("The previous message repeated once.",
293 "The previous message repeated %lu times.",
294 ms_prevCounter),
295 ms_prevCounter);
459b97df
WS
296#else
297 msg.Printf(wxT("The previous message was repeated."));
298#endif
f9837791
VZ
299 ms_prevCounter = 0;
300 ms_prevString.clear();
301 pLogger->DoLog(ms_prevLevel, msg.c_str(), ms_prevTimeStamp);
302 }
303 return retval;
304}
305
306wxLog::~wxLog()
307{
308 if ( ms_prevCounter > 0 )
309 {
310 // looks like the repeat count has not been logged yet,
311 // so let's do it now
312 wxLog::DoLogNumberOfRepeats();
313 }
314}
315
316/* static */
317void wxLog::OnLog(wxLogLevel level, const wxChar *szString, time_t t)
318{
319 if ( IsEnabled() && ms_logLevel >= level )
320 {
321 wxLog *pLogger = GetActiveTarget();
322 if ( pLogger )
323 {
324 if ( GetRepetitionCounting() && ms_prevString == szString )
325 {
326 ms_prevCounter++;
327 }
328 else
329 {
330 if ( GetRepetitionCounting() )
331 {
61d8dec7 332 DoLogNumberOfRepeats();
f9837791
VZ
333 }
334 ms_prevString = szString;
335 ms_prevLevel = level;
336 ms_prevTimeStamp = t;
337 pLogger->DoLog(level, szString, t);
338 }
339 }
340 }
341}
342
2e7f3845 343// deprecated function
dcc40ba5
VZ
344#if WXWIN_COMPATIBILITY_2_6
345
2e7f3845 346wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
04662def 347{
2e7f3845 348 return NULL;
04662def
RL
349}
350
dcc40ba5
VZ
351#endif // WXWIN_COMPATIBILITY_2_6
352
9ec05cc9
VZ
353wxLog *wxLog::GetActiveTarget()
354{
0fb67cd1
VZ
355 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
356 // prevent infinite recursion if someone calls wxLogXXX() from
357 // wxApp::CreateLogTarget()
f644b28c 358 static bool s_bInGetActiveTarget = false;
0fb67cd1 359 if ( !s_bInGetActiveTarget ) {
f644b28c 360 s_bInGetActiveTarget = true;
0fb67cd1 361
0fb67cd1
VZ
362 // ask the application to create a log target for us
363 if ( wxTheApp != NULL )
dc6d5e38 364 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
365 else
366 ms_pLogger = new wxLogStderr;
0fb67cd1 367
f644b28c 368 s_bInGetActiveTarget = false;
0fb67cd1
VZ
369
370 // do nothing if it fails - what can we do?
371 }
275bf4c1 372 }
c801d85f 373
0fb67cd1 374 return ms_pLogger;
c801d85f
KB
375}
376
c085e333 377wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 378{
0fb67cd1
VZ
379 if ( ms_pLogger != NULL ) {
380 // flush the old messages before changing because otherwise they might
381 // get lost later if this target is not restored
382 ms_pLogger->Flush();
383 }
c801d85f 384
0fb67cd1
VZ
385 wxLog *pOldLogger = ms_pLogger;
386 ms_pLogger = pLogger;
c085e333 387
0fb67cd1 388 return pOldLogger;
c801d85f
KB
389}
390
36bd6902
VZ
391void wxLog::DontCreateOnDemand()
392{
f644b28c 393 ms_bAutoCreate = false;
36bd6902
VZ
394
395 // this is usually called at the end of the program and we assume that it
396 // is *always* called at the end - so we free memory here to avoid false
397 // memory leak reports from wxWin memory tracking code
398 ClearTraceMasks();
399}
400
0fb67cd1 401void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 402{
0fb67cd1
VZ
403 int index = ms_aTraceMasks.Index(str);
404 if ( index != wxNOT_FOUND )
dc6d5e38 405 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 406}
c801d85f 407
36bd6902
VZ
408void wxLog::ClearTraceMasks()
409{
410 ms_aTraceMasks.Clear();
411}
412
d2e1ef19
VZ
413void wxLog::TimeStamp(wxString *str)
414{
7b2d1c74 415#if wxUSE_DATETIME
d2e1ef19
VZ
416 if ( ms_timestamp )
417 {
418 wxChar buf[256];
419 time_t timeNow;
420 (void)time(&timeNow);
83e8b44c
VZ
421
422 struct tm tm;
423 wxStrftime(buf, WXSIZEOF(buf),
424 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
d2e1ef19
VZ
425
426 str->Empty();
223d09f6 427 *str << buf << wxT(": ");
d2e1ef19 428 }
7b2d1c74 429#endif // wxUSE_DATETIME
d2e1ef19
VZ
430}
431
50920146 432void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 433{
0fb67cd1
VZ
434 switch ( level ) {
435 case wxLOG_FatalError:
786855a1 436 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
437 DoLogString(_("Program aborted."), t);
438 Flush();
1c193821
JS
439#ifdef __WXWINCE__
440 ExitThread(3);
441#else
0fb67cd1 442 abort();
1c193821 443#endif
0fb67cd1
VZ
444 break;
445
446 case wxLOG_Error:
786855a1 447 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
448 break;
449
450 case wxLOG_Warning:
786855a1 451 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
452 break;
453
454 case wxLOG_Info:
0fb67cd1 455 if ( GetVerbose() )
37278984 456 case wxLOG_Message:
87a1e308 457 case wxLOG_Status:
786855a1
VZ
458 default: // log unknown log levels too
459 DoLogString(szString, t);
0fb67cd1
VZ
460 break;
461
462 case wxLOG_Trace:
463 case wxLOG_Debug:
464#ifdef __WXDEBUG__
0131687b
VZ
465 {
466 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 467 : wxT("Debug: ");
0131687b
VZ
468 msg << szString;
469 DoLogString(msg, t);
470 }
471#endif // Debug
0fb67cd1 472 break;
0fb67cd1 473 }
c801d85f
KB
474}
475
74e3313b 476void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 477{
223d09f6 478 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
479}
480
481void wxLog::Flush()
482{
1ec5cbf3 483 // nothing to do here
c801d85f
KB
484}
485
df5168c4
MB
486/*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask)
487{
488 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
489 en = ms_aTraceMasks.end();
490 it != en; ++it )
491 if ( *it == mask)
492 return true;
493 return false;
494}
495
d3fc1755
VZ
496// ----------------------------------------------------------------------------
497// wxLogBuffer implementation
498// ----------------------------------------------------------------------------
499
500void wxLogBuffer::Flush()
501{
a23bbe93
VZ
502 if ( !m_str.empty() )
503 {
504 wxMessageOutputBest out;
505 out.Printf(_T("%s"), m_str.c_str());
506 m_str.clear();
507 }
d3fc1755
VZ
508}
509
83250f1a
VZ
510void wxLogBuffer::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
511{
512 switch ( level )
513 {
514 case wxLOG_Trace:
515 case wxLOG_Debug:
516#ifdef __WXDEBUG__
517 // don't put debug messages in the buffer, we don't want to show
518 // them to the user in a msg box, log them immediately
519 {
520 wxString str;
521 TimeStamp(&str);
522 str += szString;
523
c4b401b6
WS
524 wxMessageOutputDebug dbgout;
525 dbgout.Printf(_T("%s\n"), str.c_str());
83250f1a
VZ
526 }
527#endif // __WXDEBUG__
528 break;
529
530 default:
531 wxLog::DoLog(level, szString, t);
532 }
533}
534
d3fc1755
VZ
535void wxLogBuffer::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
536{
537 m_str << szString << _T("\n");
538}
539
c801d85f
KB
540// ----------------------------------------------------------------------------
541// wxLogStderr class implementation
542// ----------------------------------------------------------------------------
543
544wxLogStderr::wxLogStderr(FILE *fp)
545{
0fb67cd1
VZ
546 if ( fp == NULL )
547 m_fp = stderr;
548 else
549 m_fp = fp;
c801d85f
KB
550}
551
74e3313b 552void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 553{
d2e1ef19
VZ
554 wxString str;
555 TimeStamp(&str);
b568d04f 556 str << szString;
1e8a4bc2 557
14ff7a59
VZ
558 wxFputs(str, m_fp);
559 wxFputc(_T('\n'), m_fp);
0fb67cd1 560 fflush(m_fp);
1e8a4bc2 561
e2478fde
VZ
562 // under GUI systems such as Windows or Mac, programs usually don't have
563 // stderr at all, so show the messages also somewhere else, typically in
564 // the debugger window so that they go at least somewhere instead of being
565 // simply lost
1ec5cbf3
VZ
566 if ( m_fp == stderr )
567 {
e2478fde
VZ
568 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
569 if ( traits && !traits->HasStderr() )
570 {
254a2129 571 wxMessageOutputDebug dbgout;
30413fd0 572 dbgout.Printf(_T("%s\n"), str.c_str());
e2478fde 573 }
03147cd0 574 }
c801d85f
KB
575}
576
577// ----------------------------------------------------------------------------
578// wxLogStream implementation
579// ----------------------------------------------------------------------------
580
4bf78aae 581#if wxUSE_STD_IOSTREAM
65f19af1 582#include "wx/ioswrap.h"
dd107c50 583wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 584{
0fb67cd1 585 if ( ostr == NULL )
dd107c50 586 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
587 else
588 m_ostr = ostr;
c801d85f
KB
589}
590
74e3313b 591void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 592{
29fd317b
VZ
593 wxString str;
594 TimeStamp(&str);
416f9764 595 (*m_ostr) << wxConvertWX2MB(str) << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 596}
0fb67cd1 597#endif // wxUSE_STD_IOSTREAM
c801d85f 598
03147cd0
VZ
599// ----------------------------------------------------------------------------
600// wxLogChain
601// ----------------------------------------------------------------------------
602
603wxLogChain::wxLogChain(wxLog *logger)
604{
f644b28c 605 m_bPassMessages = true;
71debe95 606
03147cd0
VZ
607 m_logNew = logger;
608 m_logOld = wxLog::SetActiveTarget(this);
609}
610
199e91fb
VZ
611wxLogChain::~wxLogChain()
612{
e95f8fde
VZ
613 delete m_logOld;
614
615 if ( m_logNew != this )
616 delete m_logNew;
199e91fb
VZ
617}
618
03147cd0
VZ
619void wxLogChain::SetLog(wxLog *logger)
620{
621 if ( m_logNew != this )
622 delete m_logNew;
623
03147cd0
VZ
624 m_logNew = logger;
625}
626
627void wxLogChain::Flush()
628{
629 if ( m_logOld )
630 m_logOld->Flush();
631
1ec5cbf3 632 // be careful to avoid infinite recursion
03147cd0
VZ
633 if ( m_logNew && m_logNew != this )
634 m_logNew->Flush();
635}
636
637void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
638{
639 // let the previous logger show it
640 if ( m_logOld && IsPassingMessages() )
641 {
642 // bogus cast just to access protected DoLog
643 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
644 }
645
646 if ( m_logNew && m_logNew != this )
647 {
648 // as above...
649 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
650 }
651}
652
93d4c1d0
VZ
653// ----------------------------------------------------------------------------
654// wxLogPassThrough
655// ----------------------------------------------------------------------------
656
657#ifdef __VISUALC__
658 // "'this' : used in base member initializer list" - so what?
659 #pragma warning(disable:4355)
660#endif // VC++
661
662wxLogPassThrough::wxLogPassThrough()
663 : wxLogChain(this)
664{
665}
666
667#ifdef __VISUALC__
668 #pragma warning(default:4355)
669#endif // VC++
670
c801d85f
KB
671// ============================================================================
672// Global functions/variables
673// ============================================================================
674
675// ----------------------------------------------------------------------------
676// static variables
677// ----------------------------------------------------------------------------
0fb67cd1 678
f9837791
VZ
679bool wxLog::ms_bRepetCounting = false;
680wxString wxLog::ms_prevString;
374b4f1c 681unsigned int wxLog::ms_prevCounter = 0;
f9837791
VZ
682time_t wxLog::ms_prevTimeStamp= 0;
683wxLogLevel wxLog::ms_prevLevel;
684
0fb67cd1 685wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
f644b28c
WS
686bool wxLog::ms_doLog = true;
687bool wxLog::ms_bAutoCreate = true;
688bool wxLog::ms_bVerbose = false;
d2e1ef19 689
edc73852
RD
690wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
691
2ed3265e
VZ
692size_t wxLog::ms_suspendCount = 0;
693
e2478fde 694const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
d2e1ef19 695
0fb67cd1
VZ
696wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
697wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
698
699// ----------------------------------------------------------------------------
700// stdout error logging helper
701// ----------------------------------------------------------------------------
702
703// helper function: wraps the message and justifies it under given position
704// (looks more pretty on the terminal). Also adds newline at the end.
705//
0fb67cd1
VZ
706// TODO this is now disabled until I find a portable way of determining the
707// terminal window size (ok, I found it but does anybody really cares?)
708#ifdef LOG_PRETTY_WRAP
c801d85f
KB
709static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
710{
0fb67cd1
VZ
711 size_t nMax = 80; // FIXME
712 size_t nStart = strlen(pszPrefix);
713 fputs(pszPrefix, f);
714
715 size_t n;
716 while ( *psz != '\0' ) {
717 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
718 putc(*psz++, f);
719
720 // wrapped?
721 if ( *psz != '\0' ) {
722 /*putc('\n', f);*/
723 for ( n = 0; n < nStart; n++ )
724 putc(' ', f);
725
726 // as we wrapped, squeeze all white space
727 while ( isspace(*psz) )
728 psz++;
729 }
c801d85f 730 }
c801d85f 731
0fb67cd1 732 putc('\n', f);
c801d85f
KB
733}
734#endif //LOG_PRETTY_WRAP
735
736// ----------------------------------------------------------------------------
737// error code/error message retrieval functions
738// ----------------------------------------------------------------------------
739
740// get error code from syste
741unsigned long wxSysErrorCode()
742{
8cb172b4 743#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 744 return ::GetLastError();
0fb67cd1 745#else //Unix
c801d85f 746 return errno;
0fb67cd1 747#endif //Win/Unix
c801d85f
KB
748}
749
750// get error message from system
50920146 751const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 752{
0fb67cd1
VZ
753 if ( nErrCode == 0 )
754 nErrCode = wxSysErrorCode();
755
8cb172b4 756#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
2e7f3845 757 static wxChar s_szBuf[1024];
0fb67cd1
VZ
758
759 // get error message from system
760 LPVOID lpMsgBuf;
d0822e56
VZ
761 if ( ::FormatMessage
762 (
763 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
764 NULL,
765 nErrCode,
0fb67cd1
VZ
766 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
767 (LPTSTR)&lpMsgBuf,
d0822e56
VZ
768 0,
769 NULL
770 ) == 0 )
771 {
772 // if this happens, something is seriously wrong, so don't use _() here
773 // for safety
774 wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode);
c4b401b6 775 return s_szBuf;
d0822e56
VZ
776 }
777
0fb67cd1
VZ
778
779 // copy it to our buffer and free memory
d0822e56 780 // Crashes on SmartPhone (FIXME)
0c44ec97 781#if !defined(__SMARTPHONE__) /* of WinCE */
7448de8d
WS
782 if( lpMsgBuf != 0 )
783 {
8c5b1f0f 784 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
785 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
786
787 LocalFree(lpMsgBuf);
788
789 // returned string is capitalized and ended with '\r\n' - bad
790 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
791 size_t len = wxStrlen(s_szBuf);
792 if ( len > 0 ) {
793 // truncate string
794 if ( s_szBuf[len - 2] == wxT('\r') )
795 s_szBuf[len - 2] = wxT('\0');
796 }
797 }
a9928e9d 798 else
2e7f3845 799#endif // !__SMARTPHONE__
a9928e9d 800 {
8c5b1f0f 801 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
802 }
803
804 return s_szBuf;
2e7f3845
VZ
805#else // !__WXMSW__
806 #if wxUSE_UNICODE
807 static wchar_t s_wzBuf[1024];
808 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
809 WXSIZEOF(s_wzBuf) - 1);
810 return s_wzBuf;
811 #else
812 return strerror((int)nErrCode);
813 #endif
814#endif // __WXMSW__/!__WXMSW__
c801d85f
KB
815}
816
e2478fde 817#endif // wxUSE_LOG