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