]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
remove unused static function (avoids a warning about it)
[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"
e2478fde 45#include "wx/wxchar.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__
160 ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP);
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
VZ
460/* static */
461unsigned wxLog::DoLogNumberOfRepeats()
462{
463 long retval = ms_prevCounter;
464 wxLog *pLogger = GetActiveTarget();
465 if ( pLogger && ms_prevCounter > 0 )
466 {
467 wxString msg;
459b97df 468#if wxUSE_INTL
f9837791
VZ
469 msg.Printf(wxPLURAL("The previous message repeated once.",
470 "The previous message repeated %lu times.",
471 ms_prevCounter),
472 ms_prevCounter);
459b97df
WS
473#else
474 msg.Printf(wxT("The previous message was repeated."));
475#endif
f9837791
VZ
476 ms_prevCounter = 0;
477 ms_prevString.clear();
478 pLogger->DoLog(ms_prevLevel, msg.c_str(), ms_prevTimeStamp);
479 }
480 return retval;
481}
482
483wxLog::~wxLog()
484{
485 if ( ms_prevCounter > 0 )
486 {
487 // looks like the repeat count has not been logged yet,
488 // so let's do it now
489 wxLog::DoLogNumberOfRepeats();
490 }
491}
492
493/* static */
494void wxLog::OnLog(wxLogLevel level, const wxChar *szString, time_t t)
495{
496 if ( IsEnabled() && ms_logLevel >= level )
497 {
498 wxLog *pLogger = GetActiveTarget();
499 if ( pLogger )
500 {
501 if ( GetRepetitionCounting() && ms_prevString == szString )
502 {
503 ms_prevCounter++;
504 }
505 else
506 {
507 if ( GetRepetitionCounting() )
508 {
61d8dec7 509 DoLogNumberOfRepeats();
f9837791
VZ
510 }
511 ms_prevString = szString;
512 ms_prevLevel = level;
513 ms_prevTimeStamp = t;
514 pLogger->DoLog(level, szString, t);
515 }
516 }
517 }
518}
519
2e7f3845 520// deprecated function
dcc40ba5
VZ
521#if WXWIN_COMPATIBILITY_2_6
522
2e7f3845 523wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
04662def 524{
2e7f3845 525 return NULL;
04662def
RL
526}
527
dcc40ba5
VZ
528#endif // WXWIN_COMPATIBILITY_2_6
529
9ec05cc9
VZ
530wxLog *wxLog::GetActiveTarget()
531{
0fb67cd1
VZ
532 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
533 // prevent infinite recursion if someone calls wxLogXXX() from
534 // wxApp::CreateLogTarget()
f644b28c 535 static bool s_bInGetActiveTarget = false;
0fb67cd1 536 if ( !s_bInGetActiveTarget ) {
f644b28c 537 s_bInGetActiveTarget = true;
0fb67cd1 538
0fb67cd1
VZ
539 // ask the application to create a log target for us
540 if ( wxTheApp != NULL )
dc6d5e38 541 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
542 else
543 ms_pLogger = new wxLogStderr;
0fb67cd1 544
f644b28c 545 s_bInGetActiveTarget = false;
0fb67cd1
VZ
546
547 // do nothing if it fails - what can we do?
548 }
275bf4c1 549 }
c801d85f 550
0fb67cd1 551 return ms_pLogger;
c801d85f
KB
552}
553
c085e333 554wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 555{
0fb67cd1
VZ
556 if ( ms_pLogger != NULL ) {
557 // flush the old messages before changing because otherwise they might
558 // get lost later if this target is not restored
559 ms_pLogger->Flush();
560 }
c801d85f 561
0fb67cd1
VZ
562 wxLog *pOldLogger = ms_pLogger;
563 ms_pLogger = pLogger;
c085e333 564
0fb67cd1 565 return pOldLogger;
c801d85f
KB
566}
567
36bd6902
VZ
568void wxLog::DontCreateOnDemand()
569{
f644b28c 570 ms_bAutoCreate = false;
36bd6902
VZ
571
572 // this is usually called at the end of the program and we assume that it
573 // is *always* called at the end - so we free memory here to avoid false
574 // memory leak reports from wxWin memory tracking code
575 ClearTraceMasks();
576}
577
e94cd97d
DE
578void wxLog::DoCreateOnDemand()
579{
580 ms_bAutoCreate = true;
581}
582
0fb67cd1 583void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 584{
0fb67cd1
VZ
585 int index = ms_aTraceMasks.Index(str);
586 if ( index != wxNOT_FOUND )
dc6d5e38 587 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 588}
c801d85f 589
36bd6902
VZ
590void wxLog::ClearTraceMasks()
591{
592 ms_aTraceMasks.Clear();
593}
594
d2e1ef19
VZ
595void wxLog::TimeStamp(wxString *str)
596{
7b2d1c74 597#if wxUSE_DATETIME
d2e1ef19
VZ
598 if ( ms_timestamp )
599 {
600 wxChar buf[256];
601 time_t timeNow;
602 (void)time(&timeNow);
83e8b44c
VZ
603
604 struct tm tm;
605 wxStrftime(buf, WXSIZEOF(buf),
606 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
d2e1ef19
VZ
607
608 str->Empty();
223d09f6 609 *str << buf << wxT(": ");
d2e1ef19 610 }
7b2d1c74 611#endif // wxUSE_DATETIME
d2e1ef19
VZ
612}
613
50920146 614void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 615{
0fb67cd1
VZ
616 switch ( level ) {
617 case wxLOG_FatalError:
786855a1 618 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
619 DoLogString(_("Program aborted."), t);
620 Flush();
1c193821
JS
621#ifdef __WXWINCE__
622 ExitThread(3);
623#else
0fb67cd1 624 abort();
1c193821 625#endif
0fb67cd1
VZ
626 break;
627
628 case wxLOG_Error:
786855a1 629 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
630 break;
631
632 case wxLOG_Warning:
786855a1 633 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
634 break;
635
636 case wxLOG_Info:
0fb67cd1 637 if ( GetVerbose() )
37278984 638 case wxLOG_Message:
87a1e308 639 case wxLOG_Status:
786855a1
VZ
640 default: // log unknown log levels too
641 DoLogString(szString, t);
0fb67cd1
VZ
642 break;
643
644 case wxLOG_Trace:
645 case wxLOG_Debug:
646#ifdef __WXDEBUG__
0131687b
VZ
647 {
648 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 649 : wxT("Debug: ");
0131687b
VZ
650 msg << szString;
651 DoLogString(msg, t);
652 }
653#endif // Debug
0fb67cd1 654 break;
0fb67cd1 655 }
c801d85f
KB
656}
657
74e3313b 658void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 659{
223d09f6 660 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
661}
662
663void wxLog::Flush()
664{
1ec5cbf3 665 // nothing to do here
c801d85f
KB
666}
667
df5168c4
MB
668/*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask)
669{
670 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
671 en = ms_aTraceMasks.end();
672 it != en; ++it )
673 if ( *it == mask)
674 return true;
675 return false;
676}
677
d3fc1755
VZ
678// ----------------------------------------------------------------------------
679// wxLogBuffer implementation
680// ----------------------------------------------------------------------------
681
682void wxLogBuffer::Flush()
683{
a23bbe93
VZ
684 if ( !m_str.empty() )
685 {
686 wxMessageOutputBest out;
687 out.Printf(_T("%s"), m_str.c_str());
688 m_str.clear();
689 }
d3fc1755
VZ
690}
691
83250f1a
VZ
692void wxLogBuffer::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
693{
694 switch ( level )
695 {
696 case wxLOG_Trace:
697 case wxLOG_Debug:
698#ifdef __WXDEBUG__
699 // don't put debug messages in the buffer, we don't want to show
700 // them to the user in a msg box, log them immediately
701 {
702 wxString str;
703 TimeStamp(&str);
704 str += szString;
705
c4b401b6
WS
706 wxMessageOutputDebug dbgout;
707 dbgout.Printf(_T("%s\n"), str.c_str());
83250f1a
VZ
708 }
709#endif // __WXDEBUG__
710 break;
711
712 default:
713 wxLog::DoLog(level, szString, t);
714 }
715}
716
d3fc1755
VZ
717void wxLogBuffer::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
718{
719 m_str << szString << _T("\n");
720}
721
c801d85f
KB
722// ----------------------------------------------------------------------------
723// wxLogStderr class implementation
724// ----------------------------------------------------------------------------
725
726wxLogStderr::wxLogStderr(FILE *fp)
727{
0fb67cd1
VZ
728 if ( fp == NULL )
729 m_fp = stderr;
730 else
731 m_fp = fp;
c801d85f
KB
732}
733
74e3313b 734void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 735{
d2e1ef19
VZ
736 wxString str;
737 TimeStamp(&str);
b568d04f 738 str << szString;
1e8a4bc2 739
14ff7a59
VZ
740 wxFputs(str, m_fp);
741 wxFputc(_T('\n'), m_fp);
0fb67cd1 742 fflush(m_fp);
1e8a4bc2 743
e2478fde
VZ
744 // under GUI systems such as Windows or Mac, programs usually don't have
745 // stderr at all, so show the messages also somewhere else, typically in
746 // the debugger window so that they go at least somewhere instead of being
747 // simply lost
1ec5cbf3
VZ
748 if ( m_fp == stderr )
749 {
e2478fde
VZ
750 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
751 if ( traits && !traits->HasStderr() )
752 {
254a2129 753 wxMessageOutputDebug dbgout;
30413fd0 754 dbgout.Printf(_T("%s\n"), str.c_str());
e2478fde 755 }
03147cd0 756 }
c801d85f
KB
757}
758
759// ----------------------------------------------------------------------------
760// wxLogStream implementation
761// ----------------------------------------------------------------------------
762
4bf78aae 763#if wxUSE_STD_IOSTREAM
65f19af1 764#include "wx/ioswrap.h"
dd107c50 765wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 766{
0fb67cd1 767 if ( ostr == NULL )
dd107c50 768 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
769 else
770 m_ostr = ostr;
c801d85f
KB
771}
772
74e3313b 773void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 774{
29fd317b
VZ
775 wxString str;
776 TimeStamp(&str);
416f9764 777 (*m_ostr) << wxConvertWX2MB(str) << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 778}
0fb67cd1 779#endif // wxUSE_STD_IOSTREAM
c801d85f 780
03147cd0
VZ
781// ----------------------------------------------------------------------------
782// wxLogChain
783// ----------------------------------------------------------------------------
784
785wxLogChain::wxLogChain(wxLog *logger)
786{
f644b28c 787 m_bPassMessages = true;
71debe95 788
03147cd0
VZ
789 m_logNew = logger;
790 m_logOld = wxLog::SetActiveTarget(this);
791}
792
199e91fb
VZ
793wxLogChain::~wxLogChain()
794{
e95f8fde
VZ
795 delete m_logOld;
796
797 if ( m_logNew != this )
798 delete m_logNew;
199e91fb
VZ
799}
800
03147cd0
VZ
801void wxLogChain::SetLog(wxLog *logger)
802{
803 if ( m_logNew != this )
804 delete m_logNew;
805
03147cd0
VZ
806 m_logNew = logger;
807}
808
809void wxLogChain::Flush()
810{
811 if ( m_logOld )
812 m_logOld->Flush();
813
1ec5cbf3 814 // be careful to avoid infinite recursion
03147cd0
VZ
815 if ( m_logNew && m_logNew != this )
816 m_logNew->Flush();
817}
818
819void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
820{
821 // let the previous logger show it
822 if ( m_logOld && IsPassingMessages() )
823 {
824 // bogus cast just to access protected DoLog
825 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
826 }
827
828 if ( m_logNew && m_logNew != this )
829 {
830 // as above...
831 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
832 }
833}
834
93d4c1d0
VZ
835// ----------------------------------------------------------------------------
836// wxLogPassThrough
837// ----------------------------------------------------------------------------
838
839#ifdef __VISUALC__
840 // "'this' : used in base member initializer list" - so what?
841 #pragma warning(disable:4355)
842#endif // VC++
843
844wxLogPassThrough::wxLogPassThrough()
845 : wxLogChain(this)
846{
847}
848
849#ifdef __VISUALC__
850 #pragma warning(default:4355)
851#endif // VC++
852
c801d85f
KB
853// ============================================================================
854// Global functions/variables
855// ============================================================================
856
857// ----------------------------------------------------------------------------
858// static variables
859// ----------------------------------------------------------------------------
0fb67cd1 860
f9837791
VZ
861bool wxLog::ms_bRepetCounting = false;
862wxString wxLog::ms_prevString;
374b4f1c 863unsigned int wxLog::ms_prevCounter = 0;
f9837791
VZ
864time_t wxLog::ms_prevTimeStamp= 0;
865wxLogLevel wxLog::ms_prevLevel;
866
0fb67cd1 867wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
f644b28c
WS
868bool wxLog::ms_doLog = true;
869bool wxLog::ms_bAutoCreate = true;
870bool wxLog::ms_bVerbose = false;
d2e1ef19 871
edc73852
RD
872wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
873
2ed3265e
VZ
874size_t wxLog::ms_suspendCount = 0;
875
e2478fde 876const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
d2e1ef19 877
0fb67cd1
VZ
878wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
879wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
880
881// ----------------------------------------------------------------------------
882// stdout error logging helper
883// ----------------------------------------------------------------------------
884
885// helper function: wraps the message and justifies it under given position
886// (looks more pretty on the terminal). Also adds newline at the end.
887//
0fb67cd1
VZ
888// TODO this is now disabled until I find a portable way of determining the
889// terminal window size (ok, I found it but does anybody really cares?)
890#ifdef LOG_PRETTY_WRAP
c801d85f
KB
891static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
892{
0fb67cd1
VZ
893 size_t nMax = 80; // FIXME
894 size_t nStart = strlen(pszPrefix);
895 fputs(pszPrefix, f);
896
897 size_t n;
898 while ( *psz != '\0' ) {
899 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
900 putc(*psz++, f);
901
902 // wrapped?
903 if ( *psz != '\0' ) {
904 /*putc('\n', f);*/
905 for ( n = 0; n < nStart; n++ )
906 putc(' ', f);
907
908 // as we wrapped, squeeze all white space
909 while ( isspace(*psz) )
910 psz++;
911 }
c801d85f 912 }
c801d85f 913
0fb67cd1 914 putc('\n', f);
c801d85f
KB
915}
916#endif //LOG_PRETTY_WRAP
917
918// ----------------------------------------------------------------------------
919// error code/error message retrieval functions
920// ----------------------------------------------------------------------------
921
922// get error code from syste
923unsigned long wxSysErrorCode()
924{
8cb172b4 925#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 926 return ::GetLastError();
0fb67cd1 927#else //Unix
c801d85f 928 return errno;
0fb67cd1 929#endif //Win/Unix
c801d85f
KB
930}
931
932// get error message from system
50920146 933const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 934{
0fb67cd1
VZ
935 if ( nErrCode == 0 )
936 nErrCode = wxSysErrorCode();
937
8cb172b4 938#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
2e7f3845 939 static wxChar s_szBuf[1024];
0fb67cd1
VZ
940
941 // get error message from system
942 LPVOID lpMsgBuf;
d0822e56
VZ
943 if ( ::FormatMessage
944 (
945 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
946 NULL,
947 nErrCode,
0fb67cd1
VZ
948 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
949 (LPTSTR)&lpMsgBuf,
d0822e56
VZ
950 0,
951 NULL
952 ) == 0 )
953 {
954 // if this happens, something is seriously wrong, so don't use _() here
955 // for safety
956 wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode);
c4b401b6 957 return s_szBuf;
d0822e56
VZ
958 }
959
0fb67cd1
VZ
960
961 // copy it to our buffer and free memory
d0822e56 962 // Crashes on SmartPhone (FIXME)
0c44ec97 963#if !defined(__SMARTPHONE__) /* of WinCE */
7448de8d
WS
964 if( lpMsgBuf != 0 )
965 {
8c5b1f0f 966 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
967 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
968
969 LocalFree(lpMsgBuf);
970
971 // returned string is capitalized and ended with '\r\n' - bad
972 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
973 size_t len = wxStrlen(s_szBuf);
974 if ( len > 0 ) {
975 // truncate string
976 if ( s_szBuf[len - 2] == wxT('\r') )
977 s_szBuf[len - 2] = wxT('\0');
978 }
979 }
a9928e9d 980 else
2e7f3845 981#endif // !__SMARTPHONE__
a9928e9d 982 {
8c5b1f0f 983 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
984 }
985
986 return s_szBuf;
2e7f3845
VZ
987#else // !__WXMSW__
988 #if wxUSE_UNICODE
989 static wchar_t s_wzBuf[1024];
990 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
991 WXSIZEOF(s_wzBuf) - 1);
992 return s_wzBuf;
993 #else
994 return strerror((int)nErrCode);
995 #endif
996#endif // __WXMSW__/!__WXMSW__
c801d85f
KB
997}
998
e2478fde 999#endif // wxUSE_LOG