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