]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
fix darwin defines
[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>
65571936 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
77ffb593 33// wxWidgets
c801d85f 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);
5e475383
VZ
202
203 // some compilers warn about unreachable code and it shouldn't matter
204 // for the others anyhow...
205 //va_end(argptr);
1800689f
VZ
206}
207
9ef3052c 208// same as info, but only if 'verbose' mode is on
1d63fd6b 209void wxVLogVerbose(const wxChar *szFormat, va_list argptr)
9ef3052c 210{
807a903e
VZ
211 if ( IsLoggingEnabled() ) {
212 wxLog *pLog = wxLog::GetActiveTarget();
213 if ( pLog != NULL && pLog->GetVerbose() ) {
214 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
215
04662def 216 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
807a903e
VZ
217
218 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
219 }
220 }
9ef3052c
VZ
221}
222
ea44a631
GD
223void wxLogVerbose(const wxChar *szFormat, ...)
224{
225 va_list argptr;
226 va_start(argptr, szFormat);
1d63fd6b 227 wxVLogVerbose(szFormat, argptr);
ea44a631
GD
228 va_end(argptr);
229}
230
9ef3052c 231// debug functions
b2aef89b 232#ifdef __WXDEBUG__
807a903e 233#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
1d63fd6b 234 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
807a903e
VZ
235 { \
236 if ( IsLoggingEnabled() ) { \
237 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
238 \
04662def 239 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); \
807a903e
VZ
240 \
241 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
242 } \
ea44a631
GD
243 } \
244 void wxLog##level(const wxChar *szFormat, ...) \
245 { \
246 va_list argptr; \
247 va_start(argptr, szFormat); \
1d63fd6b 248 wxVLog##level(szFormat, argptr); \
ea44a631 249 va_end(argptr); \
c801d85f
KB
250 }
251
1d63fd6b 252 void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
0fb67cd1 253 {
807a903e 254 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
b568d04f
VZ
255 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
256
00c4e897 257 wxChar *p = s_szBuf;
04662def 258 size_t len = s_szBufSize;
f6bcfd97 259 wxStrncpy(s_szBuf, _T("("), len);
829f0541
VZ
260 len -= 1; // strlen("(")
261 p += 1;
f6bcfd97 262 wxStrncat(p, mask, len);
00c4e897
VZ
263 size_t lenMask = wxStrlen(mask);
264 len -= lenMask;
265 p += lenMask;
266
f6bcfd97 267 wxStrncat(p, _T(") "), len);
829f0541
VZ
268 len -= 2;
269 p += 2;
00c4e897 270
00c4e897 271 wxVsnprintf(p, len, szFormat, argptr);
d91535c4 272
0fb67cd1
VZ
273 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
274 }
275 }
276
ea44a631
GD
277 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
278 {
279 va_list argptr;
280 va_start(argptr, szFormat);
1d63fd6b 281 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
282 va_end(argptr);
283 }
284
1d63fd6b 285 void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
9ef3052c 286 {
9ef3052c
VZ
287 // we check that all of mask bits are set in the current mask, so
288 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
289 // if both bits are set.
807a903e 290 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
b568d04f
VZ
291 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
292
04662def 293 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
9ef3052c 294
0fb67cd1 295 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
9ef3052c
VZ
296 }
297 }
298
ea44a631
GD
299 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
300 {
301 va_list argptr;
302 va_start(argptr, szFormat);
1d63fd6b 303 wxVLogTrace(mask, szFormat, argptr);
ea44a631
GD
304 va_end(argptr);
305 }
306
9ef3052c
VZ
307#else // release
308 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
309#endif
310
311IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
312IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
313
314// wxLogSysError: one uses the last error code, for other you must give it
315// explicitly
316
317// common part of both wxLogSysError
318void wxLogSysErrorHelper(long lErrCode)
c801d85f 319{
50920146 320 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
378b05f7
VZ
321 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
322 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
04662def 323 wxStrncat(s_szBuf, szErrMsg, s_szBufSize - wxStrlen(s_szBuf));
c801d85f 324
0fb67cd1 325 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
9ef3052c 326}
c801d85f 327
1d63fd6b 328void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr)
9ef3052c 329{
807a903e
VZ
330 if ( IsLoggingEnabled() ) {
331 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 332
04662def 333 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
9ef3052c 334
807a903e
VZ
335 wxLogSysErrorHelper(wxSysErrorCode());
336 }
c801d85f
KB
337}
338
ea44a631
GD
339void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
340{
341 va_list argptr;
342 va_start(argptr, szFormat);
1d63fd6b 343 wxVLogSysError(szFormat, argptr);
ea44a631
GD
344 va_end(argptr);
345}
346
1d63fd6b 347void WXDLLEXPORT wxVLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr)
c801d85f 348{
807a903e
VZ
349 if ( IsLoggingEnabled() ) {
350 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 351
04662def 352 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
c801d85f 353
807a903e
VZ
354 wxLogSysErrorHelper(lErrCode);
355 }
c801d85f
KB
356}
357
ea44a631
GD
358void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
359{
360 va_list argptr;
361 va_start(argptr, szFormat);
1d63fd6b 362 wxVLogSysError(lErrCode, szFormat, argptr);
ea44a631
GD
363 va_end(argptr);
364}
365
c801d85f
KB
366// ----------------------------------------------------------------------------
367// wxLog class implementation
368// ----------------------------------------------------------------------------
369
370wxLog::wxLog()
371{
c801d85f
KB
372}
373
d91535c4 374wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size)
04662def
RL
375{
376 wxChar *oldbuf = s_szBuf;
377
378 if( buf == 0 )
379 {
380 s_szBuf = s_szBufStatic;
381 s_szBufSize = WXSIZEOF( s_szBufStatic );
382 }
383 else
384 {
385 s_szBuf = buf;
386 s_szBufSize = size;
387 }
388
389 return (oldbuf == s_szBufStatic ) ? 0 : oldbuf;
390}
391
9ec05cc9
VZ
392wxLog *wxLog::GetActiveTarget()
393{
0fb67cd1
VZ
394 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
395 // prevent infinite recursion if someone calls wxLogXXX() from
396 // wxApp::CreateLogTarget()
f644b28c 397 static bool s_bInGetActiveTarget = false;
0fb67cd1 398 if ( !s_bInGetActiveTarget ) {
f644b28c 399 s_bInGetActiveTarget = true;
0fb67cd1 400
0fb67cd1
VZ
401 // ask the application to create a log target for us
402 if ( wxTheApp != NULL )
dc6d5e38 403 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
404 else
405 ms_pLogger = new wxLogStderr;
0fb67cd1 406
f644b28c 407 s_bInGetActiveTarget = false;
0fb67cd1
VZ
408
409 // do nothing if it fails - what can we do?
410 }
275bf4c1 411 }
c801d85f 412
0fb67cd1 413 return ms_pLogger;
c801d85f
KB
414}
415
c085e333 416wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 417{
0fb67cd1
VZ
418 if ( ms_pLogger != NULL ) {
419 // flush the old messages before changing because otherwise they might
420 // get lost later if this target is not restored
421 ms_pLogger->Flush();
422 }
c801d85f 423
0fb67cd1
VZ
424 wxLog *pOldLogger = ms_pLogger;
425 ms_pLogger = pLogger;
c085e333 426
0fb67cd1 427 return pOldLogger;
c801d85f
KB
428}
429
36bd6902
VZ
430void wxLog::DontCreateOnDemand()
431{
f644b28c 432 ms_bAutoCreate = false;
36bd6902
VZ
433
434 // this is usually called at the end of the program and we assume that it
435 // is *always* called at the end - so we free memory here to avoid false
436 // memory leak reports from wxWin memory tracking code
437 ClearTraceMasks();
438}
439
0fb67cd1 440void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 441{
0fb67cd1
VZ
442 int index = ms_aTraceMasks.Index(str);
443 if ( index != wxNOT_FOUND )
dc6d5e38 444 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 445}
c801d85f 446
36bd6902
VZ
447void wxLog::ClearTraceMasks()
448{
449 ms_aTraceMasks.Clear();
450}
451
d2e1ef19
VZ
452void wxLog::TimeStamp(wxString *str)
453{
454 if ( ms_timestamp )
455 {
456 wxChar buf[256];
457 time_t timeNow;
458 (void)time(&timeNow);
c49245f8 459 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
d2e1ef19
VZ
460
461 str->Empty();
223d09f6 462 *str << buf << wxT(": ");
d2e1ef19
VZ
463 }
464}
465
50920146 466void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 467{
0fb67cd1
VZ
468 switch ( level ) {
469 case wxLOG_FatalError:
786855a1 470 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
471 DoLogString(_("Program aborted."), t);
472 Flush();
1c193821
JS
473#ifdef __WXWINCE__
474 ExitThread(3);
475#else
0fb67cd1 476 abort();
1c193821 477#endif
0fb67cd1
VZ
478 break;
479
480 case wxLOG_Error:
786855a1 481 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
482 break;
483
484 case wxLOG_Warning:
786855a1 485 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
486 break;
487
488 case wxLOG_Info:
0fb67cd1 489 if ( GetVerbose() )
37278984 490 case wxLOG_Message:
87a1e308 491 case wxLOG_Status:
786855a1
VZ
492 default: // log unknown log levels too
493 DoLogString(szString, t);
0fb67cd1
VZ
494 break;
495
496 case wxLOG_Trace:
497 case wxLOG_Debug:
498#ifdef __WXDEBUG__
0131687b
VZ
499 {
500 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 501 : wxT("Debug: ");
0131687b
VZ
502 msg << szString;
503 DoLogString(msg, t);
504 }
505#endif // Debug
0fb67cd1 506 break;
0fb67cd1 507 }
c801d85f
KB
508}
509
74e3313b 510void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 511{
223d09f6 512 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
513}
514
515void wxLog::Flush()
516{
1ec5cbf3 517 // nothing to do here
c801d85f
KB
518}
519
df5168c4
MB
520/*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask)
521{
522 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
523 en = ms_aTraceMasks.end();
524 it != en; ++it )
525 if ( *it == mask)
526 return true;
527 return false;
528}
529
c801d85f
KB
530// ----------------------------------------------------------------------------
531// wxLogStderr class implementation
532// ----------------------------------------------------------------------------
533
534wxLogStderr::wxLogStderr(FILE *fp)
535{
0fb67cd1
VZ
536 if ( fp == NULL )
537 m_fp = stderr;
538 else
539 m_fp = fp;
c801d85f
KB
540}
541
74e3313b 542void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 543{
d2e1ef19
VZ
544 wxString str;
545 TimeStamp(&str);
b568d04f 546 str << szString;
1e8a4bc2 547
50920146 548 fputs(str.mb_str(), m_fp);
b568d04f 549 fputc(_T('\n'), m_fp);
0fb67cd1 550 fflush(m_fp);
1e8a4bc2 551
e2478fde
VZ
552 // under GUI systems such as Windows or Mac, programs usually don't have
553 // stderr at all, so show the messages also somewhere else, typically in
554 // the debugger window so that they go at least somewhere instead of being
555 // simply lost
1ec5cbf3
VZ
556 if ( m_fp == stderr )
557 {
e2478fde
VZ
558 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
559 if ( traits && !traits->HasStderr() )
560 {
254a2129
WS
561 wxMessageOutputDebug dbgout;
562 dbgout.Printf(_T("%s"), str.c_str());
e2478fde 563 }
03147cd0 564 }
c801d85f
KB
565}
566
567// ----------------------------------------------------------------------------
568// wxLogStream implementation
569// ----------------------------------------------------------------------------
570
4bf78aae 571#if wxUSE_STD_IOSTREAM
65f19af1 572#include "wx/ioswrap.h"
dd107c50 573wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 574{
0fb67cd1 575 if ( ostr == NULL )
dd107c50 576 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
577 else
578 m_ostr = ostr;
c801d85f
KB
579}
580
74e3313b 581void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 582{
29fd317b
VZ
583 wxString str;
584 TimeStamp(&str);
dd107c50 585 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 586}
0fb67cd1 587#endif // wxUSE_STD_IOSTREAM
c801d85f 588
03147cd0
VZ
589// ----------------------------------------------------------------------------
590// wxLogChain
591// ----------------------------------------------------------------------------
592
593wxLogChain::wxLogChain(wxLog *logger)
594{
f644b28c 595 m_bPassMessages = true;
71debe95 596
03147cd0
VZ
597 m_logNew = logger;
598 m_logOld = wxLog::SetActiveTarget(this);
599}
600
199e91fb
VZ
601wxLogChain::~wxLogChain()
602{
e95f8fde
VZ
603 delete m_logOld;
604
605 if ( m_logNew != this )
606 delete m_logNew;
199e91fb
VZ
607}
608
03147cd0
VZ
609void wxLogChain::SetLog(wxLog *logger)
610{
611 if ( m_logNew != this )
612 delete m_logNew;
613
03147cd0
VZ
614 m_logNew = logger;
615}
616
617void wxLogChain::Flush()
618{
619 if ( m_logOld )
620 m_logOld->Flush();
621
1ec5cbf3 622 // be careful to avoid infinite recursion
03147cd0
VZ
623 if ( m_logNew && m_logNew != this )
624 m_logNew->Flush();
625}
626
627void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
628{
629 // let the previous logger show it
630 if ( m_logOld && IsPassingMessages() )
631 {
632 // bogus cast just to access protected DoLog
633 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
634 }
635
636 if ( m_logNew && m_logNew != this )
637 {
638 // as above...
639 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
640 }
641}
642
93d4c1d0
VZ
643// ----------------------------------------------------------------------------
644// wxLogPassThrough
645// ----------------------------------------------------------------------------
646
647#ifdef __VISUALC__
648 // "'this' : used in base member initializer list" - so what?
649 #pragma warning(disable:4355)
650#endif // VC++
651
652wxLogPassThrough::wxLogPassThrough()
653 : wxLogChain(this)
654{
655}
656
657#ifdef __VISUALC__
658 #pragma warning(default:4355)
659#endif // VC++
660
c801d85f
KB
661// ============================================================================
662// Global functions/variables
663// ============================================================================
664
665// ----------------------------------------------------------------------------
666// static variables
667// ----------------------------------------------------------------------------
0fb67cd1
VZ
668
669wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
f644b28c
WS
670bool wxLog::ms_doLog = true;
671bool wxLog::ms_bAutoCreate = true;
672bool wxLog::ms_bVerbose = false;
d2e1ef19 673
edc73852
RD
674wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
675
2ed3265e
VZ
676size_t wxLog::ms_suspendCount = 0;
677
e2478fde 678const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
d2e1ef19 679
0fb67cd1
VZ
680wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
681wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
682
683// ----------------------------------------------------------------------------
684// stdout error logging helper
685// ----------------------------------------------------------------------------
686
687// helper function: wraps the message and justifies it under given position
688// (looks more pretty on the terminal). Also adds newline at the end.
689//
0fb67cd1
VZ
690// TODO this is now disabled until I find a portable way of determining the
691// terminal window size (ok, I found it but does anybody really cares?)
692#ifdef LOG_PRETTY_WRAP
c801d85f
KB
693static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
694{
0fb67cd1
VZ
695 size_t nMax = 80; // FIXME
696 size_t nStart = strlen(pszPrefix);
697 fputs(pszPrefix, f);
698
699 size_t n;
700 while ( *psz != '\0' ) {
701 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
702 putc(*psz++, f);
703
704 // wrapped?
705 if ( *psz != '\0' ) {
706 /*putc('\n', f);*/
707 for ( n = 0; n < nStart; n++ )
708 putc(' ', f);
709
710 // as we wrapped, squeeze all white space
711 while ( isspace(*psz) )
712 psz++;
713 }
c801d85f 714 }
c801d85f 715
0fb67cd1 716 putc('\n', f);
c801d85f
KB
717}
718#endif //LOG_PRETTY_WRAP
719
720// ----------------------------------------------------------------------------
721// error code/error message retrieval functions
722// ----------------------------------------------------------------------------
723
724// get error code from syste
725unsigned long wxSysErrorCode()
726{
8cb172b4 727#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 728 return ::GetLastError();
0fb67cd1 729#else //Unix
c801d85f 730 return errno;
0fb67cd1 731#endif //Win/Unix
c801d85f
KB
732}
733
734// get error message from system
50920146 735const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 736{
0fb67cd1
VZ
737 if ( nErrCode == 0 )
738 nErrCode = wxSysErrorCode();
739
8cb172b4 740#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
50920146 741 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
742
743 // get error message from system
744 LPVOID lpMsgBuf;
745 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
746 NULL, nErrCode,
747 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
748 (LPTSTR)&lpMsgBuf,
749 0, NULL);
750
751 // copy it to our buffer and free memory
a9928e9d 752 // Crashes on SmartPhone
0c44ec97 753#if !defined(__SMARTPHONE__) /* of WinCE */
a9928e9d 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 }
a9928e9d
JS
769 else
770#endif
771 {
8c5b1f0f 772 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
773 }
774
775 return s_szBuf;
3a5bcc4d 776#else // Unix-WXMICROWIN
50920146
OK
777#if wxUSE_UNICODE
778 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
dcf924a3 779 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
50920146
OK
780 return s_szBuf;
781#else
13111b2a 782 return strerror((int)nErrCode);
50920146 783#endif
3a5bcc4d 784#endif // Win/Unix-WXMICROWIN
c801d85f
KB
785}
786
e2478fde 787#endif // wxUSE_LOG
04662def 788