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"
19 // ----------------------------------------------------------------------------
20 // forward declarations
21 // ----------------------------------------------------------------------------
23 class WXDLLEXPORT wxTextCtrl
;
24 class WXDLLEXPORT wxLogFrame
;
25 class WXDLLEXPORT wxFrame
;
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 typedef unsigned long wxTraceMask
;
32 typedef unsigned long wxLogLevel
;
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
40 #include <time.h> // for time_t
42 #include "wx/dynarray.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // different standard log levels (you may also define your own)
51 wxLOG_FatalError
, // program can't continue, abort immediately
52 wxLOG_Error
, // a serious error, user must be informed about it
53 wxLOG_Warning
, // user is normally informed about it but may be ignored
54 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
55 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
56 wxLOG_Status
, // informational: might go to the status line of GUI app
57 wxLOG_Debug
, // never shown to the user, disabled in release mode
58 wxLOG_Trace
, // trace messages are also only enabled in debug mode
59 wxLOG_Progress
, // used for progress indicator (not yet)
60 wxLOG_User
= 100 // user defined levels start here
63 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
64 // discarded unless the string "foo" has been added to the list of allowed
65 // ones with AddTraceMask()
67 #define wxTRACE_MemAlloc "memalloc" // trace memory allocation (new/delete)
68 #define wxTRACE_Messages "messages" // trace window messages/X callbacks
69 #define wxTRACE_ResAlloc "resalloc" // trace GDI resource allocation
70 #define wxTRACE_RefCount "refcount" // trace various ref counting operations
73 #define wxTRACE_OleCalls "ole" // OLE interface calls
76 // the trace masks have been superceded by symbolic trace constants, they're
77 // for compatibility only andwill be removed soon - do NOT use them
79 // meaning of different bits of the trace mask (which allows selectively
80 // enable/disable some trace messages)
81 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
82 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
83 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
84 #define wxTraceRefCount 0x0008 // trace various ref counting operations
87 #define wxTraceOleCalls 0x0100 // OLE interface calls
91 // N.B. BC++ doesn't have istream.h, ostream.h
92 # include <iostream.h>
95 # if defined(__VISUALC__) || defined(__MWERKS__)
100 // ----------------------------------------------------------------------------
101 // derive from this class to redirect (or suppress, or ...) log messages
102 // normally, only a single instance of this class exists but it's not enforced
103 // ----------------------------------------------------------------------------
105 class WXDLLEXPORT wxLog
111 // these functions allow to completely disable all log messages
112 // is logging disabled now?
113 static bool IsEnabled() { return ms_doLog
; }
114 // change the flag state, return the previous one
115 static bool EnableLogging(bool doIt
= TRUE
)
116 { bool doLogOld
= ms_doLog
; ms_doLog
= doIt
; return doLogOld
; }
118 // static sink function - see DoLog() for function to overload in the
120 static void OnLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
123 wxLog
*pLogger
= GetActiveTarget();
125 pLogger
->DoLog(level
, szString
, t
);
130 // flush shows all messages if they're not logged immediately (FILE
131 // and iostream logs don't need it, but wxGuiLog does to avoid showing
132 // 17 modal dialogs one after another)
133 virtual void Flush();
134 // call to Flush() may be optimized: call it only if this function
135 // returns true (although Flush() also returns immediately if there is
136 // no messages, this functions is more efficient because inline)
137 bool HasPendingMessages() const { return m_bHasMessages
; }
139 // only one sink is active at each moment
140 // get current log target, will call wxApp::CreateLogTarget() to
141 // create one if none exists
142 static wxLog
*GetActiveTarget();
143 // change log target, pLogger may be NULL
144 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
146 // functions controlling the default wxLog behaviour
147 // verbose mode is activated by standard command-line '-verbose'
149 void SetVerbose(bool bVerbose
= TRUE
) { m_bVerbose
= bVerbose
; }
150 // should GetActiveTarget() try to create a new log object if the
152 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
154 // trace mask (see wxTraceXXX constants for details)
155 static void SetTraceMask(wxTraceMask ulMask
) { ms_ulTraceMask
= ulMask
; }
156 // add string trace mask
157 static void AddTraceMask(const wxString
& str
) { ms_aTraceMasks
.Add(str
); }
158 // add string trace mask
159 static void RemoveTraceMask(const wxString
& str
);
162 // gets the verbose status
163 bool GetVerbose() const { return m_bVerbose
; }
165 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
166 // is this trace mask in the list?
167 static bool IsAllowedTraceMask(const wxChar
*mask
)
168 { return ms_aTraceMasks
.Index(mask
) != wxNOT_FOUND
; }
170 // make dtor virtual for all derived classes
174 bool m_bHasMessages
; // any messages in the queue?
175 bool m_bVerbose
; // FALSE => ignore LogInfo messages
177 // the logging functions that can be overriden
178 // default DoLog() prepends the time stamp and a prefix corresponding
179 // to the message to szString and then passes it to DoLogString()
180 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
181 // default DoLogString does nothing but is not pure virtual because if
182 // you override DoLog() you might not need it at all
183 virtual void DoLogString(const wxChar
*szString
, time_t t
);
189 static wxLog
*ms_pLogger
; // currently active log sink
190 static bool ms_doLog
; // FALSE => all logging disabled
191 static bool ms_bAutoCreate
; // create new log targets on demand?
193 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
194 static wxArrayString ms_aTraceMasks
; // more powerful filter for wxLogTrace
197 // ----------------------------------------------------------------------------
198 // "trivial" derivations of wxLog
199 // ----------------------------------------------------------------------------
201 // log everything to a "FILE *", stderr by default
202 class WXDLLEXPORT wxLogStderr
: public wxLog
205 // redirect log output to a FILE
206 wxLogStderr(FILE *fp
= (FILE *) NULL
);
209 // implement sink function
210 virtual void DoLogString(const wxChar
*szString
, time_t t
);
215 #if wxUSE_STD_IOSTREAM
216 // log everything to an "ostream", cerr by default
217 class WXDLLEXPORT wxLogStream
: public wxLog
220 // redirect log output to an ostream
221 wxLogStream(ostream
*ostr
= (ostream
*) NULL
);
224 // implement sink function
225 virtual void DoLogString(const wxChar
*szString
, time_t t
);
227 // using ptr here to avoid including <iostream.h> from this file
234 #if wxUSE_STD_IOSTREAM
235 // log everything to a text window (GUI only of course)
236 class WXDLLEXPORT wxLogTextCtrl
: public wxLogStream
239 // we just create an ostream from wxTextCtrl and use it in base class
240 wxLogTextCtrl(wxTextCtrl
*pTextCtrl
);
245 // ----------------------------------------------------------------------------
246 // GUI log target, the default one for wxWindows programs
247 // ----------------------------------------------------------------------------
248 class WXDLLEXPORT wxLogGui
: public wxLog
254 // show all messages that were logged since the last Flush()
255 virtual void Flush();
258 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
263 wxArrayString m_aMessages
;
264 wxArrayLong m_aTimes
;
265 bool m_bErrors
, // do we have any errors?
266 m_bWarnings
; // any warnings?
269 // ----------------------------------------------------------------------------
270 // (background) log window: this class forwards all log messages to the log
271 // target which was active when it was instantiated, but also collects them
272 // to the log window. This window has it's own menu which allows the user to
273 // close it, clear the log contents or save it to the file.
274 // ----------------------------------------------------------------------------
275 class WXDLLEXPORT wxLogWindow
: public wxLog
278 wxLogWindow(wxFrame
*pParent
, // the parent frame (can be NULL)
279 const wxChar
*szTitle
, // the title of the frame
280 bool bShow
= TRUE
, // show window immediately?
281 bool bPassToOld
= TRUE
); // pass log messages to the old target?
285 // show/hide the log window
286 void Show(bool bShow
= TRUE
);
287 // retrieve the pointer to the frame
288 wxFrame
*GetFrame() const;
291 // the previous log target (may be NULL)
292 wxLog
*GetOldLog() const { return m_pOldLog
; }
293 // are we passing the messages to the previous log target?
294 bool IsPassingMessages() const { return m_bPassMessages
; }
296 // we can pass the messages to the previous log target (we're in this mode by
297 // default: we collect all messages in the window, but also let the default
298 // processing take place)
299 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
301 // base class virtuals
302 // we don't need it ourselves, but we pass it to the previous logger
303 virtual void Flush();
306 // called immediately after the log frame creation allowing for
307 // any extra initializations
308 virtual void OnFrameCreate(wxFrame
*frame
);
309 // called right before the log frame is going to be deleted
310 virtual void OnFrameDelete(wxFrame
*frame
);
313 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
314 virtual void DoLogString(const wxChar
*szString
, time_t t
);
317 bool m_bPassMessages
; // pass messages to m_pOldLog?
318 wxLog
*m_pOldLog
; // previous log target
319 wxLogFrame
*m_pLogFrame
; // the log frame
322 #endif // wxUSE_NOGUI
324 // ----------------------------------------------------------------------------
325 // /dev/null log target: suppress logging until this object goes out of scope
326 // ----------------------------------------------------------------------------
333 // wxFile.Open() normally complains if file can't be opened, we don't want it
335 if ( !file.Open("bar") )
336 ... process error ourselves ...
338 // ~wxLogNull called, old log sink restored
341 class WXDLLEXPORT wxLogNull
344 wxLogNull() { m_flagOld
= wxLog::EnableLogging(FALSE
); }
345 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
348 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
351 // ============================================================================
353 // ============================================================================
355 // ----------------------------------------------------------------------------
356 // Log functions should be used by application instead of stdio, iostream &c
357 // for log messages for easy redirection
358 // ----------------------------------------------------------------------------
360 // are we in 'verbose' mode?
361 // (note that it's often handy to change this var manually from the
362 // debugger, thus enabling/disabling verbose reporting for some
363 // parts of the program only)
364 WXDLLEXPORT_DATA(extern bool) g_bVerbose
;
366 // ----------------------------------------------------------------------------
367 // get error code/error message from system in a portable way
368 // ----------------------------------------------------------------------------
370 // return the last system error code
371 WXDLLEXPORT
unsigned long wxSysErrorCode();
372 // return the error message for given (or last if 0) error code
373 WXDLLEXPORT
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
375 // define wxLog<level>
376 // -------------------
378 #define DECLARE_LOG_FUNCTION(level) \
379 extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
380 #define DECLARE_LOG_FUNCTION2(level, arg1) \
381 extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
385 // log functions do nothing at all
386 #define DECLARE_LOG_FUNCTION(level) \
387 inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {}
388 #define DECLARE_LOG_FUNCTION2(level, arg1) \
389 inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {}
391 #endif // wxUSE_LOG/!wxUSE_LOG
393 // a generic function for all levels (level is passes as parameter)
394 DECLARE_LOG_FUNCTION2(Generic
, wxLogLevel level
);
396 // one function per each level
397 DECLARE_LOG_FUNCTION(FatalError
);
398 DECLARE_LOG_FUNCTION(Error
);
399 DECLARE_LOG_FUNCTION(Warning
);
400 DECLARE_LOG_FUNCTION(Message
);
401 DECLARE_LOG_FUNCTION(Info
);
402 DECLARE_LOG_FUNCTION(Verbose
);
404 // this function sends the log message to the status line of the top level
405 // application frame, if any
406 DECLARE_LOG_FUNCTION(Status
);
408 // this one is the same as previous except that it allows to explicitly
409 // specify the frame to which the output should go
410 DECLARE_LOG_FUNCTION2(Status
, wxFrame
*pFrame
);
412 // additional one: as wxLogError, but also logs last system call error code
413 // and the corresponding error message if available
414 DECLARE_LOG_FUNCTION(SysError
);
416 // and another one which also takes the error code (for those broken APIs
417 // that don't set the errno (like registry APIs in Win32))
418 DECLARE_LOG_FUNCTION2(SysError
, long lErrCode
);
420 // debug functions do nothing in release mode
422 DECLARE_LOG_FUNCTION(Debug
);
424 // first king of LogTrace is uncoditional: it doesn't check the level,
425 DECLARE_LOG_FUNCTION(Trace
);
427 // this second version will only log the message if the mask had been
428 // added to the list of masks with AddTraceMask()
429 DECLARE_LOG_FUNCTION2(Trace
, const char *mask
);
431 // the last one does nothing if all of level bits are not set
432 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
433 // string identifiers
434 DECLARE_LOG_FUNCTION2(Trace
, wxTraceMask mask
);
436 // these functions do nothing in release builds
437 inline void wxLogDebug(const wxChar
*, ...) { }
438 inline void wxLogTrace(const wxChar
*, ...) { }
439 inline void wxLogTrace(wxTraceMask
, const wxChar
*, ...) { }
440 inline void wxLogTrace(const wxChar
*, const wxChar
*, ...) { }
441 #endif // debug/!debug
443 // ----------------------------------------------------------------------------
444 // debug only logging functions: use them with API name and error code
445 // ----------------------------------------------------------------------------
448 #define __XFILE__(x) _T(x)
449 #define __TFILE__ __XFILE__(__FILE__)
453 // make life easier for people using VC++ IDE: clicking on the message
454 // will take us immediately to the place of the failed API
456 #define wxLogApiError(api, rc) \
457 wxLogDebug(_T("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
458 __TFILE__, __LINE__, api, \
459 rc, wxSysErrorMsg(rc))
461 #define wxLogApiError(api, rc) \
462 wxLogDebug(_T("In file %s at line %d: '%s' failed with " \
463 "error 0x%08lx (%s)."), \
464 __TFILE__, __LINE__, api, \
465 rc, wxSysErrorMsg(rc))
468 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
471 inline void wxLogApiError(const wxChar
*, long) { }
472 inline void wxLogLastError(const wxChar
*) { }
473 #endif //debug/!debug