]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/log.h | |
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> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_LOG_H_ | |
13 | #define _WX_LOG_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // types | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | // NB: this is needed even if wxUSE_LOG == 0 | |
22 | typedef unsigned long wxLogLevel; | |
23 | ||
24 | // the trace masks have been superseded by symbolic trace constants, they're | |
25 | // for compatibility only and will be removed soon - do NOT use them | |
26 | #if WXWIN_COMPATIBILITY_2_8 | |
27 | #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete) | |
28 | #define wxTraceMessages 0x0002 // trace window messages/X callbacks | |
29 | #define wxTraceResAlloc 0x0004 // trace GDI resource allocation | |
30 | #define wxTraceRefCount 0x0008 // trace various ref counting operations | |
31 | ||
32 | #ifdef __WXMSW__ | |
33 | #define wxTraceOleCalls 0x0100 // OLE interface calls | |
34 | #endif | |
35 | ||
36 | typedef unsigned long wxTraceMask; | |
37 | #endif // WXWIN_COMPATIBILITY_2_8 | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // headers | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | #include "wx/string.h" | |
44 | #include "wx/strvararg.h" | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // forward declarations | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | class WXDLLIMPEXP_FWD_BASE wxObject; | |
51 | ||
52 | #if wxUSE_GUI | |
53 | class WXDLLIMPEXP_FWD_CORE wxFrame; | |
54 | #endif // wxUSE_GUI | |
55 | ||
56 | #if wxUSE_LOG | |
57 | ||
58 | #include "wx/arrstr.h" | |
59 | ||
60 | #ifndef __WXPALMOS5__ | |
61 | #ifndef __WXWINCE__ | |
62 | #include <time.h> // for time_t | |
63 | #endif | |
64 | #endif // ! __WXPALMOS5__ | |
65 | ||
66 | #include "wx/dynarray.h" | |
67 | #include "wx/hashmap.h" | |
68 | ||
69 | #if wxUSE_THREADS | |
70 | #include "wx/thread.h" | |
71 | #endif // wxUSE_THREADS | |
72 | ||
73 | // wxUSE_LOG_DEBUG enables the debug log messages | |
74 | #ifndef wxUSE_LOG_DEBUG | |
75 | #if wxDEBUG_LEVEL | |
76 | #define wxUSE_LOG_DEBUG 1 | |
77 | #else // !wxDEBUG_LEVEL | |
78 | #define wxUSE_LOG_DEBUG 0 | |
79 | #endif | |
80 | #endif | |
81 | ||
82 | // wxUSE_LOG_TRACE enables the trace messages, they are disabled by default | |
83 | #ifndef wxUSE_LOG_TRACE | |
84 | #if wxDEBUG_LEVEL | |
85 | #define wxUSE_LOG_TRACE 1 | |
86 | #else // !wxDEBUG_LEVEL | |
87 | #define wxUSE_LOG_TRACE 0 | |
88 | #endif | |
89 | #endif // wxUSE_LOG_TRACE | |
90 | ||
91 | // wxLOG_COMPONENT identifies the component which generated the log record and | |
92 | // can be #define'd to a user-defined value when compiling the user code to use | |
93 | // component-based filtering (see wxLog::SetComponentLevel()) | |
94 | #ifndef wxLOG_COMPONENT | |
95 | // this is a variable and not a macro in order to allow the user code to | |
96 | // just #define wxLOG_COMPONENT without #undef'ining it first | |
97 | extern WXDLLIMPEXP_DATA_BASE(const char *) wxLOG_COMPONENT; | |
98 | ||
99 | #ifdef WXBUILDING | |
100 | #define wxLOG_COMPONENT "wx" | |
101 | #endif | |
102 | #endif | |
103 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // constants | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
108 | // different standard log levels (you may also define your own) | |
109 | enum wxLogLevelValues | |
110 | { | |
111 | wxLOG_FatalError, // program can't continue, abort immediately | |
112 | wxLOG_Error, // a serious error, user must be informed about it | |
113 | wxLOG_Warning, // user is normally informed about it but may be ignored | |
114 | wxLOG_Message, // normal message (i.e. normal output of a non GUI app) | |
115 | wxLOG_Status, // informational: might go to the status line of GUI app | |
116 | wxLOG_Info, // informational message (a.k.a. 'Verbose') | |
117 | wxLOG_Debug, // never shown to the user, disabled in release mode | |
118 | wxLOG_Trace, // trace messages are also only enabled in debug mode | |
119 | wxLOG_Progress, // used for progress indicator (not yet) | |
120 | wxLOG_User = 100, // user defined levels start here | |
121 | wxLOG_Max = 10000 | |
122 | }; | |
123 | ||
124 | // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be | |
125 | // discarded unless the string "foo" has been added to the list of allowed | |
126 | // ones with AddTraceMask() | |
127 | ||
128 | #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete) | |
129 | #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks | |
130 | #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation | |
131 | #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations | |
132 | ||
133 | #ifdef __WXMSW__ | |
134 | #define wxTRACE_OleCalls wxT("ole") // OLE interface calls | |
135 | #endif | |
136 | ||
137 | #include "wx/iosfwrap.h" | |
138 | ||
139 | // ---------------------------------------------------------------------------- | |
140 | // information about a log record, i.e. unit of log output | |
141 | // ---------------------------------------------------------------------------- | |
142 | ||
143 | class wxLogRecordInfo | |
144 | { | |
145 | public: | |
146 | // default ctor creates an uninitialized object | |
147 | wxLogRecordInfo() | |
148 | { | |
149 | memset(this, 0, sizeof(*this)); | |
150 | } | |
151 | ||
152 | // normal ctor, used by wxLogger specifies the location of the log | |
153 | // statement; its time stamp and thread id are set up here | |
154 | wxLogRecordInfo(const char *filename_, | |
155 | int line_, | |
156 | const char *func_, | |
157 | const char *component_) | |
158 | { | |
159 | filename = filename_; | |
160 | func = func_; | |
161 | line = line_; | |
162 | component = component_; | |
163 | ||
164 | timestamp = time(NULL); | |
165 | ||
166 | #if wxUSE_THREADS | |
167 | threadId = wxThread::GetCurrentId(); | |
168 | #endif // wxUSE_THREADS | |
169 | ||
170 | m_data = NULL; | |
171 | } | |
172 | ||
173 | // we need to define copy ctor and assignment operator because of m_data | |
174 | wxLogRecordInfo(const wxLogRecordInfo& other) | |
175 | { | |
176 | Copy(other); | |
177 | } | |
178 | ||
179 | wxLogRecordInfo& operator=(const wxLogRecordInfo& other) | |
180 | { | |
181 | if ( &other != this ) | |
182 | { | |
183 | delete m_data; | |
184 | Copy(other); | |
185 | } | |
186 | ||
187 | return *this; | |
188 | } | |
189 | ||
190 | // dtor is non-virtual, this class is not meant to be derived from | |
191 | ~wxLogRecordInfo() | |
192 | { | |
193 | delete m_data; | |
194 | } | |
195 | ||
196 | ||
197 | // the file name and line number of the file where the log record was | |
198 | // generated, if available or NULL and 0 otherwise | |
199 | const char *filename; | |
200 | int line; | |
201 | ||
202 | // the name of the function where the log record was generated (may be NULL | |
203 | // if the compiler doesn't support __FUNCTION__) | |
204 | const char *func; | |
205 | ||
206 | // the name of the component which generated this message, may be NULL if | |
207 | // not set (i.e. wxLOG_COMPONENT not defined) | |
208 | const char *component; | |
209 | ||
210 | // time of record generation | |
211 | time_t timestamp; | |
212 | ||
213 | #if wxUSE_THREADS | |
214 | // id of the thread which logged this record | |
215 | wxThreadIdType threadId; | |
216 | #endif // wxUSE_THREADS | |
217 | ||
218 | ||
219 | // store an arbitrary value in this record context | |
220 | // | |
221 | // wxWidgets always uses keys starting with "wx.", e.g. "wx.sys_error" | |
222 | void StoreValue(const wxString& key, wxUIntPtr val) | |
223 | { | |
224 | if ( !m_data ) | |
225 | m_data = new ExtraData; | |
226 | ||
227 | m_data->numValues[key] = val; | |
228 | } | |
229 | ||
230 | void StoreValue(const wxString& key, const wxString& val) | |
231 | { | |
232 | if ( !m_data ) | |
233 | m_data = new ExtraData; | |
234 | ||
235 | m_data->strValues[key] = val; | |
236 | } | |
237 | ||
238 | ||
239 | // these functions retrieve the value of either numeric or string key, | |
240 | // return false if not found | |
241 | bool GetNumValue(const wxString& key, wxUIntPtr *val) const | |
242 | { | |
243 | if ( !m_data ) | |
244 | return false; | |
245 | ||
246 | wxStringToNumHashMap::const_iterator it = m_data->numValues.find(key); | |
247 | if ( it == m_data->numValues.end() ) | |
248 | return false; | |
249 | ||
250 | *val = it->second; | |
251 | ||
252 | return true; | |
253 | } | |
254 | ||
255 | bool GetStrValue(const wxString& key, wxString *val) const | |
256 | { | |
257 | if ( !m_data ) | |
258 | return false; | |
259 | ||
260 | wxStringToStringHashMap::const_iterator it = m_data->strValues.find(key); | |
261 | if ( it == m_data->strValues.end() ) | |
262 | return false; | |
263 | ||
264 | *val = it->second; | |
265 | ||
266 | return true; | |
267 | } | |
268 | ||
269 | private: | |
270 | void Copy(const wxLogRecordInfo& other) | |
271 | { | |
272 | memcpy(this, &other, sizeof(*this)); | |
273 | if ( other.m_data ) | |
274 | m_data = new ExtraData(*other.m_data); | |
275 | } | |
276 | ||
277 | // extra data associated with the log record: this is completely optional | |
278 | // and can be used to pass information from the log function to the log | |
279 | // sink (e.g. wxLogSysError() uses this to pass the error code) | |
280 | struct ExtraData | |
281 | { | |
282 | wxStringToNumHashMap numValues; | |
283 | wxStringToStringHashMap strValues; | |
284 | }; | |
285 | ||
286 | // NULL if not used | |
287 | ExtraData *m_data; | |
288 | }; | |
289 | ||
290 | #define wxLOG_KEY_TRACE_MASK "wx.trace_mask" | |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // log record: a unit of log output | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
296 | struct wxLogRecord | |
297 | { | |
298 | wxLogRecord(wxLogLevel level_, | |
299 | const wxString& msg_, | |
300 | const wxLogRecordInfo& info_) | |
301 | : level(level_), | |
302 | msg(msg_), | |
303 | info(info_) | |
304 | { | |
305 | } | |
306 | ||
307 | wxLogLevel level; | |
308 | wxString msg; | |
309 | wxLogRecordInfo info; | |
310 | }; | |
311 | ||
312 | // ---------------------------------------------------------------------------- | |
313 | // derive from this class to redirect (or suppress, or ...) log messages | |
314 | // normally, only a single instance of this class exists but it's not enforced | |
315 | // ---------------------------------------------------------------------------- | |
316 | ||
317 | class WXDLLIMPEXP_BASE wxLog | |
318 | { | |
319 | public: | |
320 | // ctor | |
321 | wxLog() { } | |
322 | ||
323 | // make dtor virtual for all derived classes | |
324 | virtual ~wxLog(); | |
325 | ||
326 | ||
327 | // log messages selection | |
328 | // ---------------------- | |
329 | ||
330 | // these functions allow to completely disable all log messages or disable | |
331 | // log messages at level less important than specified for the current | |
332 | // thread | |
333 | ||
334 | // is logging enabled at all now? | |
335 | static bool IsEnabled() | |
336 | { | |
337 | #if wxUSE_THREADS | |
338 | if ( !wxThread::IsMain() ) | |
339 | return IsThreadLoggingEnabled(); | |
340 | #endif // wxUSE_THREADS | |
341 | ||
342 | return ms_doLog; | |
343 | } | |
344 | ||
345 | // change the flag state, return the previous one | |
346 | static bool EnableLogging(bool enable = true) | |
347 | { | |
348 | #if wxUSE_THREADS | |
349 | if ( !wxThread::IsMain() ) | |
350 | return EnableThreadLogging(enable); | |
351 | #endif // wxUSE_THREADS | |
352 | ||
353 | bool doLogOld = ms_doLog; | |
354 | ms_doLog = enable; | |
355 | return doLogOld; | |
356 | } | |
357 | ||
358 | // return the current global log level | |
359 | static wxLogLevel GetLogLevel() { return ms_logLevel; } | |
360 | ||
361 | // set global log level: messages with level > logLevel will not be logged | |
362 | static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; } | |
363 | ||
364 | // set the log level for the given component | |
365 | static void SetComponentLevel(const wxString& component, wxLogLevel level); | |
366 | ||
367 | // return the effective log level for this component, falling back to | |
368 | // parent component and to the default global log level if necessary | |
369 | // | |
370 | // NB: component argument is passed by value and not const reference in an | |
371 | // attempt to encourage compiler to avoid an extra copy: as we modify | |
372 | // the component internally, we'd create one anyhow and like this it | |
373 | // can be avoided if the string is a temporary anyhow | |
374 | static wxLogLevel GetComponentLevel(wxString component); | |
375 | ||
376 | ||
377 | // is logging of messages from this component enabled at this level? | |
378 | // | |
379 | // usually always called with wxLOG_COMPONENT as second argument | |
380 | static bool IsLevelEnabled(wxLogLevel level, wxString component) | |
381 | { | |
382 | return IsEnabled() && level <= GetComponentLevel(component); | |
383 | } | |
384 | ||
385 | ||
386 | // enable/disable messages at wxLOG_Verbose level (only relevant if the | |
387 | // current log level is greater or equal to it) | |
388 | // | |
389 | // notice that verbose mode can be activated by the standard command-line | |
390 | // '--verbose' option | |
391 | static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; } | |
392 | ||
393 | // check if verbose messages are enabled | |
394 | static bool GetVerbose() { return ms_bVerbose; } | |
395 | ||
396 | ||
397 | // message buffering | |
398 | // ----------------- | |
399 | ||
400 | // flush shows all messages if they're not logged immediately (FILE | |
401 | // and iostream logs don't need it, but wxLogGui does to avoid showing | |
402 | // 17 modal dialogs one after another) | |
403 | virtual void Flush(); | |
404 | ||
405 | // flush the active target if any and also output any pending messages from | |
406 | // background threads | |
407 | static void FlushActive(); | |
408 | ||
409 | // only one sink is active at each moment get current log target, will call | |
410 | // wxAppTraits::CreateLogTarget() to create one if none exists | |
411 | static wxLog *GetActiveTarget(); | |
412 | ||
413 | // change log target, logger may be NULL | |
414 | static wxLog *SetActiveTarget(wxLog *logger); | |
415 | ||
416 | #if wxUSE_THREADS | |
417 | // change log target for the current thread only, shouldn't be called from | |
418 | // the main thread as it doesn't use thread-specific log target | |
419 | static wxLog *SetThreadActiveTarget(wxLog *logger); | |
420 | #endif // wxUSE_THREADS | |
421 | ||
422 | // suspend the message flushing of the main target until the next call | |
423 | // to Resume() - this is mainly for internal use (to prevent wxYield() | |
424 | // from flashing the messages) | |
425 | static void Suspend() { ms_suspendCount++; } | |
426 | ||
427 | // must be called for each Suspend()! | |
428 | static void Resume() { ms_suspendCount--; } | |
429 | ||
430 | // should GetActiveTarget() try to create a new log object if the | |
431 | // current is NULL? | |
432 | static void DontCreateOnDemand(); | |
433 | ||
434 | // Make GetActiveTarget() create a new log object again. | |
435 | static void DoCreateOnDemand(); | |
436 | ||
437 | // log the count of repeating messages instead of logging the messages | |
438 | // multiple times | |
439 | static void SetRepetitionCounting(bool bRepetCounting = true) | |
440 | { ms_bRepetCounting = bRepetCounting; } | |
441 | ||
442 | // gets duplicate counting status | |
443 | static bool GetRepetitionCounting() { return ms_bRepetCounting; } | |
444 | ||
445 | // add string trace mask | |
446 | static void AddTraceMask(const wxString& str); | |
447 | ||
448 | // add string trace mask | |
449 | static void RemoveTraceMask(const wxString& str); | |
450 | ||
451 | // remove all string trace masks | |
452 | static void ClearTraceMasks(); | |
453 | ||
454 | // get string trace masks: note that this is MT-unsafe if other threads can | |
455 | // call AddTraceMask() concurrently | |
456 | static const wxArrayString& GetTraceMasks() { return ms_aTraceMasks; } | |
457 | ||
458 | // sets the time stamp string format: this is used as strftime() format | |
459 | // string for the log targets which add time stamps to the messages; set | |
460 | // it to empty string to disable time stamping completely. | |
461 | static void SetTimestamp(const wxString& ts) { ms_timestamp = ts; } | |
462 | ||
463 | // disable time stamping of log messages | |
464 | static void DisableTimestamp() { SetTimestamp(wxEmptyString); } | |
465 | ||
466 | ||
467 | // is this trace mask in the list? | |
468 | static bool IsAllowedTraceMask(const wxString& mask); | |
469 | ||
470 | // get the current timestamp format string (maybe empty) | |
471 | static const wxString& GetTimestamp() { return ms_timestamp; } | |
472 | ||
473 | ||
474 | ||
475 | // helpers: all functions in this section are mostly for internal use only, | |
476 | // don't call them from your code even if they are not formally deprecated | |
477 | ||
478 | // put the time stamp into the string if ms_timestamp != NULL (don't | |
479 | // change it otherwise) | |
480 | static void TimeStamp(wxString *str); | |
481 | ||
482 | // these methods should only be called from derived classes DoLogRecord(), | |
483 | // DoLogTextAtLevel() and DoLogText() implementations respectively and | |
484 | // shouldn't be called directly, use logging functions instead | |
485 | void LogRecord(wxLogLevel level, | |
486 | const wxString& msg, | |
487 | const wxLogRecordInfo& info) | |
488 | { | |
489 | DoLogRecord(level, msg, info); | |
490 | } | |
491 | ||
492 | void LogTextAtLevel(wxLogLevel level, const wxString& msg) | |
493 | { | |
494 | DoLogTextAtLevel(level, msg); | |
495 | } | |
496 | ||
497 | void LogText(const wxString& msg) | |
498 | { | |
499 | DoLogText(msg); | |
500 | } | |
501 | ||
502 | // this is a helper used by wxLogXXX() functions, don't call it directly | |
503 | // and see DoLog() for function to overload in the derived classes | |
504 | static void OnLog(wxLogLevel level, | |
505 | const wxString& msg, | |
506 | const wxLogRecordInfo& info); | |
507 | ||
508 | // version called when no information about the location of the log record | |
509 | // generation is available (but the time stamp is), it mainly exists for | |
510 | // backwards compatibility, don't use it in new code | |
511 | static void OnLog(wxLogLevel level, const wxString& msg, time_t t); | |
512 | ||
513 | // a helper calling the above overload with current time | |
514 | static void OnLog(wxLogLevel level, const wxString& msg) | |
515 | { | |
516 | OnLog(level, msg, time(NULL)); | |
517 | } | |
518 | ||
519 | ||
520 | // this method exists for backwards compatibility only, don't use | |
521 | bool HasPendingMessages() const { return true; } | |
522 | ||
523 | #if WXWIN_COMPATIBILITY_2_6 | |
524 | // this function doesn't do anything any more, don't call it | |
525 | wxDEPRECATED_INLINE( | |
526 | static wxChar *SetLogBuffer(wxChar *, size_t = 0), return NULL; | |
527 | ); | |
528 | #endif // WXWIN_COMPATIBILITY_2_6 | |
529 | ||
530 | // don't use integer masks any more, use string trace masks instead | |
531 | #if WXWIN_COMPATIBILITY_2_8 | |
532 | wxDEPRECATED_INLINE( static void SetTraceMask(wxTraceMask ulMask), | |
533 | ms_ulTraceMask = ulMask; ) | |
534 | ||
535 | // this one can't be marked deprecated as it's used in our own wxLogger | |
536 | // below but it still is deprecated and shouldn't be used | |
537 | static wxTraceMask GetTraceMask() { return ms_ulTraceMask; } | |
538 | #endif // WXWIN_COMPATIBILITY_2_8 | |
539 | ||
540 | protected: | |
541 | // the logging functions that can be overridden: DoLogRecord() is called | |
542 | // for every "record", i.e. a unit of log output, to be logged and by | |
543 | // default formats the message and passes it to DoLogTextAtLevel() which in | |
544 | // turn passes it to DoLogText() by default | |
545 | ||
546 | // override this method if you want to change message formatting or do | |
547 | // dynamic filtering | |
548 | virtual void DoLogRecord(wxLogLevel level, | |
549 | const wxString& msg, | |
550 | const wxLogRecordInfo& info); | |
551 | ||
552 | // override this method to redirect output to different channels depending | |
553 | // on its level only; if even the level doesn't matter, override | |
554 | // DoLogText() instead | |
555 | virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg); | |
556 | ||
557 | // this function is not pure virtual as it might not be needed if you do | |
558 | // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly | |
559 | // but if you do not override them in your derived class you must override | |
560 | // this one as the default implementation of it simply asserts | |
561 | virtual void DoLogText(const wxString& msg); | |
562 | ||
563 | ||
564 | // the rest of the functions are for backwards compatibility only, don't | |
565 | // use them in new code; if you're updating your existing code you need to | |
566 | // switch to overriding DoLogRecord/Text() above (although as long as these | |
567 | // functions exist, log classes using them will continue to work) | |
568 | #if WXWIN_COMPATIBILITY_2_8 | |
569 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
570 | virtual void DoLog(wxLogLevel level, const char *szString, time_t t) | |
571 | ); | |
572 | ||
573 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
574 | virtual void DoLog(wxLogLevel level, const wchar_t *wzString, time_t t) | |
575 | ); | |
576 | ||
577 | // these shouldn't be used by new code | |
578 | wxDEPRECATED_BUT_USED_INTERNALLY_INLINE( | |
579 | virtual void DoLogString(const char *WXUNUSED(szString), | |
580 | time_t WXUNUSED(t)), | |
581 | wxEMPTY_PARAMETER_VALUE | |
582 | ) | |
583 | ||
584 | wxDEPRECATED_BUT_USED_INTERNALLY_INLINE( | |
585 | virtual void DoLogString(const wchar_t *WXUNUSED(wzString), | |
586 | time_t WXUNUSED(t)), | |
587 | wxEMPTY_PARAMETER_VALUE | |
588 | ) | |
589 | #endif // WXWIN_COMPATIBILITY_2_8 | |
590 | ||
591 | ||
592 | // log a message indicating the number of times the previous message was | |
593 | // repeated if previous repetition counter is strictly positive, does | |
594 | // nothing otherwise; return the old value of repetition counter | |
595 | unsigned LogLastRepeatIfNeeded(); | |
596 | ||
597 | private: | |
598 | #if wxUSE_THREADS | |
599 | // called from FlushActive() to really log any buffered messages logged | |
600 | // from the other threads | |
601 | void FlushThreadMessages(); | |
602 | ||
603 | // these functions are called for non-main thread only by IsEnabled() and | |
604 | // EnableLogging() respectively | |
605 | static bool IsThreadLoggingEnabled(); | |
606 | static bool EnableThreadLogging(bool enable = true); | |
607 | #endif // wxUSE_THREADS | |
608 | ||
609 | // get the active log target for the main thread, auto-creating it if | |
610 | // necessary | |
611 | // | |
612 | // this is called from GetActiveTarget() and OnLog() when they're called | |
613 | // from the main thread | |
614 | static wxLog *GetMainThreadActiveTarget(); | |
615 | ||
616 | // called from OnLog() if it's called from the main thread or if we have a | |
617 | // (presumably MT-safe) thread-specific logger and by FlushThreadMessages() | |
618 | // when it plays back the buffered messages logged from the other threads | |
619 | void CallDoLogNow(wxLogLevel level, | |
620 | const wxString& msg, | |
621 | const wxLogRecordInfo& info); | |
622 | ||
623 | ||
624 | // static variables | |
625 | // ---------------- | |
626 | ||
627 | // if true, don't log the same message multiple times, only log it once | |
628 | // with the number of times it was repeated | |
629 | static bool ms_bRepetCounting; | |
630 | ||
631 | static wxLog *ms_pLogger; // currently active log sink | |
632 | static bool ms_doLog; // false => all logging disabled | |
633 | static bool ms_bAutoCreate; // create new log targets on demand? | |
634 | static bool ms_bVerbose; // false => ignore LogInfo messages | |
635 | ||
636 | static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel | |
637 | ||
638 | static size_t ms_suspendCount; // if positive, logs are not flushed | |
639 | ||
640 | // format string for strftime(), if empty, time stamping log messages is | |
641 | // disabled | |
642 | static wxString ms_timestamp; | |
643 | ||
644 | #if WXWIN_COMPATIBILITY_2_8 | |
645 | static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour | |
646 | #endif // WXWIN_COMPATIBILITY_2_8 | |
647 | ||
648 | // currently enabled trace masks | |
649 | static wxArrayString ms_aTraceMasks; | |
650 | }; | |
651 | ||
652 | // ---------------------------------------------------------------------------- | |
653 | // "trivial" derivations of wxLog | |
654 | // ---------------------------------------------------------------------------- | |
655 | ||
656 | // log everything except for the debug/trace messages (which are passed to | |
657 | // wxMessageOutputDebug) to a buffer | |
658 | class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog | |
659 | { | |
660 | public: | |
661 | wxLogBuffer() { } | |
662 | ||
663 | // get the string contents with all messages logged | |
664 | const wxString& GetBuffer() const { return m_str; } | |
665 | ||
666 | // show the buffer contents to the user in the best possible way (this uses | |
667 | // wxMessageOutputMessageBox) and clear it | |
668 | virtual void Flush(); | |
669 | ||
670 | protected: | |
671 | virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg); | |
672 | ||
673 | private: | |
674 | wxString m_str; | |
675 | ||
676 | wxDECLARE_NO_COPY_CLASS(wxLogBuffer); | |
677 | }; | |
678 | ||
679 | ||
680 | // log everything to a "FILE *", stderr by default | |
681 | class WXDLLIMPEXP_BASE wxLogStderr : public wxLog | |
682 | { | |
683 | public: | |
684 | // redirect log output to a FILE | |
685 | wxLogStderr(FILE *fp = NULL); | |
686 | ||
687 | protected: | |
688 | // implement sink function | |
689 | virtual void DoLogText(const wxString& msg); | |
690 | ||
691 | FILE *m_fp; | |
692 | ||
693 | wxDECLARE_NO_COPY_CLASS(wxLogStderr); | |
694 | }; | |
695 | ||
696 | #if wxUSE_STD_IOSTREAM | |
697 | ||
698 | // log everything to an "ostream", cerr by default | |
699 | class WXDLLIMPEXP_BASE wxLogStream : public wxLog | |
700 | { | |
701 | public: | |
702 | // redirect log output to an ostream | |
703 | wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL); | |
704 | ||
705 | protected: | |
706 | // implement sink function | |
707 | virtual void DoLogText(const wxString& msg); | |
708 | ||
709 | // using ptr here to avoid including <iostream.h> from this file | |
710 | wxSTD ostream *m_ostr; | |
711 | }; | |
712 | ||
713 | #endif // wxUSE_STD_IOSTREAM | |
714 | ||
715 | // ---------------------------------------------------------------------------- | |
716 | // /dev/null log target: suppress logging until this object goes out of scope | |
717 | // ---------------------------------------------------------------------------- | |
718 | ||
719 | // example of usage: | |
720 | /* | |
721 | void Foo() | |
722 | { | |
723 | wxFile file; | |
724 | ||
725 | // wxFile.Open() normally complains if file can't be opened, we don't | |
726 | // want it | |
727 | wxLogNull logNo; | |
728 | ||
729 | if ( !file.Open("bar") ) | |
730 | ... process error ourselves ... | |
731 | ||
732 | // ~wxLogNull called, old log sink restored | |
733 | } | |
734 | */ | |
735 | class WXDLLIMPEXP_BASE wxLogNull | |
736 | { | |
737 | public: | |
738 | wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { } | |
739 | ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); } | |
740 | ||
741 | private: | |
742 | bool m_flagOld; // the previous value of the wxLog::ms_doLog | |
743 | }; | |
744 | ||
745 | // ---------------------------------------------------------------------------- | |
746 | // chaining log target: installs itself as a log target and passes all | |
747 | // messages to the real log target given to it in the ctor but also forwards | |
748 | // them to the previously active one | |
749 | // | |
750 | // note that you don't have to call SetActiveTarget() with this class, it | |
751 | // does it itself in its ctor | |
752 | // ---------------------------------------------------------------------------- | |
753 | ||
754 | class WXDLLIMPEXP_BASE wxLogChain : public wxLog | |
755 | { | |
756 | public: | |
757 | wxLogChain(wxLog *logger); | |
758 | virtual ~wxLogChain(); | |
759 | ||
760 | // change the new log target | |
761 | void SetLog(wxLog *logger); | |
762 | ||
763 | // this can be used to temporarily disable (and then reenable) passing | |
764 | // messages to the old logger (by default we do pass them) | |
765 | void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; } | |
766 | ||
767 | // are we passing the messages to the previous log target? | |
768 | bool IsPassingMessages() const { return m_bPassMessages; } | |
769 | ||
770 | // return the previous log target (may be NULL) | |
771 | wxLog *GetOldLog() const { return m_logOld; } | |
772 | ||
773 | // override base class version to flush the old logger as well | |
774 | virtual void Flush(); | |
775 | ||
776 | // call to avoid destroying the old log target | |
777 | void DetachOldLog() { m_logOld = NULL; } | |
778 | ||
779 | protected: | |
780 | // pass the record to the old logger if needed | |
781 | virtual void DoLogRecord(wxLogLevel level, | |
782 | const wxString& msg, | |
783 | const wxLogRecordInfo& info); | |
784 | ||
785 | private: | |
786 | // the current log target | |
787 | wxLog *m_logNew; | |
788 | ||
789 | // the previous log target | |
790 | wxLog *m_logOld; | |
791 | ||
792 | // do we pass the messages to the old logger? | |
793 | bool m_bPassMessages; | |
794 | ||
795 | wxDECLARE_NO_COPY_CLASS(wxLogChain); | |
796 | }; | |
797 | ||
798 | // a chain log target which uses itself as the new logger | |
799 | ||
800 | #define wxLogPassThrough wxLogInterposer | |
801 | ||
802 | class WXDLLIMPEXP_BASE wxLogInterposer : public wxLogChain | |
803 | { | |
804 | public: | |
805 | wxLogInterposer(); | |
806 | ||
807 | private: | |
808 | wxDECLARE_NO_COPY_CLASS(wxLogInterposer); | |
809 | }; | |
810 | ||
811 | // a temporary interposer which doesn't destroy the old log target | |
812 | // (calls DetachOldLog) | |
813 | ||
814 | class WXDLLIMPEXP_BASE wxLogInterposerTemp : public wxLogChain | |
815 | { | |
816 | public: | |
817 | wxLogInterposerTemp(); | |
818 | ||
819 | private: | |
820 | wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp); | |
821 | }; | |
822 | ||
823 | #if wxUSE_GUI | |
824 | // include GUI log targets: | |
825 | #include "wx/generic/logg.h" | |
826 | #endif // wxUSE_GUI | |
827 | ||
828 | // ---------------------------------------------------------------------------- | |
829 | // wxLogger | |
830 | // ---------------------------------------------------------------------------- | |
831 | ||
832 | // wxLogger is a helper class used by wxLogXXX() functions implementation, | |
833 | // don't use it directly as it's experimental and subject to change (OTOH it | |
834 | // might become public in the future if it's deemed to be useful enough) | |
835 | ||
836 | // contains information about the context from which a log message originates | |
837 | // and provides Log() vararg method which forwards to wxLog::OnLog() and passes | |
838 | // this context to it | |
839 | class wxLogger | |
840 | { | |
841 | public: | |
842 | // ctor takes the basic information about the log record | |
843 | wxLogger(wxLogLevel level, | |
844 | const char *filename, | |
845 | int line, | |
846 | const char *func, | |
847 | const char *component) | |
848 | : m_level(level), | |
849 | m_info(filename, line, func, component) | |
850 | { | |
851 | } | |
852 | ||
853 | // store extra data in our log record and return this object itself (so | |
854 | // that further calls to its functions could be chained) | |
855 | template <typename T> | |
856 | wxLogger& Store(const wxString& key, T val) | |
857 | { | |
858 | m_info.StoreValue(key, val); | |
859 | return *this; | |
860 | } | |
861 | ||
862 | // hack for "overloaded" wxLogXXX() functions: calling this method | |
863 | // indicates that we may have an extra first argument preceding the format | |
864 | // string and that if we do have it, we should store it in m_info using the | |
865 | // given key (while by default 0 value will be used) | |
866 | wxLogger& MaybeStore(const wxString& key, wxUIntPtr value = 0) | |
867 | { | |
868 | wxASSERT_MSG( m_optKey.empty(), "can only have one optional value" ); | |
869 | m_optKey = key; | |
870 | ||
871 | m_info.StoreValue(key, value); | |
872 | return *this; | |
873 | } | |
874 | ||
875 | ||
876 | // non-vararg function used by wxVLogXXX(): | |
877 | ||
878 | // log the message at the level specified in the ctor if this log message | |
879 | // is enabled | |
880 | void LogV(const wxString& format, va_list argptr) | |
881 | { | |
882 | // remember that fatal errors can't be disabled | |
883 | if ( m_level == wxLOG_FatalError || | |
884 | wxLog::IsLevelEnabled(m_level, m_info.component) ) | |
885 | DoCallOnLog(format, argptr); | |
886 | } | |
887 | ||
888 | // overloads used by functions with optional leading arguments (whose | |
889 | // values are stored in the key passed to MaybeStore()) | |
890 | void LogV(long num, const wxString& format, va_list argptr) | |
891 | { | |
892 | Store(m_optKey, num); | |
893 | ||
894 | LogV(format, argptr); | |
895 | } | |
896 | ||
897 | void LogV(void *ptr, const wxString& format, va_list argptr) | |
898 | { | |
899 | Store(m_optKey, wxPtrToUInt(ptr)); | |
900 | ||
901 | LogV(format, argptr); | |
902 | } | |
903 | ||
904 | void LogVTrace(const wxString& mask, const wxString& format, va_list argptr) | |
905 | { | |
906 | if ( !wxLog::IsAllowedTraceMask(mask) ) | |
907 | return; | |
908 | ||
909 | Store(wxLOG_KEY_TRACE_MASK, mask); | |
910 | ||
911 | LogV(format, argptr); | |
912 | } | |
913 | ||
914 | ||
915 | // vararg functions used by wxLogXXX(): | |
916 | ||
917 | // will log the message at the level specified in the ctor | |
918 | // | |
919 | // notice that this function supposes that the caller already checked that | |
920 | // the level was enabled and does no checks itself | |
921 | WX_DEFINE_VARARG_FUNC_VOID | |
922 | ( | |
923 | Log, | |
924 | 1, (const wxFormatString&), | |
925 | DoLog, DoLogUtf8 | |
926 | ) | |
927 | ||
928 | // same as Log() but with an extra numeric or pointer parameters: this is | |
929 | // used to pass an optional value by storing it in m_info under the name | |
930 | // passed to MaybeStore() and is required to support "overloaded" versions | |
931 | // of wxLogStatus() and wxLogSysError() | |
932 | WX_DEFINE_VARARG_FUNC_VOID | |
933 | ( | |
934 | Log, | |
935 | 2, (long, const wxFormatString&), | |
936 | DoLogWithNum, DoLogWithNumUtf8 | |
937 | ) | |
938 | ||
939 | // unfortunately we can't use "void *" here as we get overload ambiguities | |
940 | // with Log(wxFormatString, ...) when the first argument is a "char *" or | |
941 | // "wchar_t *" then -- so we only allow passing wxObject here, which is | |
942 | // ugly but fine in practice as this overload is only used by wxLogStatus() | |
943 | // whose first argument is a wxFrame | |
944 | WX_DEFINE_VARARG_FUNC_VOID | |
945 | ( | |
946 | Log, | |
947 | 2, (wxObject *, const wxFormatString&), | |
948 | DoLogWithPtr, DoLogWithPtrUtf8 | |
949 | ) | |
950 | ||
951 | // log the message at the level specified as its first argument | |
952 | // | |
953 | // as the macros don't have access to the level argument in this case, this | |
954 | // function does check that the level is enabled itself | |
955 | WX_DEFINE_VARARG_FUNC_VOID | |
956 | ( | |
957 | LogAtLevel, | |
958 | 2, (wxLogLevel, const wxFormatString&), | |
959 | DoLogAtLevel, DoLogAtLevelUtf8 | |
960 | ) | |
961 | ||
962 | // special versions for wxLogTrace() which is passed either string or | |
963 | // integer mask as first argument determining whether the message should be | |
964 | // logged or not | |
965 | WX_DEFINE_VARARG_FUNC_VOID | |
966 | ( | |
967 | LogTrace, | |
968 | 2, (const wxString&, const wxFormatString&), | |
969 | DoLogTrace, DoLogTraceUtf8 | |
970 | ) | |
971 | ||
972 | #if WXWIN_COMPATIBILITY_2_8 | |
973 | WX_DEFINE_VARARG_FUNC_VOID | |
974 | ( | |
975 | LogTrace, | |
976 | 2, (wxTraceMask, const wxFormatString&), | |
977 | DoLogTraceMask, DoLogTraceMaskUtf8 | |
978 | ) | |
979 | #endif // WXWIN_COMPATIBILITY_2_8 | |
980 | ||
981 | #ifdef __WATCOMC__ | |
982 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
983 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
984 | 1, (const wxString&), | |
985 | (wxFormatString(f1))) | |
986 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
987 | 1, (const wxCStrData&), | |
988 | (wxFormatString(f1))) | |
989 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
990 | 1, (const char*), | |
991 | (wxFormatString(f1))) | |
992 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
993 | 1, (const wchar_t*), | |
994 | (wxFormatString(f1))) | |
995 | ||
996 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
997 | 2, (long, const wxString&), | |
998 | (f1, wxFormatString(f2))) | |
999 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1000 | 2, (long, const wxCStrData&), | |
1001 | (f1, wxFormatString(f2))) | |
1002 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1003 | 2, (long, const char *), | |
1004 | (f1, wxFormatString(f2))) | |
1005 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1006 | 2, (long, const wchar_t *), | |
1007 | (f1, wxFormatString(f2))) | |
1008 | ||
1009 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1010 | 2, (wxObject *, const wxString&), | |
1011 | (f1, wxFormatString(f2))) | |
1012 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1013 | 2, (wxObject *, const wxCStrData&), | |
1014 | (f1, wxFormatString(f2))) | |
1015 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1016 | 2, (wxObject *, const char *), | |
1017 | (f1, wxFormatString(f2))) | |
1018 | WX_VARARG_WATCOM_WORKAROUND(void, Log, | |
1019 | 2, (wxObject *, const wchar_t *), | |
1020 | (f1, wxFormatString(f2))) | |
1021 | ||
1022 | WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel, | |
1023 | 2, (wxLogLevel, const wxString&), | |
1024 | (f1, wxFormatString(f2))) | |
1025 | WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel, | |
1026 | 2, (wxLogLevel, const wxCStrData&), | |
1027 | (f1, wxFormatString(f2))) | |
1028 | WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel, | |
1029 | 2, (wxLogLevel, const char *), | |
1030 | (f1, wxFormatString(f2))) | |
1031 | WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel, | |
1032 | 2, (wxLogLevel, const wchar_t *), | |
1033 | (f1, wxFormatString(f2))) | |
1034 | ||
1035 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1036 | 2, (const wxString&, const wxString&), | |
1037 | (f1, wxFormatString(f2))) | |
1038 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1039 | 2, (const wxString&, const wxCStrData&), | |
1040 | (f1, wxFormatString(f2))) | |
1041 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1042 | 2, (const wxString&, const char *), | |
1043 | (f1, wxFormatString(f2))) | |
1044 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1045 | 2, (const wxString&, const wchar_t *), | |
1046 | (f1, wxFormatString(f2))) | |
1047 | ||
1048 | #if WXWIN_COMPATIBILITY_2_8 | |
1049 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1050 | 2, (wxTraceMask, wxTraceMask), | |
1051 | (f1, wxFormatString(f2))) | |
1052 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1053 | 2, (wxTraceMask, const wxCStrData&), | |
1054 | (f1, wxFormatString(f2))) | |
1055 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1056 | 2, (wxTraceMask, const char *), | |
1057 | (f1, wxFormatString(f2))) | |
1058 | WX_VARARG_WATCOM_WORKAROUND(void, LogTrace, | |
1059 | 2, (wxTraceMask, const wchar_t *), | |
1060 | (f1, wxFormatString(f2))) | |
1061 | #endif // WXWIN_COMPATIBILITY_2_8 | |
1062 | #endif // __WATCOMC__ | |
1063 | ||
1064 | private: | |
1065 | #if !wxUSE_UTF8_LOCALE_ONLY | |
1066 | void DoLog(const wxChar *format, ...) | |
1067 | { | |
1068 | va_list argptr; | |
1069 | va_start(argptr, format); | |
1070 | DoCallOnLog(format, argptr); | |
1071 | va_end(argptr); | |
1072 | } | |
1073 | ||
1074 | void DoLogWithNum(long num, const wxChar *format, ...) | |
1075 | { | |
1076 | Store(m_optKey, num); | |
1077 | ||
1078 | va_list argptr; | |
1079 | va_start(argptr, format); | |
1080 | DoCallOnLog(format, argptr); | |
1081 | va_end(argptr); | |
1082 | } | |
1083 | ||
1084 | void DoLogWithPtr(void *ptr, const wxChar *format, ...) | |
1085 | { | |
1086 | Store(m_optKey, wxPtrToUInt(ptr)); | |
1087 | ||
1088 | va_list argptr; | |
1089 | va_start(argptr, format); | |
1090 | DoCallOnLog(format, argptr); | |
1091 | va_end(argptr); | |
1092 | } | |
1093 | ||
1094 | void DoLogAtLevel(wxLogLevel level, const wxChar *format, ...) | |
1095 | { | |
1096 | if ( !wxLog::IsLevelEnabled(level, m_info.component) ) | |
1097 | return; | |
1098 | ||
1099 | va_list argptr; | |
1100 | va_start(argptr, format); | |
1101 | DoCallOnLog(level, format, argptr); | |
1102 | va_end(argptr); | |
1103 | } | |
1104 | ||
1105 | void DoLogTrace(const wxString& mask, const wxChar *format, ...) | |
1106 | { | |
1107 | if ( !wxLog::IsAllowedTraceMask(mask) ) | |
1108 | return; | |
1109 | ||
1110 | Store(wxLOG_KEY_TRACE_MASK, mask); | |
1111 | ||
1112 | va_list argptr; | |
1113 | va_start(argptr, format); | |
1114 | DoCallOnLog(format, argptr); | |
1115 | va_end(argptr); | |
1116 | } | |
1117 | ||
1118 | #if WXWIN_COMPATIBILITY_2_8 | |
1119 | void DoLogTraceMask(wxTraceMask mask, const wxChar *format, ...) | |
1120 | { | |
1121 | if ( (wxLog::GetTraceMask() & mask) != mask ) | |
1122 | return; | |
1123 | ||
1124 | Store(wxLOG_KEY_TRACE_MASK, mask); | |
1125 | ||
1126 | va_list argptr; | |
1127 | va_start(argptr, format); | |
1128 | DoCallOnLog(format, argptr); | |
1129 | va_end(argptr); | |
1130 | } | |
1131 | #endif // WXWIN_COMPATIBILITY_2_8 | |
1132 | #endif // !wxUSE_UTF8_LOCALE_ONLY | |
1133 | ||
1134 | #if wxUSE_UNICODE_UTF8 | |
1135 | void DoLogUtf8(const char *format, ...) | |
1136 | { | |
1137 | va_list argptr; | |
1138 | va_start(argptr, format); | |
1139 | DoCallOnLog(format, argptr); | |
1140 | va_end(argptr); | |
1141 | } | |
1142 | ||
1143 | void DoLogWithNumUtf8(long num, const char *format, ...) | |
1144 | { | |
1145 | Store(m_optKey, num); | |
1146 | ||
1147 | va_list argptr; | |
1148 | va_start(argptr, format); | |
1149 | DoCallOnLog(format, argptr); | |
1150 | va_end(argptr); | |
1151 | } | |
1152 | ||
1153 | void DoLogWithPtrUtf8(void *ptr, const char *format, ...) | |
1154 | { | |
1155 | Store(m_optKey, wxPtrToUInt(ptr)); | |
1156 | ||
1157 | va_list argptr; | |
1158 | va_start(argptr, format); | |
1159 | DoCallOnLog(format, argptr); | |
1160 | va_end(argptr); | |
1161 | } | |
1162 | ||
1163 | void DoLogAtLevelUtf8(wxLogLevel level, const char *format, ...) | |
1164 | { | |
1165 | if ( !wxLog::IsLevelEnabled(level, m_info.component) ) | |
1166 | return; | |
1167 | ||
1168 | va_list argptr; | |
1169 | va_start(argptr, format); | |
1170 | DoCallOnLog(level, format, argptr); | |
1171 | va_end(argptr); | |
1172 | } | |
1173 | ||
1174 | void DoLogTraceUtf8(const wxString& mask, const char *format, ...) | |
1175 | { | |
1176 | if ( !wxLog::IsAllowedTraceMask(mask) ) | |
1177 | return; | |
1178 | ||
1179 | Store(wxLOG_KEY_TRACE_MASK, mask); | |
1180 | ||
1181 | va_list argptr; | |
1182 | va_start(argptr, format); | |
1183 | DoCallOnLog(format, argptr); | |
1184 | va_end(argptr); | |
1185 | } | |
1186 | ||
1187 | #if WXWIN_COMPATIBILITY_2_8 | |
1188 | void DoLogTraceMaskUtf8(wxTraceMask mask, const char *format, ...) | |
1189 | { | |
1190 | if ( (wxLog::GetTraceMask() & mask) != mask ) | |
1191 | return; | |
1192 | ||
1193 | Store(wxLOG_KEY_TRACE_MASK, mask); | |
1194 | ||
1195 | va_list argptr; | |
1196 | va_start(argptr, format); | |
1197 | DoCallOnLog(format, argptr); | |
1198 | va_end(argptr); | |
1199 | } | |
1200 | #endif // WXWIN_COMPATIBILITY_2_8 | |
1201 | #endif // wxUSE_UNICODE_UTF8 | |
1202 | ||
1203 | void DoCallOnLog(wxLogLevel level, const wxString& format, va_list argptr) | |
1204 | { | |
1205 | wxLog::OnLog(level, wxString::FormatV(format, argptr), m_info); | |
1206 | } | |
1207 | ||
1208 | void DoCallOnLog(const wxString& format, va_list argptr) | |
1209 | { | |
1210 | DoCallOnLog(m_level, format, argptr); | |
1211 | } | |
1212 | ||
1213 | ||
1214 | const wxLogLevel m_level; | |
1215 | wxLogRecordInfo m_info; | |
1216 | ||
1217 | wxString m_optKey; | |
1218 | ||
1219 | wxDECLARE_NO_COPY_CLASS(wxLogger); | |
1220 | }; | |
1221 | ||
1222 | // ============================================================================ | |
1223 | // global functions | |
1224 | // ============================================================================ | |
1225 | ||
1226 | // ---------------------------------------------------------------------------- | |
1227 | // get error code/error message from system in a portable way | |
1228 | // ---------------------------------------------------------------------------- | |
1229 | ||
1230 | // return the last system error code | |
1231 | WXDLLIMPEXP_BASE unsigned long wxSysErrorCode(); | |
1232 | ||
1233 | // return the error message for given (or last if 0) error code | |
1234 | WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0); | |
1235 | ||
1236 | // ---------------------------------------------------------------------------- | |
1237 | // define wxLog<level>() functions which can be used by application instead of | |
1238 | // stdio, iostream &c for log messages for easy redirection | |
1239 | // ---------------------------------------------------------------------------- | |
1240 | ||
1241 | /* | |
1242 | The code below is unreadable because it (unfortunately unavoidably) | |
1243 | contains a lot of macro magic but all it does is to define wxLogXXX() such | |
1244 | that you can call them as vararg functions to log a message at the | |
1245 | corresponding level. | |
1246 | ||
1247 | More precisely, it defines: | |
1248 | ||
1249 | - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions | |
1250 | taking the format string and additional vararg arguments if needed. | |
1251 | - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which | |
1252 | takes the log level explicitly. | |
1253 | - wxLogSysError(const wxString& format, ...) and wxLogSysError(long | |
1254 | err, const wxString& format, ...) which log a wxLOG_Error severity | |
1255 | message with the error message corresponding to the system error code | |
1256 | err or the last error. | |
1257 | - wxLogStatus(const wxString& format, ...) which logs the message into | |
1258 | the status bar of the main application window and its overload | |
1259 | wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it | |
1260 | into the status bar of the specified frame. | |
1261 | - wxLogTrace(Mask mask, const wxString& format, ...) which only logs | |
1262 | the message is the specified mask is enabled. This comes in two kinds: | |
1263 | Mask can be a wxString or a long. Both are deprecated. | |
1264 | ||
1265 | In addition, wxVLogXXX() versions of all the functions above are also | |
1266 | defined. They take a va_list argument instead of "...". | |
1267 | */ | |
1268 | ||
1269 | // creates wxLogger object for the current location | |
1270 | #define wxMAKE_LOGGER(level) \ | |
1271 | wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__, wxLOG_COMPONENT) | |
1272 | ||
1273 | // this macro generates the expression which logs whatever follows it in | |
1274 | // parentheses at the level specified as argument | |
1275 | #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log | |
1276 | ||
1277 | // this is the non-vararg equivalent | |
1278 | #define wxDO_LOGV(level, format, argptr) \ | |
1279 | wxMAKE_LOGGER(level).LogV(format, argptr) | |
1280 | ||
1281 | // this macro declares wxLog<level>() macro which logs whatever follows it if | |
1282 | // logging at specified level is enabled (notice that if it is false, the | |
1283 | // following arguments are not even evaluated which is good as it avoids | |
1284 | // unnecessary overhead) | |
1285 | // | |
1286 | // Note: the strange if/else construct is needed to make the following code | |
1287 | // | |
1288 | // if ( cond ) | |
1289 | // wxLogError("!!!"); | |
1290 | // else | |
1291 | // ... | |
1292 | // | |
1293 | // work as expected, without it the second "else" would match the "if" | |
1294 | // inside wxLogError(). Unfortunately code like | |
1295 | // | |
1296 | // if ( cond ) | |
1297 | // wxLogError("!!!"); | |
1298 | // | |
1299 | // now provokes "suggest explicit braces to avoid ambiguous 'else'" | |
1300 | // warnings from g++ 4.3 and later with -Wparentheses on but they can be | |
1301 | // easily fixed by adding curly braces around wxLogError() and at least | |
1302 | // the code still does do the right thing. | |
1303 | #define wxDO_LOG_IF_ENABLED(level) \ | |
1304 | if ( !wxLog::IsLevelEnabled(wxLOG_##level, wxLOG_COMPONENT) ) \ | |
1305 | {} \ | |
1306 | else \ | |
1307 | wxDO_LOG(level) | |
1308 | ||
1309 | // wxLogFatalError() is special as it can't be disabled | |
1310 | #define wxLogFatalError wxDO_LOG(FatalError) | |
1311 | #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr) | |
1312 | ||
1313 | #define wxLogError wxDO_LOG_IF_ENABLED(Error) | |
1314 | #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr) | |
1315 | ||
1316 | #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning) | |
1317 | #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr) | |
1318 | ||
1319 | #define wxLogMessage wxDO_LOG_IF_ENABLED(Message) | |
1320 | #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr) | |
1321 | ||
1322 | // this one is special as it only logs if we're in verbose mode | |
1323 | #define wxLogVerbose \ | |
1324 | if ( !(wxLog::IsLevelEnabled(wxLOG_Info, wxLOG_COMPONENT) && \ | |
1325 | wxLog::GetVerbose()) ) \ | |
1326 | {} \ | |
1327 | else \ | |
1328 | wxDO_LOG(Info) | |
1329 | #define wxVLogVerbose(format, argptr) \ | |
1330 | if ( !(wxLog::IsLevelEnabled(wxLOG_Info, wxLOG_COMPONENT) && \ | |
1331 | wxLog::GetVerbose()) ) \ | |
1332 | {} \ | |
1333 | else \ | |
1334 | wxDO_LOGV(Info, format, argptr) | |
1335 | ||
1336 | // deprecated synonyms for wxLogVerbose() and wxVLogVerbose() | |
1337 | #define wxLogInfo wxLogVerbose | |
1338 | #define wxVLogInfo wxVLogVerbose | |
1339 | ||
1340 | ||
1341 | // another special case: the level is passed as first argument of the function | |
1342 | // and so is not available to the macro | |
1343 | // | |
1344 | // notice that because of this, arguments of wxLogGeneric() are currently | |
1345 | // always evaluated, unlike for the other log functions | |
1346 | #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel | |
1347 | #define wxVLogGeneric(level, format, argptr) \ | |
1348 | if ( !wxLog::IsLevelEnabled(wxLOG_##level, wxLOG_COMPONENT) ) \ | |
1349 | {} \ | |
1350 | else \ | |
1351 | wxDO_LOGV(level, format, argptr) | |
1352 | ||
1353 | ||
1354 | // wxLogSysError() needs to stash the error code value in the log record info | |
1355 | // so it needs special handling too; additional complications arise because the | |
1356 | // error code may or not be present as the first argument | |
1357 | // | |
1358 | // notice that we unfortunately can't avoid the call to wxSysErrorCode() even | |
1359 | // though it may be unneeded if an explicit error code is passed to us because | |
1360 | // the message might not be logged immediately (e.g. it could be queued for | |
1361 | // logging from the main thread later) and so we can't to wait until it is | |
1362 | // logged to determine whether we have last error or not as it will be too late | |
1363 | // and it will have changed already by then (in fact it even changes when | |
1364 | // wxString::Format() is called because of vsnprintf() inside it so it can | |
1365 | // change even much sooner) | |
1366 | #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error" | |
1367 | ||
1368 | #define wxLogSysError \ | |
1369 | if ( !wxLog::IsLevelEnabled(wxLOG_Error, wxLOG_COMPONENT) ) \ | |
1370 | {} \ | |
1371 | else \ | |
1372 | wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE, \ | |
1373 | wxSysErrorCode()).Log | |
1374 | ||
1375 | // unfortunately we can't have overloaded macros so we can't define versions | |
1376 | // both with and without error code argument and have to rely on LogV() | |
1377 | // overloads in wxLogger to select between them | |
1378 | #define wxVLogSysError \ | |
1379 | wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE, \ | |
1380 | wxSysErrorCode()).LogV | |
1381 | ||
1382 | #if wxUSE_GUI | |
1383 | // wxLogStatus() is similar to wxLogSysError() as it allows to optionally | |
1384 | // specify the frame to which the message should go | |
1385 | #define wxLOG_KEY_FRAME "wx.frame" | |
1386 | ||
1387 | #define wxLogStatus \ | |
1388 | if ( !wxLog::IsLevelEnabled(wxLOG_Status, wxLOG_COMPONENT) ) \ | |
1389 | {} \ | |
1390 | else \ | |
1391 | wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log | |
1392 | ||
1393 | #define wxVLogStatus(format, argptr) \ | |
1394 | wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV | |
1395 | #endif // wxUSE_GUI | |
1396 | ||
1397 | ||
1398 | #else // !wxUSE_LOG | |
1399 | ||
1400 | #undef wxUSE_LOG_DEBUG | |
1401 | #define wxUSE_LOG_DEBUG 0 | |
1402 | ||
1403 | #undef wxUSE_LOG_TRACE | |
1404 | #define wxUSE_LOG_TRACE 0 | |
1405 | ||
1406 | #if defined(__WATCOMC__) || defined(__MINGW32__) | |
1407 | // Mingw has similar problem with wxLogSysError: | |
1408 | #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x | |
1409 | #else | |
1410 | #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) | |
1411 | #endif | |
1412 | ||
1413 | // define macros for defining log functions which do nothing at all | |
1414 | // | |
1415 | // WX_WATCOM_ONLY_CODE is needed to work around | |
1416 | // http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
1417 | #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \ | |
1418 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxFormatString&)) \ | |
1419 | WX_WATCOM_ONLY_CODE( \ | |
1420 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \ | |
1421 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \ | |
1422 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \ | |
1423 | ) \ | |
1424 | inline void wxVLog##level(const wxFormatString& WXUNUSED(format), \ | |
1425 | va_list WXUNUSED(argptr)) { } \ | |
1426 | ||
1427 | #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \ | |
1428 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxFormatString&)) \ | |
1429 | WX_WATCOM_OR_MINGW_ONLY_CODE( \ | |
1430 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \ | |
1431 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \ | |
1432 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \ | |
1433 | ) \ | |
1434 | inline void wxVLog##level(argclass WXUNUSED(arg), \ | |
1435 | const wxFormatString& WXUNUSED(format), \ | |
1436 | va_list WXUNUSED(argptr)) {} | |
1437 | ||
1438 | wxDEFINE_EMPTY_LOG_FUNCTION(FatalError); | |
1439 | wxDEFINE_EMPTY_LOG_FUNCTION(Error); | |
1440 | wxDEFINE_EMPTY_LOG_FUNCTION(SysError); | |
1441 | wxDEFINE_EMPTY_LOG_FUNCTION2(SysError, long); | |
1442 | wxDEFINE_EMPTY_LOG_FUNCTION(Warning); | |
1443 | wxDEFINE_EMPTY_LOG_FUNCTION(Message); | |
1444 | wxDEFINE_EMPTY_LOG_FUNCTION(Info); | |
1445 | wxDEFINE_EMPTY_LOG_FUNCTION(Verbose); | |
1446 | ||
1447 | wxDEFINE_EMPTY_LOG_FUNCTION2(Generic, wxLogLevel); | |
1448 | ||
1449 | #if wxUSE_GUI | |
1450 | wxDEFINE_EMPTY_LOG_FUNCTION(Status); | |
1451 | wxDEFINE_EMPTY_LOG_FUNCTION2(Status, wxFrame *); | |
1452 | #endif // wxUSE_GUI | |
1453 | ||
1454 | // Empty Class to fake wxLogNull | |
1455 | class WXDLLIMPEXP_BASE wxLogNull | |
1456 | { | |
1457 | public: | |
1458 | wxLogNull() { } | |
1459 | }; | |
1460 | ||
1461 | // Dummy macros to replace some functions. | |
1462 | #define wxSysErrorCode() (unsigned long)0 | |
1463 | #define wxSysErrorMsg( X ) (const wxChar*)NULL | |
1464 | ||
1465 | // Fake symbolic trace masks... for those that are used frequently | |
1466 | #define wxTRACE_OleCalls wxEmptyString // OLE interface calls | |
1467 | ||
1468 | #endif // wxUSE_LOG/!wxUSE_LOG | |
1469 | ||
1470 | ||
1471 | // debug functions can be completely disabled in optimized builds | |
1472 | ||
1473 | // if these log functions are disabled, we prefer to define them as (empty) | |
1474 | // variadic macros as this completely removes them and their argument | |
1475 | // evaluation from the object code but if this is not supported by compiler we | |
1476 | // use empty inline functions instead (defining them as nothing would result in | |
1477 | // compiler warnings) | |
1478 | // | |
1479 | // note that making wxVLogDebug/Trace() themselves (empty inline) functions is | |
1480 | // a bad idea as some compilers are stupid enough to not inline even empty | |
1481 | // functions if their parameters are complicated enough, but by defining them | |
1482 | // as an empty inline function we ensure that even dumbest compilers optimise | |
1483 | // them away | |
1484 | #ifdef __BORLANDC__ | |
1485 | // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need | |
1486 | // to define it differently for it to avoid these warnings (same problem as | |
1487 | // with wxUnusedVar()) | |
1488 | #define wxLogNop() { } | |
1489 | #else | |
1490 | inline void wxLogNop() { } | |
1491 | #endif | |
1492 | ||
1493 | #if wxUSE_LOG_DEBUG | |
1494 | #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug) | |
1495 | #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr) | |
1496 | #else // !wxUSE_LOG_DEBUG | |
1497 | #define wxVLogDebug(fmt, valist) wxLogNop() | |
1498 | ||
1499 | #ifdef HAVE_VARIADIC_MACROS | |
1500 | #define wxLogDebug(fmt, ...) wxLogNop() | |
1501 | #else // !HAVE_VARIADIC_MACROS | |
1502 | WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug, 1, (const wxFormatString&)) | |
1503 | #endif | |
1504 | #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG | |
1505 | ||
1506 | #if wxUSE_LOG_TRACE | |
1507 | #define wxLogTrace \ | |
1508 | if ( !wxLog::IsLevelEnabled(wxLOG_Trace, wxLOG_COMPONENT) ) \ | |
1509 | {} \ | |
1510 | else \ | |
1511 | wxMAKE_LOGGER(Trace).LogTrace | |
1512 | #define wxVLogTrace \ | |
1513 | if ( !wxLog::IsLevelEnabled(wxLOG_Trace, wxLOG_COMPONENT) ) \ | |
1514 | {} \ | |
1515 | else \ | |
1516 | wxMAKE_LOGGER(Trace).LogVTrace | |
1517 | #else // !wxUSE_LOG_TRACE | |
1518 | #define wxVLogTrace(mask, fmt, valist) wxLogNop() | |
1519 | ||
1520 | #ifdef HAVE_VARIADIC_MACROS | |
1521 | #define wxLogTrace(mask, fmt, ...) wxLogNop() | |
1522 | #else // !HAVE_VARIADIC_MACROS | |
1523 | #if WXWIN_COMPATIBILITY_2_8 | |
1524 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (wxTraceMask, const wxFormatString&)) | |
1525 | #endif | |
1526 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wxString&, const wxFormatString&)) | |
1527 | #ifdef __WATCOMC__ | |
1528 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
1529 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const char*, const char*)) | |
1530 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wchar_t*, const wchar_t*)) | |
1531 | #endif | |
1532 | #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS | |
1533 | #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE | |
1534 | ||
1535 | // wxLogFatalError helper: show the (fatal) error to the user in a safe way, | |
1536 | // i.e. without using wxMessageBox() for example because it could crash | |
1537 | void WXDLLIMPEXP_BASE | |
1538 | wxSafeShowMessage(const wxString& title, const wxString& text); | |
1539 | ||
1540 | // ---------------------------------------------------------------------------- | |
1541 | // debug only logging functions: use them with API name and error code | |
1542 | // ---------------------------------------------------------------------------- | |
1543 | ||
1544 | #if wxUSE_LOG_DEBUG | |
1545 | // make life easier for people using VC++ IDE: clicking on the message | |
1546 | // will take us immediately to the place of the failed API | |
1547 | #ifdef __VISUALC__ | |
1548 | #define wxLogApiError(api, rc) \ | |
1549 | wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \ | |
1550 | __FILE__, __LINE__, api, \ | |
1551 | (long)rc, wxSysErrorMsg(rc)) | |
1552 | #else // !VC++ | |
1553 | #define wxLogApiError(api, rc) \ | |
1554 | wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \ | |
1555 | wxT("error 0x%08lx (%s)."), \ | |
1556 | __FILE__, __LINE__, api, \ | |
1557 | (long)rc, wxSysErrorMsg(rc)) | |
1558 | #endif // VC++/!VC++ | |
1559 | ||
1560 | #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode()) | |
1561 | ||
1562 | #else // !wxUSE_LOG_DEBUG | |
1563 | #define wxLogApiError(api, err) wxLogNop() | |
1564 | #define wxLogLastError(api) wxLogNop() | |
1565 | #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG | |
1566 | ||
1567 | // wxCocoa has additiional trace masks | |
1568 | #if defined(__WXCOCOA__) | |
1569 | #include "wx/cocoa/log.h" | |
1570 | #endif | |
1571 | ||
1572 | #ifdef WX_WATCOM_ONLY_CODE | |
1573 | #undef WX_WATCOM_ONLY_CODE | |
1574 | #endif | |
1575 | ||
1576 | #endif // _WX_LOG_H_ | |
1577 |