]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
initialize m_initialized to false to avoid spurious asserts
[wxWidgets.git] / src / common / log.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: log.cpp
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>
55d99c7a 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
dd85fc6b 19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e2478fde 21 #pragma implementation "log.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
e2478fde 28 #pragma hdrstop
c801d85f
KB
29#endif
30
e2478fde
VZ
31#if wxUSE_LOG
32
c801d85f
KB
33// wxWindows
34#ifndef WX_PRECOMP
e90c1d2a 35 #include "wx/app.h"
df5168c4 36 #include "wx/arrstr.h"
e2478fde
VZ
37 #include "wx/intl.h"
38 #include "wx/string.h"
9ef3052c 39#endif //WX_PRECOMP
c801d85f 40
e2478fde
VZ
41#include "wx/apptrait.h"
42#include "wx/file.h"
43#include "wx/log.h"
44#include "wx/msgout.h"
45#include "wx/textfile.h"
46#include "wx/thread.h"
47#include "wx/utils.h"
48#include "wx/wxchar.h"
f94dfb38 49
c801d85f 50// other standard headers
1c193821 51#ifndef __WXWINCE__
e2478fde 52#include <errno.h>
1c193821
JS
53#endif
54
e2478fde 55#include <stdlib.h>
1c193821
JS
56
57#ifndef __WXWINCE__
e2478fde 58#include <time.h>
1c193821
JS
59#else
60#include "wx/msw/wince/time.h"
61#endif
31907d03 62
9cce3be7
VS
63#if defined(__WINDOWS__)
64 #include "wx/msw/private.h" // includes windows.h
65#endif
66
c801d85f
KB
67// ----------------------------------------------------------------------------
68// non member functions
69// ----------------------------------------------------------------------------
70
71// define this to enable wrapping of log messages
72//#define LOG_PRETTY_WRAP
73
9ef3052c 74#ifdef LOG_PRETTY_WRAP
c801d85f
KB
75 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
76#endif
77
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
b568d04f 83// globals
c801d85f
KB
84// ----------------------------------------------------------------------------
85
86// log functions can't allocate memory (LogError("out of memory...") should
87// work!), so we use a static buffer for all log messages
88#define LOG_BUFFER_SIZE (4096)
89
b568d04f 90// static buffer for error messages
04662def
RL
91static wxChar s_szBufStatic[LOG_BUFFER_SIZE];
92
93static wxChar *s_szBuf = s_szBufStatic;
94static size_t s_szBufSize = WXSIZEOF( s_szBufStatic );
c801d85f 95
b568d04f
VZ
96#if wxUSE_THREADS
97
98// the critical section protecting the static buffer
99static wxCriticalSection gs_csLogBuf;
100
101#endif // wxUSE_THREADS
102
807a903e
VZ
103// return true if we have a non NULL non disabled log target
104static inline bool IsLoggingEnabled()
105{
106 return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL);
107}
108
b568d04f
VZ
109// ----------------------------------------------------------------------------
110// implementation of Log functions
111//
112// NB: unfortunately we need all these distinct functions, we can't make them
113// macros and not all compilers inline vararg functions.
114// ----------------------------------------------------------------------------
115
ef0dd8e5
VZ
116// wrapper for wxVsnprintf(s_szBuf) which always NULL-terminates it
117static inline void PrintfInLogBug(const wxChar *szFormat, va_list argptr)
118{
119 if ( wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr) < 0 )
120 {
121 // must NUL-terminate it manually
122 s_szBuf[s_szBufSize - 1] = _T('\0');
123 }
124 //else: NUL-terminated by vsnprintf()
125}
126
c801d85f 127// generic log function
1d63fd6b 128void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr)
c801d85f 129{
807a903e
VZ
130 if ( IsLoggingEnabled() ) {
131 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
9ef3052c 132
ef0dd8e5 133 PrintfInLogBug(szFormat, argptr);
807a903e
VZ
134
135 wxLog::OnLog(level, s_szBuf, time(NULL));
136 }
c801d85f
KB
137}
138
ea44a631
GD
139void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
140{
141 va_list argptr;
142 va_start(argptr, szFormat);
1d63fd6b 143 wxVLogGeneric(level, szFormat, argptr);
ea44a631
GD
144 va_end(argptr);
145}
146
807a903e 147#define IMPLEMENT_LOG_FUNCTION(level) \
1d63fd6b 148 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
807a903e
VZ
149 { \
150 if ( IsLoggingEnabled() ) { \
151 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
152 \
ef0dd8e5 153 PrintfInLogBug(szFormat, argptr); \
807a903e
VZ
154 \
155 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
156 } \
ea44a631 157 } \
ef0dd8e5 158 \
ea44a631
GD
159 void wxLog##level(const wxChar *szFormat, ...) \
160 { \
161 va_list argptr; \
162 va_start(argptr, szFormat); \
1800689f 163 wxVLog##level(szFormat, argptr); \
ea44a631 164 va_end(argptr); \
c801d85f
KB
165 }
166
c801d85f
KB
167IMPLEMENT_LOG_FUNCTION(Error)
168IMPLEMENT_LOG_FUNCTION(Warning)
169IMPLEMENT_LOG_FUNCTION(Message)
170IMPLEMENT_LOG_FUNCTION(Info)
171IMPLEMENT_LOG_FUNCTION(Status)
172
c11d62a6
VZ
173void wxSafeShowMessage(const wxString& title, const wxString& text)
174{
175#ifdef __WINDOWS__
176 ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP);
177#else
178 wxFprintf(stderr, _T("%s: %s\n"), title.c_str(), text.c_str());
179#endif
180}
181
1800689f
VZ
182// fatal errors can't be suppressed nor handled by the custom log target and
183// always terminate the program
184void wxVLogFatalError(const wxChar *szFormat, va_list argptr)
185{
04662def 186 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
1800689f 187
c11d62a6 188 wxSafeShowMessage(_T("Fatal Error"), s_szBuf);
1800689f 189
1c193821
JS
190#ifdef __WXWINCE__
191 ExitThread(3);
192#else
1800689f 193 abort();
1c193821 194#endif
1800689f
VZ
195}
196
197void wxLogFatalError(const wxChar *szFormat, ...)
198{
199 va_list argptr;
200 va_start(argptr, szFormat);
201 wxVLogFatalError(szFormat, argptr);
202 va_end(argptr);
203}
204
9ef3052c 205// same as info, but only if 'verbose' mode is on
1d63fd6b 206void wxVLogVerbose(const wxChar *szFormat, va_list argptr)
9ef3052c 207{
807a903e
VZ
208 if ( IsLoggingEnabled() ) {
209 wxLog *pLog = wxLog::GetActiveTarget();
210 if ( pLog != NULL && pLog->GetVerbose() ) {
211 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
212
04662def 213 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
807a903e
VZ
214
215 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
216 }
217 }
9ef3052c
VZ
218}
219
ea44a631
GD
220void wxLogVerbose(const wxChar *szFormat, ...)
221{
222 va_list argptr;
223 va_start(argptr, szFormat);
1d63fd6b 224 wxVLogVerbose(szFormat, argptr);
ea44a631
GD
225 va_end(argptr);
226}
227
9ef3052c 228// debug functions
b2aef89b 229#ifdef __WXDEBUG__
807a903e 230#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
1d63fd6b 231 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
807a903e
VZ
232 { \
233 if ( IsLoggingEnabled() ) { \
234 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
235 \
04662def 236 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); \
807a903e
VZ
237 \
238 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
239 } \
ea44a631
GD
240 } \
241 void wxLog##level(const wxChar *szFormat, ...) \
242 { \
243 va_list argptr; \
244 va_start(argptr, szFormat); \
1d63fd6b 245 wxVLog##level(szFormat, argptr); \
ea44a631 246 va_end(argptr); \
c801d85f
KB
247 }
248
1d63fd6b 249 void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
0fb67cd1 250 {
807a903e 251 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
b568d04f
VZ
252 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
253
00c4e897 254 wxChar *p = s_szBuf;
04662def 255 size_t len = s_szBufSize;
f6bcfd97 256 wxStrncpy(s_szBuf, _T("("), len);
829f0541
VZ
257 len -= 1; // strlen("(")
258 p += 1;
f6bcfd97 259 wxStrncat(p, mask, len);
00c4e897
VZ
260 size_t lenMask = wxStrlen(mask);
261 len -= lenMask;
262 p += lenMask;
263
f6bcfd97 264 wxStrncat(p, _T(") "), len);
829f0541
VZ
265 len -= 2;
266 p += 2;
00c4e897 267
00c4e897 268 wxVsnprintf(p, len, szFormat, argptr);
d91535c4 269
0fb67cd1
VZ
270 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
271 }
272 }
273
ea44a631
GD
274 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
275 {
276 va_list argptr;
277 va_start(argptr, szFormat);
1d63fd6b 278 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
279 va_end(argptr);
280 }
281
1d63fd6b 282 void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
9ef3052c 283 {
9ef3052c
VZ
284 // we check that all of mask bits are set in the current mask, so
285 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
286 // if both bits are set.
807a903e 287 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
b568d04f
VZ
288 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
289
04662def 290 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
9ef3052c 291
0fb67cd1 292 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
9ef3052c
VZ
293 }
294 }
295
ea44a631
GD
296 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
297 {
298 va_list argptr;
299 va_start(argptr, szFormat);
1d63fd6b 300 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
301 va_end(argptr);
302 }
303
9ef3052c
VZ
304#else // release
305 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
306#endif
307
308IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
309IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
310
311// wxLogSysError: one uses the last error code, for other you must give it
312// explicitly
313
314// common part of both wxLogSysError
315void wxLogSysErrorHelper(long lErrCode)
c801d85f 316{
50920146 317 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
378b05f7
VZ
318 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
319 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
04662def 320 wxStrncat(s_szBuf, szErrMsg, s_szBufSize - wxStrlen(s_szBuf));
c801d85f 321
0fb67cd1 322 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
9ef3052c 323}
c801d85f 324
1d63fd6b 325void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr)
9ef3052c 326{
807a903e
VZ
327 if ( IsLoggingEnabled() ) {
328 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 329
04662def 330 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
9ef3052c 331
807a903e
VZ
332 wxLogSysErrorHelper(wxSysErrorCode());
333 }
c801d85f
KB
334}
335
ea44a631
GD
336void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
337{
338 va_list argptr;
339 va_start(argptr, szFormat);
1d63fd6b 340 wxVLogSysError(szFormat, argptr);
ea44a631
GD
341 va_end(argptr);
342}
343
1d63fd6b 344void WXDLLEXPORT wxVLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr)
c801d85f 345{
807a903e
VZ
346 if ( IsLoggingEnabled() ) {
347 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 348
04662def 349 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
c801d85f 350
807a903e
VZ
351 wxLogSysErrorHelper(lErrCode);
352 }
c801d85f
KB
353}
354
ea44a631
GD
355void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
356{
357 va_list argptr;
358 va_start(argptr, szFormat);
1d63fd6b 359 wxVLogSysError(lErrCode, szFormat, argptr);
ea44a631
GD
360 va_end(argptr);
361}
362
c801d85f
KB
363// ----------------------------------------------------------------------------
364// wxLog class implementation
365// ----------------------------------------------------------------------------
366
367wxLog::wxLog()
368{
c801d85f
KB
369}
370
d91535c4 371wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size)
04662def
RL
372{
373 wxChar *oldbuf = s_szBuf;
374
375 if( buf == 0 )
376 {
377 s_szBuf = s_szBufStatic;
378 s_szBufSize = WXSIZEOF( s_szBufStatic );
379 }
380 else
381 {
382 s_szBuf = buf;
383 s_szBufSize = size;
384 }
385
386 return (oldbuf == s_szBufStatic ) ? 0 : oldbuf;
387}
388
9ec05cc9
VZ
389wxLog *wxLog::GetActiveTarget()
390{
0fb67cd1
VZ
391 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
392 // prevent infinite recursion if someone calls wxLogXXX() from
393 // wxApp::CreateLogTarget()
394 static bool s_bInGetActiveTarget = FALSE;
395 if ( !s_bInGetActiveTarget ) {
396 s_bInGetActiveTarget = TRUE;
397
0fb67cd1
VZ
398 // ask the application to create a log target for us
399 if ( wxTheApp != NULL )
dc6d5e38 400 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
401 else
402 ms_pLogger = new wxLogStderr;
0fb67cd1
VZ
403
404 s_bInGetActiveTarget = FALSE;
405
406 // do nothing if it fails - what can we do?
407 }
275bf4c1 408 }
c801d85f 409
0fb67cd1 410 return ms_pLogger;
c801d85f
KB
411}
412
c085e333 413wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 414{
0fb67cd1
VZ
415 if ( ms_pLogger != NULL ) {
416 // flush the old messages before changing because otherwise they might
417 // get lost later if this target is not restored
418 ms_pLogger->Flush();
419 }
c801d85f 420
0fb67cd1
VZ
421 wxLog *pOldLogger = ms_pLogger;
422 ms_pLogger = pLogger;
c085e333 423
0fb67cd1 424 return pOldLogger;
c801d85f
KB
425}
426
36bd6902
VZ
427void wxLog::DontCreateOnDemand()
428{
429 ms_bAutoCreate = FALSE;
430
431 // this is usually called at the end of the program and we assume that it
432 // is *always* called at the end - so we free memory here to avoid false
433 // memory leak reports from wxWin memory tracking code
434 ClearTraceMasks();
435}
436
0fb67cd1 437void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 438{
0fb67cd1
VZ
439 int index = ms_aTraceMasks.Index(str);
440 if ( index != wxNOT_FOUND )
dc6d5e38 441 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 442}
c801d85f 443
36bd6902
VZ
444void wxLog::ClearTraceMasks()
445{
446 ms_aTraceMasks.Clear();
447}
448
d2e1ef19
VZ
449void wxLog::TimeStamp(wxString *str)
450{
451 if ( ms_timestamp )
452 {
453 wxChar buf[256];
454 time_t timeNow;
455 (void)time(&timeNow);
c49245f8 456 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
d2e1ef19
VZ
457
458 str->Empty();
223d09f6 459 *str << buf << wxT(": ");
d2e1ef19
VZ
460 }
461}
462
50920146 463void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 464{
0fb67cd1
VZ
465 switch ( level ) {
466 case wxLOG_FatalError:
786855a1 467 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
468 DoLogString(_("Program aborted."), t);
469 Flush();
1c193821
JS
470#ifdef __WXWINCE__
471 ExitThread(3);
472#else
0fb67cd1 473 abort();
1c193821 474#endif
0fb67cd1
VZ
475 break;
476
477 case wxLOG_Error:
786855a1 478 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
479 break;
480
481 case wxLOG_Warning:
786855a1 482 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
483 break;
484
485 case wxLOG_Info:
0fb67cd1 486 if ( GetVerbose() )
37278984 487 case wxLOG_Message:
87a1e308 488 case wxLOG_Status:
786855a1
VZ
489 default: // log unknown log levels too
490 DoLogString(szString, t);
0fb67cd1
VZ
491 break;
492
493 case wxLOG_Trace:
494 case wxLOG_Debug:
495#ifdef __WXDEBUG__
0131687b
VZ
496 {
497 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 498 : wxT("Debug: ");
0131687b
VZ
499 msg << szString;
500 DoLogString(msg, t);
501 }
502#endif // Debug
0fb67cd1 503 break;
0fb67cd1 504 }
c801d85f
KB
505}
506
74e3313b 507void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 508{
223d09f6 509 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
510}
511
512void wxLog::Flush()
513{
1ec5cbf3 514 // nothing to do here
c801d85f
KB
515}
516
df5168c4
MB
517/*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask)
518{
519 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
520 en = ms_aTraceMasks.end();
521 it != en; ++it )
522 if ( *it == mask)
523 return true;
524 return false;
525}
526
c801d85f
KB
527// ----------------------------------------------------------------------------
528// wxLogStderr class implementation
529// ----------------------------------------------------------------------------
530
531wxLogStderr::wxLogStderr(FILE *fp)
532{
0fb67cd1
VZ
533 if ( fp == NULL )
534 m_fp = stderr;
535 else
536 m_fp = fp;
c801d85f
KB
537}
538
74e3313b 539void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 540{
d2e1ef19
VZ
541 wxString str;
542 TimeStamp(&str);
b568d04f 543 str << szString;
1e8a4bc2 544
50920146 545 fputs(str.mb_str(), m_fp);
b568d04f 546 fputc(_T('\n'), m_fp);
0fb67cd1 547 fflush(m_fp);
1e8a4bc2 548
e2478fde
VZ
549 // under GUI systems such as Windows or Mac, programs usually don't have
550 // stderr at all, so show the messages also somewhere else, typically in
551 // the debugger window so that they go at least somewhere instead of being
552 // simply lost
1ec5cbf3
VZ
553 if ( m_fp == stderr )
554 {
e2478fde
VZ
555 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
556 if ( traits && !traits->HasStderr() )
557 {
558 wxMessageOutputDebug().Printf(_T("%s"), str.c_str());
559 }
03147cd0 560 }
c801d85f
KB
561}
562
563// ----------------------------------------------------------------------------
564// wxLogStream implementation
565// ----------------------------------------------------------------------------
566
4bf78aae 567#if wxUSE_STD_IOSTREAM
65f19af1 568#include "wx/ioswrap.h"
dd107c50 569wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 570{
0fb67cd1 571 if ( ostr == NULL )
dd107c50 572 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
573 else
574 m_ostr = ostr;
c801d85f
KB
575}
576
74e3313b 577void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 578{
29fd317b
VZ
579 wxString str;
580 TimeStamp(&str);
dd107c50 581 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 582}
0fb67cd1 583#endif // wxUSE_STD_IOSTREAM
c801d85f 584
03147cd0
VZ
585// ----------------------------------------------------------------------------
586// wxLogChain
587// ----------------------------------------------------------------------------
588
589wxLogChain::wxLogChain(wxLog *logger)
590{
71debe95
VZ
591 m_bPassMessages = TRUE;
592
03147cd0
VZ
593 m_logNew = logger;
594 m_logOld = wxLog::SetActiveTarget(this);
595}
596
199e91fb
VZ
597wxLogChain::~wxLogChain()
598{
e95f8fde
VZ
599 delete m_logOld;
600
601 if ( m_logNew != this )
602 delete m_logNew;
199e91fb
VZ
603}
604
03147cd0
VZ
605void wxLogChain::SetLog(wxLog *logger)
606{
607 if ( m_logNew != this )
608 delete m_logNew;
609
03147cd0
VZ
610 m_logNew = logger;
611}
612
613void wxLogChain::Flush()
614{
615 if ( m_logOld )
616 m_logOld->Flush();
617
1ec5cbf3 618 // be careful to avoid infinite recursion
03147cd0
VZ
619 if ( m_logNew && m_logNew != this )
620 m_logNew->Flush();
621}
622
623void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
624{
625 // let the previous logger show it
626 if ( m_logOld && IsPassingMessages() )
627 {
628 // bogus cast just to access protected DoLog
629 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
630 }
631
632 if ( m_logNew && m_logNew != this )
633 {
634 // as above...
635 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
636 }
637}
638
93d4c1d0
VZ
639// ----------------------------------------------------------------------------
640// wxLogPassThrough
641// ----------------------------------------------------------------------------
642
643#ifdef __VISUALC__
644 // "'this' : used in base member initializer list" - so what?
645 #pragma warning(disable:4355)
646#endif // VC++
647
648wxLogPassThrough::wxLogPassThrough()
649 : wxLogChain(this)
650{
651}
652
653#ifdef __VISUALC__
654 #pragma warning(default:4355)
655#endif // VC++
656
c801d85f
KB
657// ============================================================================
658// Global functions/variables
659// ============================================================================
660
661// ----------------------------------------------------------------------------
662// static variables
663// ----------------------------------------------------------------------------
0fb67cd1
VZ
664
665wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
666bool wxLog::ms_doLog = TRUE;
667bool wxLog::ms_bAutoCreate = TRUE;
fd7718b2 668bool wxLog::ms_bVerbose = FALSE;
d2e1ef19 669
edc73852
RD
670wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
671
2ed3265e
VZ
672size_t wxLog::ms_suspendCount = 0;
673
e2478fde 674const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
d2e1ef19 675
0fb67cd1
VZ
676wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
677wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
678
679// ----------------------------------------------------------------------------
680// stdout error logging helper
681// ----------------------------------------------------------------------------
682
683// helper function: wraps the message and justifies it under given position
684// (looks more pretty on the terminal). Also adds newline at the end.
685//
0fb67cd1
VZ
686// TODO this is now disabled until I find a portable way of determining the
687// terminal window size (ok, I found it but does anybody really cares?)
688#ifdef LOG_PRETTY_WRAP
c801d85f
KB
689static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
690{
0fb67cd1
VZ
691 size_t nMax = 80; // FIXME
692 size_t nStart = strlen(pszPrefix);
693 fputs(pszPrefix, f);
694
695 size_t n;
696 while ( *psz != '\0' ) {
697 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
698 putc(*psz++, f);
699
700 // wrapped?
701 if ( *psz != '\0' ) {
702 /*putc('\n', f);*/
703 for ( n = 0; n < nStart; n++ )
704 putc(' ', f);
705
706 // as we wrapped, squeeze all white space
707 while ( isspace(*psz) )
708 psz++;
709 }
c801d85f 710 }
c801d85f 711
0fb67cd1 712 putc('\n', f);
c801d85f
KB
713}
714#endif //LOG_PRETTY_WRAP
715
716// ----------------------------------------------------------------------------
717// error code/error message retrieval functions
718// ----------------------------------------------------------------------------
719
720// get error code from syste
721unsigned long wxSysErrorCode()
722{
8cb172b4 723#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1
VZ
724#ifdef __WIN32__
725 return ::GetLastError();
726#else //WIN16
727 // TODO what to do on Windows 3.1?
728 return 0;
729#endif //WIN16/32
730#else //Unix
c801d85f 731 return errno;
0fb67cd1 732#endif //Win/Unix
c801d85f
KB
733}
734
735// get error message from system
50920146 736const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 737{
0fb67cd1
VZ
738 if ( nErrCode == 0 )
739 nErrCode = wxSysErrorCode();
740
8cb172b4 741#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 742#ifdef __WIN32__
50920146 743 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
744
745 // get error message from system
746 LPVOID lpMsgBuf;
747 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
748 NULL, nErrCode,
749 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
750 (LPTSTR)&lpMsgBuf,
751 0, NULL);
752
753 // copy it to our buffer and free memory
251244a0 754 if( lpMsgBuf != 0 ) {
8c5b1f0f 755 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
756 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
757
758 LocalFree(lpMsgBuf);
759
760 // returned string is capitalized and ended with '\r\n' - bad
761 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
762 size_t len = wxStrlen(s_szBuf);
763 if ( len > 0 ) {
764 // truncate string
765 if ( s_szBuf[len - 2] == wxT('\r') )
766 s_szBuf[len - 2] = wxT('\0');
767 }
768 }
769 else {
8c5b1f0f 770 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
771 }
772
773 return s_szBuf;
774#else //Win16
775 // TODO
776 return NULL;
777#endif // Win16/32
778#else // Unix
50920146
OK
779#if wxUSE_UNICODE
780 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
dcf924a3 781 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
50920146
OK
782 return s_szBuf;
783#else
13111b2a 784 return strerror((int)nErrCode);
50920146 785#endif
0fb67cd1 786#endif // Win/Unix
c801d85f
KB
787}
788
e2478fde 789#endif // wxUSE_LOG
04662def 790