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