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 // derive from this class to redirect (or suppress, or ...) log messages
54 // normally, only a single instance of this class exists but it's not enforced
55 // ----------------------------------------------------------------------------
56 class WXDLLEXPORT wxLog
63 static void OnLog(wxLogLevel level
, const char *szString
)
64 { if ( ms_pLogger
!= 0 ) ms_pLogger
->DoLog(level
, szString
); }
67 // flush shows all messages if they're not logged immediately
68 // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
69 // showing 17 modal dialogs one after another)
71 // call to Flush() may be optimized: call it only if this function
72 // returns true (although Flush() also returns immediately if there
73 // is no messages, this functions is more efficient because inline)
74 bool HasPendingMessages() const { return m_bHasMessages
; }
76 // only one sink is active at each moment
77 // get current log target, will call wxApp::CreateLogTarget() to create one
79 static wxLog
*GetActiveTarget();
80 // change log target, pLogger = NULL disables logging,
81 // returns the previous log target
82 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
84 // functions controlling the default wxLog behaviour
85 // verbose mode is activated by standard command-line '-verbose' option
86 void SetVerbose(bool bVerbose
= TRUE
) { m_bVerbose
= bVerbose
; }
87 // sets the format for timestamp prepended by wxLog::DoLog(): it's
88 // passed to strftime() function, see it's documentation for details.
89 // no time stamp at all if szTF is NULL or empty
90 // NB: the string is not copied, so it's lifetime must be long enough!
91 void SetTimeStampFormat(const char *szTF
) { m_szTimeFormat
= szTF
; }
92 // trace mask (see wxTraceXXX constants for details)
93 static void SetTraceMask(wxTraceMask ulMask
) { ms_ulTraceMask
= ulMask
; }
94 // should GetActiveTarget() try to create a new log object if the current
96 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
99 // gets the verbose status
100 bool GetVerbose() const { return m_bVerbose
; }
101 // get current time format
102 const char *GetTimeStampFormat() const { return m_szTimeFormat
; }
104 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
106 // make dtor virtual for all derived classes
112 bool m_bVerbose
; // FALSE => ignore LogInfo messages
113 const char *m_szTimeFormat
; // format for strftime()
115 // the logging functions that can be overriden
116 // default DoLog() prepends the time stamp and a prefix corresponding
117 // to the message to szString and then passes it to DoLogString()
118 virtual void DoLog(wxLogLevel level
, const char *szString
);
119 // default DoLogString does nothing but is not pure virtual because if
120 // you override DoLog() you might not need it at all
121 virtual void DoLogString(const char *szString
);
124 // put the time stamp in the current format into the string
125 wxString
TimeStamp() const;
130 static wxLog
*ms_pLogger
; // currently active log sink
131 static bool ms_bAutoCreate
; // automatically create new log targets?
132 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
135 // ----------------------------------------------------------------------------
136 // "trivial" derivations of wxLog
137 // ----------------------------------------------------------------------------
139 // log everything to a "FILE *", stderr by default
140 class WXDLLEXPORT wxLogStderr
: public wxLog
143 // redirect log output to a FILE
144 wxLogStderr(FILE *fp
= NULL
);
147 // implement sink function
148 virtual void DoLogString(const char *szString
);
153 // log everything to an "ostream", cerr by default
154 class WXDLLEXPORT wxLogStream
: public wxLog
157 // redirect log output to an ostream
158 wxLogStream(ostream
*ostr
= NULL
);
161 // implement sink function
162 virtual void DoLogString(const char *szString
);
164 // @@ using ptr here to avoid including <iostream.h> from this file
168 // log everything to a text window (GUI only of course)
170 class WXDLLEXPORT wxLogTextCtrl
: public wxLogStream
173 // we just create an ostream from wxTextCtrl and use it in base class
174 wxLogTextCtrl(wxTextCtrl
*pTextCtrl
);
178 // ----------------------------------------------------------------------------
179 // GUI log target, the default one for wxWindows programs
180 // ----------------------------------------------------------------------------
181 class WXDLLEXPORT wxLogGui
: public wxLog
187 // show all messages that were logged since the last Flush()
188 virtual void Flush();
191 virtual void DoLog(wxLogLevel level
, const char *szString
);
193 wxArrayString m_aMessages
;
197 // ----------------------------------------------------------------------------
198 // (background) log window: this class forwards all log messages to the log
199 // target which was active when it was instantiated, but also collects them
200 // to the log window. This window has it's own menu which allows the user to
201 // close it, clear the log contents or save it to the file.
202 // ----------------------------------------------------------------------------
205 class WXDLLEXPORT wxLogWindow
: public wxLog
208 wxLogWindow(const char *szTitle
, // the title of the frame
209 bool bShow
= TRUE
, // show window immediately?
210 bool bPassToOld
= TRUE
); // pass log messages to the old target?
214 // show/hide the log window
215 void Show(bool bShow
= TRUE
);
216 // retrieve the pointer to the frame
217 wxFrame
*GetFrame() const;
220 // the previous log target (may be NULL)
221 wxLog
*GetOldLog() const { return m_pOldLog
; }
222 // are we passing the messages to the previous log target?
223 bool IsPassingMessages() const { return m_bPassMessages
; }
225 // we can pass the messages to the previous log target (we're in this mode by
226 // default: we collect all messages in the window, but also let the default
227 // processing take place)
228 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
230 // base class virtuals
231 // we don't need it ourselves, but we pass it to the previous logger
232 virtual void Flush();
235 // called immediately after the log frame creation allowing for
236 // any extra initializations
237 virtual void OnFrameCreate(wxFrame
*frame
);
238 // called right before the log frame is going to be deleted
239 virtual void OnFrameDelete(wxFrame
*frame
);
242 virtual void DoLog(wxLogLevel level
, const char *szString
);
243 virtual void DoLogString(const char *szString
);
246 bool m_bPassMessages
; // pass messages to m_pOldLog?
247 wxLog
*m_pOldLog
; // previous log target
248 wxLogFrame
*m_pLogFrame
; // the log frame
251 // ----------------------------------------------------------------------------
252 // /dev/null log target: suppress logging until this object goes out of scope
253 // ----------------------------------------------------------------------------
260 // wxFile.Open() normally complains if file can't be opened, we don't want it
262 if ( !file.Open("bar") )
263 ... process error ourselves ...
265 // ~wxLogNull called, old log sink restored
268 class WXDLLEXPORT wxLogNull
271 // ctor saves old log target, dtor restores it
272 wxLogNull() { m_pPrevLogger
= wxLog::SetActiveTarget(NULL
); }
273 ~wxLogNull() { (void)wxLog::SetActiveTarget(m_pPrevLogger
); }
276 wxLog
*m_pPrevLogger
; // old log target
279 // ============================================================================
281 // ============================================================================
283 // ----------------------------------------------------------------------------
284 // Log functions should be used by application instead of stdio, iostream &c
285 // for log messages for easy redirection
286 // ----------------------------------------------------------------------------
288 // define wxLog<level>
289 // -------------------
291 #define DECLARE_LOG_FUNCTION(level) \
292 extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...)
293 #define DECLARE_LOG_FUNCTION2(level, arg1) \
294 extern void WXDLLEXPORT wxLog##level(arg1, const char *szFormat, ...)
296 // a generic function for all levels (level is passes as parameter)
297 DECLARE_LOG_FUNCTION2(Generic
, wxLogLevel level
);
299 // one function per each level
300 DECLARE_LOG_FUNCTION(FatalError
);
301 DECLARE_LOG_FUNCTION(Error
);
302 DECLARE_LOG_FUNCTION(Warning
);
303 DECLARE_LOG_FUNCTION(Message
);
304 DECLARE_LOG_FUNCTION(Info
);
305 DECLARE_LOG_FUNCTION(Status
);
306 DECLARE_LOG_FUNCTION(Verbose
);
308 // additional one: as wxLogError, but also logs last system call error code
309 // and the corresponding error message if available
310 DECLARE_LOG_FUNCTION(SysError
);
312 // and another one which also takes the error code (for those broken APIs
313 // that don't set the errno (like registry APIs in Win32))
314 DECLARE_LOG_FUNCTION2(SysError
, long lErrCode
);
316 // debug functions do nothing in release mode
318 DECLARE_LOG_FUNCTION(Debug
);
320 // first king of LogTrace is uncoditional: it doesn't check the level,
321 // while the second one does nothing if all of level bits are not set
322 // in wxLog::GetActive()->GetTraceMask().
323 DECLARE_LOG_FUNCTION(Trace
);
324 DECLARE_LOG_FUNCTION2(Trace
, wxTraceMask mask
);
326 // these functions do nothing
327 inline void wxLogDebug(const char *, ...) { }
328 inline void wxLogTrace(const char *, ...) { }
329 inline void wxLogTrace(wxTraceMask
, const char *, ...) { }
333 // are we in 'verbose' mode?
334 // (note that it's often handy to change this var manually from the
335 // debugger, thus enabling/disabling verbose reporting for some
336 // parts of the program only)
337 WXDLLEXPORT_DATA(extern bool) g_bVerbose
;
339 // fwd decl to avoid including iostream.h here
342 // ----------------------------------------------------------------------------
343 // get error code/error message from system in a portable way
344 // ----------------------------------------------------------------------------
346 // return the last system error code
347 unsigned long WXDLLEXPORT
wxSysErrorCode();
348 // return the error message for given (or last if 0) error code
349 const char* WXDLLEXPORT
wxSysErrorMsg(unsigned long nErrCode
= 0);
351 // ----------------------------------------------------------------------------
352 // debug only logging functions: use them with API name and error code
353 // ----------------------------------------------------------------------------
356 #define wxLogApiError(api, rc) \
357 wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
358 __FILE__, __LINE__, api, \
359 rc, wxSysErrorMsg(rc))
360 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
362 inline void wxLogApiError(const char *, long) { }
363 inline void wxLogLastError(const char *) { }
364 #endif //debug/!debug