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