]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
introduce wxBrushStyle enum and replace 'int style' occurrences in wxBrush code with...
[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"
3a3dde0d 45#include "wx/crt.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 53
9b4da627 54#ifndef __WXPALMOS5__
1c193821 55#ifndef __WXWINCE__
e2478fde 56#include <time.h>
1c193821
JS
57#else
58#include "wx/msw/wince/time.h"
59#endif
9b4da627 60#endif /* ! __WXPALMOS5__ */
31907d03 61
9cce3be7
VS
62#if defined(__WINDOWS__)
63 #include "wx/msw/private.h" // includes windows.h
64#endif
65
c801d85f
KB
66// ----------------------------------------------------------------------------
67// non member functions
68// ----------------------------------------------------------------------------
69
70// define this to enable wrapping of log messages
71//#define LOG_PRETTY_WRAP
72
9ef3052c 73#ifdef LOG_PRETTY_WRAP
c801d85f
KB
74 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
75#endif
76
77// ============================================================================
78// implementation
79// ============================================================================
80
b568d04f
VZ
81// ----------------------------------------------------------------------------
82// implementation of Log functions
83//
84// NB: unfortunately we need all these distinct functions, we can't make them
85// macros and not all compilers inline vararg functions.
86// ----------------------------------------------------------------------------
87
c801d85f 88// generic log function
81727065 89void wxVLogGeneric(wxLogLevel level, const wxString& format, va_list argptr)
c801d85f 90{
d68d8590 91 if ( wxLog::IsEnabled() ) {
81727065 92 wxLog::OnLog(level, wxString::FormatV(format, argptr), time(NULL));
807a903e 93 }
c801d85f
KB
94}
95
d1f6e2cf
VS
96#if !wxUSE_UTF8_LOCALE_ONLY
97void wxDoLogGenericWchar(wxLogLevel level, const wxChar *format, ...)
ea44a631
GD
98{
99 va_list argptr;
2523e9b7
VS
100 va_start(argptr, format);
101 wxVLogGeneric(level, format, argptr);
ea44a631
GD
102 va_end(argptr);
103}
d1f6e2cf
VS
104#endif // wxUSE_UTF8_LOCALE_ONLY
105
106#if wxUSE_UNICODE_UTF8
107void wxDoLogGenericUtf8(wxLogLevel level, const char *format, ...)
108{
109 va_list argptr;
110 va_start(argptr, format);
111 wxVLogGeneric(level, format, argptr);
112 va_end(argptr);
113}
114#endif // wxUSE_UNICODE_UTF8
115
116#if !wxUSE_UTF8_LOCALE_ONLY
117 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
118 void wxDoLog##level##Wchar(const wxChar *format, ...) \
119 { \
120 va_list argptr; \
121 va_start(argptr, format); \
122 wxVLog##level(format, argptr); \
123 va_end(argptr); \
124 }
125#else
126 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level)
127#endif
128
129#if wxUSE_UNICODE_UTF8
130 #define IMPLEMENT_LOG_FUNCTION_UTF8(level) \
131 void wxDoLog##level##Utf8(const char *format, ...) \
132 { \
133 va_list argptr; \
134 va_start(argptr, format); \
135 wxVLog##level(format, argptr); \
136 va_end(argptr); \
137 }
138#else
139 #define IMPLEMENT_LOG_FUNCTION_UTF8(level)
140#endif
ea44a631 141
807a903e 142#define IMPLEMENT_LOG_FUNCTION(level) \
81727065 143 void wxVLog##level(const wxString& format, va_list argptr) \
807a903e 144 { \
d68d8590 145 if ( wxLog::IsEnabled() ) { \
2e7f3845 146 wxLog::OnLog(wxLOG_##level, \
81727065 147 wxString::FormatV(format, argptr), time(NULL)); \
807a903e 148 } \
ea44a631 149 } \
d1f6e2cf
VS
150 IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
151 IMPLEMENT_LOG_FUNCTION_UTF8(level)
c801d85f 152
c801d85f
KB
153IMPLEMENT_LOG_FUNCTION(Error)
154IMPLEMENT_LOG_FUNCTION(Warning)
155IMPLEMENT_LOG_FUNCTION(Message)
156IMPLEMENT_LOG_FUNCTION(Info)
157IMPLEMENT_LOG_FUNCTION(Status)
158
c11d62a6
VZ
159void wxSafeShowMessage(const wxString& title, const wxString& text)
160{
161#ifdef __WINDOWS__
e0a050e3 162 ::MessageBox(NULL, text.wx_str(), title.wx_str(), MB_OK | MB_ICONSTOP);
c11d62a6 163#else
a0516656 164 wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str());
65f06384 165 fflush(stderr);
c11d62a6
VZ
166#endif
167}
168
1800689f
VZ
169// fatal errors can't be suppressed nor handled by the custom log target and
170// always terminate the program
81727065 171void wxVLogFatalError(const wxString& format, va_list argptr)
1800689f 172{
a0516656 173 wxSafeShowMessage(wxS("Fatal Error"), wxString::FormatV(format, argptr));
1800689f 174
1c193821
JS
175#ifdef __WXWINCE__
176 ExitThread(3);
177#else
1800689f 178 abort();
1c193821 179#endif
1800689f
VZ
180}
181
d1f6e2cf
VS
182#if !wxUSE_UTF8_LOCALE_ONLY
183void wxDoLogFatalErrorWchar(const wxChar *format, ...)
1800689f
VZ
184{
185 va_list argptr;
2523e9b7
VS
186 va_start(argptr, format);
187 wxVLogFatalError(format, argptr);
5e475383
VZ
188
189 // some compilers warn about unreachable code and it shouldn't matter
190 // for the others anyhow...
191 //va_end(argptr);
1800689f 192}
d1f6e2cf
VS
193#endif // wxUSE_UTF8_LOCALE_ONLY
194
195#if wxUSE_UNICODE_UTF8
196void wxDoLogFatalErrorUtf8(const char *format, ...)
197{
198 va_list argptr;
199 va_start(argptr, format);
200 wxVLogFatalError(format, argptr);
201
202 // some compilers warn about unreachable code and it shouldn't matter
203 // for the others anyhow...
204 //va_end(argptr);
205}
206#endif // wxUSE_UNICODE_UTF8
1800689f 207
9ef3052c 208// same as info, but only if 'verbose' mode is on
81727065 209void wxVLogVerbose(const wxString& format, va_list argptr)
9ef3052c 210{
d68d8590 211 if ( wxLog::IsEnabled() ) {
2a1f999f 212 if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) {
2e7f3845 213 wxLog::OnLog(wxLOG_Info,
81727065 214 wxString::FormatV(format, argptr), time(NULL));
807a903e
VZ
215 }
216 }
9ef3052c
VZ
217}
218
d1f6e2cf
VS
219#if !wxUSE_UTF8_LOCALE_ONLY
220void wxDoLogVerboseWchar(const wxChar *format, ...)
ea44a631
GD
221{
222 va_list argptr;
2523e9b7
VS
223 va_start(argptr, format);
224 wxVLogVerbose(format, argptr);
ea44a631
GD
225 va_end(argptr);
226}
d1f6e2cf
VS
227#endif // !wxUSE_UTF8_LOCALE_ONLY
228
229#if wxUSE_UNICODE_UTF8
230void wxDoLogVerboseUtf8(const char *format, ...)
231{
232 va_list argptr;
233 va_start(argptr, format);
234 wxVLogVerbose(format, argptr);
235 va_end(argptr);
236}
237#endif // wxUSE_UNICODE_UTF8
ea44a631 238
9ef3052c 239// debug functions
b2aef89b 240#ifdef __WXDEBUG__
d1f6e2cf
VS
241
242#if !wxUSE_UTF8_LOCALE_ONLY
243 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
244 void wxDoLog##level##Wchar(const wxChar *format, ...) \
245 { \
246 va_list argptr; \
247 va_start(argptr, format); \
248 wxVLog##level(format, argptr); \
249 va_end(argptr); \
250 }
251#else
252 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level)
253#endif
254
255#if wxUSE_UNICODE_UTF8
256 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level) \
257 void wxDoLog##level##Utf8(const char *format, ...) \
258 { \
259 va_list argptr; \
260 va_start(argptr, format); \
261 wxVLog##level(format, argptr); \
262 va_end(argptr); \
263 }
264#else
265 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
266#endif
267
807a903e 268#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
2523e9b7 269 void wxVLog##level(const wxString& format, va_list argptr) \
807a903e 270 { \
d68d8590 271 if ( wxLog::IsEnabled() ) { \
2e7f3845 272 wxLog::OnLog(wxLOG_##level, \
2523e9b7 273 wxString::FormatV(format, argptr), time(NULL)); \
807a903e 274 } \
ea44a631 275 } \
d1f6e2cf
VS
276 IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
277 IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
278
c801d85f 279
81727065 280 void wxVLogTrace(const wxString& mask, const wxString& format, va_list argptr)
0fb67cd1 281 {
d68d8590 282 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
2e7f3845 283 wxString msg;
a0516656 284 msg << wxS("(") << mask << wxS(") ") << wxString::FormatV(format, argptr);
c9f78968 285
2e7f3845 286 wxLog::OnLog(wxLOG_Trace, msg, time(NULL));
0fb67cd1
VZ
287 }
288 }
289
d1f6e2cf
VS
290#if !wxUSE_UTF8_LOCALE_ONLY
291 void wxDoLogTraceWchar(const wxString& mask, const wxChar *format, ...)
ea44a631
GD
292 {
293 va_list argptr;
2523e9b7
VS
294 va_start(argptr, format);
295 wxVLogTrace(mask, format, argptr);
ea44a631
GD
296 va_end(argptr);
297 }
d1f6e2cf
VS
298#endif // !wxUSE_UTF8_LOCALE_ONLY
299
300#if wxUSE_UNICODE_UTF8
301 void wxDoLogTraceUtf8(const wxString& mask, const char *format, ...)
302 {
303 va_list argptr;
304 va_start(argptr, format);
305 wxVLogTrace(mask, format, argptr);
306 va_end(argptr);
307 }
308#endif // wxUSE_UNICODE_UTF8
ea44a631 309
81727065 310 void wxVLogTrace(wxTraceMask mask, const wxString& format, va_list argptr)
9ef3052c 311 {
9ef3052c
VZ
312 // we check that all of mask bits are set in the current mask, so
313 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
314 // if both bits are set.
d68d8590 315 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
81727065 316 wxLog::OnLog(wxLOG_Trace, wxString::FormatV(format, argptr), time(NULL));
9ef3052c
VZ
317 }
318 }
319
8bd37efc 320#if !wxUSE_UTF8_LOCALE_ONLY
d1f6e2cf
VS
321 void wxDoLogTraceWchar(wxTraceMask mask, const wxChar *format, ...)
322 {
323 va_list argptr;
324 va_start(argptr, format);
325 wxVLogTrace(mask, format, argptr);
326 va_end(argptr);
327 }
8bd37efc 328#endif // !wxUSE_UTF8_LOCALE_ONLY
d1f6e2cf
VS
329
330#if wxUSE_UNICODE_UTF8
331 void wxDoLogTraceUtf8(wxTraceMask mask, const char *format, ...)
2523e9b7
VS
332 {
333 va_list argptr;
334 va_start(argptr, format);
335 wxVLogTrace(mask, format, argptr);
336 va_end(argptr);
337 }
d1f6e2cf 338#endif // wxUSE_UNICODE_UTF8
2523e9b7
VS
339
340#ifdef __WATCOMC__
341 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69 342 void wxDoLogTraceWchar(int mask, const wxChar *format, ...)
2523e9b7
VS
343 {
344 va_list argptr;
345 va_start(argptr, format);
346 wxVLogTrace(mask, format, argptr);
347 va_end(argptr);
348 }
349
59a14f69 350 void wxDoLogTraceWchar(const char *mask, const wxChar *format, ...)
ea44a631
GD
351 {
352 va_list argptr;
2523e9b7
VS
353 va_start(argptr, format);
354 wxVLogTrace(mask, format, argptr);
ea44a631
GD
355 va_end(argptr);
356 }
357
59a14f69 358 void wxDoLogTraceWchar(const wchar_t *mask, const wxChar *format, ...)
2523e9b7
VS
359 {
360 va_list argptr;
361 va_start(argptr, format);
362 wxVLogTrace(mask, format, argptr);
363 va_end(argptr);
364 }
365
366 void wxVLogTrace(int mask, const wxString& format, va_list argptr)
367 { wxVLogTrace((wxTraceMask)mask, format, argptr); }
368 void wxVLogTrace(const char *mask, const wxString& format, va_list argptr)
369 { wxVLogTrace(wxString(mask), format, argptr); }
370 void wxVLogTrace(const wchar_t *mask, const wxString& format, va_list argptr)
371 { wxVLogTrace(wxString(mask), format, argptr); }
372#endif // __WATCOMC__
373
9ef3052c
VZ
374#else // release
375 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
376#endif
377
378IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
379IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
380
381// wxLogSysError: one uses the last error code, for other you must give it
382// explicitly
383
2e7f3845
VZ
384// return the system error message description
385static inline wxString wxLogSysErrorHelper(long err)
c801d85f 386{
2e7f3845 387 return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
9ef3052c 388}
c801d85f 389
163b3ad7 390void WXDLLIMPEXP_BASE wxVLogSysError(const wxString& format, va_list argptr)
9ef3052c 391{
81727065 392 wxVLogSysError(wxSysErrorCode(), format, argptr);
c801d85f
KB
393}
394
d1f6e2cf 395#if !wxUSE_UTF8_LOCALE_ONLY
163b3ad7 396void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(const wxChar *format, ...)
ea44a631
GD
397{
398 va_list argptr;
2523e9b7
VS
399 va_start(argptr, format);
400 wxVLogSysError(format, argptr);
ea44a631
GD
401 va_end(argptr);
402}
d1f6e2cf
VS
403#endif // !wxUSE_UTF8_LOCALE_ONLY
404
405#if wxUSE_UNICODE_UTF8
163b3ad7 406void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(const char *format, ...)
d1f6e2cf
VS
407{
408 va_list argptr;
409 va_start(argptr, format);
410 wxVLogSysError(format, argptr);
411 va_end(argptr);
412}
413#endif // wxUSE_UNICODE_UTF8
ea44a631 414
163b3ad7 415void WXDLLIMPEXP_BASE wxVLogSysError(long err, const wxString& format, va_list argptr)
c801d85f 416{
d68d8590 417 if ( wxLog::IsEnabled() ) {
2e7f3845 418 wxLog::OnLog(wxLOG_Error,
81727065 419 wxString::FormatV(format, argptr) + wxLogSysErrorHelper(err),
2e7f3845 420 time(NULL));
807a903e 421 }
c801d85f
KB
422}
423
d1f6e2cf 424#if !wxUSE_UTF8_LOCALE_ONLY
163b3ad7 425void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(long lErrCode, const wxChar *format, ...)
d1f6e2cf
VS
426{
427 va_list argptr;
428 va_start(argptr, format);
429 wxVLogSysError(lErrCode, format, argptr);
430 va_end(argptr);
431}
432#endif // !wxUSE_UTF8_LOCALE_ONLY
433
434#if wxUSE_UNICODE_UTF8
163b3ad7 435void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(long lErrCode, const char *format, ...)
ea44a631
GD
436{
437 va_list argptr;
2523e9b7
VS
438 va_start(argptr, format);
439 wxVLogSysError(lErrCode, format, argptr);
ea44a631
GD
440 va_end(argptr);
441}
d1f6e2cf 442#endif // wxUSE_UNICODE_UTF8
ea44a631 443
2523e9b7
VS
444#ifdef __WATCOMC__
445// workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
163b3ad7 446void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(unsigned long lErrCode, const wxChar *format, ...)
2523e9b7
VS
447{
448 va_list argptr;
449 va_start(argptr, format);
450 wxVLogSysError(lErrCode, format, argptr);
451 va_end(argptr);
452}
453
163b3ad7 454void WXDLLIMPEXP_BASE wxVLogSysError(unsigned long err, const wxString& format, va_list argptr)
2523e9b7
VS
455 { wxVLogSysError((long)err, format, argptr); }
456#endif // __WATCOMC__
457
c801d85f
KB
458// ----------------------------------------------------------------------------
459// wxLog class implementation
460// ----------------------------------------------------------------------------
461
dbaa16de 462unsigned wxLog::LogLastRepeatIfNeeded()
f9837791 463{
a2d38265
VZ
464 wxCRIT_SECT_LOCKER(lock, ms_prevCS);
465
dbaa16de
VZ
466 return LogLastRepeatIfNeededUnlocked();
467}
468
469unsigned wxLog::LogLastRepeatIfNeededUnlocked()
470{
0250efd6
VZ
471 const unsigned count = ms_prevCounter;
472
c1f80bc0 473 if ( ms_prevCounter )
f9837791
VZ
474 {
475 wxString msg;
459b97df 476#if wxUSE_INTL
f9837791
VZ
477 msg.Printf(wxPLURAL("The previous message repeated once.",
478 "The previous message repeated %lu times.",
479 ms_prevCounter),
480 ms_prevCounter);
459b97df 481#else
a0516656 482 msg.Printf(wxS("The previous message was repeated %lu times."),
2064113c 483 ms_prevCounter);
459b97df 484#endif
f9837791
VZ
485 ms_prevCounter = 0;
486 ms_prevString.clear();
c1f80bc0 487 DoLog(ms_prevLevel, msg, ms_prevTimeStamp);
f9837791 488 }
0250efd6
VZ
489
490 return count;
f9837791
VZ
491}
492
493wxLog::~wxLog()
494{
6f6b48f1
VZ
495 // Flush() must be called before destroying the object as otherwise some
496 // messages could be lost
497 if ( ms_prevCounter )
498 {
499 wxMessageOutputDebug().Printf
500 (
501 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
502 ms_prevString,
503 ms_prevCounter
504 );
505 }
f9837791
VZ
506}
507
508/* static */
5a20d2ce 509void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
f9837791
VZ
510{
511 if ( IsEnabled() && ms_logLevel >= level )
512 {
513 wxLog *pLogger = GetActiveTarget();
514 if ( pLogger )
515 {
2064113c 516 if ( GetRepetitionCounting() )
f9837791 517 {
a2d38265
VZ
518 wxCRIT_SECT_LOCKER(lock, ms_prevCS);
519
2064113c 520 if ( szString == ms_prevString )
f9837791 521 {
2064113c
VZ
522 ms_prevCounter++;
523
524 // nothing else to do, in particular, don't log the
525 // repeated message
526 return;
f9837791 527 }
2064113c 528
dbaa16de 529 pLogger->LogLastRepeatIfNeededUnlocked();
2064113c
VZ
530
531 // reset repetition counter for a new message
f9837791
VZ
532 ms_prevString = szString;
533 ms_prevLevel = level;
534 ms_prevTimeStamp = t;
f9837791 535 }
2064113c
VZ
536
537 pLogger->DoLog(level, szString, t);
f9837791
VZ
538 }
539 }
540}
541
2e7f3845 542// deprecated function
dcc40ba5
VZ
543#if WXWIN_COMPATIBILITY_2_6
544
2e7f3845 545wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
04662def 546{
2e7f3845 547 return NULL;
04662def
RL
548}
549
dcc40ba5
VZ
550#endif // WXWIN_COMPATIBILITY_2_6
551
5d88a6b5
VZ
552#if WXWIN_COMPATIBILITY_2_8
553
554void wxLog::DoLog(wxLogLevel WXUNUSED(level),
555 const char *WXUNUSED(szString),
556 time_t WXUNUSED(t))
557{
558}
559
560void wxLog::DoLog(wxLogLevel WXUNUSED(level),
561 const wchar_t *WXUNUSED(wzString),
562 time_t WXUNUSED(t))
563{
564}
565
566#endif // WXWIN_COMPATIBILITY_2_8
567
9ec05cc9
VZ
568wxLog *wxLog::GetActiveTarget()
569{
0fb67cd1
VZ
570 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
571 // prevent infinite recursion if someone calls wxLogXXX() from
572 // wxApp::CreateLogTarget()
f644b28c 573 static bool s_bInGetActiveTarget = false;
0fb67cd1 574 if ( !s_bInGetActiveTarget ) {
f644b28c 575 s_bInGetActiveTarget = true;
0fb67cd1 576
0fb67cd1
VZ
577 // ask the application to create a log target for us
578 if ( wxTheApp != NULL )
dc6d5e38 579 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
580 else
581 ms_pLogger = new wxLogStderr;
0fb67cd1 582
f644b28c 583 s_bInGetActiveTarget = false;
0fb67cd1
VZ
584
585 // do nothing if it fails - what can we do?
586 }
275bf4c1 587 }
c801d85f 588
0fb67cd1 589 return ms_pLogger;
c801d85f
KB
590}
591
c085e333 592wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 593{
0fb67cd1
VZ
594 if ( ms_pLogger != NULL ) {
595 // flush the old messages before changing because otherwise they might
596 // get lost later if this target is not restored
597 ms_pLogger->Flush();
598 }
c801d85f 599
0fb67cd1
VZ
600 wxLog *pOldLogger = ms_pLogger;
601 ms_pLogger = pLogger;
c085e333 602
0fb67cd1 603 return pOldLogger;
c801d85f
KB
604}
605
36bd6902
VZ
606void wxLog::DontCreateOnDemand()
607{
f644b28c 608 ms_bAutoCreate = false;
36bd6902
VZ
609
610 // this is usually called at the end of the program and we assume that it
611 // is *always* called at the end - so we free memory here to avoid false
612 // memory leak reports from wxWin memory tracking code
613 ClearTraceMasks();
614}
615
e94cd97d
DE
616void wxLog::DoCreateOnDemand()
617{
618 ms_bAutoCreate = true;
619}
620
f96233d5
VZ
621void wxLog::AddTraceMask(const wxString& str)
622{
623 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
624
625 ms_aTraceMasks.push_back(str);
626}
627
0fb67cd1 628void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 629{
f96233d5
VZ
630 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
631
0fb67cd1
VZ
632 int index = ms_aTraceMasks.Index(str);
633 if ( index != wxNOT_FOUND )
dc6d5e38 634 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 635}
c801d85f 636
36bd6902
VZ
637void wxLog::ClearTraceMasks()
638{
f96233d5
VZ
639 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
640
36bd6902
VZ
641 ms_aTraceMasks.Clear();
642}
643
d2e1ef19
VZ
644void wxLog::TimeStamp(wxString *str)
645{
7b2d1c74 646#if wxUSE_DATETIME
cb296f30 647 if ( !ms_timestamp.empty() )
d2e1ef19
VZ
648 {
649 wxChar buf[256];
650 time_t timeNow;
651 (void)time(&timeNow);
83e8b44c
VZ
652
653 struct tm tm;
654 wxStrftime(buf, WXSIZEOF(buf),
655 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
d2e1ef19
VZ
656
657 str->Empty();
a0516656 658 *str << buf << wxS(": ");
d2e1ef19 659 }
7b2d1c74 660#endif // wxUSE_DATETIME
d2e1ef19
VZ
661}
662
5a20d2ce 663void wxLog::DoLog(wxLogLevel level, const wxString& szString, time_t t)
0fb67cd1 664{
5a20d2ce
VS
665#if WXWIN_COMPATIBILITY_2_8
666 // DoLog() signature changed since 2.8, so we call the old versions here
667 // so that existing custom log classes still work:
668 DoLog(level, (const char*)szString.mb_str(), t);
669 DoLog(level, (const wchar_t*)szString.wc_str(), t);
670#endif
671
0fb67cd1
VZ
672 switch ( level ) {
673 case wxLOG_FatalError:
5a20d2ce
VS
674 LogString(_("Fatal error: ") + szString, t);
675 LogString(_("Program aborted."), t);
0fb67cd1 676 Flush();
1c193821
JS
677#ifdef __WXWINCE__
678 ExitThread(3);
679#else
0fb67cd1 680 abort();
1c193821 681#endif
0fb67cd1
VZ
682 break;
683
684 case wxLOG_Error:
5a20d2ce 685 LogString(_("Error: ") + szString, t);
0fb67cd1
VZ
686 break;
687
688 case wxLOG_Warning:
5a20d2ce 689 LogString(_("Warning: ") + szString, t);
0fb67cd1
VZ
690 break;
691
692 case wxLOG_Info:
0fb67cd1 693 if ( GetVerbose() )
37278984 694 case wxLOG_Message:
87a1e308 695 case wxLOG_Status:
786855a1 696 default: // log unknown log levels too
5a20d2ce 697 LogString(szString, t);
0fb67cd1
VZ
698 break;
699
700 case wxLOG_Trace:
701 case wxLOG_Debug:
702#ifdef __WXDEBUG__
0131687b 703 {
a0516656
VZ
704 wxString msg = level == wxLOG_Trace ? wxS("Trace: ")
705 : wxS("Debug: ");
0131687b 706 msg << szString;
5a20d2ce 707 LogString(msg, t);
0131687b
VZ
708 }
709#endif // Debug
0fb67cd1 710 break;
0fb67cd1 711 }
c801d85f
KB
712}
713
5a20d2ce 714void wxLog::DoLogString(const wxString& szString, time_t t)
c801d85f 715{
5a20d2ce
VS
716#if WXWIN_COMPATIBILITY_2_8
717 // DoLogString() signature changed since 2.8, so we call the old versions
718 // here so that existing custom log classes still work; unfortunately this
719 // also means that we can't have the wxFAIL_MSG below in compat mode
720 DoLogString((const char*)szString.mb_str(), t);
721 DoLogString((const wchar_t*)szString.wc_str(), t);
722#else
a0516656 723 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
5a20d2ce
VS
724 wxUnusedVar(szString);
725 wxUnusedVar(t);
726#endif
c801d85f
KB
727}
728
729void wxLog::Flush()
730{
6f6b48f1 731 LogLastRepeatIfNeeded();
c801d85f
KB
732}
733
5a20d2ce 734/*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
df5168c4 735{
f96233d5
VZ
736 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
737
df5168c4
MB
738 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
739 en = ms_aTraceMasks.end();
740 it != en; ++it )
f96233d5 741 {
df5168c4
MB
742 if ( *it == mask)
743 return true;
f96233d5
VZ
744 }
745
df5168c4
MB
746 return false;
747}
748
d3fc1755
VZ
749// ----------------------------------------------------------------------------
750// wxLogBuffer implementation
751// ----------------------------------------------------------------------------
752
753void wxLogBuffer::Flush()
754{
a23bbe93
VZ
755 if ( !m_str.empty() )
756 {
757 wxMessageOutputBest out;
a0516656 758 out.Printf(wxS("%s"), m_str.c_str());
a23bbe93
VZ
759 m_str.clear();
760 }
d3fc1755
VZ
761}
762
5a20d2ce 763void wxLogBuffer::DoLog(wxLogLevel level, const wxString& szString, time_t t)
83250f1a
VZ
764{
765 switch ( level )
766 {
767 case wxLOG_Trace:
768 case wxLOG_Debug:
769#ifdef __WXDEBUG__
770 // don't put debug messages in the buffer, we don't want to show
771 // them to the user in a msg box, log them immediately
772 {
773 wxString str;
774 TimeStamp(&str);
775 str += szString;
776
c4b401b6 777 wxMessageOutputDebug dbgout;
a0516656 778 dbgout.Printf(wxS("%s\n"), str.c_str());
83250f1a
VZ
779 }
780#endif // __WXDEBUG__
781 break;
782
783 default:
784 wxLog::DoLog(level, szString, t);
785 }
786}
787
5a20d2ce 788void wxLogBuffer::DoLogString(const wxString& szString, time_t WXUNUSED(t))
d3fc1755 789{
a0516656 790 m_str << szString << wxS("\n");
d3fc1755
VZ
791}
792
c801d85f
KB
793// ----------------------------------------------------------------------------
794// wxLogStderr class implementation
795// ----------------------------------------------------------------------------
796
797wxLogStderr::wxLogStderr(FILE *fp)
798{
0fb67cd1
VZ
799 if ( fp == NULL )
800 m_fp = stderr;
801 else
802 m_fp = fp;
c801d85f
KB
803}
804
5a20d2ce 805void wxLogStderr::DoLogString(const wxString& szString, time_t WXUNUSED(t))
c801d85f 806{
d2e1ef19
VZ
807 wxString str;
808 TimeStamp(&str);
b568d04f 809 str << szString;
1e8a4bc2 810
14ff7a59 811 wxFputs(str, m_fp);
a0516656 812 wxFputc(wxS('\n'), m_fp);
0fb67cd1 813 fflush(m_fp);
1e8a4bc2 814
e2478fde
VZ
815 // under GUI systems such as Windows or Mac, programs usually don't have
816 // stderr at all, so show the messages also somewhere else, typically in
817 // the debugger window so that they go at least somewhere instead of being
818 // simply lost
1ec5cbf3
VZ
819 if ( m_fp == stderr )
820 {
e2478fde
VZ
821 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
822 if ( traits && !traits->HasStderr() )
823 {
254a2129 824 wxMessageOutputDebug dbgout;
a0516656 825 dbgout.Printf(wxS("%s\n"), str.c_str());
e2478fde 826 }
03147cd0 827 }
c801d85f
KB
828}
829
830// ----------------------------------------------------------------------------
831// wxLogStream implementation
832// ----------------------------------------------------------------------------
833
4bf78aae 834#if wxUSE_STD_IOSTREAM
65f19af1 835#include "wx/ioswrap.h"
dd107c50 836wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 837{
0fb67cd1 838 if ( ostr == NULL )
dd107c50 839 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
840 else
841 m_ostr = ostr;
c801d85f
KB
842}
843
5a20d2ce 844void wxLogStream::DoLogString(const wxString& szString, time_t WXUNUSED(t))
c801d85f 845{
5a20d2ce
VS
846 wxString stamp;
847 TimeStamp(&stamp);
848 (*m_ostr) << stamp << szString << wxSTD endl;
c801d85f 849}
0fb67cd1 850#endif // wxUSE_STD_IOSTREAM
c801d85f 851
03147cd0
VZ
852// ----------------------------------------------------------------------------
853// wxLogChain
854// ----------------------------------------------------------------------------
855
856wxLogChain::wxLogChain(wxLog *logger)
857{
f644b28c 858 m_bPassMessages = true;
71debe95 859
03147cd0
VZ
860 m_logNew = logger;
861 m_logOld = wxLog::SetActiveTarget(this);
862}
863
199e91fb
VZ
864wxLogChain::~wxLogChain()
865{
e95f8fde
VZ
866 delete m_logOld;
867
868 if ( m_logNew != this )
869 delete m_logNew;
199e91fb
VZ
870}
871
03147cd0
VZ
872void wxLogChain::SetLog(wxLog *logger)
873{
874 if ( m_logNew != this )
875 delete m_logNew;
876
03147cd0
VZ
877 m_logNew = logger;
878}
879
880void wxLogChain::Flush()
881{
882 if ( m_logOld )
883 m_logOld->Flush();
884
1ec5cbf3 885 // be careful to avoid infinite recursion
03147cd0
VZ
886 if ( m_logNew && m_logNew != this )
887 m_logNew->Flush();
888}
889
5a20d2ce 890void wxLogChain::DoLog(wxLogLevel level, const wxString& szString, time_t t)
03147cd0
VZ
891{
892 // let the previous logger show it
893 if ( m_logOld && IsPassingMessages() )
894 {
895 // bogus cast just to access protected DoLog
896 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
897 }
898
899 if ( m_logNew && m_logNew != this )
900 {
901 // as above...
902 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
903 }
904}
905
93d4c1d0
VZ
906#ifdef __VISUALC__
907 // "'this' : used in base member initializer list" - so what?
908 #pragma warning(disable:4355)
909#endif // VC++
910
47fe7ff3
JS
911// ----------------------------------------------------------------------------
912// wxLogInterposer
913// ----------------------------------------------------------------------------
914
915wxLogInterposer::wxLogInterposer()
916 : wxLogChain(this)
917{
918}
919
920// ----------------------------------------------------------------------------
921// wxLogInterposerTemp
922// ----------------------------------------------------------------------------
923
924wxLogInterposerTemp::wxLogInterposerTemp()
93d4c1d0
VZ
925 : wxLogChain(this)
926{
47fe7ff3 927 DetachOldLog();
93d4c1d0
VZ
928}
929
930#ifdef __VISUALC__
931 #pragma warning(default:4355)
932#endif // VC++
933
c801d85f
KB
934// ============================================================================
935// Global functions/variables
936// ============================================================================
937
938// ----------------------------------------------------------------------------
939// static variables
940// ----------------------------------------------------------------------------
0fb67cd1 941
0250efd6 942#if wxUSE_THREADS
f96233d5
VZ
943wxCriticalSection wxLog::ms_prevCS,
944 wxLog::ms_traceCS;
0250efd6 945#endif // wxUSE_THREADS
f9837791
VZ
946bool wxLog::ms_bRepetCounting = false;
947wxString wxLog::ms_prevString;
374b4f1c 948unsigned int wxLog::ms_prevCounter = 0;
f9837791
VZ
949time_t wxLog::ms_prevTimeStamp= 0;
950wxLogLevel wxLog::ms_prevLevel;
951
0fb67cd1 952wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
f644b28c
WS
953bool wxLog::ms_doLog = true;
954bool wxLog::ms_bAutoCreate = true;
955bool wxLog::ms_bVerbose = false;
d2e1ef19 956
edc73852
RD
957wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
958
2ed3265e
VZ
959size_t wxLog::ms_suspendCount = 0;
960
a0516656 961wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
d2e1ef19 962
0fb67cd1
VZ
963wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
964wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
965
966// ----------------------------------------------------------------------------
967// stdout error logging helper
968// ----------------------------------------------------------------------------
969
970// helper function: wraps the message and justifies it under given position
971// (looks more pretty on the terminal). Also adds newline at the end.
972//
0fb67cd1
VZ
973// TODO this is now disabled until I find a portable way of determining the
974// terminal window size (ok, I found it but does anybody really cares?)
975#ifdef LOG_PRETTY_WRAP
c801d85f
KB
976static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
977{
0fb67cd1
VZ
978 size_t nMax = 80; // FIXME
979 size_t nStart = strlen(pszPrefix);
980 fputs(pszPrefix, f);
981
982 size_t n;
983 while ( *psz != '\0' ) {
984 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
985 putc(*psz++, f);
986
987 // wrapped?
988 if ( *psz != '\0' ) {
989 /*putc('\n', f);*/
990 for ( n = 0; n < nStart; n++ )
991 putc(' ', f);
992
993 // as we wrapped, squeeze all white space
994 while ( isspace(*psz) )
995 psz++;
996 }
c801d85f 997 }
c801d85f 998
0fb67cd1 999 putc('\n', f);
c801d85f
KB
1000}
1001#endif //LOG_PRETTY_WRAP
1002
1003// ----------------------------------------------------------------------------
1004// error code/error message retrieval functions
1005// ----------------------------------------------------------------------------
1006
1007// get error code from syste
1008unsigned long wxSysErrorCode()
1009{
8cb172b4 1010#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 1011 return ::GetLastError();
0fb67cd1 1012#else //Unix
c801d85f 1013 return errno;
0fb67cd1 1014#endif //Win/Unix
c801d85f
KB
1015}
1016
1017// get error message from system
50920146 1018const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 1019{
0fb67cd1
VZ
1020 if ( nErrCode == 0 )
1021 nErrCode = wxSysErrorCode();
1022
8cb172b4 1023#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
2e7f3845 1024 static wxChar s_szBuf[1024];
0fb67cd1
VZ
1025
1026 // get error message from system
1027 LPVOID lpMsgBuf;
d0822e56
VZ
1028 if ( ::FormatMessage
1029 (
1030 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1031 NULL,
1032 nErrCode,
0fb67cd1
VZ
1033 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1034 (LPTSTR)&lpMsgBuf,
d0822e56
VZ
1035 0,
1036 NULL
1037 ) == 0 )
1038 {
1039 // if this happens, something is seriously wrong, so don't use _() here
1040 // for safety
a0516656 1041 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
c4b401b6 1042 return s_szBuf;
d0822e56
VZ
1043 }
1044
0fb67cd1
VZ
1045
1046 // copy it to our buffer and free memory
d0822e56 1047 // Crashes on SmartPhone (FIXME)
0c44ec97 1048#if !defined(__SMARTPHONE__) /* of WinCE */
7448de8d
WS
1049 if( lpMsgBuf != 0 )
1050 {
8c5b1f0f 1051 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
a0516656 1052 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxS('\0');
251244a0
VZ
1053
1054 LocalFree(lpMsgBuf);
1055
1056 // returned string is capitalized and ended with '\r\n' - bad
1057 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
1058 size_t len = wxStrlen(s_szBuf);
1059 if ( len > 0 ) {
1060 // truncate string
a0516656
VZ
1061 if ( s_szBuf[len - 2] == wxS('\r') )
1062 s_szBuf[len - 2] = wxS('\0');
251244a0
VZ
1063 }
1064 }
a9928e9d 1065 else
2e7f3845 1066#endif // !__SMARTPHONE__
a9928e9d 1067 {
a0516656 1068 s_szBuf[0] = wxS('\0');
0fb67cd1
VZ
1069 }
1070
1071 return s_szBuf;
2e7f3845
VZ
1072#else // !__WXMSW__
1073 #if wxUSE_UNICODE
1074 static wchar_t s_wzBuf[1024];
1075 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1076 WXSIZEOF(s_wzBuf) - 1);
1077 return s_wzBuf;
1078 #else
1079 return strerror((int)nErrCode);
1080 #endif
1081#endif // __WXMSW__/!__WXMSW__
c801d85f
KB
1082}
1083
e2478fde 1084#endif // wxUSE_LOG