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