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