]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
Some things needed in the base class of OS/2
[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>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
dd85fc6b 19
c801d85f
KB
20#ifdef __GNUG__
21 #pragma implementation "log.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31// wxWindows
32#ifndef WX_PRECOMP
e90c1d2a
VZ
33 #include "wx/string.h"
34 #include "wx/intl.h"
35 #include "wx/app.h"
36
37 #if wxUSE_GUI
38 #include "wx/window.h"
39 #ifdef __WXMSW__
40 #include "wx/msw/private.h"
41 #endif
e90c1d2a 42 #endif
9ef3052c 43#endif //WX_PRECOMP
c801d85f 44
dbf3cd7a
RR
45#include "wx/file.h"
46#include "wx/textfile.h"
47#include "wx/utils.h"
a324a7bc 48#include "wx/wxchar.h"
dbf3cd7a 49#include "wx/log.h"
b568d04f 50#include "wx/thread.h"
c801d85f 51
f94dfb38
VS
52#if wxUSE_LOG
53
c801d85f
KB
54// other standard headers
55#include <errno.h>
56#include <stdlib.h>
57#include <time.h>
58
8cb172b4 59#if defined(__WXMSW__)
b568d04f 60 #include "wx/msw/private.h" // includes windows.h for OutputDebugString
8cb172b4
JS
61#endif
62
76a5e5d2
SC
63#if defined(__WXMAC__)
64 #include "wx/mac/private.h" // includes mac headers
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
50920146 91static wxChar s_szBuf[LOG_BUFFER_SIZE];
c801d85f 92
b568d04f
VZ
93#if wxUSE_THREADS
94
95// the critical section protecting the static buffer
96static wxCriticalSection gs_csLogBuf;
97
98#endif // wxUSE_THREADS
99
807a903e
VZ
100// return true if we have a non NULL non disabled log target
101static inline bool IsLoggingEnabled()
102{
103 return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL);
104}
105
b568d04f
VZ
106// ----------------------------------------------------------------------------
107// implementation of Log functions
108//
109// NB: unfortunately we need all these distinct functions, we can't make them
110// macros and not all compilers inline vararg functions.
111// ----------------------------------------------------------------------------
112
c801d85f 113// generic log function
50920146 114void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
c801d85f 115{
807a903e
VZ
116 if ( IsLoggingEnabled() ) {
117 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
9ef3052c 118
807a903e
VZ
119 va_list argptr;
120 va_start(argptr, szFormat);
121 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
122 va_end(argptr);
123
124 wxLog::OnLog(level, s_szBuf, time(NULL));
125 }
c801d85f
KB
126}
127
807a903e
VZ
128#define IMPLEMENT_LOG_FUNCTION(level) \
129 void wxLog##level(const wxChar *szFormat, ...) \
130 { \
131 if ( IsLoggingEnabled() ) { \
132 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
133 \
134 va_list argptr; \
135 va_start(argptr, szFormat); \
136 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
137 va_end(argptr); \
138 \
139 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
140 } \
c801d85f
KB
141 }
142
143IMPLEMENT_LOG_FUNCTION(FatalError)
144IMPLEMENT_LOG_FUNCTION(Error)
145IMPLEMENT_LOG_FUNCTION(Warning)
146IMPLEMENT_LOG_FUNCTION(Message)
147IMPLEMENT_LOG_FUNCTION(Info)
148IMPLEMENT_LOG_FUNCTION(Status)
149
9ef3052c 150// same as info, but only if 'verbose' mode is on
50920146 151void wxLogVerbose(const wxChar *szFormat, ...)
9ef3052c 152{
807a903e
VZ
153 if ( IsLoggingEnabled() ) {
154 wxLog *pLog = wxLog::GetActiveTarget();
155 if ( pLog != NULL && pLog->GetVerbose() ) {
156 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
157
158 va_list argptr;
159 va_start(argptr, szFormat);
160 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
161 va_end(argptr);
162
163 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
164 }
165 }
9ef3052c
VZ
166}
167
168// debug functions
b2aef89b 169#ifdef __WXDEBUG__
807a903e
VZ
170#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
171 void wxLog##level(const wxChar *szFormat, ...) \
172 { \
173 if ( IsLoggingEnabled() ) { \
174 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
175 \
176 va_list argptr; \
177 va_start(argptr, szFormat); \
178 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
179 va_end(argptr); \
180 \
181 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
182 } \
c801d85f
KB
183 }
184
50920146 185 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
0fb67cd1 186 {
807a903e 187 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
b568d04f
VZ
188 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
189
00c4e897
VZ
190 wxChar *p = s_szBuf;
191 size_t len = WXSIZEOF(s_szBuf);
f6bcfd97 192 wxStrncpy(s_szBuf, _T("("), len);
829f0541
VZ
193 len -= 1; // strlen("(")
194 p += 1;
f6bcfd97 195 wxStrncat(p, mask, len);
00c4e897
VZ
196 size_t lenMask = wxStrlen(mask);
197 len -= lenMask;
198 p += lenMask;
199
f6bcfd97 200 wxStrncat(p, _T(") "), len);
829f0541
VZ
201 len -= 2;
202 p += 2;
00c4e897 203
0fb67cd1
VZ
204 va_list argptr;
205 va_start(argptr, szFormat);
00c4e897 206 wxVsnprintf(p, len, szFormat, argptr);
0fb67cd1
VZ
207 va_end(argptr);
208
209 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
210 }
211 }
212
50920146 213 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
9ef3052c 214 {
9ef3052c
VZ
215 // we check that all of mask bits are set in the current mask, so
216 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
217 // if both bits are set.
807a903e 218 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
b568d04f
VZ
219 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
220
9ef3052c
VZ
221 va_list argptr;
222 va_start(argptr, szFormat);
378b05f7 223 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
9ef3052c
VZ
224 va_end(argptr);
225
0fb67cd1 226 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
9ef3052c
VZ
227 }
228 }
229
230#else // release
231 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
232#endif
233
234IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
235IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
236
237// wxLogSysError: one uses the last error code, for other you must give it
238// explicitly
239
240// common part of both wxLogSysError
241void wxLogSysErrorHelper(long lErrCode)
c801d85f 242{
50920146 243 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
378b05f7
VZ
244 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
245 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
50920146 246 wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf));
c801d85f 247
0fb67cd1 248 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
9ef3052c 249}
c801d85f 250
50920146 251void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
9ef3052c 252{
807a903e
VZ
253 if ( IsLoggingEnabled() ) {
254 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 255
807a903e
VZ
256 va_list argptr;
257 va_start(argptr, szFormat);
258 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
259 va_end(argptr);
9ef3052c 260
807a903e
VZ
261 wxLogSysErrorHelper(wxSysErrorCode());
262 }
c801d85f
KB
263}
264
50920146 265void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
c801d85f 266{
807a903e
VZ
267 if ( IsLoggingEnabled() ) {
268 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 269
807a903e
VZ
270 va_list argptr;
271 va_start(argptr, szFormat);
272 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
273 va_end(argptr);
c801d85f 274
807a903e
VZ
275 wxLogSysErrorHelper(lErrCode);
276 }
c801d85f
KB
277}
278
279// ----------------------------------------------------------------------------
280// wxLog class implementation
281// ----------------------------------------------------------------------------
282
283wxLog::wxLog()
284{
0fb67cd1 285 m_bHasMessages = FALSE;
c801d85f
KB
286}
287
9ec05cc9
VZ
288wxLog *wxLog::GetActiveTarget()
289{
0fb67cd1
VZ
290 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
291 // prevent infinite recursion if someone calls wxLogXXX() from
292 // wxApp::CreateLogTarget()
293 static bool s_bInGetActiveTarget = FALSE;
294 if ( !s_bInGetActiveTarget ) {
295 s_bInGetActiveTarget = TRUE;
296
0fb67cd1
VZ
297 // ask the application to create a log target for us
298 if ( wxTheApp != NULL )
299 ms_pLogger = wxTheApp->CreateLogTarget();
300 else
301 ms_pLogger = new wxLogStderr;
0fb67cd1
VZ
302
303 s_bInGetActiveTarget = FALSE;
304
305 // do nothing if it fails - what can we do?
306 }
275bf4c1 307 }
c801d85f 308
0fb67cd1 309 return ms_pLogger;
c801d85f
KB
310}
311
c085e333 312wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 313{
0fb67cd1
VZ
314 if ( ms_pLogger != NULL ) {
315 // flush the old messages before changing because otherwise they might
316 // get lost later if this target is not restored
317 ms_pLogger->Flush();
318 }
c801d85f 319
0fb67cd1
VZ
320 wxLog *pOldLogger = ms_pLogger;
321 ms_pLogger = pLogger;
c085e333 322
0fb67cd1 323 return pOldLogger;
c801d85f
KB
324}
325
36bd6902
VZ
326void wxLog::DontCreateOnDemand()
327{
328 ms_bAutoCreate = FALSE;
329
330 // this is usually called at the end of the program and we assume that it
331 // is *always* called at the end - so we free memory here to avoid false
332 // memory leak reports from wxWin memory tracking code
333 ClearTraceMasks();
334}
335
0fb67cd1 336void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 337{
0fb67cd1
VZ
338 int index = ms_aTraceMasks.Index(str);
339 if ( index != wxNOT_FOUND )
340 ms_aTraceMasks.Remove((size_t)index);
341}
c801d85f 342
36bd6902
VZ
343void wxLog::ClearTraceMasks()
344{
345 ms_aTraceMasks.Clear();
346}
347
d2e1ef19
VZ
348void wxLog::TimeStamp(wxString *str)
349{
350 if ( ms_timestamp )
351 {
352 wxChar buf[256];
353 time_t timeNow;
354 (void)time(&timeNow);
c49245f8 355 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
d2e1ef19
VZ
356
357 str->Empty();
223d09f6 358 *str << buf << wxT(": ");
d2e1ef19
VZ
359 }
360}
361
50920146 362void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 363{
0fb67cd1
VZ
364 switch ( level ) {
365 case wxLOG_FatalError:
786855a1 366 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
367 DoLogString(_("Program aborted."), t);
368 Flush();
369 abort();
370 break;
371
372 case wxLOG_Error:
786855a1 373 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
374 break;
375
376 case wxLOG_Warning:
786855a1 377 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
378 break;
379
380 case wxLOG_Info:
0fb67cd1 381 if ( GetVerbose() )
37278984 382 case wxLOG_Message:
87a1e308 383 case wxLOG_Status:
786855a1
VZ
384 default: // log unknown log levels too
385 DoLogString(szString, t);
0fb67cd1
VZ
386 break;
387
388 case wxLOG_Trace:
389 case wxLOG_Debug:
390#ifdef __WXDEBUG__
0131687b
VZ
391 {
392 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 393 : wxT("Debug: ");
0131687b
VZ
394 msg << szString;
395 DoLogString(msg, t);
396 }
397#endif // Debug
0fb67cd1 398 break;
0fb67cd1 399 }
c801d85f
KB
400}
401
74e3313b 402void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 403{
223d09f6 404 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
405}
406
407void wxLog::Flush()
408{
03147cd0
VZ
409 // remember that we don't have any more messages to show
410 m_bHasMessages = FALSE;
c801d85f
KB
411}
412
413// ----------------------------------------------------------------------------
414// wxLogStderr class implementation
415// ----------------------------------------------------------------------------
416
417wxLogStderr::wxLogStderr(FILE *fp)
418{
0fb67cd1
VZ
419 if ( fp == NULL )
420 m_fp = stderr;
421 else
422 m_fp = fp;
c801d85f
KB
423}
424
0cbb9297 425#if defined(__WXMAC__) && !defined(__DARWIN__)
7d610b90 426
0adad2d7
SC
427#ifndef __MetroNubUtils__
428#include "MetroNubUtils.h"
429#endif
430
431#ifdef __cplusplus
432 extern "C" {
433#endif
434
435#ifndef __GESTALT__
436#include <Gestalt.h>
437#endif
438
439#ifndef true
440#define true 1
441#endif
442
443#ifndef false
444#define false 0
445#endif
446
447#if TARGET_API_MAC_CARBON
448
449 #include <CodeFragments.h>
450
451 EXTERN_API_C( long )
452 CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
453
454 ProcPtr gCallUniversalProc_Proc = NULL;
455
456#endif
457
458static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
459
460static long fRunOnce = false;
461
462Boolean IsCompatibleVersion(short inVersion);
463
464/* ---------------------------------------------------------------------------
465 IsCompatibleVersion
466 --------------------------------------------------------------------------- */
467
468Boolean IsCompatibleVersion(short inVersion)
03147cd0 469{
0adad2d7
SC
470 Boolean result = false;
471
472 if (fRunOnce)
473 {
474 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
475
476 result = (inVersion <= block->apiHiVersion);
477 }
478
479 return result;
480}
03147cd0 481
0adad2d7
SC
482/* ---------------------------------------------------------------------------
483 IsMetroNubInstalled
484 --------------------------------------------------------------------------- */
03147cd0 485
0adad2d7
SC
486Boolean IsMetroNubInstalled()
487{
488 if (!fRunOnce)
489 {
490 long result, value;
491
492 fRunOnce = true;
493 gMetroNubEntry = NULL;
494
495 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
496 {
497 /* look for MetroNub's Gestalt selector */
498 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
499 {
500
501 #if TARGET_API_MAC_CARBON
502 if (gCallUniversalProc_Proc == NULL)
503 {
504 CFragConnectionID connectionID;
505 Ptr mainAddress;
506 Str255 errorString;
507 ProcPtr symbolAddress;
508 OSErr err;
509 CFragSymbolClass symbolClass;
510
511 symbolAddress = NULL;
512 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
513 &connectionID, &mainAddress, errorString);
514
515 if (err != noErr)
516 {
517 gCallUniversalProc_Proc = NULL;
518 goto end;
519 }
520
521 err = FindSymbol(connectionID, "\pCallUniversalProc",
522 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
523
524 if (err != noErr)
525 {
526 gCallUniversalProc_Proc = NULL;
527 goto end;
528 }
529 }
530 #endif
531
532 {
533 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
534
535 /* make sure the version of the API is compatible */
536 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
537 kMetroNubUserAPIVersion <= block->apiHiVersion)
538 gMetroNubEntry = block; /* success! */
539 }
540
541 }
542 }
543 }
544
545end:
546
547#if TARGET_API_MAC_CARBON
548 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
549#else
550 return (gMetroNubEntry != NULL);
551#endif
552}
03147cd0 553
0adad2d7
SC
554/* ---------------------------------------------------------------------------
555 IsMWDebuggerRunning [v1 API]
556 --------------------------------------------------------------------------- */
557
558Boolean IsMWDebuggerRunning()
559{
560 if (IsMetroNubInstalled())
561 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
562 else
563 return false;
564}
565
566/* ---------------------------------------------------------------------------
567 AmIBeingMWDebugged [v1 API]
568 --------------------------------------------------------------------------- */
569
570Boolean AmIBeingMWDebugged()
571{
572 if (IsMetroNubInstalled())
573 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
574 else
575 return false;
7d610b90
SC
576}
577
0adad2d7
SC
578/* ---------------------------------------------------------------------------
579 UserSetWatchPoint [v2 API]
580 --------------------------------------------------------------------------- */
581
582OSErr UserSetWatchPoint (Ptr address, long length, WatchPointIDT* watchPointID)
7d610b90 583{
0adad2d7
SC
584 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
585 return CallUserSetWatchPointProc(gMetroNubEntry->userSetWatchPoint,
586 address, length, watchPointID);
587 else
588 return errProcessIsNotClient;
7d610b90
SC
589}
590
0adad2d7
SC
591/* ---------------------------------------------------------------------------
592 ClearWatchPoint [v2 API]
593 --------------------------------------------------------------------------- */
594
595OSErr ClearWatchPoint (WatchPointIDT watchPointID)
7d610b90 596{
0adad2d7
SC
597 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
598 return CallClearWatchPointProc(gMetroNubEntry->clearWatchPoint,
599 watchPointID);
600 else
601 return errProcessIsNotClient;
7d610b90 602}
0adad2d7
SC
603
604#ifdef __cplusplus
605 }
606#endif
607
7d610b90
SC
608#endif
609
74e3313b 610void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 611{
d2e1ef19
VZ
612 wxString str;
613 TimeStamp(&str);
b568d04f 614 str << szString;
1e8a4bc2 615
50920146 616 fputs(str.mb_str(), m_fp);
b568d04f 617 fputc(_T('\n'), m_fp);
0fb67cd1 618 fflush(m_fp);
1e8a4bc2 619
378b05f7 620 // under Windows, programs usually don't have stderr at all, so show the
b568d04f 621 // messages also under debugger - unless it's a console program
8cb172b4 622#if defined(__WXMSW__) && wxUSE_GUI && !defined(__WXMICROWIN__)
f6bcfd97
BP
623 str += wxT("\r\n") ;
624 OutputDebugString(str.c_str());
1e8a4bc2 625#endif // MSW
0cbb9297 626#if defined(__WXMAC__) && !defined(__DARWIN__) && wxUSE_GUI
03147cd0
VZ
627 Str255 pstr ;
628 strcpy( (char*) pstr , str.c_str() ) ;
629 strcat( (char*) pstr , ";g" ) ;
630 c2pstr( (char*) pstr ) ;
0adad2d7 631
03147cd0
VZ
632 Boolean running = false ;
633
0adad2d7 634 if ( IsMWDebuggerRunning() && AmIBeingMWDebugged() )
03147cd0 635 {
0adad2d7 636 running = true ;
03147cd0 637 }
0adad2d7 638
03147cd0
VZ
639 if (running)
640 {
641#ifdef __powerc
642 DebugStr(pstr);
643#else
644 SysBreakStr(pstr);
7d610b90 645#endif
03147cd0 646 }
03e11df5 647#endif // Mac
c801d85f
KB
648}
649
650// ----------------------------------------------------------------------------
651// wxLogStream implementation
652// ----------------------------------------------------------------------------
653
4bf78aae 654#if wxUSE_STD_IOSTREAM
dd107c50 655wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 656{
0fb67cd1 657 if ( ostr == NULL )
dd107c50 658 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
659 else
660 m_ostr = ostr;
c801d85f
KB
661}
662
74e3313b 663void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 664{
29fd317b
VZ
665 wxString str;
666 TimeStamp(&str);
dd107c50 667 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 668}
0fb67cd1 669#endif // wxUSE_STD_IOSTREAM
c801d85f 670
03147cd0
VZ
671// ----------------------------------------------------------------------------
672// wxLogChain
673// ----------------------------------------------------------------------------
674
675wxLogChain::wxLogChain(wxLog *logger)
676{
677 m_logNew = logger;
678 m_logOld = wxLog::SetActiveTarget(this);
679}
680
681void wxLogChain::SetLog(wxLog *logger)
682{
683 if ( m_logNew != this )
684 delete m_logNew;
685
686 wxLog::SetActiveTarget(logger);
687
688 m_logNew = logger;
689}
690
691void wxLogChain::Flush()
692{
693 if ( m_logOld )
694 m_logOld->Flush();
695
696 // be careful to avoid inifinite recursion
697 if ( m_logNew && m_logNew != this )
698 m_logNew->Flush();
699}
700
701void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
702{
703 // let the previous logger show it
704 if ( m_logOld && IsPassingMessages() )
705 {
706 // bogus cast just to access protected DoLog
707 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
708 }
709
710 if ( m_logNew && m_logNew != this )
711 {
712 // as above...
713 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
714 }
715}
716
93d4c1d0
VZ
717// ----------------------------------------------------------------------------
718// wxLogPassThrough
719// ----------------------------------------------------------------------------
720
721#ifdef __VISUALC__
722 // "'this' : used in base member initializer list" - so what?
723 #pragma warning(disable:4355)
724#endif // VC++
725
726wxLogPassThrough::wxLogPassThrough()
727 : wxLogChain(this)
728{
729}
730
731#ifdef __VISUALC__
732 #pragma warning(default:4355)
733#endif // VC++
734
c801d85f
KB
735// ============================================================================
736// Global functions/variables
737// ============================================================================
738
739// ----------------------------------------------------------------------------
740// static variables
741// ----------------------------------------------------------------------------
0fb67cd1
VZ
742
743wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
744bool wxLog::ms_doLog = TRUE;
745bool wxLog::ms_bAutoCreate = TRUE;
fd7718b2 746bool wxLog::ms_bVerbose = FALSE;
d2e1ef19 747
2ed3265e
VZ
748size_t wxLog::ms_suspendCount = 0;
749
9f83044f
VZ
750#if wxUSE_GUI
751 const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
752#else
753 const wxChar *wxLog::ms_timestamp = NULL; // save space
754#endif
d2e1ef19 755
0fb67cd1
VZ
756wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
757wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
758
759// ----------------------------------------------------------------------------
760// stdout error logging helper
761// ----------------------------------------------------------------------------
762
763// helper function: wraps the message and justifies it under given position
764// (looks more pretty on the terminal). Also adds newline at the end.
765//
0fb67cd1
VZ
766// TODO this is now disabled until I find a portable way of determining the
767// terminal window size (ok, I found it but does anybody really cares?)
768#ifdef LOG_PRETTY_WRAP
c801d85f
KB
769static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
770{
0fb67cd1
VZ
771 size_t nMax = 80; // FIXME
772 size_t nStart = strlen(pszPrefix);
773 fputs(pszPrefix, f);
774
775 size_t n;
776 while ( *psz != '\0' ) {
777 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
778 putc(*psz++, f);
779
780 // wrapped?
781 if ( *psz != '\0' ) {
782 /*putc('\n', f);*/
783 for ( n = 0; n < nStart; n++ )
784 putc(' ', f);
785
786 // as we wrapped, squeeze all white space
787 while ( isspace(*psz) )
788 psz++;
789 }
c801d85f 790 }
c801d85f 791
0fb67cd1 792 putc('\n', f);
c801d85f
KB
793}
794#endif //LOG_PRETTY_WRAP
795
796// ----------------------------------------------------------------------------
797// error code/error message retrieval functions
798// ----------------------------------------------------------------------------
799
800// get error code from syste
801unsigned long wxSysErrorCode()
802{
8cb172b4 803#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1
VZ
804#ifdef __WIN32__
805 return ::GetLastError();
806#else //WIN16
807 // TODO what to do on Windows 3.1?
808 return 0;
809#endif //WIN16/32
810#else //Unix
c801d85f 811 return errno;
0fb67cd1 812#endif //Win/Unix
c801d85f
KB
813}
814
815// get error message from system
50920146 816const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 817{
0fb67cd1
VZ
818 if ( nErrCode == 0 )
819 nErrCode = wxSysErrorCode();
820
8cb172b4 821#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 822#ifdef __WIN32__
50920146 823 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
824
825 // get error message from system
826 LPVOID lpMsgBuf;
827 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
828 NULL, nErrCode,
829 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
830 (LPTSTR)&lpMsgBuf,
831 0, NULL);
832
833 // copy it to our buffer and free memory
251244a0 834 if( lpMsgBuf != 0 ) {
8c5b1f0f 835 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
836 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
837
838 LocalFree(lpMsgBuf);
839
840 // returned string is capitalized and ended with '\r\n' - bad
841 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
842 size_t len = wxStrlen(s_szBuf);
843 if ( len > 0 ) {
844 // truncate string
845 if ( s_szBuf[len - 2] == wxT('\r') )
846 s_szBuf[len - 2] = wxT('\0');
847 }
848 }
849 else {
8c5b1f0f 850 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
851 }
852
853 return s_szBuf;
854#else //Win16
855 // TODO
856 return NULL;
857#endif // Win16/32
858#else // Unix
50920146
OK
859#if wxUSE_UNICODE
860 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
dcf924a3 861 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
50920146
OK
862 return s_szBuf;
863#else
13111b2a 864 return strerror((int)nErrCode);
50920146 865#endif
0fb67cd1 866#endif // Win/Unix
c801d85f
KB
867}
868
f94dfb38 869#endif //wxUSE_LOG