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