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