]> git.saurik.com Git - wxWidgets.git/blame - include/wx/log.h
no changes
[wxWidgets.git] / include / wx / log.h
CommitLineData
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
12#ifndef __LOGH__
13#define __LOGH__
14
15#ifdef __GNUG__
9ef3052c 16 #pragma interface
c801d85f
KB
17#endif //GNU C++
18
9ef3052c
VZ
19// ----------------------------------------------------------------------------
20// constants
21// ----------------------------------------------------------------------------
22
23// different standard log levels (you may also define your own)
24enum
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 __WINDOWS__
46 #define wxTraceOleCalls 0x0100 // OLE interface calls
47#endif
48
49typedef unsigned long wxTraceMask;
50typedef unsigned long wxLogLevel;
51
c801d85f 52// ----------------------------------------------------------------------------
cdb7168d 53>>>>>>> 1.2
c801d85f
KB
54// derive from this class to redirect (or suppress, or ...) log messages
55// normally, only a single instance of this class exists but it's not enforced
c801d85f
KB
56// ----------------------------------------------------------------------------
57class WXDLLEXPORT wxLog
58{
59public:
c801d85f
KB
60 // ctor
61 wxLog();
62
63 // sink function
9ef3052c 64 static void OnLog(wxLogLevel level, const char *szString)
c801d85f
KB
65 { if ( ms_pLogger != 0 ) ms_pLogger->DoLog(level, szString); }
66
67 // message buffering
68 // flush shows all messages if they're not logged immediately
69 // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
70 // showing 17 modal dialogs one after another)
71 virtual void Flush();
9ef3052c
VZ
72 // call to Flush() may be optimized: call it only if this function
73 // returns true (although Flush() also returns immediately if there
74 // is no messages, this functions is more efficient because inline)
c801d85f
KB
75 bool HasPendingMessages() const { return m_bHasMessages; }
76
77 // only one sink is active at each moment
78 // get current log target
79 static wxLog *GetActiveTarget();
80 // change log target, pLogger = NULL disables logging,
81 // returns the previous log target
82 static wxLog *SetActiveTarget(wxLog *pLogger);
83
84 // functions controlling the default wxLog behaviour
85 // verbose mode is activated by standard command-line '-verbose' option
9ef3052c 86 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
c801d85f
KB
87 // sets the format for timestamp prepended by wxLog::DoLog(): it's
88 // passed to strftime() function, see it's documentation for details.
9ef3052c
VZ
89 // no time stamp at all if szTF is NULL or empty
90 // NB: the string is not copied, so it's lifetime must be long enough!
91 void SetTimeStampFormat(const char *szTF) { m_szTimeFormat = szTF; }
92 // trace mask (see wxTraceXXX constants for details)
93 void SetTraceMask(wxTraceMask ulMask) { m_ulTraceMask = ulMask; }
c801d85f
KB
94
95 // accessors
96 // gets the verbose status
9ef3052c
VZ
97 bool GetVerbose() const { return m_bVerbose; }
98 // get current time format
99 const char *GetTimeStampFormat() const { return m_szTimeFormat; }
100 // get trace mask
101 wxTraceMask GetTraceMask() const { return m_ulTraceMask; }
c801d85f
KB
102
103 // make dtor virtual for all derived classes
104 virtual ~wxLog() { }
105
106protected:
107 bool m_bHasMessages;
108
9ef3052c
VZ
109 bool m_bVerbose; // FALSE => ignore LogInfo messages
110 const char *m_szTimeFormat; // format for strftime()
111 wxTraceMask m_ulTraceMask; // controls wxLogTrace behaviour
c801d85f 112
c801d85f
KB
113 // the logging functions that can be overriden
114 // default DoLog() prepends the time stamp and a prefix corresponding
115 // to the message to szString and then passes it to DoLogString()
9ef3052c 116 virtual void DoLog(wxLogLevel level, const char *szString);
c801d85f
KB
117 // default DoLogString does nothing but is not pure virtual because if
118 // you override DoLog() you might not need it at all
119 virtual void DoLogString(const char *szString);
120
9ef3052c
VZ
121private:
122 // static variables
123 // ----------------
c801d85f
KB
124 static wxLog *ms_pLogger; // currently active log sink
125 static bool ms_bInitialized; // any log targets created?
126};
127
128// ----------------------------------------------------------------------------
129// "trivial" derivations of wxLog
130// ----------------------------------------------------------------------------
131
132// log everything to a "FILE *", stderr by default
133class WXDLLEXPORT wxLogStderr : public wxLog
134{
135public:
136 // redirect log output to a FILE
137 wxLogStderr(FILE *fp = NULL);
138
139private:
140 // implement sink function
141 virtual void DoLogString(const char *szString);
142
143 FILE *m_fp;
144};
145
146// log everything to an "ostream", cerr by default
147class WXDLLEXPORT wxLogStream : public wxLog
148{
149public:
150 // redirect log output to an ostream
151 wxLogStream(ostream *ostr = NULL);
152
153protected:
154 // implement sink function
155 virtual void DoLogString(const char *szString);
156
157 // @@ using ptr here to avoid including <iostream.h> from this file
158 ostream *m_ostr;
159};
160
c801d85f 161// log everything to a text window (GUI only of course)
9ef3052c 162class wxTextCtrl;
c801d85f
KB
163class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
164{
165public:
166 // we just create an ostream from wxTextCtrl and use it in base class
167 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
168 ~wxLogTextCtrl();
169};
c801d85f
KB
170
171// ----------------------------------------------------------------------------
172// GUI log target, the default one for wxWindows programs
173// ----------------------------------------------------------------------------
174class WXDLLEXPORT wxLogGui : public wxLog
175{
176public:
177 // ctor
178 wxLogGui();
179
180 // show all messages that were logged since the last Flush()
181 virtual void Flush();
182
183protected:
9ef3052c 184 virtual void DoLog(wxLogLevel level, const char *szString);
c801d85f
KB
185
186 wxArrayString m_aMessages;
187 bool m_bErrors;
188};
189
9ef3052c
VZ
190// ----------------------------------------------------------------------------
191// (background) log window: this class forwards all log messages to the log
192// target which was active when it was instantiated, but also collects them
193// to the log window. This window has it's own menu which allows the user to
194// close it, clear the log contents or save it to the file.
195// ----------------------------------------------------------------------------
196class wxLogFrame;
197class WXDLLEXPORT wxLogWindow : public wxLog
198{
199public:
200 wxLogWindow(const wxTString& strTitle);
201 ~wxLogWindow();
202
203 // show/hide the log window
204 void Show(bool bShow = TRUE);
205
206protected:
207 virtual void DoLog(wxLogLevel level, const char *szString);
208 virtual void DoLogString(const char *szString);
209
210private:
211 wxLog *m_pOldLog; // previous log target
212 wxLogFrame *m_pLogFrame; // the log frame
213};
214
c801d85f
KB
215// ----------------------------------------------------------------------------
216// /dev/null log target: suppress logging until this object goes out of scope
217// ----------------------------------------------------------------------------
218
219// example of usage:
220/*
221void Foo() {
222 wxFile file;
223
224 // wxFile.Open() normally complains if file can't be opened, we don't want it
225 wxLogNull logNo;
226 if ( !file.Open("bar") )
227 ... process error ourselves ...
228
229 // ~wxLogNull called, old log sink restored
230}
231*/
232class WXDLLEXPORT wxLogNull
233{
234public:
235 // ctor saves old log target, dtor restores it
236 wxLogNull() { m_pPrevLogger = wxLog::SetActiveTarget(NULL); }
237 ~wxLogNull() { (void)wxLog::SetActiveTarget(m_pPrevLogger); }
238
239private:
240 wxLog *m_pPrevLogger; // old log target
241};
242
243// ============================================================================
244// global functions
245// ============================================================================
246
247// ----------------------------------------------------------------------------
248// Log functions should be used by application instead of stdio, iostream &c
249// for log messages for easy redirection
250// ----------------------------------------------------------------------------
251
252// define wxLog<level>
253// -------------------
254
255// NB: all these functions take `wxTString' and not
256// `const wxTString&' because according to C++ standard
257// the first argument to a vararg function can not be
258// an array, function or reference :-(
259
260// the most generic log function
9ef3052c 261void WXDLLEXPORT wxLogGeneric(wxLogLevel level, wxTString strFormat, ...);
c801d85f
KB
262
263#define DECLARE_LOG_FUNCTION(level) \
264 extern void WXDLLEXPORT wxLog##level(wxTString strFormat, ...)
265
266// one function per each level
267DECLARE_LOG_FUNCTION(FatalError);
268DECLARE_LOG_FUNCTION(Error);
269DECLARE_LOG_FUNCTION(Warning);
270DECLARE_LOG_FUNCTION(Message);
271DECLARE_LOG_FUNCTION(Info);
272DECLARE_LOG_FUNCTION(Status);
273DECLARE_LOG_FUNCTION(Verbose);
274
275// additional one: as wxLogError, but also logs last system call error code
276// and the corresponding error message if available
277DECLARE_LOG_FUNCTION(SysError);
278
279// and another one which also takes the error code (for those broken APIs
280// that don't set the errno (like registry APIs in Win32))
281void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...);
282
9ef3052c
VZ
283// debug functions do nothing in release mode
284#ifdef __DEBUG__
285 // NB: debug functions don't translate their arguments
286 extern void WXDLLEXPORT wxLogDebug(const char *szFormat, ...);
287
288 // first king of LogTrace is uncoditional: it doesn't check the level,
289 // while the second one does nothing if all of level bits are not set
290 // in wxLog::GetActive()->GetTraceMask().
291 extern void WXDLLEXPORT wxLogTrace(const char *szFormat, ...);
292 extern void WXDLLEXPORT wxLogTrace(wxTraceMask mask,
293 const char *szFormat, ...);
294#else //!debug
295 #define wxLogDebug
296 #define wxLogTrace
297#endif
c801d85f 298
c801d85f
KB
299
300// are we in 'verbose' mode?
301// (note that it's often handy to change this var manually from the
302// debugger, thus enabling/disabling verbose reporting for some
303// parts of the program only)
304WXDLLEXPORT_DATA(extern bool) g_bVerbose;
305
306// fwd decl to avoid including iostream.h here
307class ostream;
308
309// ----------------------------------------------------------------------------
310// get error code/error message from system in a portable way
311// ----------------------------------------------------------------------------
312
313// return the last system error code
314unsigned long WXDLLEXPORT wxSysErrorCode();
315// return the error message for given (or last if 0) error code
316const char* WXDLLEXPORT wxSysErrorMsg(unsigned long nErrCode = 0);
317
318// ----------------------------------------------------------------------------
319// debug only logging functions: use them with API name and error code
320// ----------------------------------------------------------------------------
321
322#ifdef __DEBUG__
323 #define wxLogApiError(api, rc) \
324 wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
325 __FILE__, __LINE__, api, \
326 rc, wxSysErrorMsg(rc))
9ef3052c 327 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
c801d85f
KB
328#else //!debug
329 #define wxLogApiError(api, rc)
330 #define wxLogLastError(api)
331#endif //debug/!debug
332
333#endif //__LOGH__