]>
Commit | Line | Data |
---|---|---|
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 | // ---------------------------------------------------------------------------- | |
19 | ||
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 | |
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 | |
42 | #endif | |
43 | #endif //WX_PRECOMP | |
44 | ||
45 | #include "wx/file.h" | |
46 | #include "wx/textfile.h" | |
47 | #include "wx/utils.h" | |
48 | #include "wx/wxchar.h" | |
49 | #include "wx/log.h" | |
50 | #include "wx/thread.h" | |
51 | ||
52 | #if wxUSE_LOG | |
53 | ||
54 | // other standard headers | |
55 | #include <errno.h> | |
56 | #include <stdlib.h> | |
57 | #include <time.h> | |
58 | ||
59 | #if defined(__WXMSW__) | |
60 | #include "wx/msw/private.h" // includes windows.h for OutputDebugString | |
61 | #endif | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // non member functions | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | // define this to enable wrapping of log messages | |
68 | //#define LOG_PRETTY_WRAP | |
69 | ||
70 | #ifdef LOG_PRETTY_WRAP | |
71 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); | |
72 | #endif | |
73 | ||
74 | // ============================================================================ | |
75 | // implementation | |
76 | // ============================================================================ | |
77 | ||
78 | // ---------------------------------------------------------------------------- | |
79 | // globals | |
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 | ||
86 | // static buffer for error messages | |
87 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; | |
88 | ||
89 | #if wxUSE_THREADS | |
90 | ||
91 | // the critical section protecting the static buffer | |
92 | static wxCriticalSection gs_csLogBuf; | |
93 | ||
94 | #endif // wxUSE_THREADS | |
95 | ||
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 | ||
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 | ||
109 | // generic log function | |
110 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) | |
111 | { | |
112 | if ( IsLoggingEnabled() ) { | |
113 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
114 | ||
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 | } | |
122 | } | |
123 | ||
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 | } \ | |
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 | ||
146 | // same as info, but only if 'verbose' mode is on | |
147 | void wxLogVerbose(const wxChar *szFormat, ...) | |
148 | { | |
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 | } | |
162 | } | |
163 | ||
164 | // debug functions | |
165 | #ifdef __WXDEBUG__ | |
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 | } \ | |
179 | } | |
180 | ||
181 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) | |
182 | { | |
183 | if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) { | |
184 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
185 | ||
186 | wxChar *p = s_szBuf; | |
187 | size_t len = WXSIZEOF(s_szBuf); | |
188 | wxStrncpy(s_szBuf, _T("("), len); | |
189 | len -= 1; // strlen("(") | |
190 | p += 1; | |
191 | wxStrncat(p, mask, len); | |
192 | size_t lenMask = wxStrlen(mask); | |
193 | len -= lenMask; | |
194 | p += lenMask; | |
195 | ||
196 | wxStrncat(p, _T(") "), len); | |
197 | len -= 2; | |
198 | p += 2; | |
199 | ||
200 | va_list argptr; | |
201 | va_start(argptr, szFormat); | |
202 | wxVsnprintf(p, len, szFormat, argptr); | |
203 | va_end(argptr); | |
204 | ||
205 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
206 | } | |
207 | } | |
208 | ||
209 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) | |
210 | { | |
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. | |
214 | if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) { | |
215 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
216 | ||
217 | va_list argptr; | |
218 | va_start(argptr, szFormat); | |
219 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
220 | va_end(argptr); | |
221 | ||
222 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
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) | |
238 | { | |
239 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; | |
240 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), | |
241 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
242 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); | |
243 | ||
244 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); | |
245 | } | |
246 | ||
247 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) | |
248 | { | |
249 | if ( IsLoggingEnabled() ) { | |
250 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
251 | ||
252 | va_list argptr; | |
253 | va_start(argptr, szFormat); | |
254 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
255 | va_end(argptr); | |
256 | ||
257 | wxLogSysErrorHelper(wxSysErrorCode()); | |
258 | } | |
259 | } | |
260 | ||
261 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) | |
262 | { | |
263 | if ( IsLoggingEnabled() ) { | |
264 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
265 | ||
266 | va_list argptr; | |
267 | va_start(argptr, szFormat); | |
268 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
269 | va_end(argptr); | |
270 | ||
271 | wxLogSysErrorHelper(lErrCode); | |
272 | } | |
273 | } | |
274 | ||
275 | // ---------------------------------------------------------------------------- | |
276 | // wxLog class implementation | |
277 | // ---------------------------------------------------------------------------- | |
278 | ||
279 | wxLog::wxLog() | |
280 | { | |
281 | m_bHasMessages = FALSE; | |
282 | } | |
283 | ||
284 | wxLog *wxLog::GetActiveTarget() | |
285 | { | |
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 | ||
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; | |
298 | ||
299 | s_bInGetActiveTarget = FALSE; | |
300 | ||
301 | // do nothing if it fails - what can we do? | |
302 | } | |
303 | } | |
304 | ||
305 | return ms_pLogger; | |
306 | } | |
307 | ||
308 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
309 | { | |
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 | } | |
315 | ||
316 | wxLog *pOldLogger = ms_pLogger; | |
317 | ms_pLogger = pLogger; | |
318 | ||
319 | return pOldLogger; | |
320 | } | |
321 | ||
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 | ||
332 | void wxLog::RemoveTraceMask(const wxString& str) | |
333 | { | |
334 | int index = ms_aTraceMasks.Index(str); | |
335 | if ( index != wxNOT_FOUND ) | |
336 | ms_aTraceMasks.Remove((size_t)index); | |
337 | } | |
338 | ||
339 | void wxLog::ClearTraceMasks() | |
340 | { | |
341 | ms_aTraceMasks.Clear(); | |
342 | } | |
343 | ||
344 | void wxLog::TimeStamp(wxString *str) | |
345 | { | |
346 | if ( ms_timestamp ) | |
347 | { | |
348 | wxChar buf[256]; | |
349 | time_t timeNow; | |
350 | (void)time(&timeNow); | |
351 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); | |
352 | ||
353 | str->Empty(); | |
354 | *str << buf << wxT(": "); | |
355 | } | |
356 | } | |
357 | ||
358 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
359 | { | |
360 | switch ( level ) { | |
361 | case wxLOG_FatalError: | |
362 | DoLogString(wxString(_("Fatal error: ")) + szString, t); | |
363 | DoLogString(_("Program aborted."), t); | |
364 | Flush(); | |
365 | abort(); | |
366 | break; | |
367 | ||
368 | case wxLOG_Error: | |
369 | DoLogString(wxString(_("Error: ")) + szString, t); | |
370 | break; | |
371 | ||
372 | case wxLOG_Warning: | |
373 | DoLogString(wxString(_("Warning: ")) + szString, t); | |
374 | break; | |
375 | ||
376 | case wxLOG_Info: | |
377 | if ( GetVerbose() ) | |
378 | case wxLOG_Message: | |
379 | case wxLOG_Status: | |
380 | default: // log unknown log levels too | |
381 | DoLogString(szString, t); | |
382 | break; | |
383 | ||
384 | case wxLOG_Trace: | |
385 | case wxLOG_Debug: | |
386 | #ifdef __WXDEBUG__ | |
387 | { | |
388 | wxString msg = level == wxLOG_Trace ? wxT("Trace: ") | |
389 | : wxT("Debug: "); | |
390 | msg << szString; | |
391 | DoLogString(msg, t); | |
392 | } | |
393 | #endif // Debug | |
394 | break; | |
395 | } | |
396 | } | |
397 | ||
398 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) | |
399 | { | |
400 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); | |
401 | } | |
402 | ||
403 | void wxLog::Flush() | |
404 | { | |
405 | // remember that we don't have any more messages to show | |
406 | m_bHasMessages = FALSE; | |
407 | } | |
408 | ||
409 | // ---------------------------------------------------------------------------- | |
410 | // wxLogStderr class implementation | |
411 | // ---------------------------------------------------------------------------- | |
412 | ||
413 | wxLogStderr::wxLogStderr(FILE *fp) | |
414 | { | |
415 | if ( fp == NULL ) | |
416 | m_fp = stderr; | |
417 | else | |
418 | m_fp = fp; | |
419 | } | |
420 | ||
421 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
422 | #define kDebuggerSignature 'MWDB' | |
423 | ||
424 | static Boolean FindProcessBySignature(OSType signature, ProcessInfoRec* info) | |
425 | { | |
426 | OSErr err; | |
427 | ProcessSerialNumber psn; | |
428 | Boolean found = false; | |
429 | psn.highLongOfPSN = 0; | |
430 | psn.lowLongOfPSN = kNoProcess; | |
431 | ||
432 | if (!info) return false; | |
433 | ||
434 | info->processInfoLength = sizeof(ProcessInfoRec); | |
435 | info->processName = NULL; | |
436 | info->processAppSpec = NULL; | |
437 | ||
438 | err = noErr; | |
439 | while (!found && err == noErr) | |
440 | { | |
441 | err = GetNextProcess(&psn); | |
442 | if (err == noErr) | |
443 | { | |
444 | err = GetProcessInformation(&psn, info); | |
445 | found = err == noErr && info->processSignature == signature; | |
446 | } | |
447 | } | |
448 | return found; | |
449 | } | |
450 | ||
451 | pascal Boolean MWDebuggerIsRunning(void) | |
452 | { | |
453 | ProcessInfoRec info; | |
454 | return FindProcessBySignature(kDebuggerSignature, &info); | |
455 | } | |
456 | ||
457 | pascal OSErr AmIBeingMWDebugged(Boolean* result) | |
458 | { | |
459 | OSErr err; | |
460 | ProcessSerialNumber psn; | |
461 | OSType sig = kDebuggerSignature; | |
462 | AppleEvent theAE = {typeNull, NULL}; | |
463 | AppleEvent theReply = {typeNull, NULL}; | |
464 | AEAddressDesc addr = {typeNull, NULL}; | |
465 | DescType actualType; | |
466 | Size actualSize; | |
467 | ||
468 | if (!result) return paramErr; | |
469 | ||
470 | err = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &addr); | |
471 | if (err != noErr) goto exit; | |
472 | ||
473 | err = AECreateAppleEvent(kDebuggerSignature, 'Dbg?', &addr, | |
474 | kAutoGenerateReturnID, kAnyTransactionID, &theAE); | |
475 | if (err != noErr) goto exit; | |
476 | ||
477 | GetCurrentProcess(&psn); | |
478 | err = AEPutParamPtr(&theAE, keyDirectObject, typeProcessSerialNumber, | |
479 | &psn, sizeof(psn)); | |
480 | if (err != noErr) goto exit; | |
481 | ||
482 | err = AESend(&theAE, &theReply, kAEWaitReply, kAENormalPriority, | |
483 | kAEDefaultTimeout, NULL, NULL); | |
484 | if (err != noErr) goto exit; | |
485 | ||
486 | err = AEGetParamPtr(&theReply, keyAEResult, typeBoolean, &actualType, result, | |
487 | sizeof(Boolean), &actualSize); | |
488 | ||
489 | exit: | |
490 | if (addr.dataHandle) | |
491 | AEDisposeDesc(&addr); | |
492 | if (theAE.dataHandle) | |
493 | AEDisposeDesc(&theAE); | |
494 | if (theReply.dataHandle) | |
495 | AEDisposeDesc(&theReply); | |
496 | ||
497 | return err; | |
498 | } | |
499 | #endif | |
500 | ||
501 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
502 | { | |
503 | wxString str; | |
504 | TimeStamp(&str); | |
505 | str << szString; | |
506 | ||
507 | fputs(str.mb_str(), m_fp); | |
508 | fputc(_T('\n'), m_fp); | |
509 | fflush(m_fp); | |
510 | ||
511 | // under Windows, programs usually don't have stderr at all, so show the | |
512 | // messages also under debugger - unless it's a console program | |
513 | #if defined(__WXMSW__) && wxUSE_GUI && !defined(__WXMICROWIN__) | |
514 | str += wxT("\r\n") ; | |
515 | OutputDebugString(str.c_str()); | |
516 | #endif // MSW | |
517 | #if defined(__WXMAC__) && !defined(__DARWIN__) && wxUSE_GUI | |
518 | Str255 pstr ; | |
519 | strcpy( (char*) pstr , str.c_str() ) ; | |
520 | strcat( (char*) pstr , ";g" ) ; | |
521 | c2pstr( (char*) pstr ) ; | |
522 | #if __WXDEBUG__ | |
523 | Boolean running = false ; | |
524 | ||
525 | /* | |
526 | if ( MWDebuggerIsRunning() ) | |
527 | { | |
528 | AmIBeingMWDebugged( &running ) ; | |
529 | } | |
530 | */ | |
531 | if (running) | |
532 | { | |
533 | #ifdef __powerc | |
534 | DebugStr(pstr); | |
535 | #else | |
536 | SysBreakStr(pstr); | |
537 | #endif | |
538 | } | |
539 | else | |
540 | #endif | |
541 | { | |
542 | #ifdef __powerc | |
543 | DebugStr(pstr); | |
544 | #else | |
545 | DebugStr(pstr); | |
546 | #endif | |
547 | } | |
548 | #endif // Mac | |
549 | } | |
550 | ||
551 | // ---------------------------------------------------------------------------- | |
552 | // wxLogStream implementation | |
553 | // ---------------------------------------------------------------------------- | |
554 | ||
555 | #if wxUSE_STD_IOSTREAM | |
556 | wxLogStream::wxLogStream(wxSTD ostream *ostr) | |
557 | { | |
558 | if ( ostr == NULL ) | |
559 | m_ostr = &wxSTD cerr; | |
560 | else | |
561 | m_ostr = ostr; | |
562 | } | |
563 | ||
564 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
565 | { | |
566 | wxString str; | |
567 | TimeStamp(&str); | |
568 | (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl; | |
569 | } | |
570 | #endif // wxUSE_STD_IOSTREAM | |
571 | ||
572 | // ---------------------------------------------------------------------------- | |
573 | // wxLogChain | |
574 | // ---------------------------------------------------------------------------- | |
575 | ||
576 | wxLogChain::wxLogChain(wxLog *logger) | |
577 | { | |
578 | m_logNew = logger; | |
579 | m_logOld = wxLog::SetActiveTarget(this); | |
580 | } | |
581 | ||
582 | void wxLogChain::SetLog(wxLog *logger) | |
583 | { | |
584 | if ( m_logNew != this ) | |
585 | delete m_logNew; | |
586 | ||
587 | wxLog::SetActiveTarget(logger); | |
588 | ||
589 | m_logNew = logger; | |
590 | } | |
591 | ||
592 | void wxLogChain::Flush() | |
593 | { | |
594 | if ( m_logOld ) | |
595 | m_logOld->Flush(); | |
596 | ||
597 | // be careful to avoid inifinite recursion | |
598 | if ( m_logNew && m_logNew != this ) | |
599 | m_logNew->Flush(); | |
600 | } | |
601 | ||
602 | void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
603 | { | |
604 | // let the previous logger show it | |
605 | if ( m_logOld && IsPassingMessages() ) | |
606 | { | |
607 | // bogus cast just to access protected DoLog | |
608 | ((wxLogChain *)m_logOld)->DoLog(level, szString, t); | |
609 | } | |
610 | ||
611 | if ( m_logNew && m_logNew != this ) | |
612 | { | |
613 | // as above... | |
614 | ((wxLogChain *)m_logNew)->DoLog(level, szString, t); | |
615 | } | |
616 | } | |
617 | ||
618 | // ---------------------------------------------------------------------------- | |
619 | // wxLogPassThrough | |
620 | // ---------------------------------------------------------------------------- | |
621 | ||
622 | #ifdef __VISUALC__ | |
623 | // "'this' : used in base member initializer list" - so what? | |
624 | #pragma warning(disable:4355) | |
625 | #endif // VC++ | |
626 | ||
627 | wxLogPassThrough::wxLogPassThrough() | |
628 | : wxLogChain(this) | |
629 | { | |
630 | } | |
631 | ||
632 | #ifdef __VISUALC__ | |
633 | #pragma warning(default:4355) | |
634 | #endif // VC++ | |
635 | ||
636 | // ============================================================================ | |
637 | // Global functions/variables | |
638 | // ============================================================================ | |
639 | ||
640 | // ---------------------------------------------------------------------------- | |
641 | // static variables | |
642 | // ---------------------------------------------------------------------------- | |
643 | ||
644 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
645 | bool wxLog::ms_doLog = TRUE; | |
646 | bool wxLog::ms_bAutoCreate = TRUE; | |
647 | bool wxLog::ms_bVerbose = FALSE; | |
648 | ||
649 | size_t wxLog::ms_suspendCount = 0; | |
650 | ||
651 | #if wxUSE_GUI | |
652 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
653 | #else | |
654 | const wxChar *wxLog::ms_timestamp = NULL; // save space | |
655 | #endif | |
656 | ||
657 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; | |
658 | wxArrayString wxLog::ms_aTraceMasks; | |
659 | ||
660 | // ---------------------------------------------------------------------------- | |
661 | // stdout error logging helper | |
662 | // ---------------------------------------------------------------------------- | |
663 | ||
664 | // helper function: wraps the message and justifies it under given position | |
665 | // (looks more pretty on the terminal). Also adds newline at the end. | |
666 | // | |
667 | // TODO this is now disabled until I find a portable way of determining the | |
668 | // terminal window size (ok, I found it but does anybody really cares?) | |
669 | #ifdef LOG_PRETTY_WRAP | |
670 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
671 | { | |
672 | size_t nMax = 80; // FIXME | |
673 | size_t nStart = strlen(pszPrefix); | |
674 | fputs(pszPrefix, f); | |
675 | ||
676 | size_t n; | |
677 | while ( *psz != '\0' ) { | |
678 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
679 | putc(*psz++, f); | |
680 | ||
681 | // wrapped? | |
682 | if ( *psz != '\0' ) { | |
683 | /*putc('\n', f);*/ | |
684 | for ( n = 0; n < nStart; n++ ) | |
685 | putc(' ', f); | |
686 | ||
687 | // as we wrapped, squeeze all white space | |
688 | while ( isspace(*psz) ) | |
689 | psz++; | |
690 | } | |
691 | } | |
692 | ||
693 | putc('\n', f); | |
694 | } | |
695 | #endif //LOG_PRETTY_WRAP | |
696 | ||
697 | // ---------------------------------------------------------------------------- | |
698 | // error code/error message retrieval functions | |
699 | // ---------------------------------------------------------------------------- | |
700 | ||
701 | // get error code from syste | |
702 | unsigned long wxSysErrorCode() | |
703 | { | |
704 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
705 | #ifdef __WIN32__ | |
706 | return ::GetLastError(); | |
707 | #else //WIN16 | |
708 | // TODO what to do on Windows 3.1? | |
709 | return 0; | |
710 | #endif //WIN16/32 | |
711 | #else //Unix | |
712 | return errno; | |
713 | #endif //Win/Unix | |
714 | } | |
715 | ||
716 | // get error message from system | |
717 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) | |
718 | { | |
719 | if ( nErrCode == 0 ) | |
720 | nErrCode = wxSysErrorCode(); | |
721 | ||
722 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
723 | #ifdef __WIN32__ | |
724 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
725 | ||
726 | // get error message from system | |
727 | LPVOID lpMsgBuf; | |
728 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
729 | NULL, nErrCode, | |
730 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
731 | (LPTSTR)&lpMsgBuf, | |
732 | 0, NULL); | |
733 | ||
734 | // copy it to our buffer and free memory | |
735 | if( lpMsgBuf != 0 ) | |
736 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
737 | else | |
738 | s_szBuf[0] = wxT('\0'); | |
739 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); | |
740 | LocalFree(lpMsgBuf); | |
741 | ||
742 | // returned string is capitalized and ended with '\r\n' - bad | |
743 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); | |
744 | size_t len = wxStrlen(s_szBuf); | |
745 | if ( len > 0 ) { | |
746 | // truncate string | |
747 | if ( s_szBuf[len - 2] == wxT('\r') ) | |
748 | s_szBuf[len - 2] = wxT('\0'); | |
749 | } | |
750 | ||
751 | return s_szBuf; | |
752 | #else //Win16 | |
753 | // TODO | |
754 | return NULL; | |
755 | #endif // Win16/32 | |
756 | #else // Unix | |
757 | #if wxUSE_UNICODE | |
758 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
759 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); | |
760 | return s_szBuf; | |
761 | #else | |
762 | return strerror((int)nErrCode); | |
763 | #endif | |
764 | #endif // Win/Unix | |
765 | } | |
766 | ||
767 | #endif //wxUSE_LOG |