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 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // different standard log levels (you may also define your own)
26 wxLOG_FatalError
, // program can't continue, abort immediately
27 wxLOG_Error
, // a serious error, user must be informed about it
28 wxLOG_Warning
, // user is normally informed about it but may be ignored
29 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
30 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
31 wxLOG_Status
, // informational: might go to the status line of GUI app
32 wxLOG_Debug
, // never shown to the user, disabled in release mode
33 wxLOG_Trace
, // trace messages are also only enabled in debug mode
34 wxLOG_Progress
, // used for progress indicator (not yet)
35 wxLOG_User
= 100 // user defined levels start here
38 // meaning of different bits of the trace mask (which allows selectively
39 // enable/disable some trace messages)
40 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
41 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
42 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
43 #define wxTraceRefCount 0x0008 // trace various ref counting operations
46 #define wxTraceOleCalls 0x0100 // OLE interface calls
49 typedef unsigned long wxTraceMask
;
50 typedef unsigned long wxLogLevel
;
52 // ----------------------------------------------------------------------------
53 // forward declarations
54 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxTextCtrl
;
56 class WXDLLEXPORT wxLogFrame
;
57 class WXDLLEXPORT wxFrame
;
68 // ----------------------------------------------------------------------------
69 // derive from this class to redirect (or suppress, or ...) log messages
70 // normally, only a single instance of this class exists but it's not enforced
71 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxLog
79 static void OnLog(wxLogLevel level
, const char *szString
)
81 wxLog
*pLogger
= GetActiveTarget();
84 pLogger
->DoLog(level
, szString
);
89 // flush shows all messages if they're not logged immediately
90 // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
91 // showing 17 modal dialogs one after another)
93 // call to Flush() may be optimized: call it only if this function
94 // returns true (although Flush() also returns immediately if there
95 // is no messages, this functions is more efficient because inline)
96 bool HasPendingMessages() const { return m_bHasMessages
; }
98 // only one sink is active at each moment
99 // get current log target, will call wxApp::CreateLogTarget() to create one
101 static wxLog
*GetActiveTarget();
102 // change log target, pLogger = NULL disables logging. if bNoFlashOld is true,
103 // the old log target isn't flashed which might lead to loss of messages!
104 // returns the previous log target
105 static wxLog
*SetActiveTarget(wxLog
*pLogger
, bool bNoFlashOld
= FALSE
);
107 // functions controlling the default wxLog behaviour
108 // verbose mode is activated by standard command-line '-verbose' option
109 void SetVerbose(bool bVerbose
= TRUE
) { m_bVerbose
= bVerbose
; }
110 // sets the format for timestamp prepended by wxLog::DoLog(): it's
111 // passed to strftime() function, see it's documentation for details.
112 // no time stamp at all if szTF is NULL or empty
113 // NB: the string is not copied, so it's lifetime must be long enough!
114 void SetTimeStampFormat(const char *szTF
) { m_szTimeFormat
= szTF
; }
115 // trace mask (see wxTraceXXX constants for details)
116 static void SetTraceMask(wxTraceMask ulMask
) { ms_ulTraceMask
= ulMask
; }
117 // should GetActiveTarget() try to create a new log object if the current
119 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
122 // gets the verbose status
123 bool GetVerbose() const { return m_bVerbose
; }
124 // get current time format
125 const char *GetTimeStampFormat() const { return m_szTimeFormat
; }
127 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
129 // make dtor virtual for all derived classes
135 bool m_bVerbose
; // FALSE => ignore LogInfo messages
136 const char *m_szTimeFormat
; // format for strftime()
138 // the logging functions that can be overriden
139 // default DoLog() prepends the time stamp and a prefix corresponding
140 // to the message to szString and then passes it to DoLogString()
141 virtual void DoLog(wxLogLevel level
, const char *szString
);
142 // default DoLogString does nothing but is not pure virtual because if
143 // you override DoLog() you might not need it at all
144 virtual void DoLogString(const char *szString
);
147 // put the time stamp in the current format into the string
148 wxString
TimeStamp() const;
153 static wxLog
*ms_pLogger
; // currently active log sink
154 static bool ms_bAutoCreate
; // automatically create new log targets?
155 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
158 // ----------------------------------------------------------------------------
159 // "trivial" derivations of wxLog
160 // ----------------------------------------------------------------------------
162 // log everything to a "FILE *", stderr by default
163 class WXDLLEXPORT wxLogStderr
: public wxLog
166 // redirect log output to a FILE
167 wxLogStderr(FILE *fp
= (FILE *) NULL
);
170 // implement sink function
171 virtual void DoLogString(const char *szString
);
176 // log everything to an "ostream", cerr by default
177 class WXDLLEXPORT wxLogStream
: public wxLog
180 // redirect log output to an ostream
181 wxLogStream(ostream
*ostr
= (ostream
*) NULL
);
184 // implement sink function
185 virtual void DoLogString(const char *szString
);
187 // @@ using ptr here to avoid including <iostream.h> from this file
191 // log everything to a text window (GUI only of course)
192 class WXDLLEXPORT wxLogTextCtrl
: public wxLogStream
195 // we just create an ostream from wxTextCtrl and use it in base class
196 wxLogTextCtrl(wxTextCtrl
*pTextCtrl
);
200 // ----------------------------------------------------------------------------
201 // GUI log target, the default one for wxWindows programs
202 // ----------------------------------------------------------------------------
203 class WXDLLEXPORT wxLogGui
: public wxLog
209 // show all messages that were logged since the last Flush()
210 virtual void Flush();
213 virtual void DoLog(wxLogLevel level
, const char *szString
);
215 wxArrayString m_aMessages
;
219 // ----------------------------------------------------------------------------
220 // (background) log window: this class forwards all log messages to the log
221 // target which was active when it was instantiated, but also collects them
222 // to the log window. This window has it's own menu which allows the user to
223 // close it, clear the log contents or save it to the file.
224 // ----------------------------------------------------------------------------
225 class WXDLLEXPORT wxLogWindow
: public wxLog
228 wxLogWindow(wxFrame
*pParent
, // the parent frame (can be NULL)
229 const char *szTitle
, // the title of the frame
230 bool bShow
= TRUE
, // show window immediately?
231 bool bPassToOld
= TRUE
); // pass log messages to the old target?
235 // show/hide the log window
236 void Show(bool bShow
= TRUE
);
237 // retrieve the pointer to the frame
238 wxFrame
*GetFrame() const;
241 // the previous log target (may be NULL)
242 wxLog
*GetOldLog() const { return m_pOldLog
; }
243 // are we passing the messages to the previous log target?
244 bool IsPassingMessages() const { return m_bPassMessages
; }
246 // we can pass the messages to the previous log target (we're in this mode by
247 // default: we collect all messages in the window, but also let the default
248 // processing take place)
249 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
251 // base class virtuals
252 // we don't need it ourselves, but we pass it to the previous logger
253 virtual void Flush();
256 // called immediately after the log frame creation allowing for
257 // any extra initializations
258 virtual void OnFrameCreate(wxFrame
*frame
);
259 // called right before the log frame is going to be deleted
260 virtual void OnFrameDelete(wxFrame
*frame
);
263 virtual void DoLog(wxLogLevel level
, const char *szString
);
264 virtual void DoLogString(const char *szString
);
267 bool m_bPassMessages
; // pass messages to m_pOldLog?
268 wxLog
*m_pOldLog
; // previous log target
269 wxLogFrame
*m_pLogFrame
; // the log frame
272 // ----------------------------------------------------------------------------
273 // /dev/null log target: suppress logging until this object goes out of scope
274 // ----------------------------------------------------------------------------
281 // wxFile.Open() normally complains if file can't be opened, we don't want it
283 if ( !file.Open("bar") )
284 ... process error ourselves ...
286 // ~wxLogNull called, old log sink restored
289 class WXDLLEXPORT wxLogNull
292 // ctor saves old log target, dtor restores it
293 wxLogNull() { m_pPrevLogger
= wxLog::SetActiveTarget((wxLog
*)NULL
, TRUE
); }
294 ~wxLogNull() { (void)wxLog::SetActiveTarget(m_pPrevLogger
); }
297 wxLog
*m_pPrevLogger
; // old log target
300 // ============================================================================
302 // ============================================================================
304 // ----------------------------------------------------------------------------
305 // Log functions should be used by application instead of stdio, iostream &c
306 // for log messages for easy redirection
307 // ----------------------------------------------------------------------------
309 // define wxLog<level>
310 // -------------------
312 #define DECLARE_LOG_FUNCTION(level) \
313 extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...)
314 #define DECLARE_LOG_FUNCTION2(level, arg1) \
315 extern void WXDLLEXPORT wxLog##level(arg1, const char *szFormat, ...)
317 // a generic function for all levels (level is passes as parameter)
318 DECLARE_LOG_FUNCTION2(Generic
, wxLogLevel level
);
320 // one function per each level
321 DECLARE_LOG_FUNCTION(FatalError
);
322 DECLARE_LOG_FUNCTION(Error
);
323 DECLARE_LOG_FUNCTION(Warning
);
324 DECLARE_LOG_FUNCTION(Message
);
325 DECLARE_LOG_FUNCTION(Info
);
326 DECLARE_LOG_FUNCTION(Verbose
);
328 // this function sends the log message to the status line of the top level
329 // application frame, if any
330 DECLARE_LOG_FUNCTION(Status
);
332 // this one is the same as previous except that it allows to explicitly
333 // specify the frame to which the output should go
334 DECLARE_LOG_FUNCTION2(Status
, wxFrame
*pFrame
);
336 // additional one: as wxLogError, but also logs last system call error code
337 // and the corresponding error message if available
338 DECLARE_LOG_FUNCTION(SysError
);
340 // and another one which also takes the error code (for those broken APIs
341 // that don't set the errno (like registry APIs in Win32))
342 DECLARE_LOG_FUNCTION2(SysError
, long lErrCode
);
344 // debug functions do nothing in release mode
346 DECLARE_LOG_FUNCTION(Debug
);
348 // first king of LogTrace is uncoditional: it doesn't check the level,
349 // while the second one does nothing if all of level bits are not set
350 // in wxLog::GetActive()->GetTraceMask().
351 DECLARE_LOG_FUNCTION(Trace
);
352 DECLARE_LOG_FUNCTION2(Trace
, wxTraceMask mask
);
354 // these functions do nothing
355 inline void wxLogDebug(const char *, ...) { }
356 inline void wxLogTrace(const char *, ...) { }
357 inline void wxLogTrace(wxTraceMask
, const char *, ...) { }
361 // are we in 'verbose' mode?
362 // (note that it's often handy to change this var manually from the
363 // debugger, thus enabling/disabling verbose reporting for some
364 // parts of the program only)
365 WXDLLEXPORT_DATA(extern bool) g_bVerbose
;
367 // ----------------------------------------------------------------------------
368 // get error code/error message from system in a portable way
369 // ----------------------------------------------------------------------------
371 // return the last system error code
372 unsigned long WXDLLEXPORT
wxSysErrorCode();
373 // return the error message for given (or last if 0) error code
374 const char* WXDLLEXPORT
wxSysErrorMsg(unsigned long nErrCode
= 0);
376 // ----------------------------------------------------------------------------
377 // debug only logging functions: use them with API name and error code
378 // ----------------------------------------------------------------------------
381 #define wxLogApiError(api, rc) \
382 wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
383 __FILE__, __LINE__, api, \
384 rc, wxSysErrorMsg(rc))
385 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
387 inline void wxLogApiError(const char *, long) { }
388 inline void wxLogLastError(const char *) { }
389 #endif //debug/!debug