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