]> git.saurik.com Git - wxWidgets.git/blob - include/wx/log.h
restored wxPluginLibrary::ms_classes handling code that was removed by mistake
[wxWidgets.git] / include / wx / log.h
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "log.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_LOG
22
23 #include "wx/string.h"
24 #include "wx/arrstr.h"
25
26 // ----------------------------------------------------------------------------
27 // forward declarations
28 // ----------------------------------------------------------------------------
29
30 #if wxUSE_GUI
31 class WXDLLIMPEXP_CORE wxTextCtrl;
32 class WXDLLIMPEXP_CORE wxLogFrame;
33 class WXDLLIMPEXP_CORE wxFrame;
34 #endif // wxUSE_GUI
35
36 // ----------------------------------------------------------------------------
37 // types
38 // ----------------------------------------------------------------------------
39
40 typedef unsigned long wxTraceMask;
41 typedef unsigned long wxLogLevel;
42
43 // ----------------------------------------------------------------------------
44 // headers
45 // ----------------------------------------------------------------------------
46
47 #ifndef __WXWINCE__
48 #include <time.h> // for time_t
49 #endif
50
51 #include "wx/dynarray.h"
52
53 #ifndef wxUSE_LOG_DEBUG
54 # ifdef __WXDEBUG__
55 # define wxUSE_LOG_DEBUG 1
56 # else // !__WXDEBUG__
57 # define wxUSE_LOG_DEBUG 0
58 # endif
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 // different standard log levels (you may also define your own)
66 enum
67 {
68 wxLOG_FatalError, // program can't continue, abort immediately
69 wxLOG_Error, // a serious error, user must be informed about it
70 wxLOG_Warning, // user is normally informed about it but may be ignored
71 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
72 wxLOG_Status, // informational: might go to the status line of GUI app
73 wxLOG_Info, // informational message (a.k.a. 'Verbose')
74 wxLOG_Debug, // never shown to the user, disabled in release mode
75 wxLOG_Trace, // trace messages are also only enabled in debug mode
76 wxLOG_Progress, // used for progress indicator (not yet)
77 wxLOG_User = 100, // user defined levels start here
78 wxLOG_Max = 10000
79 };
80
81 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
82 // discarded unless the string "foo" has been added to the list of allowed
83 // ones with AddTraceMask()
84
85 #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
86 #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
87 #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
88 #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
89
90 #ifdef __WXMSW__
91 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
92 #endif
93
94 // the trace masks have been superceded by symbolic trace constants, they're
95 // for compatibility only andwill be removed soon - do NOT use them
96
97 // meaning of different bits of the trace mask (which allows selectively
98 // enable/disable some trace messages)
99 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
100 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
101 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
102 #define wxTraceRefCount 0x0008 // trace various ref counting operations
103
104 #ifdef __WXMSW__
105 #define wxTraceOleCalls 0x0100 // OLE interface calls
106 #endif
107
108 #include "wx/iosfwrap.h"
109
110 // ----------------------------------------------------------------------------
111 // derive from this class to redirect (or suppress, or ...) log messages
112 // normally, only a single instance of this class exists but it's not enforced
113 // ----------------------------------------------------------------------------
114
115 class WXDLLIMPEXP_BASE wxLog
116 {
117 public:
118 // ctor
119 wxLog();
120
121 // Internal buffer.
122
123 // Allow replacement of the fixed size static buffer with
124 // a user allocated one. Pass in NULL to restore the
125 // built in static buffer.
126 static wxChar *SetLogBuffer( wxChar *buf, size_t size = 0 );
127
128 // these functions allow to completely disable all log messages
129
130 // is logging disabled now?
131 static bool IsEnabled() { return ms_doLog; }
132
133 // change the flag state, return the previous one
134 static bool EnableLogging(bool doIt = true)
135 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
136
137 // static sink function - see DoLog() for function to overload in the
138 // derived classes
139 static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
140 {
141 if ( IsEnabled() && ms_logLevel >= level )
142 {
143 wxLog *pLogger = GetActiveTarget();
144 if ( pLogger )
145 pLogger->DoLog(level, szString, t);
146 }
147 }
148
149 // message buffering
150
151 // flush shows all messages if they're not logged immediately (FILE
152 // and iostream logs don't need it, but wxGuiLog does to avoid showing
153 // 17 modal dialogs one after another)
154 virtual void Flush();
155
156 // flush the active target if any
157 static void FlushActive()
158 {
159 if ( !ms_suspendCount )
160 {
161 wxLog *log = GetActiveTarget();
162 if ( log )
163 log->Flush();
164 }
165 }
166
167 // only one sink is active at each moment
168 // get current log target, will call wxApp::CreateLogTarget() to
169 // create one if none exists
170 static wxLog *GetActiveTarget();
171
172 // change log target, pLogger may be NULL
173 static wxLog *SetActiveTarget(wxLog *pLogger);
174
175 // suspend the message flushing of the main target until the next call
176 // to Resume() - this is mainly for internal use (to prevent wxYield()
177 // from flashing the messages)
178 static void Suspend() { ms_suspendCount++; }
179
180 // must be called for each Suspend()!
181 static void Resume() { ms_suspendCount--; }
182
183 // functions controlling the default wxLog behaviour
184 // verbose mode is activated by standard command-line '-verbose'
185 // option
186 static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; }
187
188 // Set log level. Log messages with level > logLevel will not be logged.
189 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
190
191 // should GetActiveTarget() try to create a new log object if the
192 // current is NULL?
193 static void DontCreateOnDemand();
194
195 // trace mask (see wxTraceXXX constants for details)
196 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
197
198 // add string trace mask
199 static void AddTraceMask(const wxString& str)
200 { ms_aTraceMasks.push_back(str); }
201
202 // add string trace mask
203 static void RemoveTraceMask(const wxString& str);
204
205 // remove all string trace masks
206 static void ClearTraceMasks();
207
208 // get string trace masks
209 static const wxArrayString &GetTraceMasks() { return ms_aTraceMasks; }
210
211 // sets the timestamp string: this is used as strftime() format string
212 // for the log targets which add time stamps to the messages - set it
213 // to NULL to disable time stamping completely.
214 static void SetTimestamp(const wxChar *ts) { ms_timestamp = ts; }
215
216
217 // accessors
218
219 // gets the verbose status
220 static bool GetVerbose() { return ms_bVerbose; }
221
222 // get trace mask
223 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
224
225 // is this trace mask in the list?
226 static bool IsAllowedTraceMask(const wxChar *mask);
227
228 // return the current loglevel limit
229 static wxLogLevel GetLogLevel() { return ms_logLevel; }
230
231 // get the current timestamp format string (may be NULL)
232 static const wxChar *GetTimestamp() { return ms_timestamp; }
233
234
235 // helpers
236
237 // put the time stamp into the string if ms_timestamp != NULL (don't
238 // change it otherwise)
239 static void TimeStamp(wxString *str);
240
241 // make dtor virtual for all derived classes
242 virtual ~wxLog() { }
243
244
245 // this method exists for backwards compatibility only, don't use
246 bool HasPendingMessages() const { return true; }
247
248 protected:
249 // the logging functions that can be overriden
250
251 // default DoLog() prepends the time stamp and a prefix corresponding
252 // to the message to szString and then passes it to DoLogString()
253 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
254
255 // default DoLogString does nothing but is not pure virtual because if
256 // you override DoLog() you might not need it at all
257 virtual void DoLogString(const wxChar *szString, time_t t);
258
259 private:
260 // static variables
261 // ----------------
262
263 static wxLog *ms_pLogger; // currently active log sink
264 static bool ms_doLog; // false => all logging disabled
265 static bool ms_bAutoCreate; // create new log targets on demand?
266 static bool ms_bVerbose; // false => ignore LogInfo messages
267
268 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
269
270 static size_t ms_suspendCount; // if positive, logs are not flushed
271
272 // format string for strftime(), if NULL, time stamping log messages is
273 // disabled
274 static const wxChar *ms_timestamp;
275
276 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
277 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
278 };
279
280 // ----------------------------------------------------------------------------
281 // "trivial" derivations of wxLog
282 // ----------------------------------------------------------------------------
283
284 // log everything to a "FILE *", stderr by default
285 class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
286 {
287 DECLARE_NO_COPY_CLASS(wxLogStderr)
288
289 public:
290 // redirect log output to a FILE
291 wxLogStderr(FILE *fp = (FILE *) NULL);
292
293 protected:
294 // implement sink function
295 virtual void DoLogString(const wxChar *szString, time_t t);
296
297 FILE *m_fp;
298 };
299
300 #if wxUSE_STD_IOSTREAM
301
302 // log everything to an "ostream", cerr by default
303 class WXDLLIMPEXP_BASE wxLogStream : public wxLog
304 {
305 public:
306 // redirect log output to an ostream
307 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
308
309 protected:
310 // implement sink function
311 virtual void DoLogString(const wxChar *szString, time_t t);
312
313 // using ptr here to avoid including <iostream.h> from this file
314 wxSTD ostream *m_ostr;
315 };
316
317 #endif // wxUSE_STD_IOSTREAM
318
319 // ----------------------------------------------------------------------------
320 // /dev/null log target: suppress logging until this object goes out of scope
321 // ----------------------------------------------------------------------------
322
323 // example of usage:
324 /*
325 void Foo()
326 {
327 wxFile file;
328
329 // wxFile.Open() normally complains if file can't be opened, we don't
330 // want it
331 wxLogNull logNo;
332
333 if ( !file.Open("bar") )
334 ... process error ourselves ...
335
336 // ~wxLogNull called, old log sink restored
337 }
338 */
339 class WXDLLIMPEXP_BASE wxLogNull
340 {
341 public:
342 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
343 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
344
345 private:
346 bool m_flagOld; // the previous value of the wxLog::ms_doLog
347 };
348
349 // ----------------------------------------------------------------------------
350 // chaining log target: installs itself as a log target and passes all
351 // messages to the real log target given to it in the ctor but also forwards
352 // them to the previously active one
353 //
354 // note that you don't have to call SetActiveTarget() with this class, it
355 // does it itself in its ctor
356 // ----------------------------------------------------------------------------
357
358 class WXDLLIMPEXP_BASE wxLogChain : public wxLog
359 {
360 public:
361 wxLogChain(wxLog *logger);
362 virtual ~wxLogChain();
363
364 // change the new log target
365 void SetLog(wxLog *logger);
366
367 // this can be used to temporarily disable (and then reenable) passing
368 // messages to the old logger (by default we do pass them)
369 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
370
371 // are we passing the messages to the previous log target?
372 bool IsPassingMessages() const { return m_bPassMessages; }
373
374 // return the previous log target (may be NULL)
375 wxLog *GetOldLog() const { return m_logOld; }
376
377 // override base class version to flush the old logger as well
378 virtual void Flush();
379
380 protected:
381 // pass the chain to the old logger if needed
382 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
383
384 private:
385 // the current log target
386 wxLog *m_logNew;
387
388 // the previous log target
389 wxLog *m_logOld;
390
391 // do we pass the messages to the old logger?
392 bool m_bPassMessages;
393
394 DECLARE_NO_COPY_CLASS(wxLogChain)
395 };
396
397 // a chain log target which uses itself as the new logger
398 class WXDLLIMPEXP_BASE wxLogPassThrough : public wxLogChain
399 {
400 public:
401 wxLogPassThrough();
402
403 private:
404 DECLARE_NO_COPY_CLASS(wxLogPassThrough)
405 };
406
407 #if wxUSE_GUI
408 // include GUI log targets:
409 #include "wx/generic/logg.h"
410 #endif // wxUSE_GUI
411
412 // ============================================================================
413 // global functions
414 // ============================================================================
415
416 // ----------------------------------------------------------------------------
417 // Log functions should be used by application instead of stdio, iostream &c
418 // for log messages for easy redirection
419 // ----------------------------------------------------------------------------
420
421 // ----------------------------------------------------------------------------
422 // get error code/error message from system in a portable way
423 // ----------------------------------------------------------------------------
424
425 // return the last system error code
426 WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
427
428 // return the error message for given (or last if 0) error code
429 WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
430
431 // ----------------------------------------------------------------------------
432 // define wxLog<level>
433 // ----------------------------------------------------------------------------
434
435 #define DECLARE_LOG_FUNCTION(level) \
436 extern void WXDLLIMPEXP_BASE wxVLog##level(const wxChar *szFormat, \
437 va_list argptr); \
438 extern void WXDLLIMPEXP_BASE wxLog##level(const wxChar *szFormat, \
439 ...) ATTRIBUTE_PRINTF_1
440 #define DECLARE_LOG_FUNCTION2_EXP(level, arg, expdecl) \
441 extern void expdecl wxVLog##level(arg, const wxChar *szFormat, \
442 va_list argptr); \
443 extern void expdecl wxLog##level(arg, const wxChar *szFormat, \
444 ...) ATTRIBUTE_PRINTF_2
445 #define DECLARE_LOG_FUNCTION2(level, arg) \
446 DECLARE_LOG_FUNCTION2_EXP(level, arg, WXDLLIMPEXP_BASE)
447
448 #else // !wxUSE_LOG
449
450 // log functions do nothing at all
451 #define DECLARE_LOG_FUNCTION(level) \
452 inline void wxVLog##level(const wxChar *szFormat, \
453 va_list argptr) { } \
454 inline void wxLog##level(const wxChar *szFormat, ...) { }
455 #define DECLARE_LOG_FUNCTION2(level, arg) \
456 inline void wxVLog##level(arg, const wxChar *szFormat, \
457 va_list argptr) {} \
458 inline void wxLog##level(arg, const wxChar *szFormat, ...) { }
459
460 // Empty Class to fake wxLogNull
461 class WXDLLIMPEXP_BASE wxLogNull
462 {
463 public:
464 wxLogNull() { }
465 };
466
467 // Dummy macros to replace some functions.
468 #define wxSysErrorCode() (unsigned long)0
469 #define wxSysErrorMsg( X ) (const wxChar*)NULL
470
471 // Fake symbolic trace masks... for those that are used frequently
472 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
473
474 #endif // wxUSE_LOG/!wxUSE_LOG
475
476 // a generic function for all levels (level is passes as parameter)
477 DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
478
479 // one function per each level
480 DECLARE_LOG_FUNCTION(FatalError);
481 DECLARE_LOG_FUNCTION(Error);
482 DECLARE_LOG_FUNCTION(Warning);
483 DECLARE_LOG_FUNCTION(Message);
484 DECLARE_LOG_FUNCTION(Info);
485 DECLARE_LOG_FUNCTION(Verbose);
486
487 // this function sends the log message to the status line of the top level
488 // application frame, if any
489 DECLARE_LOG_FUNCTION(Status);
490
491 #if wxUSE_GUI
492 // this one is the same as previous except that it allows to explicitly
493 // specify the frame to which the output should go
494 DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *pFrame, WXDLLIMPEXP_CORE);
495 #endif // wxUSE_GUI
496
497 // additional one: as wxLogError, but also logs last system call error code
498 // and the corresponding error message if available
499 DECLARE_LOG_FUNCTION(SysError);
500
501 // and another one which also takes the error code (for those broken APIs
502 // that don't set the errno (like registry APIs in Win32))
503 DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
504
505 // debug functions do nothing in release mode
506 #if wxUSE_LOG_DEBUG
507 DECLARE_LOG_FUNCTION(Debug);
508
509 // there is no more unconditional LogTrace: it is not different from
510 // LogDebug and it creates overload ambiguities
511 //DECLARE_LOG_FUNCTION(Trace);
512
513 // this version only logs the message if the mask had been added to the
514 // list of masks with AddTraceMask()
515 DECLARE_LOG_FUNCTION2(Trace, const wxChar *mask);
516
517 // and this one does nothing if all of level bits are not set in
518 // wxLog::GetActive()->GetTraceMask() -- it's deprecated in favour of
519 // string identifiers
520 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
521 #else //!debug
522 // these functions do nothing in release builds
523 inline void wxVLogDebug(const wxChar *, va_list) { }
524 inline void wxLogDebug(const wxChar *, ...) { }
525 inline void wxVLogTrace(wxTraceMask, const wxChar *, va_list) { }
526 inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { }
527 inline void wxVLogTrace(const wxChar *, const wxChar *, va_list) { }
528 inline void wxLogTrace(const wxChar *, const wxChar *, ...) { }
529 #endif // debug/!debug
530
531 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
532 // i.e. without using wxMessageBox() for example because it could crash
533 void WXDLLIMPEXP_BASE
534 wxSafeShowMessage(const wxString& title, const wxString& text);
535
536 // ----------------------------------------------------------------------------
537 // debug only logging functions: use them with API name and error code
538 // ----------------------------------------------------------------------------
539
540 #ifdef __WXDEBUG__
541 // make life easier for people using VC++ IDE: clicking on the message
542 // will take us immediately to the place of the failed API
543 #ifdef __VISUALC__
544 #define wxLogApiError(api, rc) \
545 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
546 __TFILE__, __LINE__, api, \
547 (long)rc, wxSysErrorMsg(rc))
548 #else // !VC++
549 #define wxLogApiError(api, rc) \
550 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
551 wxT("error 0x%08lx (%s)."), \
552 __TFILE__, __LINE__, api, \
553 (long)rc, wxSysErrorMsg(rc))
554 #endif // VC++/!VC++
555
556 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
557
558 #else //!debug
559 inline void wxLogApiError(const wxChar *, long) { }
560 inline void wxLogLastError(const wxChar *) { }
561 #endif //debug/!debug
562
563 #endif // _WX_LOG_H_
564