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