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