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