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