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