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