]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
corrected DoReadLong
[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
5c922642 425#if defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
7d610b90 426
5c922642 427#if !TARGET_API_MAC_CARBON
925ee99f
GD
428// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
429
0adad2d7
SC
430#ifndef __MetroNubUtils__
431#include "MetroNubUtils.h"
432#endif
433
434#ifdef __cplusplus
435 extern "C" {
436#endif
437
438#ifndef __GESTALT__
439#include <Gestalt.h>
440#endif
441
442#ifndef true
443#define true 1
444#endif
445
446#ifndef false
447#define false 0
448#endif
449
450#if TARGET_API_MAC_CARBON
451
452 #include <CodeFragments.h>
453
454 EXTERN_API_C( long )
455 CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
456
457 ProcPtr gCallUniversalProc_Proc = NULL;
458
459#endif
460
461static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
462
463static long fRunOnce = false;
464
465Boolean IsCompatibleVersion(short inVersion);
466
467/* ---------------------------------------------------------------------------
468 IsCompatibleVersion
469 --------------------------------------------------------------------------- */
470
471Boolean IsCompatibleVersion(short inVersion)
03147cd0 472{
0adad2d7
SC
473 Boolean result = false;
474
475 if (fRunOnce)
476 {
477 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
478
479 result = (inVersion <= block->apiHiVersion);
480 }
481
482 return result;
483}
03147cd0 484
0adad2d7
SC
485/* ---------------------------------------------------------------------------
486 IsMetroNubInstalled
487 --------------------------------------------------------------------------- */
03147cd0 488
0adad2d7
SC
489Boolean IsMetroNubInstalled()
490{
491 if (!fRunOnce)
492 {
493 long result, value;
494
495 fRunOnce = true;
496 gMetroNubEntry = NULL;
497
498 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
499 {
500 /* look for MetroNub's Gestalt selector */
501 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
502 {
503
504 #if TARGET_API_MAC_CARBON
505 if (gCallUniversalProc_Proc == NULL)
506 {
507 CFragConnectionID connectionID;
508 Ptr mainAddress;
509 Str255 errorString;
510 ProcPtr symbolAddress;
511 OSErr err;
512 CFragSymbolClass symbolClass;
513
514 symbolAddress = NULL;
515 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
516 &connectionID, &mainAddress, errorString);
517
518 if (err != noErr)
519 {
520 gCallUniversalProc_Proc = NULL;
521 goto end;
522 }
523
524 err = FindSymbol(connectionID, "\pCallUniversalProc",
525 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
526
527 if (err != noErr)
528 {
529 gCallUniversalProc_Proc = NULL;
530 goto end;
531 }
532 }
533 #endif
534
535 {
536 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
537
538 /* make sure the version of the API is compatible */
539 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
540 kMetroNubUserAPIVersion <= block->apiHiVersion)
541 gMetroNubEntry = block; /* success! */
542 }
543
544 }
545 }
546 }
547
548end:
549
550#if TARGET_API_MAC_CARBON
551 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
552#else
553 return (gMetroNubEntry != NULL);
554#endif
555}
03147cd0 556
0adad2d7
SC
557/* ---------------------------------------------------------------------------
558 IsMWDebuggerRunning [v1 API]
559 --------------------------------------------------------------------------- */
560
561Boolean IsMWDebuggerRunning()
562{
563 if (IsMetroNubInstalled())
564 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
565 else
566 return false;
567}
568
569/* ---------------------------------------------------------------------------
570 AmIBeingMWDebugged [v1 API]
571 --------------------------------------------------------------------------- */
572
573Boolean AmIBeingMWDebugged()
574{
575 if (IsMetroNubInstalled())
576 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
577 else
578 return false;
7d610b90
SC
579}
580
0adad2d7
SC
581/* ---------------------------------------------------------------------------
582 UserSetWatchPoint [v2 API]
583 --------------------------------------------------------------------------- */
584
585OSErr UserSetWatchPoint (Ptr address, long length, WatchPointIDT* watchPointID)
7d610b90 586{
0adad2d7
SC
587 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
588 return CallUserSetWatchPointProc(gMetroNubEntry->userSetWatchPoint,
589 address, length, watchPointID);
590 else
591 return errProcessIsNotClient;
7d610b90
SC
592}
593
0adad2d7
SC
594/* ---------------------------------------------------------------------------
595 ClearWatchPoint [v2 API]
596 --------------------------------------------------------------------------- */
597
598OSErr ClearWatchPoint (WatchPointIDT watchPointID)
7d610b90 599{
0adad2d7
SC
600 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
601 return CallClearWatchPointProc(gMetroNubEntry->clearWatchPoint,
602 watchPointID);
603 else
604 return errProcessIsNotClient;
7d610b90 605}
0adad2d7
SC
606
607#ifdef __cplusplus
608 }
609#endif
610
5c922642 611#endif // !TARGET_API_MAC_CARBON
925ee99f 612
5c922642 613#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
7d610b90 614
74e3313b 615void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 616{
d2e1ef19
VZ
617 wxString str;
618 TimeStamp(&str);
b568d04f 619 str << szString;
1e8a4bc2 620
50920146 621 fputs(str.mb_str(), m_fp);
b568d04f 622 fputc(_T('\n'), m_fp);
0fb67cd1 623 fflush(m_fp);
1e8a4bc2 624
378b05f7 625 // under Windows, programs usually don't have stderr at all, so show the
b568d04f 626 // messages also under debugger - unless it's a console program
8cb172b4 627#if defined(__WXMSW__) && wxUSE_GUI && !defined(__WXMICROWIN__)
f6bcfd97
BP
628 str += wxT("\r\n") ;
629 OutputDebugString(str.c_str());
1e8a4bc2 630#endif // MSW
0cbb9297 631#if defined(__WXMAC__) && !defined(__DARWIN__) && wxUSE_GUI
03147cd0
VZ
632 Str255 pstr ;
633 strcpy( (char*) pstr , str.c_str() ) ;
634 strcat( (char*) pstr , ";g" ) ;
635 c2pstr( (char*) pstr ) ;
0adad2d7 636
03147cd0
VZ
637 Boolean running = false ;
638
5c922642 639#if !TARGET_API_MAC_CARBON && (__MWERKS__ > 0x5300)
925ee99f 640
0adad2d7 641 if ( IsMWDebuggerRunning() && AmIBeingMWDebugged() )
03147cd0 642 {
0adad2d7 643 running = true ;
03147cd0 644 }
0adad2d7 645
925ee99f
GD
646#endif
647
03147cd0
VZ
648 if (running)
649 {
650#ifdef __powerc
651 DebugStr(pstr);
652#else
653 SysBreakStr(pstr);
7d610b90 654#endif
03147cd0 655 }
03e11df5 656#endif // Mac
c801d85f
KB
657}
658
659// ----------------------------------------------------------------------------
660// wxLogStream implementation
661// ----------------------------------------------------------------------------
662
4bf78aae 663#if wxUSE_STD_IOSTREAM
dd107c50 664wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 665{
0fb67cd1 666 if ( ostr == NULL )
dd107c50 667 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
668 else
669 m_ostr = ostr;
c801d85f
KB
670}
671
74e3313b 672void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 673{
29fd317b
VZ
674 wxString str;
675 TimeStamp(&str);
dd107c50 676 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 677}
0fb67cd1 678#endif // wxUSE_STD_IOSTREAM
c801d85f 679
03147cd0
VZ
680// ----------------------------------------------------------------------------
681// wxLogChain
682// ----------------------------------------------------------------------------
683
684wxLogChain::wxLogChain(wxLog *logger)
685{
686 m_logNew = logger;
687 m_logOld = wxLog::SetActiveTarget(this);
688}
689
690void wxLogChain::SetLog(wxLog *logger)
691{
692 if ( m_logNew != this )
693 delete m_logNew;
694
695 wxLog::SetActiveTarget(logger);
696
697 m_logNew = logger;
698}
699
700void wxLogChain::Flush()
701{
702 if ( m_logOld )
703 m_logOld->Flush();
704
705 // be careful to avoid inifinite recursion
706 if ( m_logNew && m_logNew != this )
707 m_logNew->Flush();
708}
709
710void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
711{
712 // let the previous logger show it
713 if ( m_logOld && IsPassingMessages() )
714 {
715 // bogus cast just to access protected DoLog
716 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
717 }
718
719 if ( m_logNew && m_logNew != this )
720 {
721 // as above...
722 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
723 }
724}
725
93d4c1d0
VZ
726// ----------------------------------------------------------------------------
727// wxLogPassThrough
728// ----------------------------------------------------------------------------
729
730#ifdef __VISUALC__
731 // "'this' : used in base member initializer list" - so what?
732 #pragma warning(disable:4355)
733#endif // VC++
734
735wxLogPassThrough::wxLogPassThrough()
736 : wxLogChain(this)
737{
738}
739
740#ifdef __VISUALC__
741 #pragma warning(default:4355)
742#endif // VC++
743
c801d85f
KB
744// ============================================================================
745// Global functions/variables
746// ============================================================================
747
748// ----------------------------------------------------------------------------
749// static variables
750// ----------------------------------------------------------------------------
0fb67cd1
VZ
751
752wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
753bool wxLog::ms_doLog = TRUE;
754bool wxLog::ms_bAutoCreate = TRUE;
fd7718b2 755bool wxLog::ms_bVerbose = FALSE;
d2e1ef19 756
2ed3265e
VZ
757size_t wxLog::ms_suspendCount = 0;
758
9f83044f
VZ
759#if wxUSE_GUI
760 const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
761#else
762 const wxChar *wxLog::ms_timestamp = NULL; // save space
763#endif
d2e1ef19 764
0fb67cd1
VZ
765wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
766wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
767
768// ----------------------------------------------------------------------------
769// stdout error logging helper
770// ----------------------------------------------------------------------------
771
772// helper function: wraps the message and justifies it under given position
773// (looks more pretty on the terminal). Also adds newline at the end.
774//
0fb67cd1
VZ
775// TODO this is now disabled until I find a portable way of determining the
776// terminal window size (ok, I found it but does anybody really cares?)
777#ifdef LOG_PRETTY_WRAP
c801d85f
KB
778static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
779{
0fb67cd1
VZ
780 size_t nMax = 80; // FIXME
781 size_t nStart = strlen(pszPrefix);
782 fputs(pszPrefix, f);
783
784 size_t n;
785 while ( *psz != '\0' ) {
786 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
787 putc(*psz++, f);
788
789 // wrapped?
790 if ( *psz != '\0' ) {
791 /*putc('\n', f);*/
792 for ( n = 0; n < nStart; n++ )
793 putc(' ', f);
794
795 // as we wrapped, squeeze all white space
796 while ( isspace(*psz) )
797 psz++;
798 }
c801d85f 799 }
c801d85f 800
0fb67cd1 801 putc('\n', f);
c801d85f
KB
802}
803#endif //LOG_PRETTY_WRAP
804
805// ----------------------------------------------------------------------------
806// error code/error message retrieval functions
807// ----------------------------------------------------------------------------
808
809// get error code from syste
810unsigned long wxSysErrorCode()
811{
8cb172b4 812#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1
VZ
813#ifdef __WIN32__
814 return ::GetLastError();
815#else //WIN16
816 // TODO what to do on Windows 3.1?
817 return 0;
818#endif //WIN16/32
819#else //Unix
c801d85f 820 return errno;
0fb67cd1 821#endif //Win/Unix
c801d85f
KB
822}
823
824// get error message from system
50920146 825const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 826{
0fb67cd1
VZ
827 if ( nErrCode == 0 )
828 nErrCode = wxSysErrorCode();
829
8cb172b4 830#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 831#ifdef __WIN32__
50920146 832 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
833
834 // get error message from system
835 LPVOID lpMsgBuf;
836 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
837 NULL, nErrCode,
838 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
839 (LPTSTR)&lpMsgBuf,
840 0, NULL);
841
842 // copy it to our buffer and free memory
251244a0 843 if( lpMsgBuf != 0 ) {
8c5b1f0f 844 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
845 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
846
847 LocalFree(lpMsgBuf);
848
849 // returned string is capitalized and ended with '\r\n' - bad
850 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
851 size_t len = wxStrlen(s_szBuf);
852 if ( len > 0 ) {
853 // truncate string
854 if ( s_szBuf[len - 2] == wxT('\r') )
855 s_szBuf[len - 2] = wxT('\0');
856 }
857 }
858 else {
8c5b1f0f 859 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
860 }
861
862 return s_szBuf;
863#else //Win16
864 // TODO
865 return NULL;
866#endif // Win16/32
867#else // Unix
50920146
OK
868#if wxUSE_UNICODE
869 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
dcf924a3 870 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
50920146
OK
871 return s_szBuf;
872#else
13111b2a 873 return strerror((int)nErrCode);
50920146 874#endif
0fb67cd1 875#endif // Win/Unix
c801d85f
KB
876}
877
f94dfb38 878#endif //wxUSE_LOG