]> git.saurik.com Git - wxWidgets.git/blob - include/wx/log.h
wxTextFile::Type changed to ::wxTextFileType (portability)
[wxWidgets.git] / include / wx / log.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: log.h
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_LOG_H_
13 #define _WX_LOG_H_
14
15 #ifdef __GNUG__
16 #pragma interface "log.h"
17 #endif
18
19 // ----------------------------------------------------------------------------
20 // constants
21 // ----------------------------------------------------------------------------
22
23 // different standard log levels (you may also define your own)
24 enum
25 {
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
36 };
37
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
44
45 #ifdef __WXMSW__
46 #define wxTraceOleCalls 0x0100 // OLE interface calls
47 #endif
48
49 typedef unsigned long wxTraceMask;
50 typedef unsigned long wxLogLevel;
51
52 // ----------------------------------------------------------------------------
53 // forward declarations
54 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxTextCtrl;
56 class WXDLLEXPORT wxLogFrame;
57 class WXDLLEXPORT wxFrame;
58
59 #if wxUSE_IOSTREAMH
60 // N.B. BC++ doesn't have istream.h, ostream.h
61 # include <iostream.h>
62 #else
63 # include <ostream>
64 # ifdef _MSC_VER
65 using namespace std;
66 # endif
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // derive from this class to redirect (or suppress, or ...) log messages
71 // normally, only a single instance of this class exists but it's not enforced
72 // ----------------------------------------------------------------------------
73 class WXDLLEXPORT wxLog
74 {
75 public:
76 // ctor
77 wxLog();
78
79 // these functions allow to completely disable all log messages
80 // is logging disabled now?
81 static bool IsEnabled() { return ms_doLog; }
82 // change the flag state, return the previous one
83 static bool EnableLogging(bool doIt = TRUE)
84 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
85
86 // sink function
87 static void OnLog(wxLogLevel level, const char *szString)
88 {
89 if ( IsEnabled() ) {
90 wxLog *pLogger = GetActiveTarget();
91 if ( pLogger )
92 pLogger->DoLog(level, szString);
93 }
94 }
95
96 // message buffering
97 // flush shows all messages if they're not logged immediately
98 // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
99 // showing 17 modal dialogs one after another)
100 virtual void Flush();
101 // call to Flush() may be optimized: call it only if this function
102 // returns true (although Flush() also returns immediately if there
103 // is no messages, this functions is more efficient because inline)
104 bool HasPendingMessages() const { return m_bHasMessages; }
105
106 // only one sink is active at each moment
107 // get current log target, will call wxApp::CreateLogTarget() to create one
108 // if none exists
109 static wxLog *GetActiveTarget();
110 // change log target, pLogger may be NULL
111 static wxLog *SetActiveTarget(wxLog *pLogger);
112
113 // functions controlling the default wxLog behaviour
114 // verbose mode is activated by standard command-line '-verbose' option
115 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
116 // sets the format for timestamp prepended by wxLog::DoLog(): it's
117 // passed to strftime() function, see it's documentation for details.
118 // no time stamp at all if szTF is NULL or empty
119 // NB: the string is not copied, so it's lifetime must be long enough!
120 void SetTimeStampFormat(const char *szTF) { m_szTimeFormat = szTF; }
121 // trace mask (see wxTraceXXX constants for details)
122 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
123 // should GetActiveTarget() try to create a new log object if the current
124 // is NULL?
125 static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; }
126
127 // accessors
128 // gets the verbose status
129 bool GetVerbose() const { return m_bVerbose; }
130 // get current time format
131 const char *GetTimeStampFormat() const { return m_szTimeFormat; }
132 // get trace mask
133 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
134
135 // make dtor virtual for all derived classes
136 virtual ~wxLog() { }
137
138 protected:
139 bool m_bHasMessages;
140
141 bool m_bVerbose; // FALSE => ignore LogInfo messages
142 const char *m_szTimeFormat; // format for strftime()
143
144 // the logging functions that can be overriden
145 // default DoLog() prepends the time stamp and a prefix corresponding
146 // to the message to szString and then passes it to DoLogString()
147 virtual void DoLog(wxLogLevel level, const char *szString);
148 // default DoLogString does nothing but is not pure virtual because if
149 // you override DoLog() you might not need it at all
150 virtual void DoLogString(const char *szString);
151
152 // helpers
153 // put the time stamp in the current format into the string
154 wxString TimeStamp() const;
155
156 private:
157 // static variables
158 // ----------------
159 static wxLog *ms_pLogger; // currently active log sink
160 static bool ms_doLog; // FALSE => all logging disabled
161 static bool ms_bAutoCreate; // automatically create new log targets?
162 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
163 };
164
165 // ----------------------------------------------------------------------------
166 // "trivial" derivations of wxLog
167 // ----------------------------------------------------------------------------
168
169 // log everything to a "FILE *", stderr by default
170 class WXDLLEXPORT wxLogStderr : public wxLog
171 {
172 public:
173 // redirect log output to a FILE
174 wxLogStderr(FILE *fp = (FILE *) NULL);
175
176 private:
177 // implement sink function
178 virtual void DoLogString(const char *szString);
179
180 FILE *m_fp;
181 };
182
183 // log everything to an "ostream", cerr by default
184 class WXDLLEXPORT wxLogStream : public wxLog
185 {
186 public:
187 // redirect log output to an ostream
188 wxLogStream(ostream *ostr = (ostream *) NULL);
189
190 protected:
191 // implement sink function
192 virtual void DoLogString(const char *szString);
193
194 // @@ using ptr here to avoid including <iostream.h> from this file
195 ostream *m_ostr;
196 };
197
198 #ifndef wxUSE_NOGUI
199
200 // log everything to a text window (GUI only of course)
201 class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
202 {
203 public:
204 // we just create an ostream from wxTextCtrl and use it in base class
205 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
206 ~wxLogTextCtrl();
207 };
208
209 // ----------------------------------------------------------------------------
210 // GUI log target, the default one for wxWindows programs
211 // ----------------------------------------------------------------------------
212 class WXDLLEXPORT wxLogGui : public wxLog
213 {
214 public:
215 // ctor
216 wxLogGui();
217
218 // show all messages that were logged since the last Flush()
219 virtual void Flush();
220
221 protected:
222 virtual void DoLog(wxLogLevel level, const char *szString);
223
224 wxArrayString m_aMessages;
225 bool m_bErrors;
226 };
227
228 // ----------------------------------------------------------------------------
229 // (background) log window: this class forwards all log messages to the log
230 // target which was active when it was instantiated, but also collects them
231 // to the log window. This window has it's own menu which allows the user to
232 // close it, clear the log contents or save it to the file.
233 // ----------------------------------------------------------------------------
234 class WXDLLEXPORT wxLogWindow : public wxLog
235 {
236 public:
237 wxLogWindow(wxFrame *pParent, // the parent frame (can be NULL)
238 const char *szTitle, // the title of the frame
239 bool bShow = TRUE, // show window immediately?
240 bool bPassToOld = TRUE); // pass log messages to the old target?
241 ~wxLogWindow();
242
243 // window operations
244 // show/hide the log window
245 void Show(bool bShow = TRUE);
246 // retrieve the pointer to the frame
247 wxFrame *GetFrame() const;
248
249 // accessors
250 // the previous log target (may be NULL)
251 wxLog *GetOldLog() const { return m_pOldLog; }
252 // are we passing the messages to the previous log target?
253 bool IsPassingMessages() const { return m_bPassMessages; }
254
255 // we can pass the messages to the previous log target (we're in this mode by
256 // default: we collect all messages in the window, but also let the default
257 // processing take place)
258 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
259
260 // base class virtuals
261 // we don't need it ourselves, but we pass it to the previous logger
262 virtual void Flush();
263
264 // overridables
265 // called immediately after the log frame creation allowing for
266 // any extra initializations
267 virtual void OnFrameCreate(wxFrame *frame);
268 // called right before the log frame is going to be deleted
269 virtual void OnFrameDelete(wxFrame *frame);
270
271 protected:
272 virtual void DoLog(wxLogLevel level, const char *szString);
273 virtual void DoLogString(const char *szString);
274
275 private:
276 bool m_bPassMessages; // pass messages to m_pOldLog?
277 wxLog *m_pOldLog; // previous log target
278 wxLogFrame *m_pLogFrame; // the log frame
279 };
280
281 #endif // wxUSE_NOGUI
282
283 // ----------------------------------------------------------------------------
284 // /dev/null log target: suppress logging until this object goes out of scope
285 // ----------------------------------------------------------------------------
286
287 // example of usage:
288 /*
289 void Foo() {
290 wxFile file;
291
292 // wxFile.Open() normally complains if file can't be opened, we don't want it
293 wxLogNull logNo;
294 if ( !file.Open("bar") )
295 ... process error ourselves ...
296
297 // ~wxLogNull called, old log sink restored
298 }
299 */
300 class WXDLLEXPORT wxLogNull
301 {
302 public:
303 wxLogNull() { m_flagOld = wxLog::EnableLogging(FALSE); }
304 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
305
306 private:
307 bool m_flagOld; // the previous value of the wxLog::ms_doLog
308 };
309
310 // ============================================================================
311 // global functions
312 // ============================================================================
313
314 // ----------------------------------------------------------------------------
315 // Log functions should be used by application instead of stdio, iostream &c
316 // for log messages for easy redirection
317 // ----------------------------------------------------------------------------
318
319 // define wxLog<level>
320 // -------------------
321
322 #define DECLARE_LOG_FUNCTION(level) \
323 extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...)
324 #define DECLARE_LOG_FUNCTION2(level, arg1) \
325 extern void WXDLLEXPORT wxLog##level(arg1, const char *szFormat, ...)
326
327 // a generic function for all levels (level is passes as parameter)
328 DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
329
330 // one function per each level
331 DECLARE_LOG_FUNCTION(FatalError);
332 DECLARE_LOG_FUNCTION(Error);
333 DECLARE_LOG_FUNCTION(Warning);
334 DECLARE_LOG_FUNCTION(Message);
335 DECLARE_LOG_FUNCTION(Info);
336 DECLARE_LOG_FUNCTION(Verbose);
337
338 // this function sends the log message to the status line of the top level
339 // application frame, if any
340 DECLARE_LOG_FUNCTION(Status);
341
342 // this one is the same as previous except that it allows to explicitly
343 // specify the frame to which the output should go
344 DECLARE_LOG_FUNCTION2(Status, wxFrame *pFrame);
345
346 // additional one: as wxLogError, but also logs last system call error code
347 // and the corresponding error message if available
348 DECLARE_LOG_FUNCTION(SysError);
349
350 // and another one which also takes the error code (for those broken APIs
351 // that don't set the errno (like registry APIs in Win32))
352 DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
353
354 // debug functions do nothing in release mode
355 #ifdef __WXDEBUG__
356 DECLARE_LOG_FUNCTION(Debug);
357
358 // first king of LogTrace is uncoditional: it doesn't check the level,
359 // while the second one does nothing if all of level bits are not set
360 // in wxLog::GetActive()->GetTraceMask().
361 DECLARE_LOG_FUNCTION(Trace);
362 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
363 #else //!debug
364 // these functions do nothing
365 inline void wxLogDebug(const char *, ...) { }
366 inline void wxLogTrace(const char *, ...) { }
367 inline void wxLogTrace(wxTraceMask, const char *, ...) { }
368 #endif
369
370
371 // are we in 'verbose' mode?
372 // (note that it's often handy to change this var manually from the
373 // debugger, thus enabling/disabling verbose reporting for some
374 // parts of the program only)
375 WXDLLEXPORT_DATA(extern bool) g_bVerbose;
376
377 // ----------------------------------------------------------------------------
378 // get error code/error message from system in a portable way
379 // ----------------------------------------------------------------------------
380
381 // return the last system error code
382 WXDLLEXPORT unsigned long wxSysErrorCode();
383 // return the error message for given (or last if 0) error code
384 WXDLLEXPORT const char* wxSysErrorMsg(unsigned long nErrCode = 0);
385
386 // ----------------------------------------------------------------------------
387 // debug only logging functions: use them with API name and error code
388 // ----------------------------------------------------------------------------
389
390 #ifdef __WXDEBUG__
391 #define wxLogApiError(api, rc) \
392 wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
393 __FILE__, __LINE__, api, \
394 rc, wxSysErrorMsg(rc))
395 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
396 #else //!debug
397 inline void wxLogApiError(const char *, long) { }
398 inline void wxLogLastError(const char *) { }
399 #endif //debug/!debug
400
401 #endif // _WX_LOG_H_