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