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