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