]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/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 licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_LOG | |
28 | ||
29 | // wxWidgets | |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/log.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/arrstr.h" | |
34 | #include "wx/intl.h" | |
35 | #include "wx/string.h" | |
36 | #include "wx/utils.h" | |
37 | #endif //WX_PRECOMP | |
38 | ||
39 | #include "wx/apptrait.h" | |
40 | #include "wx/datetime.h" | |
41 | #include "wx/file.h" | |
42 | #include "wx/msgout.h" | |
43 | #include "wx/textfile.h" | |
44 | #include "wx/thread.h" | |
45 | #include "wx/wxchar.h" | |
46 | ||
47 | // other standard headers | |
48 | #ifndef __WXWINCE__ | |
49 | #include <errno.h> | |
50 | #endif | |
51 | ||
52 | #include <stdlib.h> | |
53 | ||
54 | #ifndef __WXWINCE__ | |
55 | #include <time.h> | |
56 | #else | |
57 | #include "wx/msw/wince/time.h" | |
58 | #endif | |
59 | ||
60 | #if defined(__WINDOWS__) | |
61 | #include "wx/msw/private.h" // includes windows.h | |
62 | #endif | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // non member functions | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | // define this to enable wrapping of log messages | |
69 | //#define LOG_PRETTY_WRAP | |
70 | ||
71 | #ifdef LOG_PRETTY_WRAP | |
72 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); | |
73 | #endif | |
74 | ||
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // implementation of Log functions | |
81 | // | |
82 | // NB: unfortunately we need all these distinct functions, we can't make them | |
83 | // macros and not all compilers inline vararg functions. | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | // generic log function | |
87 | void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr) | |
88 | { | |
89 | if ( wxLog::IsEnabled() ) { | |
90 | wxLog::OnLog(level, wxString::FormatV(szFormat, argptr), time(NULL)); | |
91 | } | |
92 | } | |
93 | ||
94 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) | |
95 | { | |
96 | va_list argptr; | |
97 | va_start(argptr, szFormat); | |
98 | wxVLogGeneric(level, szFormat, argptr); | |
99 | va_end(argptr); | |
100 | } | |
101 | ||
102 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
103 | void wxVLog##level(const wxChar *szFormat, va_list argptr) \ | |
104 | { \ | |
105 | if ( wxLog::IsEnabled() ) { \ | |
106 | wxLog::OnLog(wxLOG_##level, \ | |
107 | wxString::FormatV(szFormat, argptr), time(NULL));\ | |
108 | } \ | |
109 | } \ | |
110 | \ | |
111 | void wxLog##level(const wxChar *szFormat, ...) \ | |
112 | { \ | |
113 | va_list argptr; \ | |
114 | va_start(argptr, szFormat); \ | |
115 | wxVLog##level(szFormat, argptr); \ | |
116 | va_end(argptr); \ | |
117 | } | |
118 | ||
119 | IMPLEMENT_LOG_FUNCTION(Error) | |
120 | IMPLEMENT_LOG_FUNCTION(Warning) | |
121 | IMPLEMENT_LOG_FUNCTION(Message) | |
122 | IMPLEMENT_LOG_FUNCTION(Info) | |
123 | IMPLEMENT_LOG_FUNCTION(Status) | |
124 | ||
125 | void wxSafeShowMessage(const wxString& title, const wxString& text) | |
126 | { | |
127 | #ifdef __WINDOWS__ | |
128 | ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP); | |
129 | #else | |
130 | wxFprintf(stderr, _T("%s: %s\n"), title.c_str(), text.c_str()); | |
131 | fflush(stderr); | |
132 | #endif | |
133 | } | |
134 | ||
135 | // fatal errors can't be suppressed nor handled by the custom log target and | |
136 | // always terminate the program | |
137 | void wxVLogFatalError(const wxChar *szFormat, va_list argptr) | |
138 | { | |
139 | wxSafeShowMessage(_T("Fatal Error"), wxString::FormatV(szFormat, argptr)); | |
140 | ||
141 | #ifdef __WXWINCE__ | |
142 | ExitThread(3); | |
143 | #else | |
144 | abort(); | |
145 | #endif | |
146 | } | |
147 | ||
148 | void wxLogFatalError(const wxChar *szFormat, ...) | |
149 | { | |
150 | va_list argptr; | |
151 | va_start(argptr, szFormat); | |
152 | wxVLogFatalError(szFormat, argptr); | |
153 | ||
154 | // some compilers warn about unreachable code and it shouldn't matter | |
155 | // for the others anyhow... | |
156 | //va_end(argptr); | |
157 | } | |
158 | ||
159 | // same as info, but only if 'verbose' mode is on | |
160 | void wxVLogVerbose(const wxChar *szFormat, va_list argptr) | |
161 | { | |
162 | if ( wxLog::IsEnabled() ) { | |
163 | if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) { | |
164 | wxLog::OnLog(wxLOG_Info, | |
165 | wxString::FormatV(szFormat, argptr), time(NULL)); | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | void wxLogVerbose(const wxChar *szFormat, ...) | |
171 | { | |
172 | va_list argptr; | |
173 | va_start(argptr, szFormat); | |
174 | wxVLogVerbose(szFormat, argptr); | |
175 | va_end(argptr); | |
176 | } | |
177 | ||
178 | // debug functions | |
179 | #ifdef __WXDEBUG__ | |
180 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ | |
181 | void wxVLog##level(const wxChar *szFormat, va_list argptr) \ | |
182 | { \ | |
183 | if ( wxLog::IsEnabled() ) { \ | |
184 | wxLog::OnLog(wxLOG_##level, \ | |
185 | wxString::FormatV(szFormat, argptr), time(NULL));\ | |
186 | } \ | |
187 | } \ | |
188 | \ | |
189 | void wxLog##level(const wxChar *szFormat, ...) \ | |
190 | { \ | |
191 | va_list argptr; \ | |
192 | va_start(argptr, szFormat); \ | |
193 | wxVLog##level(szFormat, argptr); \ | |
194 | va_end(argptr); \ | |
195 | } | |
196 | ||
197 | void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr) | |
198 | { | |
199 | if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) { | |
200 | wxString msg; | |
201 | msg << _T("(") << mask << _T(") ") << wxString::FormatV(szFormat, argptr); | |
202 | ||
203 | wxLog::OnLog(wxLOG_Trace, msg, time(NULL)); | |
204 | } | |
205 | } | |
206 | ||
207 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) | |
208 | { | |
209 | va_list argptr; | |
210 | va_start(argptr, szFormat); | |
211 | wxVLogTrace(mask, szFormat, argptr); | |
212 | va_end(argptr); | |
213 | } | |
214 | ||
215 | void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr) | |
216 | { | |
217 | // we check that all of mask bits are set in the current mask, so | |
218 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
219 | // if both bits are set. | |
220 | if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) { | |
221 | wxLog::OnLog(wxLOG_Trace, wxString::FormatV(szFormat, argptr), time(NULL)); | |
222 | } | |
223 | } | |
224 | ||
225 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) | |
226 | { | |
227 | va_list argptr; | |
228 | va_start(argptr, szFormat); | |
229 | wxVLogTrace(mask, szFormat, argptr); | |
230 | va_end(argptr); | |
231 | } | |
232 | ||
233 | #else // release | |
234 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
235 | #endif | |
236 | ||
237 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
238 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
239 | ||
240 | // wxLogSysError: one uses the last error code, for other you must give it | |
241 | // explicitly | |
242 | ||
243 | // return the system error message description | |
244 | static inline wxString wxLogSysErrorHelper(long err) | |
245 | { | |
246 | return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err)); | |
247 | } | |
248 | ||
249 | void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr) | |
250 | { | |
251 | wxVLogSysError(wxSysErrorCode(), szFormat, argptr); | |
252 | } | |
253 | ||
254 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) | |
255 | { | |
256 | va_list argptr; | |
257 | va_start(argptr, szFormat); | |
258 | wxVLogSysError(szFormat, argptr); | |
259 | va_end(argptr); | |
260 | } | |
261 | ||
262 | void WXDLLEXPORT wxVLogSysError(long err, const wxChar *fmt, va_list argptr) | |
263 | { | |
264 | if ( wxLog::IsEnabled() ) { | |
265 | wxLog::OnLog(wxLOG_Error, | |
266 | wxString::FormatV(fmt, argptr) + wxLogSysErrorHelper(err), | |
267 | time(NULL)); | |
268 | } | |
269 | } | |
270 | ||
271 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) | |
272 | { | |
273 | va_list argptr; | |
274 | va_start(argptr, szFormat); | |
275 | wxVLogSysError(lErrCode, szFormat, argptr); | |
276 | va_end(argptr); | |
277 | } | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // wxLog class implementation | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | /* static */ | |
284 | unsigned wxLog::DoLogNumberOfRepeats() | |
285 | { | |
286 | long retval = ms_prevCounter; | |
287 | wxLog *pLogger = GetActiveTarget(); | |
288 | if ( pLogger && ms_prevCounter > 0 ) | |
289 | { | |
290 | wxString msg; | |
291 | #if wxUSE_INTL | |
292 | msg.Printf(wxPLURAL("The previous message repeated once.", | |
293 | "The previous message repeated %lu times.", | |
294 | ms_prevCounter), | |
295 | ms_prevCounter); | |
296 | #else | |
297 | msg.Printf(wxT("The previous message was repeated.")); | |
298 | #endif | |
299 | ms_prevCounter = 0; | |
300 | ms_prevString.clear(); | |
301 | pLogger->DoLog(ms_prevLevel, msg.c_str(), ms_prevTimeStamp); | |
302 | } | |
303 | return retval; | |
304 | } | |
305 | ||
306 | wxLog::~wxLog() | |
307 | { | |
308 | if ( ms_prevCounter > 0 ) | |
309 | { | |
310 | // looks like the repeat count has not been logged yet, | |
311 | // so let's do it now | |
312 | wxLog::DoLogNumberOfRepeats(); | |
313 | } | |
314 | } | |
315 | ||
316 | /* static */ | |
317 | void wxLog::OnLog(wxLogLevel level, const wxChar *szString, time_t t) | |
318 | { | |
319 | if ( IsEnabled() && ms_logLevel >= level ) | |
320 | { | |
321 | wxLog *pLogger = GetActiveTarget(); | |
322 | if ( pLogger ) | |
323 | { | |
324 | if ( GetRepetitionCounting() && ms_prevString == szString ) | |
325 | { | |
326 | ms_prevCounter++; | |
327 | } | |
328 | else | |
329 | { | |
330 | if ( GetRepetitionCounting() ) | |
331 | { | |
332 | pLogger->DoLogNumberOfRepeats(); | |
333 | } | |
334 | ms_prevString = szString; | |
335 | ms_prevLevel = level; | |
336 | ms_prevTimeStamp = t; | |
337 | pLogger->DoLog(level, szString, t); | |
338 | } | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | // deprecated function | |
344 | #if WXWIN_COMPATIBILITY_2_6 | |
345 | ||
346 | wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size)) | |
347 | { | |
348 | return NULL; | |
349 | } | |
350 | ||
351 | #endif // WXWIN_COMPATIBILITY_2_6 | |
352 | ||
353 | wxLog *wxLog::GetActiveTarget() | |
354 | { | |
355 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { | |
356 | // prevent infinite recursion if someone calls wxLogXXX() from | |
357 | // wxApp::CreateLogTarget() | |
358 | static bool s_bInGetActiveTarget = false; | |
359 | if ( !s_bInGetActiveTarget ) { | |
360 | s_bInGetActiveTarget = true; | |
361 | ||
362 | // ask the application to create a log target for us | |
363 | if ( wxTheApp != NULL ) | |
364 | ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget(); | |
365 | else | |
366 | ms_pLogger = new wxLogStderr; | |
367 | ||
368 | s_bInGetActiveTarget = false; | |
369 | ||
370 | // do nothing if it fails - what can we do? | |
371 | } | |
372 | } | |
373 | ||
374 | return ms_pLogger; | |
375 | } | |
376 | ||
377 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
378 | { | |
379 | if ( ms_pLogger != NULL ) { | |
380 | // flush the old messages before changing because otherwise they might | |
381 | // get lost later if this target is not restored | |
382 | ms_pLogger->Flush(); | |
383 | } | |
384 | ||
385 | wxLog *pOldLogger = ms_pLogger; | |
386 | ms_pLogger = pLogger; | |
387 | ||
388 | return pOldLogger; | |
389 | } | |
390 | ||
391 | void wxLog::DontCreateOnDemand() | |
392 | { | |
393 | ms_bAutoCreate = false; | |
394 | ||
395 | // this is usually called at the end of the program and we assume that it | |
396 | // is *always* called at the end - so we free memory here to avoid false | |
397 | // memory leak reports from wxWin memory tracking code | |
398 | ClearTraceMasks(); | |
399 | } | |
400 | ||
401 | void wxLog::RemoveTraceMask(const wxString& str) | |
402 | { | |
403 | int index = ms_aTraceMasks.Index(str); | |
404 | if ( index != wxNOT_FOUND ) | |
405 | ms_aTraceMasks.RemoveAt((size_t)index); | |
406 | } | |
407 | ||
408 | void wxLog::ClearTraceMasks() | |
409 | { | |
410 | ms_aTraceMasks.Clear(); | |
411 | } | |
412 | ||
413 | void wxLog::TimeStamp(wxString *str) | |
414 | { | |
415 | #if wxUSE_DATETIME | |
416 | if ( ms_timestamp ) | |
417 | { | |
418 | wxChar buf[256]; | |
419 | time_t timeNow; | |
420 | (void)time(&timeNow); | |
421 | ||
422 | struct tm tm; | |
423 | wxStrftime(buf, WXSIZEOF(buf), | |
424 | ms_timestamp, wxLocaltime_r(&timeNow, &tm)); | |
425 | ||
426 | str->Empty(); | |
427 | *str << buf << wxT(": "); | |
428 | } | |
429 | #endif // wxUSE_DATETIME | |
430 | } | |
431 | ||
432 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
433 | { | |
434 | switch ( level ) { | |
435 | case wxLOG_FatalError: | |
436 | DoLogString(wxString(_("Fatal error: ")) + szString, t); | |
437 | DoLogString(_("Program aborted."), t); | |
438 | Flush(); | |
439 | #ifdef __WXWINCE__ | |
440 | ExitThread(3); | |
441 | #else | |
442 | abort(); | |
443 | #endif | |
444 | break; | |
445 | ||
446 | case wxLOG_Error: | |
447 | DoLogString(wxString(_("Error: ")) + szString, t); | |
448 | break; | |
449 | ||
450 | case wxLOG_Warning: | |
451 | DoLogString(wxString(_("Warning: ")) + szString, t); | |
452 | break; | |
453 | ||
454 | case wxLOG_Info: | |
455 | if ( GetVerbose() ) | |
456 | case wxLOG_Message: | |
457 | case wxLOG_Status: | |
458 | default: // log unknown log levels too | |
459 | DoLogString(szString, t); | |
460 | break; | |
461 | ||
462 | case wxLOG_Trace: | |
463 | case wxLOG_Debug: | |
464 | #ifdef __WXDEBUG__ | |
465 | { | |
466 | wxString msg = level == wxLOG_Trace ? wxT("Trace: ") | |
467 | : wxT("Debug: "); | |
468 | msg << szString; | |
469 | DoLogString(msg, t); | |
470 | } | |
471 | #endif // Debug | |
472 | break; | |
473 | } | |
474 | } | |
475 | ||
476 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) | |
477 | { | |
478 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); | |
479 | } | |
480 | ||
481 | void wxLog::Flush() | |
482 | { | |
483 | // nothing to do here | |
484 | } | |
485 | ||
486 | /*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask) | |
487 | { | |
488 | for ( wxArrayString::iterator it = ms_aTraceMasks.begin(), | |
489 | en = ms_aTraceMasks.end(); | |
490 | it != en; ++it ) | |
491 | if ( *it == mask) | |
492 | return true; | |
493 | return false; | |
494 | } | |
495 | ||
496 | // ---------------------------------------------------------------------------- | |
497 | // wxLogBuffer implementation | |
498 | // ---------------------------------------------------------------------------- | |
499 | ||
500 | void wxLogBuffer::Flush() | |
501 | { | |
502 | if ( !m_str.empty() ) | |
503 | { | |
504 | wxMessageOutputBest out; | |
505 | out.Printf(_T("%s"), m_str.c_str()); | |
506 | m_str.clear(); | |
507 | } | |
508 | } | |
509 | ||
510 | void wxLogBuffer::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
511 | { | |
512 | switch ( level ) | |
513 | { | |
514 | case wxLOG_Trace: | |
515 | case wxLOG_Debug: | |
516 | #ifdef __WXDEBUG__ | |
517 | // don't put debug messages in the buffer, we don't want to show | |
518 | // them to the user in a msg box, log them immediately | |
519 | { | |
520 | wxString str; | |
521 | TimeStamp(&str); | |
522 | str += szString; | |
523 | ||
524 | wxMessageOutputDebug dbgout; | |
525 | dbgout.Printf(_T("%s\n"), str.c_str()); | |
526 | } | |
527 | #endif // __WXDEBUG__ | |
528 | break; | |
529 | ||
530 | default: | |
531 | wxLog::DoLog(level, szString, t); | |
532 | } | |
533 | } | |
534 | ||
535 | void wxLogBuffer::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
536 | { | |
537 | m_str << szString << _T("\n"); | |
538 | } | |
539 | ||
540 | // ---------------------------------------------------------------------------- | |
541 | // wxLogStderr class implementation | |
542 | // ---------------------------------------------------------------------------- | |
543 | ||
544 | wxLogStderr::wxLogStderr(FILE *fp) | |
545 | { | |
546 | if ( fp == NULL ) | |
547 | m_fp = stderr; | |
548 | else | |
549 | m_fp = fp; | |
550 | } | |
551 | ||
552 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
553 | { | |
554 | wxString str; | |
555 | TimeStamp(&str); | |
556 | str << szString; | |
557 | ||
558 | wxFputs(str, m_fp); | |
559 | wxFputc(_T('\n'), m_fp); | |
560 | fflush(m_fp); | |
561 | ||
562 | // under GUI systems such as Windows or Mac, programs usually don't have | |
563 | // stderr at all, so show the messages also somewhere else, typically in | |
564 | // the debugger window so that they go at least somewhere instead of being | |
565 | // simply lost | |
566 | if ( m_fp == stderr ) | |
567 | { | |
568 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
569 | if ( traits && !traits->HasStderr() ) | |
570 | { | |
571 | wxMessageOutputDebug dbgout; | |
572 | dbgout.Printf(_T("%s\n"), str.c_str()); | |
573 | } | |
574 | } | |
575 | } | |
576 | ||
577 | // ---------------------------------------------------------------------------- | |
578 | // wxLogStream implementation | |
579 | // ---------------------------------------------------------------------------- | |
580 | ||
581 | #if wxUSE_STD_IOSTREAM | |
582 | #include "wx/ioswrap.h" | |
583 | wxLogStream::wxLogStream(wxSTD ostream *ostr) | |
584 | { | |
585 | if ( ostr == NULL ) | |
586 | m_ostr = &wxSTD cerr; | |
587 | else | |
588 | m_ostr = ostr; | |
589 | } | |
590 | ||
591 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
592 | { | |
593 | wxString str; | |
594 | TimeStamp(&str); | |
595 | (*m_ostr) << wxConvertWX2MB(str) << wxConvertWX2MB(szString) << wxSTD endl; | |
596 | } | |
597 | #endif // wxUSE_STD_IOSTREAM | |
598 | ||
599 | // ---------------------------------------------------------------------------- | |
600 | // wxLogChain | |
601 | // ---------------------------------------------------------------------------- | |
602 | ||
603 | wxLogChain::wxLogChain(wxLog *logger) | |
604 | { | |
605 | m_bPassMessages = true; | |
606 | ||
607 | m_logNew = logger; | |
608 | m_logOld = wxLog::SetActiveTarget(this); | |
609 | } | |
610 | ||
611 | wxLogChain::~wxLogChain() | |
612 | { | |
613 | delete m_logOld; | |
614 | ||
615 | if ( m_logNew != this ) | |
616 | delete m_logNew; | |
617 | } | |
618 | ||
619 | void wxLogChain::SetLog(wxLog *logger) | |
620 | { | |
621 | if ( m_logNew != this ) | |
622 | delete m_logNew; | |
623 | ||
624 | m_logNew = logger; | |
625 | } | |
626 | ||
627 | void wxLogChain::Flush() | |
628 | { | |
629 | if ( m_logOld ) | |
630 | m_logOld->Flush(); | |
631 | ||
632 | // be careful to avoid infinite recursion | |
633 | if ( m_logNew && m_logNew != this ) | |
634 | m_logNew->Flush(); | |
635 | } | |
636 | ||
637 | void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
638 | { | |
639 | // let the previous logger show it | |
640 | if ( m_logOld && IsPassingMessages() ) | |
641 | { | |
642 | // bogus cast just to access protected DoLog | |
643 | ((wxLogChain *)m_logOld)->DoLog(level, szString, t); | |
644 | } | |
645 | ||
646 | if ( m_logNew && m_logNew != this ) | |
647 | { | |
648 | // as above... | |
649 | ((wxLogChain *)m_logNew)->DoLog(level, szString, t); | |
650 | } | |
651 | } | |
652 | ||
653 | // ---------------------------------------------------------------------------- | |
654 | // wxLogPassThrough | |
655 | // ---------------------------------------------------------------------------- | |
656 | ||
657 | #ifdef __VISUALC__ | |
658 | // "'this' : used in base member initializer list" - so what? | |
659 | #pragma warning(disable:4355) | |
660 | #endif // VC++ | |
661 | ||
662 | wxLogPassThrough::wxLogPassThrough() | |
663 | : wxLogChain(this) | |
664 | { | |
665 | } | |
666 | ||
667 | #ifdef __VISUALC__ | |
668 | #pragma warning(default:4355) | |
669 | #endif // VC++ | |
670 | ||
671 | // ============================================================================ | |
672 | // Global functions/variables | |
673 | // ============================================================================ | |
674 | ||
675 | // ---------------------------------------------------------------------------- | |
676 | // static variables | |
677 | // ---------------------------------------------------------------------------- | |
678 | ||
679 | bool wxLog::ms_bRepetCounting = false; | |
680 | wxString wxLog::ms_prevString; | |
681 | unsigned int wxLog::ms_prevCounter = 0; | |
682 | time_t wxLog::ms_prevTimeStamp= 0; | |
683 | wxLogLevel wxLog::ms_prevLevel; | |
684 | ||
685 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
686 | bool wxLog::ms_doLog = true; | |
687 | bool wxLog::ms_bAutoCreate = true; | |
688 | bool wxLog::ms_bVerbose = false; | |
689 | ||
690 | wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default | |
691 | ||
692 | size_t wxLog::ms_suspendCount = 0; | |
693 | ||
694 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
695 | ||
696 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; | |
697 | wxArrayString wxLog::ms_aTraceMasks; | |
698 | ||
699 | // ---------------------------------------------------------------------------- | |
700 | // stdout error logging helper | |
701 | // ---------------------------------------------------------------------------- | |
702 | ||
703 | // helper function: wraps the message and justifies it under given position | |
704 | // (looks more pretty on the terminal). Also adds newline at the end. | |
705 | // | |
706 | // TODO this is now disabled until I find a portable way of determining the | |
707 | // terminal window size (ok, I found it but does anybody really cares?) | |
708 | #ifdef LOG_PRETTY_WRAP | |
709 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
710 | { | |
711 | size_t nMax = 80; // FIXME | |
712 | size_t nStart = strlen(pszPrefix); | |
713 | fputs(pszPrefix, f); | |
714 | ||
715 | size_t n; | |
716 | while ( *psz != '\0' ) { | |
717 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
718 | putc(*psz++, f); | |
719 | ||
720 | // wrapped? | |
721 | if ( *psz != '\0' ) { | |
722 | /*putc('\n', f);*/ | |
723 | for ( n = 0; n < nStart; n++ ) | |
724 | putc(' ', f); | |
725 | ||
726 | // as we wrapped, squeeze all white space | |
727 | while ( isspace(*psz) ) | |
728 | psz++; | |
729 | } | |
730 | } | |
731 | ||
732 | putc('\n', f); | |
733 | } | |
734 | #endif //LOG_PRETTY_WRAP | |
735 | ||
736 | // ---------------------------------------------------------------------------- | |
737 | // error code/error message retrieval functions | |
738 | // ---------------------------------------------------------------------------- | |
739 | ||
740 | // get error code from syste | |
741 | unsigned long wxSysErrorCode() | |
742 | { | |
743 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
744 | return ::GetLastError(); | |
745 | #else //Unix | |
746 | return errno; | |
747 | #endif //Win/Unix | |
748 | } | |
749 | ||
750 | // get error message from system | |
751 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) | |
752 | { | |
753 | if ( nErrCode == 0 ) | |
754 | nErrCode = wxSysErrorCode(); | |
755 | ||
756 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
757 | static wxChar s_szBuf[1024]; | |
758 | ||
759 | // get error message from system | |
760 | LPVOID lpMsgBuf; | |
761 | if ( ::FormatMessage | |
762 | ( | |
763 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
764 | NULL, | |
765 | nErrCode, | |
766 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
767 | (LPTSTR)&lpMsgBuf, | |
768 | 0, | |
769 | NULL | |
770 | ) == 0 ) | |
771 | { | |
772 | // if this happens, something is seriously wrong, so don't use _() here | |
773 | // for safety | |
774 | wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode); | |
775 | return s_szBuf; | |
776 | } | |
777 | ||
778 | ||
779 | // copy it to our buffer and free memory | |
780 | // Crashes on SmartPhone (FIXME) | |
781 | #if !defined(__SMARTPHONE__) /* of WinCE */ | |
782 | if( lpMsgBuf != 0 ) | |
783 | { | |
784 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
785 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); | |
786 | ||
787 | LocalFree(lpMsgBuf); | |
788 | ||
789 | // returned string is capitalized and ended with '\r\n' - bad | |
790 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); | |
791 | size_t len = wxStrlen(s_szBuf); | |
792 | if ( len > 0 ) { | |
793 | // truncate string | |
794 | if ( s_szBuf[len - 2] == wxT('\r') ) | |
795 | s_szBuf[len - 2] = wxT('\0'); | |
796 | } | |
797 | } | |
798 | else | |
799 | #endif // !__SMARTPHONE__ | |
800 | { | |
801 | s_szBuf[0] = wxT('\0'); | |
802 | } | |
803 | ||
804 | return s_szBuf; | |
805 | #else // !__WXMSW__ | |
806 | #if wxUSE_UNICODE | |
807 | static wchar_t s_wzBuf[1024]; | |
808 | wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode), | |
809 | WXSIZEOF(s_wzBuf) - 1); | |
810 | return s_wzBuf; | |
811 | #else | |
812 | return strerror((int)nErrCode); | |
813 | #endif | |
814 | #endif // __WXMSW__/!__WXMSW__ | |
815 | } | |
816 | ||
817 | #endif // wxUSE_LOG |