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