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