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