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