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