]> git.saurik.com Git - wxWidgets.git/blob - include/wx/log.h
Typos.
[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 #include "wx/setup.h"
20
21 // ----------------------------------------------------------------------------
22 // forward declarations
23 // ----------------------------------------------------------------------------
24
25 class WXDLLEXPORT wxTextCtrl;
26 class WXDLLEXPORT wxLogFrame;
27 class WXDLLEXPORT wxFrame;
28
29 // ----------------------------------------------------------------------------
30 // types
31 // ----------------------------------------------------------------------------
32
33 typedef unsigned long wxTraceMask;
34 typedef unsigned long wxLogLevel;
35
36 // ----------------------------------------------------------------------------
37 // headers
38 // ----------------------------------------------------------------------------
39
40 #if wxUSE_LOG
41
42 #include <time.h> // for time_t
43
44 #include "wx/dynarray.h"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // different standard log levels (you may also define your own)
51 enum
52 {
53 wxLOG_FatalError, // program can't continue, abort immediately
54 wxLOG_Error, // a serious error, user must be informed about it
55 wxLOG_Warning, // user is normally informed about it but may be ignored
56 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
57 wxLOG_Info, // informational message (a.k.a. 'Verbose')
58 wxLOG_Status, // informational: might go to the status line of GUI app
59 wxLOG_Debug, // never shown to the user, disabled in release mode
60 wxLOG_Trace, // trace messages are also only enabled in debug mode
61 wxLOG_Progress, // used for progress indicator (not yet)
62 wxLOG_User = 100 // user defined levels start here
63 };
64
65 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
66 // discarded unless the string "foo" has been added to the list of allowed
67 // ones with AddTraceMask()
68
69 #define wxTRACE_MemAlloc "memalloc" // trace memory allocation (new/delete)
70 #define wxTRACE_Messages "messages" // trace window messages/X callbacks
71 #define wxTRACE_ResAlloc "resalloc" // trace GDI resource allocation
72 #define wxTRACE_RefCount "refcount" // trace various ref counting operations
73
74 #ifdef __WXMSW__
75 #define wxTRACE_OleCalls "ole" // OLE interface calls
76 #endif
77
78 // the trace masks have been superceded by symbolic trace constants, they're
79 // for compatibility only andwill be removed soon - do NOT use them
80
81 // meaning of different bits of the trace mask (which allows selectively
82 // enable/disable some trace messages)
83 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
84 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
85 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
86 #define wxTraceRefCount 0x0008 // trace various ref counting operations
87
88 #ifdef __WXMSW__
89 #define wxTraceOleCalls 0x0100 // OLE interface calls
90 #endif
91
92 #include "wx/ioswrap.h"
93
94 // ----------------------------------------------------------------------------
95 // derive from this class to redirect (or suppress, or ...) log messages
96 // normally, only a single instance of this class exists but it's not enforced
97 // ----------------------------------------------------------------------------
98
99 class WXDLLEXPORT wxLog
100 {
101 public:
102 // ctor
103 wxLog();
104
105 // these functions allow to completely disable all log messages
106 // is logging disabled now?
107 static bool IsEnabled() { return ms_doLog; }
108 // change the flag state, return the previous one
109 static bool EnableLogging(bool doIt = TRUE)
110 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
111
112 // static sink function - see DoLog() for function to overload in the
113 // derived classes
114 static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
115 {
116 if ( IsEnabled() ) {
117 wxLog *pLogger = GetActiveTarget();
118 if ( pLogger )
119 pLogger->DoLog(level, szString, t);
120 }
121 }
122
123 // message buffering
124 // flush shows all messages if they're not logged immediately (FILE
125 // and iostream logs don't need it, but wxGuiLog does to avoid showing
126 // 17 modal dialogs one after another)
127 virtual void Flush();
128 // call to Flush() may be optimized: call it only if this function
129 // returns true (although Flush() also returns immediately if there is
130 // no messages, this functions is more efficient because inline)
131 bool HasPendingMessages() const { return m_bHasMessages; }
132
133 // only one sink is active at each moment
134 // get current log target, will call wxApp::CreateLogTarget() to
135 // create one if none exists
136 static wxLog *GetActiveTarget();
137 // change log target, pLogger may be NULL
138 static wxLog *SetActiveTarget(wxLog *pLogger);
139
140 // functions controlling the default wxLog behaviour
141 // verbose mode is activated by standard command-line '-verbose'
142 // option
143 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
144 // should GetActiveTarget() try to create a new log object if the
145 // current is NULL?
146 static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; }
147
148 // trace mask (see wxTraceXXX constants for details)
149 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
150 // add string trace mask
151 static void AddTraceMask(const wxString& str) { ms_aTraceMasks.Add(str); }
152 // add string trace mask
153 static void RemoveTraceMask(const wxString& str);
154
155 // accessors
156 // gets the verbose status
157 bool GetVerbose() const { return m_bVerbose; }
158 // get trace mask
159 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
160 // is this trace mask in the list?
161 static bool IsAllowedTraceMask(const wxChar *mask)
162 { return ms_aTraceMasks.Index(mask) != wxNOT_FOUND; }
163
164 // make dtor virtual for all derived classes
165 virtual ~wxLog() { }
166
167 protected:
168 bool m_bHasMessages; // any messages in the queue?
169 bool m_bVerbose; // FALSE => ignore LogInfo messages
170
171 // the logging functions that can be overriden
172 // default DoLog() prepends the time stamp and a prefix corresponding
173 // to the message to szString and then passes it to DoLogString()
174 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
175 // default DoLogString does nothing but is not pure virtual because if
176 // you override DoLog() you might not need it at all
177 virtual void DoLogString(const wxChar *szString, time_t t);
178
179 private:
180 // static variables
181 // ----------------
182
183 static wxLog *ms_pLogger; // currently active log sink
184 static bool ms_doLog; // FALSE => all logging disabled
185 static bool ms_bAutoCreate; // create new log targets on demand?
186
187 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
188 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
189 };
190
191 // ----------------------------------------------------------------------------
192 // "trivial" derivations of wxLog
193 // ----------------------------------------------------------------------------
194
195 // log everything to a "FILE *", stderr by default
196 class WXDLLEXPORT wxLogStderr : public wxLog
197 {
198 public:
199 // redirect log output to a FILE
200 wxLogStderr(FILE *fp = (FILE *) NULL);
201
202 private:
203 // implement sink function
204 virtual void DoLogString(const wxChar *szString, time_t t);
205
206 FILE *m_fp;
207 };
208
209 #if wxUSE_STD_IOSTREAM
210 // log everything to an "ostream", cerr by default
211 class WXDLLEXPORT wxLogStream : public wxLog
212 {
213 public:
214 // redirect log output to an ostream
215 wxLogStream(ostream *ostr = (ostream *) NULL);
216
217 protected:
218 // implement sink function
219 virtual void DoLogString(const wxChar *szString, time_t t);
220
221 // using ptr here to avoid including <iostream.h> from this file
222 ostream *m_ostr;
223 };
224 #endif
225
226 #ifndef wxUSE_NOGUI
227
228 #if wxUSE_STD_IOSTREAM
229 // log everything to a text window (GUI only of course)
230 class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
231 {
232 public:
233 // we just create an ostream from wxTextCtrl and use it in base class
234 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
235 ~wxLogTextCtrl();
236 };
237 #endif
238
239 // ----------------------------------------------------------------------------
240 // GUI log target, the default one for wxWindows programs
241 // ----------------------------------------------------------------------------
242 class WXDLLEXPORT wxLogGui : public wxLog
243 {
244 public:
245 // ctor
246 wxLogGui();
247
248 // show all messages that were logged since the last Flush()
249 virtual void Flush();
250
251 protected:
252 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
253
254 // empty everything
255 void Clear();
256
257 wxArrayString m_aMessages;
258 wxArrayLong m_aTimes;
259 bool m_bErrors, // do we have any errors?
260 m_bWarnings; // any warnings?
261 };
262
263 // ----------------------------------------------------------------------------
264 // (background) log window: this class forwards all log messages to the log
265 // target which was active when it was instantiated, but also collects them
266 // to the log window. This window has it's own menu which allows the user to
267 // close it, clear the log contents or save it to the file.
268 // ----------------------------------------------------------------------------
269 class WXDLLEXPORT wxLogWindow : public wxLog
270 {
271 public:
272 wxLogWindow(wxFrame *pParent, // the parent frame (can be NULL)
273 const wxChar *szTitle, // the title of the frame
274 bool bShow = TRUE, // show window immediately?
275 bool bPassToOld = TRUE); // pass log messages to the old target?
276 ~wxLogWindow();
277
278 // window operations
279 // show/hide the log window
280 void Show(bool bShow = TRUE);
281 // retrieve the pointer to the frame
282 wxFrame *GetFrame() const;
283
284 // accessors
285 // the previous log target (may be NULL)
286 wxLog *GetOldLog() const { return m_pOldLog; }
287 // are we passing the messages to the previous log target?
288 bool IsPassingMessages() const { return m_bPassMessages; }
289
290 // we can pass the messages to the previous log target (we're in this mode by
291 // default: we collect all messages in the window, but also let the default
292 // processing take place)
293 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
294
295 // base class virtuals
296 // we don't need it ourselves, but we pass it to the previous logger
297 virtual void Flush();
298
299 // overridables
300 // called immediately after the log frame creation allowing for
301 // any extra initializations
302 virtual void OnFrameCreate(wxFrame *frame);
303 // called right before the log frame is going to be deleted
304 virtual void OnFrameDelete(wxFrame *frame);
305
306 protected:
307 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
308 virtual void DoLogString(const wxChar *szString, time_t t);
309
310 private:
311 bool m_bPassMessages; // pass messages to m_pOldLog?
312 wxLog *m_pOldLog; // previous log target
313 wxLogFrame *m_pLogFrame; // the log frame
314 };
315
316 #endif // wxUSE_NOGUI
317
318 // ----------------------------------------------------------------------------
319 // /dev/null log target: suppress logging until this object goes out of scope
320 // ----------------------------------------------------------------------------
321
322 // example of usage:
323 /*
324 void Foo() {
325 wxFile file;
326
327 // wxFile.Open() normally complains if file can't be opened, we don't want it
328 wxLogNull logNo;
329 if ( !file.Open("bar") )
330 ... process error ourselves ...
331
332 // ~wxLogNull called, old log sink restored
333 }
334 */
335 class WXDLLEXPORT wxLogNull
336 {
337 public:
338 wxLogNull() { m_flagOld = wxLog::EnableLogging(FALSE); }
339 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
340
341 private:
342 bool m_flagOld; // the previous value of the wxLog::ms_doLog
343 };
344
345 // ============================================================================
346 // global functions
347 // ============================================================================
348
349 // ----------------------------------------------------------------------------
350 // Log functions should be used by application instead of stdio, iostream &c
351 // for log messages for easy redirection
352 // ----------------------------------------------------------------------------
353
354 // are we in 'verbose' mode?
355 // (note that it's often handy to change this var manually from the
356 // debugger, thus enabling/disabling verbose reporting for some
357 // parts of the program only)
358 WXDLLEXPORT_DATA(extern bool) g_bVerbose;
359
360 // ----------------------------------------------------------------------------
361 // get error code/error message from system in a portable way
362 // ----------------------------------------------------------------------------
363
364 // return the last system error code
365 WXDLLEXPORT unsigned long wxSysErrorCode();
366 // return the error message for given (or last if 0) error code
367 WXDLLEXPORT const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
368
369 // define wxLog<level>
370 // -------------------
371
372 #define DECLARE_LOG_FUNCTION(level) \
373 extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
374 #define DECLARE_LOG_FUNCTION2(level, arg1) \
375 extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
376
377 #else // !wxUSE_LOG
378
379 // log functions do nothing at all
380 #define DECLARE_LOG_FUNCTION(level) \
381 inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {}
382 #define DECLARE_LOG_FUNCTION2(level, arg1) \
383 inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {}
384
385 #endif // wxUSE_LOG/!wxUSE_LOG
386
387 // a generic function for all levels (level is passes as parameter)
388 DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
389
390 // one function per each level
391 DECLARE_LOG_FUNCTION(FatalError);
392 DECLARE_LOG_FUNCTION(Error);
393 DECLARE_LOG_FUNCTION(Warning);
394 DECLARE_LOG_FUNCTION(Message);
395 DECLARE_LOG_FUNCTION(Info);
396 DECLARE_LOG_FUNCTION(Verbose);
397
398 // this function sends the log message to the status line of the top level
399 // application frame, if any
400 DECLARE_LOG_FUNCTION(Status);
401
402 // this one is the same as previous except that it allows to explicitly
403 // specify the frame to which the output should go
404 DECLARE_LOG_FUNCTION2(Status, wxFrame *pFrame);
405
406 // additional one: as wxLogError, but also logs last system call error code
407 // and the corresponding error message if available
408 DECLARE_LOG_FUNCTION(SysError);
409
410 // and another one which also takes the error code (for those broken APIs
411 // that don't set the errno (like registry APIs in Win32))
412 DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
413
414 // debug functions do nothing in release mode
415 #ifdef __WXDEBUG__
416 DECLARE_LOG_FUNCTION(Debug);
417
418 // first king of LogTrace is uncoditional: it doesn't check the level,
419 DECLARE_LOG_FUNCTION(Trace);
420
421 // this second version will only log the message if the mask had been
422 // added to the list of masks with AddTraceMask()
423 DECLARE_LOG_FUNCTION2(Trace, const char *mask);
424
425 // the last one does nothing if all of level bits are not set
426 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
427 // string identifiers
428 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
429 #else //!debug
430 // these functions do nothing in release builds
431 inline void wxLogDebug(const wxChar *, ...) { }
432 inline void wxLogTrace(const wxChar *, ...) { }
433 inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { }
434 inline void wxLogTrace(const wxChar *, const wxChar *, ...) { }
435 #endif // debug/!debug
436
437 // ----------------------------------------------------------------------------
438 // debug only logging functions: use them with API name and error code
439 // ----------------------------------------------------------------------------
440
441 #ifndef __TFILE__
442 #define __XFILE__(x) _T(x)
443 #define __TFILE__ __XFILE__(__FILE__)
444 #endif
445
446 #ifdef __WXDEBUG__
447 // make life easier for people using VC++ IDE: clicking on the message
448 // will take us immediately to the place of the failed API
449 #ifdef __VISUALC__
450 #define wxLogApiError(api, rc) \
451 wxLogDebug(_T("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
452 __TFILE__, __LINE__, api, \
453 rc, wxSysErrorMsg(rc))
454 #else // !VC++
455 #define wxLogApiError(api, rc) \
456 wxLogDebug(_T("In file %s at line %d: '%s' failed with " \
457 "error 0x%08lx (%s)."), \
458 __TFILE__, __LINE__, api, \
459 rc, wxSysErrorMsg(rc))
460 #endif // VC++/!VC++
461
462 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
463
464 #else //!debug
465 inline void wxLogApiError(const wxChar *, long) { }
466 inline void wxLogLastError(const wxChar *) { }
467 #endif //debug/!debug
468
469 #endif // _WX_LOG_H_