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 #include <time.h> // for time_t
21 #include "wx/dynarray.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // different standard log levels (you may also define your own)
30 wxLOG_FatalError
, // program can't continue, abort immediately
31 wxLOG_Error
, // a serious error, user must be informed about it
32 wxLOG_Warning
, // user is normally informed about it but may be ignored
33 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
34 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
35 wxLOG_Status
, // informational: might go to the status line of GUI app
36 wxLOG_Debug
, // never shown to the user, disabled in release mode
37 wxLOG_Trace
, // trace messages are also only enabled in debug mode
38 wxLOG_Progress
, // used for progress indicator (not yet)
39 wxLOG_User
= 100 // user defined levels start here
42 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
43 // discarded unless the string "foo" has been added to the list of allowed
44 // ones with AddTraceMask()
46 #define wxTRACE_MemAlloc "memalloc" // trace memory allocation (new/delete)
47 #define wxTRACE_Messages "messages" // trace window messages/X callbacks
48 #define wxTRACE_ResAlloc "resalloc" // trace GDI resource allocation
49 #define wxTRACE_RefCount "refcount" // trace various ref counting operations
52 #define wxTRACE_OleCalls "ole" // OLE interface calls
55 // the trace masks have been superceded by symbolic trace constants, they're
56 // for compatibility only andwill be removed soon - do NOT use them
58 // meaning of different bits of the trace mask (which allows selectively
59 // enable/disable some trace messages)
60 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
61 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
62 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
63 #define wxTraceRefCount 0x0008 // trace various ref counting operations
66 #define wxTraceOleCalls 0x0100 // OLE interface calls
69 typedef unsigned long wxTraceMask
;
70 typedef unsigned long wxLogLevel
;
72 // ----------------------------------------------------------------------------
73 // forward declarations
74 // ----------------------------------------------------------------------------
76 class WXDLLEXPORT wxTextCtrl
;
77 class WXDLLEXPORT wxLogFrame
;
78 class WXDLLEXPORT wxFrame
;
81 // N.B. BC++ doesn't have istream.h, ostream.h
82 # include <iostream.h>
85 # if defined(__VISUALC__) || defined(__MWERKS__)
90 // ----------------------------------------------------------------------------
91 // derive from this class to redirect (or suppress, or ...) log messages
92 // normally, only a single instance of this class exists but it's not enforced
93 // ----------------------------------------------------------------------------
95 class WXDLLEXPORT wxLog
101 // these functions allow to completely disable all log messages
102 // is logging disabled now?
103 static bool IsEnabled() { return ms_doLog
; }
104 // change the flag state, return the previous one
105 static bool EnableLogging(bool doIt
= TRUE
)
106 { bool doLogOld
= ms_doLog
; ms_doLog
= doIt
; return doLogOld
; }
108 // static sink function - see DoLog() for function to overload in the
110 static void OnLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
113 wxLog
*pLogger
= GetActiveTarget();
115 pLogger
->DoLog(level
, szString
, t
);
120 // flush shows all messages if they're not logged immediately (FILE
121 // and iostream logs don't need it, but wxGuiLog does to avoid showing
122 // 17 modal dialogs one after another)
123 virtual void Flush();
124 // call to Flush() may be optimized: call it only if this function
125 // returns true (although Flush() also returns immediately if there is
126 // no messages, this functions is more efficient because inline)
127 bool HasPendingMessages() const { return m_bHasMessages
; }
129 // only one sink is active at each moment
130 // get current log target, will call wxApp::CreateLogTarget() to
131 // create one if none exists
132 static wxLog
*GetActiveTarget();
133 // change log target, pLogger may be NULL
134 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
136 // functions controlling the default wxLog behaviour
137 // verbose mode is activated by standard command-line '-verbose'
139 void SetVerbose(bool bVerbose
= TRUE
) { m_bVerbose
= bVerbose
; }
140 // should GetActiveTarget() try to create a new log object if the
142 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
144 // trace mask (see wxTraceXXX constants for details)
145 static void SetTraceMask(wxTraceMask ulMask
) { ms_ulTraceMask
= ulMask
; }
146 // add string trace mask
147 static void AddTraceMask(const wxString
& str
) { ms_aTraceMasks
.Add(str
); }
148 // add string trace mask
149 static void RemoveTraceMask(const wxString
& str
);
152 // gets the verbose status
153 bool GetVerbose() const { return m_bVerbose
; }
155 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
156 // is this trace mask in the list?
157 static bool IsAllowedTraceMask(const wxChar
*mask
)
158 { return ms_aTraceMasks
.Index(mask
) != wxNOT_FOUND
; }
160 // make dtor virtual for all derived classes
164 bool m_bHasMessages
; // any messages in the queue?
165 bool m_bVerbose
; // FALSE => ignore LogInfo messages
167 // the logging functions that can be overriden
168 // default DoLog() prepends the time stamp and a prefix corresponding
169 // to the message to szString and then passes it to DoLogString()
170 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
171 // default DoLogString does nothing but is not pure virtual because if
172 // you override DoLog() you might not need it at all
173 virtual void DoLogString(const wxChar
*szString
, time_t t
);
179 static wxLog
*ms_pLogger
; // currently active log sink
180 static bool ms_doLog
; // FALSE => all logging disabled
181 static bool ms_bAutoCreate
; // create new log targets on demand?
183 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
184 static wxArrayString ms_aTraceMasks
; // more powerful filter for wxLogTrace
187 // ----------------------------------------------------------------------------
188 // "trivial" derivations of wxLog
189 // ----------------------------------------------------------------------------
191 // log everything to a "FILE *", stderr by default
192 class WXDLLEXPORT wxLogStderr
: public wxLog
195 // redirect log output to a FILE
196 wxLogStderr(FILE *fp
= (FILE *) NULL
);
199 // implement sink function
200 virtual void DoLogString(const wxChar
*szString
, time_t t
);
205 #if wxUSE_STD_IOSTREAM
206 // log everything to an "ostream", cerr by default
207 class WXDLLEXPORT wxLogStream
: public wxLog
210 // redirect log output to an ostream
211 wxLogStream(ostream
*ostr
= (ostream
*) NULL
);
214 // implement sink function
215 virtual void DoLogString(const wxChar
*szString
, time_t t
);
217 // using ptr here to avoid including <iostream.h> from this file
224 #if wxUSE_STD_IOSTREAM
225 // log everything to a text window (GUI only of course)
226 class WXDLLEXPORT wxLogTextCtrl
: public wxLogStream
229 // we just create an ostream from wxTextCtrl and use it in base class
230 wxLogTextCtrl(wxTextCtrl
*pTextCtrl
);
235 // ----------------------------------------------------------------------------
236 // GUI log target, the default one for wxWindows programs
237 // ----------------------------------------------------------------------------
238 class WXDLLEXPORT wxLogGui
: public wxLog
244 // show all messages that were logged since the last Flush()
245 virtual void Flush();
248 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
253 wxArrayString m_aMessages
;
254 wxArrayLong m_aTimes
;
255 bool m_bErrors
, // do we have any errors?
256 m_bWarnings
; // any warnings?
259 // ----------------------------------------------------------------------------
260 // (background) log window: this class forwards all log messages to the log
261 // target which was active when it was instantiated, but also collects them
262 // to the log window. This window has it's own menu which allows the user to
263 // close it, clear the log contents or save it to the file.
264 // ----------------------------------------------------------------------------
265 class WXDLLEXPORT wxLogWindow
: public wxLog
268 wxLogWindow(wxFrame
*pParent
, // the parent frame (can be NULL)
269 const wxChar
*szTitle
, // the title of the frame
270 bool bShow
= TRUE
, // show window immediately?
271 bool bPassToOld
= TRUE
); // pass log messages to the old target?
275 // show/hide the log window
276 void Show(bool bShow
= TRUE
);
277 // retrieve the pointer to the frame
278 wxFrame
*GetFrame() const;
281 // the previous log target (may be NULL)
282 wxLog
*GetOldLog() const { return m_pOldLog
; }
283 // are we passing the messages to the previous log target?
284 bool IsPassingMessages() const { return m_bPassMessages
; }
286 // we can pass the messages to the previous log target (we're in this mode by
287 // default: we collect all messages in the window, but also let the default
288 // processing take place)
289 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
291 // base class virtuals
292 // we don't need it ourselves, but we pass it to the previous logger
293 virtual void Flush();
296 // called immediately after the log frame creation allowing for
297 // any extra initializations
298 virtual void OnFrameCreate(wxFrame
*frame
);
299 // called right before the log frame is going to be deleted
300 virtual void OnFrameDelete(wxFrame
*frame
);
303 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
);
304 virtual void DoLogString(const wxChar
*szString
, time_t t
);
307 bool m_bPassMessages
; // pass messages to m_pOldLog?
308 wxLog
*m_pOldLog
; // previous log target
309 wxLogFrame
*m_pLogFrame
; // the log frame
312 #endif // wxUSE_NOGUI
314 // ----------------------------------------------------------------------------
315 // /dev/null log target: suppress logging until this object goes out of scope
316 // ----------------------------------------------------------------------------
323 // wxFile.Open() normally complains if file can't be opened, we don't want it
325 if ( !file.Open("bar") )
326 ... process error ourselves ...
328 // ~wxLogNull called, old log sink restored
331 class WXDLLEXPORT wxLogNull
334 wxLogNull() { m_flagOld
= wxLog::EnableLogging(FALSE
); }
335 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
338 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
341 // ============================================================================
343 // ============================================================================
345 // ----------------------------------------------------------------------------
346 // Log functions should be used by application instead of stdio, iostream &c
347 // for log messages for easy redirection
348 // ----------------------------------------------------------------------------
350 // define wxLog<level>
351 // -------------------
353 #define DECLARE_LOG_FUNCTION(level) \
354 extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
355 #define DECLARE_LOG_FUNCTION2(level, arg1) \
356 extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
358 // a generic function for all levels (level is passes as parameter)
359 DECLARE_LOG_FUNCTION2(Generic
, wxLogLevel level
);
361 // one function per each level
362 DECLARE_LOG_FUNCTION(FatalError
);
363 DECLARE_LOG_FUNCTION(Error
);
364 DECLARE_LOG_FUNCTION(Warning
);
365 DECLARE_LOG_FUNCTION(Message
);
366 DECLARE_LOG_FUNCTION(Info
);
367 DECLARE_LOG_FUNCTION(Verbose
);
369 // this function sends the log message to the status line of the top level
370 // application frame, if any
371 DECLARE_LOG_FUNCTION(Status
);
373 // this one is the same as previous except that it allows to explicitly
374 // specify the frame to which the output should go
375 DECLARE_LOG_FUNCTION2(Status
, wxFrame
*pFrame
);
377 // additional one: as wxLogError, but also logs last system call error code
378 // and the corresponding error message if available
379 DECLARE_LOG_FUNCTION(SysError
);
381 // and another one which also takes the error code (for those broken APIs
382 // that don't set the errno (like registry APIs in Win32))
383 DECLARE_LOG_FUNCTION2(SysError
, long lErrCode
);
385 // debug functions do nothing in release mode
387 DECLARE_LOG_FUNCTION(Debug
);
389 // first king of LogTrace is uncoditional: it doesn't check the level,
390 DECLARE_LOG_FUNCTION(Trace
);
392 // this second version will only log the message if the mask had been
393 // added to the list of masks with AddTraceMask()
394 DECLARE_LOG_FUNCTION2(Trace
, const char *mask
);
396 // the last one does nothing if all of level bits are not set
397 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
398 // string identifiers
399 DECLARE_LOG_FUNCTION2(Trace
, wxTraceMask mask
);
401 // these functions do nothing in release builds
402 inline void wxLogDebug(const wxChar
*, ...) { }
403 inline void wxLogTrace(const wxChar
*, ...) { }
404 inline void wxLogTrace(wxTraceMask
, const wxChar
*, ...) { }
405 inline void wxLogTrace(const wxChar
*, const wxChar
*, ...) { }
409 // are we in 'verbose' mode?
410 // (note that it's often handy to change this var manually from the
411 // debugger, thus enabling/disabling verbose reporting for some
412 // parts of the program only)
413 WXDLLEXPORT_DATA(extern bool) g_bVerbose
;
415 // ----------------------------------------------------------------------------
416 // get error code/error message from system in a portable way
417 // ----------------------------------------------------------------------------
419 // return the last system error code
420 WXDLLEXPORT
unsigned long wxSysErrorCode();
421 // return the error message for given (or last if 0) error code
422 WXDLLEXPORT
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
424 // ----------------------------------------------------------------------------
425 // debug only logging functions: use them with API name and error code
426 // ----------------------------------------------------------------------------
429 #define __XFILE__(x) _T(x)
430 #define __TFILE__ __XFILE__(__FILE__)
434 // make life easier for people using VC++ IDE: clicking on the message
435 // will take us immediately to the place of the failed API
437 #define wxLogApiError(api, rc) \
438 wxLogDebug(_T("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
439 __TFILE__, __LINE__, api, \
440 rc, wxSysErrorMsg(rc))
442 #define wxLogApiError(api, rc) \
443 wxLogDebug(_T("In file %s at line %d: '%s' failed with " \
444 "error 0x%08lx (%s)."), \
445 __TFILE__, __LINE__, api, \
446 rc, wxSysErrorMsg(rc))
449 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
452 inline void wxLogApiError(const wxChar
*, long) { }
453 inline void wxLogLastError(const wxChar
*) { }
454 #endif //debug/!debug