]>
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 | 39 | #include "wx/apptrait.h" |
7b2d1c74 | 40 | #include "wx/datetime.h" |
e2478fde | 41 | #include "wx/file.h" |
e2478fde VZ |
42 | #include "wx/msgout.h" |
43 | #include "wx/textfile.h" | |
44 | #include "wx/thread.h" | |
acad886c | 45 | #include "wx/private/threadinfo.h" |
3a3dde0d | 46 | #include "wx/crt.h" |
232addd1 | 47 | #include "wx/vector.h" |
f94dfb38 | 48 | |
c801d85f | 49 | // other standard headers |
1c193821 | 50 | #ifndef __WXWINCE__ |
e2478fde | 51 | #include <errno.h> |
1c193821 JS |
52 | #endif |
53 | ||
e2478fde | 54 | #include <stdlib.h> |
1c193821 | 55 | |
9b4da627 | 56 | #ifndef __WXPALMOS5__ |
1c193821 | 57 | #ifndef __WXWINCE__ |
e2478fde | 58 | #include <time.h> |
1c193821 JS |
59 | #else |
60 | #include "wx/msw/wince/time.h" | |
61 | #endif | |
9b4da627 | 62 | #endif /* ! __WXPALMOS5__ */ |
31907d03 | 63 | |
9cce3be7 VS |
64 | #if defined(__WINDOWS__) |
65 | #include "wx/msw/private.h" // includes windows.h | |
66 | #endif | |
67 | ||
c602c59b VZ |
68 | #undef wxLOG_COMPONENT |
69 | const char *wxLOG_COMPONENT = ""; | |
70 | ||
d5e7d2ec VZ |
71 | // this macro allows to define an object which will be initialized before any |
72 | // other function in this file is called: this is necessary to allow log | |
73 | // functions to be used during static initialization (this is not advisable | |
74 | // anyhow but we should at least try to not crash) and to also ensure that they | |
75 | // are initialized by the time static initialization is done, i.e. before any | |
76 | // threads are created hopefully | |
77 | // | |
bc1e0d4d VZ |
78 | // the net effect of all this is that you can use Get##name() function to |
79 | // access the object without worrying about it being not initialized | |
d5e7d2ec VZ |
80 | // |
81 | // see also WX_DEFINE_GLOBAL_CONV2() in src/common/strconv.cpp | |
bc1e0d4d VZ |
82 | #define WX_DEFINE_GLOBAL_VAR(type, name) \ |
83 | inline type& Get##name() \ | |
d5e7d2ec | 84 | { \ |
bc1e0d4d VZ |
85 | static type s_##name; \ |
86 | return s_##name; \ | |
d5e7d2ec VZ |
87 | } \ |
88 | \ | |
bc1e0d4d VZ |
89 | type *gs_##name##Ptr = &Get##name() |
90 | ||
9d7cb671 VZ |
91 | #if wxUSE_THREADS |
92 | ||
93 | wxTLS_TYPE(wxThreadSpecificInfo) wxThreadInfoVar; | |
94 | ||
bc1e0d4d VZ |
95 | namespace |
96 | { | |
97 | ||
98 | // contains messages logged by the other threads and waiting to be shown until | |
99 | // Flush() is called in the main one | |
100 | typedef wxVector<wxLogRecord> wxLogRecords; | |
101 | wxLogRecords gs_bufferedLogRecords; | |
102 | ||
103 | #define WX_DEFINE_LOG_CS(name) WX_DEFINE_GLOBAL_VAR(wxCriticalSection, name##CS) | |
766aecab | 104 | |
232addd1 VZ |
105 | // this critical section is used for buffering the messages from threads other |
106 | // than main, i.e. it protects all accesses to gs_bufferedLogRecords above | |
d5e7d2ec | 107 | WX_DEFINE_LOG_CS(BackgroundLog); |
232addd1 | 108 | |
55e5154d | 109 | // this one is used for protecting TraceMasks() from concurrent access |
d5e7d2ec | 110 | WX_DEFINE_LOG_CS(TraceMask); |
c602c59b | 111 | |
03647350 | 112 | // and this one is used for GetComponentLevels() |
d5e7d2ec | 113 | WX_DEFINE_LOG_CS(Levels); |
c602c59b | 114 | |
232addd1 VZ |
115 | } // anonymous namespace |
116 | ||
766aecab VZ |
117 | #endif // wxUSE_THREADS |
118 | ||
c801d85f KB |
119 | // ---------------------------------------------------------------------------- |
120 | // non member functions | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
123 | // define this to enable wrapping of log messages | |
124 | //#define LOG_PRETTY_WRAP | |
125 | ||
9ef3052c | 126 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
127 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
128 | #endif | |
129 | ||
bc73d5ae VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // module globals | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | namespace | |
135 | { | |
136 | ||
137 | // this struct is used to store information about the previous log message used | |
138 | // by OnLog() to (optionally) avoid logging multiple copies of the same message | |
139 | struct PreviousLogInfo | |
140 | { | |
141 | PreviousLogInfo() | |
142 | { | |
143 | numRepeated = 0; | |
144 | } | |
145 | ||
146 | ||
147 | // previous message itself | |
148 | wxString msg; | |
149 | ||
150 | // its level | |
151 | wxLogLevel level; | |
152 | ||
153 | // other information about it | |
154 | wxLogRecordInfo info; | |
155 | ||
156 | // the number of times it was already repeated | |
157 | unsigned numRepeated; | |
158 | }; | |
159 | ||
160 | PreviousLogInfo gs_prevLog; | |
161 | ||
c602c59b VZ |
162 | |
163 | // map containing all components for which log level was explicitly set | |
164 | // | |
165 | // NB: all accesses to it must be protected by GetLevelsCS() critical section | |
bc1e0d4d | 166 | WX_DEFINE_GLOBAL_VAR(wxStringToNumHashMap, ComponentLevels); |
c602c59b | 167 | |
55b24eb8 VZ |
168 | // ---------------------------------------------------------------------------- |
169 | // wxLogOutputBest: wxLog wrapper around wxMessageOutputBest | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | class wxLogOutputBest : public wxLog | |
173 | { | |
174 | public: | |
175 | wxLogOutputBest() { } | |
176 | ||
177 | protected: | |
178 | virtual void DoLogText(const wxString& msg) | |
179 | { | |
180 | wxMessageOutputBest().Output(msg); | |
181 | } | |
182 | ||
183 | private: | |
184 | wxDECLARE_NO_COPY_CLASS(wxLogOutputBest); | |
185 | }; | |
186 | ||
bc73d5ae VZ |
187 | } // anonymous namespace |
188 | ||
c801d85f KB |
189 | // ============================================================================ |
190 | // implementation | |
191 | // ============================================================================ | |
192 | ||
b568d04f | 193 | // ---------------------------------------------------------------------------- |
af588446 | 194 | // helper global functions |
b568d04f VZ |
195 | // ---------------------------------------------------------------------------- |
196 | ||
c11d62a6 VZ |
197 | void wxSafeShowMessage(const wxString& title, const wxString& text) |
198 | { | |
199 | #ifdef __WINDOWS__ | |
dc874fb4 | 200 | ::MessageBox(NULL, text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP); |
c11d62a6 | 201 | #else |
a0516656 | 202 | wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str()); |
65f06384 | 203 | fflush(stderr); |
c11d62a6 VZ |
204 | #endif |
205 | } | |
206 | ||
c801d85f KB |
207 | // ---------------------------------------------------------------------------- |
208 | // wxLog class implementation | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
dbaa16de | 211 | unsigned wxLog::LogLastRepeatIfNeeded() |
dbaa16de | 212 | { |
bc73d5ae | 213 | const unsigned count = gs_prevLog.numRepeated; |
0250efd6 | 214 | |
bc73d5ae | 215 | if ( gs_prevLog.numRepeated ) |
f9837791 VZ |
216 | { |
217 | wxString msg; | |
459b97df | 218 | #if wxUSE_INTL |
f9837791 VZ |
219 | msg.Printf(wxPLURAL("The previous message repeated once.", |
220 | "The previous message repeated %lu times.", | |
bc73d5ae VZ |
221 | gs_prevLog.numRepeated), |
222 | gs_prevLog.numRepeated); | |
459b97df | 223 | #else |
a0516656 | 224 | msg.Printf(wxS("The previous message was repeated %lu times."), |
bc73d5ae | 225 | gs_prevLog.numRepeated); |
459b97df | 226 | #endif |
bc73d5ae VZ |
227 | gs_prevLog.numRepeated = 0; |
228 | gs_prevLog.msg.clear(); | |
229 | DoLogRecord(gs_prevLog.level, msg, gs_prevLog.info); | |
f9837791 | 230 | } |
0250efd6 VZ |
231 | |
232 | return count; | |
f9837791 VZ |
233 | } |
234 | ||
235 | wxLog::~wxLog() | |
236 | { | |
6f6b48f1 VZ |
237 | // Flush() must be called before destroying the object as otherwise some |
238 | // messages could be lost | |
bc73d5ae | 239 | if ( gs_prevLog.numRepeated ) |
6f6b48f1 VZ |
240 | { |
241 | wxMessageOutputDebug().Printf | |
242 | ( | |
243 | wxS("Last repeated message (\"%s\", %lu times) wasn't output"), | |
bc73d5ae VZ |
244 | gs_prevLog.msg, |
245 | gs_prevLog.numRepeated | |
6f6b48f1 VZ |
246 | ); |
247 | } | |
f9837791 VZ |
248 | } |
249 | ||
bc73d5ae VZ |
250 | // ---------------------------------------------------------------------------- |
251 | // wxLog logging functions | |
252 | // ---------------------------------------------------------------------------- | |
253 | ||
254 | /* static */ | |
255 | void | |
256 | wxLog::OnLog(wxLogLevel level, const wxString& msg, time_t t) | |
257 | { | |
258 | wxLogRecordInfo info; | |
259 | info.timestamp = t; | |
260 | #if wxUSE_THREADS | |
261 | info.threadId = wxThread::GetCurrentId(); | |
262 | #endif // wxUSE_THREADS | |
263 | ||
264 | OnLog(level, msg, info); | |
265 | } | |
266 | ||
f9837791 | 267 | /* static */ |
bc73d5ae VZ |
268 | void |
269 | wxLog::OnLog(wxLogLevel level, | |
270 | const wxString& msg, | |
271 | const wxLogRecordInfo& info) | |
f9837791 | 272 | { |
af588446 VZ |
273 | // fatal errors can't be suppressed nor handled by the custom log target |
274 | // and always terminate the program | |
275 | if ( level == wxLOG_FatalError ) | |
f9837791 | 276 | { |
af588446 | 277 | wxSafeShowMessage(wxS("Fatal Error"), msg); |
a2d38265 | 278 | |
af588446 VZ |
279 | #ifdef __WXWINCE__ |
280 | ExitThread(3); | |
281 | #else | |
282 | abort(); | |
283 | #endif | |
284 | } | |
2064113c | 285 | |
acad886c | 286 | wxLog *logger; |
2064113c | 287 | |
232addd1 VZ |
288 | #if wxUSE_THREADS |
289 | if ( !wxThread::IsMain() ) | |
290 | { | |
acad886c VZ |
291 | logger = wxThreadInfo.logger; |
292 | if ( !logger ) | |
293 | { | |
294 | if ( ms_pLogger ) | |
295 | { | |
296 | // buffer the messages until they can be shown from the main | |
297 | // thread | |
298 | wxCriticalSectionLocker lock(GetBackgroundLogCS()); | |
232addd1 | 299 | |
acad886c | 300 | gs_bufferedLogRecords.push_back(wxLogRecord(level, msg, info)); |
232addd1 | 301 | |
acad886c VZ |
302 | // ensure that our Flush() will be called soon |
303 | wxWakeUpIdle(); | |
304 | } | |
305 | //else: we don't have any logger at all, there is no need to log | |
306 | // anything | |
232addd1 | 307 | |
acad886c VZ |
308 | return; |
309 | } | |
310 | //else: we have a thread-specific logger, we can send messages to it | |
311 | // directly | |
232addd1 | 312 | } |
acad886c | 313 | else |
232addd1 | 314 | #endif // wxUSE_THREADS |
acad886c | 315 | { |
5d7526b0 | 316 | logger = GetMainThreadActiveTarget(); |
acad886c VZ |
317 | if ( !logger ) |
318 | return; | |
319 | } | |
232addd1 | 320 | |
acad886c | 321 | logger->CallDoLogNow(level, msg, info); |
232addd1 VZ |
322 | } |
323 | ||
324 | void | |
acad886c VZ |
325 | wxLog::CallDoLogNow(wxLogLevel level, |
326 | const wxString& msg, | |
327 | const wxLogRecordInfo& info) | |
232addd1 | 328 | { |
af588446 VZ |
329 | if ( GetRepetitionCounting() ) |
330 | { | |
af588446 VZ |
331 | if ( msg == gs_prevLog.msg ) |
332 | { | |
333 | gs_prevLog.numRepeated++; | |
2064113c | 334 | |
af588446 VZ |
335 | // nothing else to do, in particular, don't log the |
336 | // repeated message | |
337 | return; | |
f9837791 | 338 | } |
af588446 | 339 | |
16b1f6fc | 340 | LogLastRepeatIfNeeded(); |
af588446 VZ |
341 | |
342 | // reset repetition counter for a new message | |
343 | gs_prevLog.msg = msg; | |
344 | gs_prevLog.level = level; | |
345 | gs_prevLog.info = info; | |
f9837791 | 346 | } |
af588446 VZ |
347 | |
348 | // handle extra data which may be passed to us by wxLogXXX() | |
349 | wxString prefix, suffix; | |
0758c46e | 350 | wxUIntPtr num = 0; |
af588446 VZ |
351 | if ( info.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE, &num) ) |
352 | { | |
b804f992 | 353 | const long err = static_cast<long>(num); |
af588446 VZ |
354 | |
355 | suffix.Printf(_(" (error %ld: %s)"), err, wxSysErrorMsg(err)); | |
356 | } | |
357 | ||
358 | #if wxUSE_LOG_TRACE | |
359 | wxString str; | |
360 | if ( level == wxLOG_Trace && info.GetStrValue(wxLOG_KEY_TRACE_MASK, &str) ) | |
361 | { | |
362 | prefix = "(" + str + ") "; | |
363 | } | |
364 | #endif // wxUSE_LOG_TRACE | |
365 | ||
232addd1 | 366 | DoLogRecord(level, prefix + msg + suffix, info); |
f9837791 VZ |
367 | } |
368 | ||
bc73d5ae VZ |
369 | void wxLog::DoLogRecord(wxLogLevel level, |
370 | const wxString& msg, | |
371 | const wxLogRecordInfo& info) | |
04662def | 372 | { |
bc73d5ae VZ |
373 | #if WXWIN_COMPATIBILITY_2_8 |
374 | // call the old DoLog() to ensure that existing custom log classes still | |
375 | // work | |
376 | // | |
377 | // as the user code could have defined it as either taking "const char *" | |
378 | // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice | |
379 | // but to call both of them | |
380 | DoLog(level, (const char*)msg.mb_str(), info.timestamp); | |
381 | DoLog(level, (const wchar_t*)msg.wc_str(), info.timestamp); | |
36a6cfd2 VZ |
382 | #else // !WXWIN_COMPATIBILITY_2_8 |
383 | wxUnusedVar(info); | |
384 | #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8 | |
bc73d5ae VZ |
385 | |
386 | ||
387 | // TODO: it would be better to extract message formatting in a separate | |
388 | // wxLogFormatter class but for now we hard code formatting here | |
389 | ||
390 | wxString prefix; | |
391 | ||
392 | // don't time stamp debug messages under MSW as debug viewers usually | |
393 | // already have an option to do it | |
394 | #ifdef __WXMSW__ | |
395 | if ( level != wxLOG_Debug && level != wxLOG_Trace ) | |
396 | #endif // __WXMSW__ | |
397 | TimeStamp(&prefix); | |
398 | ||
399 | // TODO: use the other wxLogRecordInfo fields | |
400 | ||
401 | switch ( level ) | |
402 | { | |
403 | case wxLOG_Error: | |
404 | prefix += _("Error: "); | |
405 | break; | |
406 | ||
407 | case wxLOG_Warning: | |
408 | prefix += _("Warning: "); | |
409 | break; | |
410 | ||
411 | // don't prepend "debug/trace" prefix under MSW as it goes to the debug | |
412 | // window anyhow and so can't be confused with something else | |
413 | #ifndef __WXMSW__ | |
414 | case wxLOG_Debug: | |
415 | // this prefix (as well as the one below) is intentionally not | |
416 | // translated as nobody translates debug messages anyhow | |
417 | prefix += "Debug: "; | |
418 | break; | |
419 | ||
420 | case wxLOG_Trace: | |
421 | prefix += "Trace: "; | |
422 | break; | |
423 | #endif // !__WXMSW__ | |
424 | } | |
425 | ||
426 | DoLogTextAtLevel(level, prefix + msg); | |
04662def RL |
427 | } |
428 | ||
bc73d5ae VZ |
429 | void wxLog::DoLogTextAtLevel(wxLogLevel level, const wxString& msg) |
430 | { | |
431 | // we know about debug messages (because using wxMessageOutputDebug is the | |
432 | // right thing to do in 99% of all cases and also for compatibility) but | |
433 | // anything else needs to be handled in the derived class | |
434 | if ( level == wxLOG_Debug || level == wxLOG_Trace ) | |
435 | { | |
436 | wxMessageOutputDebug().Output(msg + wxS('\n')); | |
437 | } | |
438 | else | |
439 | { | |
440 | DoLogText(msg); | |
441 | } | |
442 | } | |
dcc40ba5 | 443 | |
bc73d5ae VZ |
444 | void wxLog::DoLogText(const wxString& WXUNUSED(msg)) |
445 | { | |
446 | // in 2.8-compatible build the derived class might override DoLog() or | |
447 | // DoLogString() instead so we can't have this assert there | |
448 | #if !WXWIN_COMPATIBILITY_2_8 | |
449 | wxFAIL_MSG( "must be overridden if it is called" ); | |
450 | #endif // WXWIN_COMPATIBILITY_2_8 | |
451 | } | |
5d88a6b5 | 452 | |
fbbc4e8f VZ |
453 | #if WXWIN_COMPATIBILITY_2_8 |
454 | ||
bc73d5ae | 455 | void wxLog::DoLog(wxLogLevel WXUNUSED(level), const char *szString, time_t t) |
5d88a6b5 | 456 | { |
bc73d5ae | 457 | DoLogString(szString, t); |
5d88a6b5 VZ |
458 | } |
459 | ||
bc73d5ae | 460 | void wxLog::DoLog(wxLogLevel WXUNUSED(level), const wchar_t *wzString, time_t t) |
5d88a6b5 | 461 | { |
bc73d5ae | 462 | DoLogString(wzString, t); |
5d88a6b5 VZ |
463 | } |
464 | ||
fbbc4e8f VZ |
465 | #endif // WXWIN_COMPATIBILITY_2_8 |
466 | ||
bc73d5ae VZ |
467 | // ---------------------------------------------------------------------------- |
468 | // wxLog active target management | |
469 | // ---------------------------------------------------------------------------- | |
5d88a6b5 | 470 | |
9ec05cc9 VZ |
471 | wxLog *wxLog::GetActiveTarget() |
472 | { | |
acad886c VZ |
473 | #if wxUSE_THREADS |
474 | if ( !wxThread::IsMain() ) | |
475 | { | |
476 | // check if we have a thread-specific log target | |
477 | wxLog * const logger = wxThreadInfo.logger; | |
478 | ||
479 | // the code below should be only executed for the main thread as | |
480 | // CreateLogTarget() is not meant for auto-creating log targets for | |
481 | // worker threads so skip it in any case | |
482 | return logger ? logger : ms_pLogger; | |
483 | } | |
484 | #endif // wxUSE_THREADS | |
485 | ||
5d7526b0 VZ |
486 | return GetMainThreadActiveTarget(); |
487 | } | |
488 | ||
489 | /* static */ | |
490 | wxLog *wxLog::GetMainThreadActiveTarget() | |
491 | { | |
0fb67cd1 VZ |
492 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
493 | // prevent infinite recursion if someone calls wxLogXXX() from | |
494 | // wxApp::CreateLogTarget() | |
f644b28c | 495 | static bool s_bInGetActiveTarget = false; |
0fb67cd1 | 496 | if ( !s_bInGetActiveTarget ) { |
f644b28c | 497 | s_bInGetActiveTarget = true; |
0fb67cd1 | 498 | |
0fb67cd1 VZ |
499 | // ask the application to create a log target for us |
500 | if ( wxTheApp != NULL ) | |
dc6d5e38 | 501 | ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget(); |
0fb67cd1 | 502 | else |
55b24eb8 | 503 | ms_pLogger = new wxLogOutputBest; |
0fb67cd1 | 504 | |
f644b28c | 505 | s_bInGetActiveTarget = false; |
0fb67cd1 VZ |
506 | |
507 | // do nothing if it fails - what can we do? | |
508 | } | |
275bf4c1 | 509 | } |
c801d85f | 510 | |
0fb67cd1 | 511 | return ms_pLogger; |
c801d85f KB |
512 | } |
513 | ||
c085e333 | 514 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 515 | { |
0fb67cd1 VZ |
516 | if ( ms_pLogger != NULL ) { |
517 | // flush the old messages before changing because otherwise they might | |
518 | // get lost later if this target is not restored | |
519 | ms_pLogger->Flush(); | |
520 | } | |
c801d85f | 521 | |
0fb67cd1 VZ |
522 | wxLog *pOldLogger = ms_pLogger; |
523 | ms_pLogger = pLogger; | |
c085e333 | 524 | |
0fb67cd1 | 525 | return pOldLogger; |
c801d85f KB |
526 | } |
527 | ||
acad886c VZ |
528 | #if wxUSE_THREADS |
529 | /* static */ | |
530 | wxLog *wxLog::SetThreadActiveTarget(wxLog *logger) | |
531 | { | |
532 | wxASSERT_MSG( !wxThread::IsMain(), "use SetActiveTarget() for main thread" ); | |
533 | ||
534 | wxLog * const oldLogger = wxThreadInfo.logger; | |
535 | if ( oldLogger ) | |
536 | oldLogger->Flush(); | |
537 | ||
538 | wxThreadInfo.logger = logger; | |
539 | ||
540 | return oldLogger; | |
541 | } | |
542 | #endif // wxUSE_THREADS | |
543 | ||
36bd6902 VZ |
544 | void wxLog::DontCreateOnDemand() |
545 | { | |
f644b28c | 546 | ms_bAutoCreate = false; |
36bd6902 VZ |
547 | |
548 | // this is usually called at the end of the program and we assume that it | |
549 | // is *always* called at the end - so we free memory here to avoid false | |
550 | // memory leak reports from wxWin memory tracking code | |
551 | ClearTraceMasks(); | |
552 | } | |
553 | ||
e94cd97d DE |
554 | void wxLog::DoCreateOnDemand() |
555 | { | |
556 | ms_bAutoCreate = true; | |
557 | } | |
558 | ||
c602c59b VZ |
559 | // ---------------------------------------------------------------------------- |
560 | // wxLog components levels | |
561 | // ---------------------------------------------------------------------------- | |
562 | ||
563 | /* static */ | |
564 | void wxLog::SetComponentLevel(const wxString& component, wxLogLevel level) | |
565 | { | |
566 | if ( component.empty() ) | |
567 | { | |
568 | SetLogLevel(level); | |
569 | } | |
570 | else | |
571 | { | |
572 | wxCRIT_SECT_LOCKER(lock, GetLevelsCS()); | |
573 | ||
bc1e0d4d | 574 | GetComponentLevels()[component] = level; |
c602c59b VZ |
575 | } |
576 | } | |
577 | ||
578 | /* static */ | |
579 | wxLogLevel wxLog::GetComponentLevel(wxString component) | |
580 | { | |
581 | wxCRIT_SECT_LOCKER(lock, GetLevelsCS()); | |
582 | ||
bc1e0d4d | 583 | const wxStringToNumHashMap& componentLevels = GetComponentLevels(); |
c602c59b VZ |
584 | while ( !component.empty() ) |
585 | { | |
586 | wxStringToNumHashMap::const_iterator | |
bc1e0d4d VZ |
587 | it = componentLevels.find(component); |
588 | if ( it != componentLevels.end() ) | |
c602c59b VZ |
589 | return static_cast<wxLogLevel>(it->second); |
590 | ||
591 | component = component.BeforeLast('/'); | |
592 | } | |
593 | ||
594 | return GetLogLevel(); | |
595 | } | |
596 | ||
597 | // ---------------------------------------------------------------------------- | |
598 | // wxLog trace masks | |
599 | // ---------------------------------------------------------------------------- | |
600 | ||
55e5154d VZ |
601 | namespace |
602 | { | |
603 | ||
604 | // because IsAllowedTraceMask() may be called during static initialization | |
605 | // (this is not recommended but it may still happen, see #11592) we can't use a | |
606 | // simple static variable which might be not initialized itself just yet to | |
607 | // store the trace masks, but need this accessor function which will ensure | |
608 | // that the variable is always correctly initialized before being accessed | |
609 | // | |
610 | // notice that this doesn't make accessing it MT-safe, of course, you need to | |
611 | // serialize accesses to it using GetTraceMaskCS() for this | |
612 | wxArrayString& TraceMasks() | |
613 | { | |
614 | static wxArrayString s_traceMasks; | |
615 | ||
616 | return s_traceMasks; | |
617 | } | |
618 | ||
619 | } // anonymous namespace | |
620 | ||
621 | /* static */ const wxArrayString& wxLog::GetTraceMasks() | |
622 | { | |
623 | // because of this function signature (it returns a reference, not the | |
624 | // object), it is inherently MT-unsafe so there is no need to acquire the | |
625 | // lock here anyhow | |
626 | ||
627 | return TraceMasks(); | |
628 | } | |
629 | ||
f96233d5 VZ |
630 | void wxLog::AddTraceMask(const wxString& str) |
631 | { | |
766aecab | 632 | wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS()); |
f96233d5 | 633 | |
55e5154d | 634 | TraceMasks().push_back(str); |
f96233d5 VZ |
635 | } |
636 | ||
0fb67cd1 | 637 | void wxLog::RemoveTraceMask(const wxString& str) |
c801d85f | 638 | { |
766aecab | 639 | wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS()); |
f96233d5 | 640 | |
55e5154d | 641 | int index = TraceMasks().Index(str); |
0fb67cd1 | 642 | if ( index != wxNOT_FOUND ) |
55e5154d | 643 | TraceMasks().RemoveAt((size_t)index); |
0fb67cd1 | 644 | } |
c801d85f | 645 | |
36bd6902 VZ |
646 | void wxLog::ClearTraceMasks() |
647 | { | |
766aecab | 648 | wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS()); |
f96233d5 | 649 | |
55e5154d | 650 | TraceMasks().Clear(); |
36bd6902 VZ |
651 | } |
652 | ||
c602c59b VZ |
653 | /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask) |
654 | { | |
655 | wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS()); | |
656 | ||
55e5154d VZ |
657 | const wxArrayString& masks = GetTraceMasks(); |
658 | for ( wxArrayString::const_iterator it = masks.begin(), | |
659 | en = masks.end(); | |
660 | it != en; | |
661 | ++it ) | |
c602c59b VZ |
662 | { |
663 | if ( *it == mask) | |
664 | return true; | |
665 | } | |
666 | ||
667 | return false; | |
668 | } | |
669 | ||
670 | // ---------------------------------------------------------------------------- | |
671 | // wxLog miscellaneous other methods | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
d2e1ef19 VZ |
674 | void wxLog::TimeStamp(wxString *str) |
675 | { | |
7b2d1c74 | 676 | #if wxUSE_DATETIME |
cb296f30 | 677 | if ( !ms_timestamp.empty() ) |
d2e1ef19 VZ |
678 | { |
679 | wxChar buf[256]; | |
680 | time_t timeNow; | |
681 | (void)time(&timeNow); | |
83e8b44c VZ |
682 | |
683 | struct tm tm; | |
684 | wxStrftime(buf, WXSIZEOF(buf), | |
685 | ms_timestamp, wxLocaltime_r(&timeNow, &tm)); | |
d2e1ef19 VZ |
686 | |
687 | str->Empty(); | |
a0516656 | 688 | *str << buf << wxS(": "); |
d2e1ef19 | 689 | } |
7b2d1c74 | 690 | #endif // wxUSE_DATETIME |
d2e1ef19 VZ |
691 | } |
692 | ||
232addd1 | 693 | #if wxUSE_THREADS |
232addd1 | 694 | |
acad886c VZ |
695 | void wxLog::FlushThreadMessages() |
696 | { | |
232addd1 VZ |
697 | // check if we have queued messages from other threads |
698 | wxLogRecords bufferedLogRecords; | |
699 | ||
700 | { | |
701 | wxCriticalSectionLocker lock(GetBackgroundLogCS()); | |
702 | bufferedLogRecords.swap(gs_bufferedLogRecords); | |
703 | ||
704 | // release the lock now to not keep it while we are logging the | |
705 | // messages below, allowing background threads to run | |
706 | } | |
707 | ||
708 | if ( !bufferedLogRecords.empty() ) | |
709 | { | |
710 | for ( wxLogRecords::const_iterator it = bufferedLogRecords.begin(); | |
711 | it != bufferedLogRecords.end(); | |
712 | ++it ) | |
713 | { | |
acad886c | 714 | CallDoLogNow(it->level, it->msg, it->info); |
232addd1 VZ |
715 | } |
716 | } | |
acad886c VZ |
717 | } |
718 | ||
53ff8df7 VZ |
719 | /* static */ |
720 | bool wxLog::IsThreadLoggingEnabled() | |
721 | { | |
722 | return !wxThreadInfo.loggingDisabled; | |
723 | } | |
724 | ||
725 | /* static */ | |
726 | bool wxLog::EnableThreadLogging(bool enable) | |
727 | { | |
728 | const bool wasEnabled = !wxThreadInfo.loggingDisabled; | |
729 | wxThreadInfo.loggingDisabled = !enable; | |
730 | return wasEnabled; | |
731 | } | |
732 | ||
232addd1 VZ |
733 | #endif // wxUSE_THREADS |
734 | ||
acad886c VZ |
735 | void wxLog::Flush() |
736 | { | |
6f6b48f1 | 737 | LogLastRepeatIfNeeded(); |
c801d85f KB |
738 | } |
739 | ||
acad886c VZ |
740 | /* static */ |
741 | void wxLog::FlushActive() | |
742 | { | |
743 | if ( ms_suspendCount ) | |
744 | return; | |
745 | ||
746 | wxLog * const log = GetActiveTarget(); | |
747 | if ( log ) | |
748 | { | |
749 | #if wxUSE_THREADS | |
750 | if ( wxThread::IsMain() ) | |
751 | log->FlushThreadMessages(); | |
752 | #endif // wxUSE_THREADS | |
753 | ||
754 | log->Flush(); | |
755 | } | |
756 | } | |
757 | ||
d3fc1755 VZ |
758 | // ---------------------------------------------------------------------------- |
759 | // wxLogBuffer implementation | |
760 | // ---------------------------------------------------------------------------- | |
761 | ||
762 | void wxLogBuffer::Flush() | |
763 | { | |
232addd1 VZ |
764 | wxLog::Flush(); |
765 | ||
a23bbe93 VZ |
766 | if ( !m_str.empty() ) |
767 | { | |
768 | wxMessageOutputBest out; | |
a0516656 | 769 | out.Printf(wxS("%s"), m_str.c_str()); |
a23bbe93 VZ |
770 | m_str.clear(); |
771 | } | |
d3fc1755 VZ |
772 | } |
773 | ||
bc73d5ae | 774 | void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level, const wxString& msg) |
83250f1a | 775 | { |
711f12ef VZ |
776 | // don't put debug messages in the buffer, we don't want to show |
777 | // them to the user in a msg box, log them immediately | |
bc73d5ae | 778 | switch ( level ) |
711f12ef | 779 | { |
bc73d5ae VZ |
780 | case wxLOG_Debug: |
781 | case wxLOG_Trace: | |
782 | wxLog::DoLogTextAtLevel(level, msg); | |
783 | break; | |
83250f1a | 784 | |
bc73d5ae VZ |
785 | default: |
786 | m_str << msg << wxS("\n"); | |
83250f1a VZ |
787 | } |
788 | } | |
789 | ||
c801d85f KB |
790 | // ---------------------------------------------------------------------------- |
791 | // wxLogStderr class implementation | |
792 | // ---------------------------------------------------------------------------- | |
793 | ||
794 | wxLogStderr::wxLogStderr(FILE *fp) | |
795 | { | |
0fb67cd1 VZ |
796 | if ( fp == NULL ) |
797 | m_fp = stderr; | |
798 | else | |
799 | m_fp = fp; | |
c801d85f KB |
800 | } |
801 | ||
bc73d5ae | 802 | void wxLogStderr::DoLogText(const wxString& msg) |
c801d85f | 803 | { |
bc73d5ae | 804 | wxFputs(msg + '\n', m_fp); |
0fb67cd1 | 805 | fflush(m_fp); |
1e8a4bc2 | 806 | |
e2478fde VZ |
807 | // under GUI systems such as Windows or Mac, programs usually don't have |
808 | // stderr at all, so show the messages also somewhere else, typically in | |
809 | // the debugger window so that they go at least somewhere instead of being | |
810 | // simply lost | |
1ec5cbf3 VZ |
811 | if ( m_fp == stderr ) |
812 | { | |
e2478fde VZ |
813 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; |
814 | if ( traits && !traits->HasStderr() ) | |
815 | { | |
bc73d5ae | 816 | wxMessageOutputDebug().Output(msg + wxS('\n')); |
e2478fde | 817 | } |
03147cd0 | 818 | } |
c801d85f KB |
819 | } |
820 | ||
821 | // ---------------------------------------------------------------------------- | |
822 | // wxLogStream implementation | |
823 | // ---------------------------------------------------------------------------- | |
824 | ||
4bf78aae | 825 | #if wxUSE_STD_IOSTREAM |
65f19af1 | 826 | #include "wx/ioswrap.h" |
dd107c50 | 827 | wxLogStream::wxLogStream(wxSTD ostream *ostr) |
c801d85f | 828 | { |
0fb67cd1 | 829 | if ( ostr == NULL ) |
dd107c50 | 830 | m_ostr = &wxSTD cerr; |
0fb67cd1 VZ |
831 | else |
832 | m_ostr = ostr; | |
c801d85f KB |
833 | } |
834 | ||
bc73d5ae | 835 | void wxLogStream::DoLogText(const wxString& msg) |
c801d85f | 836 | { |
bc73d5ae | 837 | (*m_ostr) << msg << wxSTD endl; |
c801d85f | 838 | } |
0fb67cd1 | 839 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 840 | |
03147cd0 VZ |
841 | // ---------------------------------------------------------------------------- |
842 | // wxLogChain | |
843 | // ---------------------------------------------------------------------------- | |
844 | ||
845 | wxLogChain::wxLogChain(wxLog *logger) | |
846 | { | |
f644b28c | 847 | m_bPassMessages = true; |
71debe95 | 848 | |
03147cd0 VZ |
849 | m_logNew = logger; |
850 | m_logOld = wxLog::SetActiveTarget(this); | |
851 | } | |
852 | ||
199e91fb VZ |
853 | wxLogChain::~wxLogChain() |
854 | { | |
e95f8fde VZ |
855 | delete m_logOld; |
856 | ||
857 | if ( m_logNew != this ) | |
858 | delete m_logNew; | |
199e91fb VZ |
859 | } |
860 | ||
03147cd0 VZ |
861 | void wxLogChain::SetLog(wxLog *logger) |
862 | { | |
863 | if ( m_logNew != this ) | |
864 | delete m_logNew; | |
865 | ||
03147cd0 VZ |
866 | m_logNew = logger; |
867 | } | |
868 | ||
869 | void wxLogChain::Flush() | |
870 | { | |
871 | if ( m_logOld ) | |
872 | m_logOld->Flush(); | |
873 | ||
1ec5cbf3 | 874 | // be careful to avoid infinite recursion |
03147cd0 VZ |
875 | if ( m_logNew && m_logNew != this ) |
876 | m_logNew->Flush(); | |
877 | } | |
878 | ||
bc73d5ae VZ |
879 | void wxLogChain::DoLogRecord(wxLogLevel level, |
880 | const wxString& msg, | |
881 | const wxLogRecordInfo& info) | |
03147cd0 VZ |
882 | { |
883 | // let the previous logger show it | |
884 | if ( m_logOld && IsPassingMessages() ) | |
bc73d5ae | 885 | m_logOld->LogRecord(level, msg, info); |
03147cd0 | 886 | |
efce878a | 887 | // and also send it to the new one |
51eb0606 VZ |
888 | if ( m_logNew ) |
889 | { | |
890 | // don't call m_logNew->LogRecord() to avoid infinite recursion when | |
891 | // m_logNew is this object itself | |
892 | if ( m_logNew != this ) | |
893 | m_logNew->LogRecord(level, msg, info); | |
894 | else | |
895 | wxLog::DoLogRecord(level, msg, info); | |
896 | } | |
03147cd0 VZ |
897 | } |
898 | ||
93d4c1d0 VZ |
899 | #ifdef __VISUALC__ |
900 | // "'this' : used in base member initializer list" - so what? | |
901 | #pragma warning(disable:4355) | |
902 | #endif // VC++ | |
903 | ||
47fe7ff3 JS |
904 | // ---------------------------------------------------------------------------- |
905 | // wxLogInterposer | |
906 | // ---------------------------------------------------------------------------- | |
907 | ||
908 | wxLogInterposer::wxLogInterposer() | |
909 | : wxLogChain(this) | |
910 | { | |
911 | } | |
912 | ||
913 | // ---------------------------------------------------------------------------- | |
914 | // wxLogInterposerTemp | |
915 | // ---------------------------------------------------------------------------- | |
916 | ||
917 | wxLogInterposerTemp::wxLogInterposerTemp() | |
93d4c1d0 VZ |
918 | : wxLogChain(this) |
919 | { | |
766aecab | 920 | DetachOldLog(); |
93d4c1d0 VZ |
921 | } |
922 | ||
923 | #ifdef __VISUALC__ | |
924 | #pragma warning(default:4355) | |
925 | #endif // VC++ | |
926 | ||
c801d85f KB |
927 | // ============================================================================ |
928 | // Global functions/variables | |
929 | // ============================================================================ | |
930 | ||
931 | // ---------------------------------------------------------------------------- | |
932 | // static variables | |
933 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 934 | |
f9837791 | 935 | bool wxLog::ms_bRepetCounting = false; |
f9837791 | 936 | |
d3b9f782 | 937 | wxLog *wxLog::ms_pLogger = NULL; |
f644b28c WS |
938 | bool wxLog::ms_doLog = true; |
939 | bool wxLog::ms_bAutoCreate = true; | |
940 | bool wxLog::ms_bVerbose = false; | |
d2e1ef19 | 941 | |
edc73852 RD |
942 | wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default |
943 | ||
2ed3265e VZ |
944 | size_t wxLog::ms_suspendCount = 0; |
945 | ||
a0516656 | 946 | wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date |
d2e1ef19 | 947 | |
34085a0d | 948 | #if WXWIN_COMPATIBILITY_2_8 |
0fb67cd1 | 949 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
34085a0d VZ |
950 | #endif // wxDEBUG_LEVEL |
951 | ||
c801d85f KB |
952 | // ---------------------------------------------------------------------------- |
953 | // stdout error logging helper | |
954 | // ---------------------------------------------------------------------------- | |
955 | ||
956 | // helper function: wraps the message and justifies it under given position | |
957 | // (looks more pretty on the terminal). Also adds newline at the end. | |
958 | // | |
0fb67cd1 VZ |
959 | // TODO this is now disabled until I find a portable way of determining the |
960 | // terminal window size (ok, I found it but does anybody really cares?) | |
961 | #ifdef LOG_PRETTY_WRAP | |
c801d85f KB |
962 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
963 | { | |
0fb67cd1 VZ |
964 | size_t nMax = 80; // FIXME |
965 | size_t nStart = strlen(pszPrefix); | |
966 | fputs(pszPrefix, f); | |
967 | ||
968 | size_t n; | |
969 | while ( *psz != '\0' ) { | |
970 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
971 | putc(*psz++, f); | |
972 | ||
973 | // wrapped? | |
974 | if ( *psz != '\0' ) { | |
975 | /*putc('\n', f);*/ | |
976 | for ( n = 0; n < nStart; n++ ) | |
977 | putc(' ', f); | |
978 | ||
979 | // as we wrapped, squeeze all white space | |
980 | while ( isspace(*psz) ) | |
981 | psz++; | |
982 | } | |
c801d85f | 983 | } |
c801d85f | 984 | |
0fb67cd1 | 985 | putc('\n', f); |
c801d85f KB |
986 | } |
987 | #endif //LOG_PRETTY_WRAP | |
988 | ||
989 | // ---------------------------------------------------------------------------- | |
990 | // error code/error message retrieval functions | |
991 | // ---------------------------------------------------------------------------- | |
992 | ||
993 | // get error code from syste | |
994 | unsigned long wxSysErrorCode() | |
995 | { | |
8cb172b4 | 996 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
0fb67cd1 | 997 | return ::GetLastError(); |
0fb67cd1 | 998 | #else //Unix |
c801d85f | 999 | return errno; |
0fb67cd1 | 1000 | #endif //Win/Unix |
c801d85f KB |
1001 | } |
1002 | ||
1003 | // get error message from system | |
50920146 | 1004 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
c801d85f | 1005 | { |
0fb67cd1 VZ |
1006 | if ( nErrCode == 0 ) |
1007 | nErrCode = wxSysErrorCode(); | |
1008 | ||
8cb172b4 | 1009 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
2e7f3845 | 1010 | static wxChar s_szBuf[1024]; |
0fb67cd1 VZ |
1011 | |
1012 | // get error message from system | |
1013 | LPVOID lpMsgBuf; | |
d0822e56 VZ |
1014 | if ( ::FormatMessage |
1015 | ( | |
1016 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
1017 | NULL, | |
1018 | nErrCode, | |
0fb67cd1 VZ |
1019 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
1020 | (LPTSTR)&lpMsgBuf, | |
d0822e56 VZ |
1021 | 0, |
1022 | NULL | |
1023 | ) == 0 ) | |
1024 | { | |
1025 | // if this happens, something is seriously wrong, so don't use _() here | |
1026 | // for safety | |
a0516656 | 1027 | wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode); |
c4b401b6 | 1028 | return s_szBuf; |
d0822e56 VZ |
1029 | } |
1030 | ||
0fb67cd1 VZ |
1031 | |
1032 | // copy it to our buffer and free memory | |
d0822e56 | 1033 | // Crashes on SmartPhone (FIXME) |
0c44ec97 | 1034 | #if !defined(__SMARTPHONE__) /* of WinCE */ |
7448de8d WS |
1035 | if( lpMsgBuf != 0 ) |
1036 | { | |
e408bf52 | 1037 | wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf)); |
251244a0 VZ |
1038 | |
1039 | LocalFree(lpMsgBuf); | |
1040 | ||
1041 | // returned string is capitalized and ended with '\r\n' - bad | |
1042 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); | |
1043 | size_t len = wxStrlen(s_szBuf); | |
1044 | if ( len > 0 ) { | |
1045 | // truncate string | |
a0516656 VZ |
1046 | if ( s_szBuf[len - 2] == wxS('\r') ) |
1047 | s_szBuf[len - 2] = wxS('\0'); | |
251244a0 VZ |
1048 | } |
1049 | } | |
a9928e9d | 1050 | else |
2e7f3845 | 1051 | #endif // !__SMARTPHONE__ |
a9928e9d | 1052 | { |
a0516656 | 1053 | s_szBuf[0] = wxS('\0'); |
0fb67cd1 VZ |
1054 | } |
1055 | ||
1056 | return s_szBuf; | |
2e7f3845 VZ |
1057 | #else // !__WXMSW__ |
1058 | #if wxUSE_UNICODE | |
1059 | static wchar_t s_wzBuf[1024]; | |
1060 | wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode), | |
1061 | WXSIZEOF(s_wzBuf) - 1); | |
1062 | return s_wzBuf; | |
1063 | #else | |
1064 | return strerror((int)nErrCode); | |
1065 | #endif | |
1066 | #endif // __WXMSW__/!__WXMSW__ | |
c801d85f KB |
1067 | } |
1068 | ||
e2478fde | 1069 | #endif // wxUSE_LOG |