1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "log.h"
21 // ----------------------------------------------------------------------------
22 // forward declarations
23 // ----------------------------------------------------------------------------
25 class WXDLLEXPORT wxTextCtrl
;
26 class WXDLLEXPORT wxLogFrame
;
27 class WXDLLEXPORT wxFrame
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 typedef unsigned long wxTraceMask
;
34 typedef unsigned long wxLogLevel
;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
42 #include <time.h> // for time_t
44 #include "wx/dynarray.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // different standard log levels (you may also define your own)
53 wxLOG_FatalError
, // program can't continue, abort immediately
54 wxLOG_Error
, // a serious error, user must be informed about it
55 wxLOG_Warning
, // user is normally informed about it but may be ignored
56 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
57 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
58 wxLOG_Status
, // informational: might go to the status line of GUI app
59 wxLOG_Debug
, // never shown to the user, disabled in release mode
60 wxLOG_Trace
, // trace messages are also only enabled in debug mode
61 wxLOG_Progress
, // used for progress indicator (not yet)
62 wxLOG_User
= 100 // user defined levels start here
65 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
66 // discarded unless the string "foo" has been added to the list of allowed
67 // ones with AddTraceMask()
69 #define wxTRACE_MemAlloc "memalloc" // trace memory allocation (new/delete)
70 #define wxTRACE_Messages "messages" // trace window messages/X callbacks
71 #define wxTRACE_ResAlloc "resalloc" // trace GDI resource allocation
72 #define wxTRACE_RefCount "refcount" // trace various ref counting operations
75 #define wxTRACE_OleCalls "ole" // OLE interface calls
78 // the trace masks have been superceded by symbolic trace constants, they're
79 // for compatibility only andwill be removed soon - do NOT use them
81 // meaning of different bits of the trace mask (which allows selectively
82 // enable/disable some trace messages)
83 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
84 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
85 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
86 #define wxTraceRefCount 0x0008 // trace various ref counting operations
89 #define wxTraceOleCalls 0x0100 // OLE interface calls
92 #include "wx/ioswrap.h"
94 // ----------------------------------------------------------------------------
95 // derive from this class to redirect (or suppress, or ...) log messages
96 // normally, only a single instance of this class exists but it's not enforced
97 // ----------------------------------------------------------------------------
99 class WXDLLEXPORT wxLog
105 // these functions allow to completely disable all log messages
106 // is logging disabled now?
107 static bool IsEnabled() { return ms_doLog
; }
108 // change the flag state, return the previous one
109 static bool EnableLogging(bool doIt
= TRUE
)
110 { bool doLogOld
= ms_doLog
; ms_doLog
= doIt
; return doLogOld
; }
112 // static sink function - see DoLog() for function to overload in the
114 static void OnLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
117 wxLog
*pLogger
= GetActiveTarget();
119 pLogger
->DoLog(level
, szString
, t
);
124 // flush shows all messages if they're not logged immediately (FILE
125 // and iostream logs don't need it, but wxGuiLog does to avoid showing
126 // 17 modal dialogs one after another)
127 virtual void Flush();
128 // call to Flush() may be optimized: call it only if this function
129 // returns true (although Flush() also returns immediately if there is
130 // no messages, this functions is more efficient because inline)
131 bool HasPendingMessages() const { return m_bHasMessages
; }
133 // only one sink is active at each moment
134 // get current log target, will call wxApp::CreateLogTarget() to
135 // create one if none exists
136 static wxLog
*GetActiveTarget();
137 // change log target, pLogger may be NULL
138 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
140 // functions controlling the default wxLog behaviour
141 // verbose mode is activated by standard command-line '-verbose'
143 void SetVerbose(bool bVerbose
= TRUE
) { m_bVerbose
= bVerbose
; }
144 // should GetActiveTarget() try to create a new log object if the
146 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
148 // trace mask (see wxTraceXXX constants for details)
149 static void SetTraceMask(wxTraceMask ulMask
) { ms_ulTraceMask
= ulMask
; }
150 // add string trace mask
151 static void AddTraceMask(const wxString
& str
) { ms_aTraceMasks
.Add(str
); }
152 // add string trace mask
153 static void RemoveTraceMask(const wxString
& str
);
156 // gets the verbose status
157 bool GetVerbose() const { return m_bVerbose
; }
159 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
160 // is this trace mask in the list?
161 static bool IsAllowedTraceMask(const wxChar
*mask
)
162 { return ms_aTraceMasks
.Index(mask
) != wxNOT_FOUND
; }
164 // make dtor virtual for all derived classes
168 bool m_bHasMessages
; // any messages in the queue?
169 bool m_bVerbose
; // FALSE => ignore LogInfo messages
171 // the logging functions that can be overriden
172 // default DoLog() prepends the time stamp and a prefix corresponding
173 // to the message to szString and then passes it to DoLogString()
174 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
175 // default DoLogString does nothing but is not pure virtual because if
176 // you override DoLog() you might not need it at all
177 virtual void DoLogString(const wxChar
*szString
, time_t t
);
183 static wxLog
*ms_pLogger
; // currently active log sink
184 static bool ms_doLog
; // FALSE => all logging disabled
185 static bool ms_bAutoCreate
; // create new log targets on demand?
187 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
188 static wxArrayString ms_aTraceMasks
; // more powerful filter for wxLogTrace
191 // ----------------------------------------------------------------------------
192 // "trivial" derivations of wxLog
193 // ----------------------------------------------------------------------------
195 // log everything to a "FILE *", stderr by default
196 class WXDLLEXPORT wxLogStderr
: public wxLog
199 // redirect log output to a FILE
200 wxLogStderr(FILE *fp
= (FILE *) NULL
);
203 // implement sink function
204 virtual void DoLogString(const wxChar
*szString
, time_t t
);
209 #if wxUSE_STD_IOSTREAM
210 // log everything to an "ostream", cerr by default
211 class WXDLLEXPORT wxLogStream
: public wxLog
214 // redirect log output to an ostream
215 wxLogStream(ostream
*ostr
= (ostream
*) NULL
);
218 // implement sink function
219 virtual void DoLogString(const wxChar
*szString
, time_t t
);
221 // using ptr here to avoid including <iostream.h> from this file
228 #if wxUSE_STD_IOSTREAM
229 // log everything to a text window (GUI only of course)
230 class WXDLLEXPORT wxLogTextCtrl
: public wxLogStream
233 // we just create an ostream from wxTextCtrl and use it in base class
234 wxLogTextCtrl(wxTextCtrl
*pTextCtrl
);
239 // ----------------------------------------------------------------------------
240 // GUI log target, the default one for wxWindows programs
241 // ----------------------------------------------------------------------------
242 class WXDLLEXPORT wxLogGui
: public wxLog
248 // show all messages that were logged since the last Flush()
249 virtual void Flush();
252 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
257 wxArrayString m_aMessages
;
258 wxArrayLong m_aTimes
;
259 bool m_bErrors
, // do we have any errors?
260 m_bWarnings
; // any warnings?
263 // ----------------------------------------------------------------------------
264 // (background) log window: this class forwards all log messages to the log
265 // target which was active when it was instantiated, but also collects them
266 // to the log window. This window has it's own menu which allows the user to
267 // close it, clear the log contents or save it to the file.
268 // ----------------------------------------------------------------------------
269 class WXDLLEXPORT wxLogWindow
: public wxLog
272 wxLogWindow(wxFrame
*pParent
, // the parent frame (can be NULL)
273 const wxChar
*szTitle
, // the title of the frame
274 bool bShow
= TRUE
, // show window immediately?
275 bool bPassToOld
= TRUE
); // pass log messages to the old target?
279 // show/hide the log window
280 void Show(bool bShow
= TRUE
);
281 // retrieve the pointer to the frame
282 wxFrame
*GetFrame() const;
285 // the previous log target (may be NULL)
286 wxLog
*GetOldLog() const { return m_pOldLog
; }
287 // are we passing the messages to the previous log target?
288 bool IsPassingMessages() const { return m_bPassMessages
; }
290 // we can pass the messages to the previous log target (we're in this mode by
291 // default: we collect all messages in the window, but also let the default
292 // processing take place)
293 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
295 // base class virtuals
296 // we don't need it ourselves, but we pass it to the previous logger
297 virtual void Flush();
300 // called immediately after the log frame creation allowing for
301 // any extra initializations
302 virtual void OnFrameCreate(wxFrame
*frame
);
303 // called right before the log frame is going to be deleted
304 virtual void OnFrameDelete(wxFrame
*frame
);
307 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
308 virtual void DoLogString(const wxChar
*szString
, time_t t
);
311 bool m_bPassMessages
; // pass messages to m_pOldLog?
312 wxLog
*m_pOldLog
; // previous log target
313 wxLogFrame
*m_pLogFrame
; // the log frame
316 #endif // wxUSE_NOGUI
318 // ----------------------------------------------------------------------------
319 // /dev/null log target: suppress logging until this object goes out of scope
320 // ----------------------------------------------------------------------------
327 // wxFile.Open() normally complains if file can't be opened, we don't want it
329 if ( !file.Open("bar") )
330 ... process error ourselves ...
332 // ~wxLogNull called, old log sink restored
335 class WXDLLEXPORT wxLogNull
338 wxLogNull() { m_flagOld
= wxLog::EnableLogging(FALSE
); }
339 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
342 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
345 // ============================================================================
347 // ============================================================================
349 // ----------------------------------------------------------------------------
350 // Log functions should be used by application instead of stdio, iostream &c
351 // for log messages for easy redirection
352 // ----------------------------------------------------------------------------
354 // are we in 'verbose' mode?
355 // (note that it's often handy to change this var manually from the
356 // debugger, thus enabling/disabling verbose reporting for some
357 // parts of the program only)
358 WXDLLEXPORT_DATA(extern bool) g_bVerbose
;
360 // ----------------------------------------------------------------------------
361 // get error code/error message from system in a portable way
362 // ----------------------------------------------------------------------------
364 // return the last system error code
365 WXDLLEXPORT
unsigned long wxSysErrorCode();
366 // return the error message for given (or last if 0) error code
367 WXDLLEXPORT
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
369 // define wxLog<level>
370 // -------------------
372 #define DECLARE_LOG_FUNCTION(level) \
373 extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
374 #define DECLARE_LOG_FUNCTION2(level, arg1) \
375 extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
379 // log functions do nothing at all
380 #define DECLARE_LOG_FUNCTION(level) \
381 inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {}
382 #define DECLARE_LOG_FUNCTION2(level, arg1) \
383 inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {}
385 #endif // wxUSE_LOG/!wxUSE_LOG
387 // a generic function for all levels (level is passes as parameter)
388 DECLARE_LOG_FUNCTION2(Generic
, wxLogLevel level
);
390 // one function per each level
391 DECLARE_LOG_FUNCTION(FatalError
);
392 DECLARE_LOG_FUNCTION(Error
);
393 DECLARE_LOG_FUNCTION(Warning
);
394 DECLARE_LOG_FUNCTION(Message
);
395 DECLARE_LOG_FUNCTION(Info
);
396 DECLARE_LOG_FUNCTION(Verbose
);
398 // this function sends the log message to the status line of the top level
399 // application frame, if any
400 DECLARE_LOG_FUNCTION(Status
);
402 // this one is the same as previous except that it allows to explicitly
403 // specify the frame to which the output should go
404 DECLARE_LOG_FUNCTION2(Status
, wxFrame
*pFrame
);
406 // additional one: as wxLogError, but also logs last system call error code
407 // and the corresponding error message if available
408 DECLARE_LOG_FUNCTION(SysError
);
410 // and another one which also takes the error code (for those broken APIs
411 // that don't set the errno (like registry APIs in Win32))
412 DECLARE_LOG_FUNCTION2(SysError
, long lErrCode
);
414 // debug functions do nothing in release mode
416 DECLARE_LOG_FUNCTION(Debug
);
418 // first king of LogTrace is uncoditional: it doesn't check the level,
419 DECLARE_LOG_FUNCTION(Trace
);
421 // this second version will only log the message if the mask had been
422 // added to the list of masks with AddTraceMask()
423 DECLARE_LOG_FUNCTION2(Trace
, const char *mask
);
425 // the last one does nothing if all of level bits are not set
426 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
427 // string identifiers
428 DECLARE_LOG_FUNCTION2(Trace
, wxTraceMask mask
);
430 // these functions do nothing in release builds
431 inline void wxLogDebug(const wxChar
*, ...) { }
432 inline void wxLogTrace(const wxChar
*, ...) { }
433 inline void wxLogTrace(wxTraceMask
, const wxChar
*, ...) { }
434 inline void wxLogTrace(const wxChar
*, const wxChar
*, ...) { }
435 #endif // debug/!debug
437 // ----------------------------------------------------------------------------
438 // debug only logging functions: use them with API name and error code
439 // ----------------------------------------------------------------------------
442 #define __XFILE__(x) _T(x)
443 #define __TFILE__ __XFILE__(__FILE__)
447 // make life easier for people using VC++ IDE: clicking on the message
448 // will take us immediately to the place of the failed API
450 #define wxLogApiError(api, rc) \
451 wxLogDebug(_T("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
452 __TFILE__, __LINE__, api, \
453 rc, wxSysErrorMsg(rc))
455 #define wxLogApiError(api, rc) \
456 wxLogDebug(_T("In file %s at line %d: '%s' failed with " \
457 "error 0x%08lx (%s)."), \
458 __TFILE__, __LINE__, api, \
459 rc, wxSysErrorMsg(rc))
462 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
465 inline void wxLogApiError(const wxChar
*, long) { }
466 inline void wxLogLastError(const wxChar
*) { }
467 #endif //debug/!debug