]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/log.h
added more HP-UX charset names
[wxWidgets.git] / include / wx / log.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/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 licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_LOG_H_
13#define _WX_LOG_H_
14
15#include "wx/defs.h"
16
17// ----------------------------------------------------------------------------
18// common constants for use in wxUSE_LOG/!wxUSE_LOG
19// ----------------------------------------------------------------------------
20
21// the trace masks have been superceded by symbolic trace constants, they're
22// for compatibility only andwill be removed soon - do NOT use them
23
24// meaning of different bits of the trace mask (which allows selectively
25// enable/disable some trace messages)
26#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
27#define wxTraceMessages 0x0002 // trace window messages/X callbacks
28#define wxTraceResAlloc 0x0004 // trace GDI resource allocation
29#define wxTraceRefCount 0x0008 // trace various ref counting operations
30
31#ifdef __WXMSW__
32 #define wxTraceOleCalls 0x0100 // OLE interface calls
33#endif
34
35// ----------------------------------------------------------------------------
36// types
37// ----------------------------------------------------------------------------
38
39// NB: these types are needed even if wxUSE_LOG == 0
40typedef unsigned long wxTraceMask;
41typedef unsigned long wxLogLevel;
42
43// ----------------------------------------------------------------------------
44// headers
45// ----------------------------------------------------------------------------
46
47#include "wx/string.h"
48
49#if wxUSE_LOG
50
51#include "wx/arrstr.h"
52
53#ifndef __WXWINCE__
54 #include <time.h> // for time_t
55#endif
56
57#include "wx/dynarray.h"
58
59#ifndef wxUSE_LOG_DEBUG
60# ifdef __WXDEBUG__
61# define wxUSE_LOG_DEBUG 1
62# else // !__WXDEBUG__
63# define wxUSE_LOG_DEBUG 0
64# endif
65#endif
66
67// ----------------------------------------------------------------------------
68// forward declarations
69// ----------------------------------------------------------------------------
70
71#if wxUSE_GUI
72 class WXDLLIMPEXP_CORE wxTextCtrl;
73 class WXDLLIMPEXP_CORE wxLogFrame;
74 class WXDLLIMPEXP_CORE wxFrame;
75 class WXDLLIMPEXP_CORE wxWindow;
76#endif // wxUSE_GUI
77
78// ----------------------------------------------------------------------------
79// constants
80// ----------------------------------------------------------------------------
81
82// different standard log levels (you may also define your own)
83enum
84{
85 wxLOG_FatalError, // program can't continue, abort immediately
86 wxLOG_Error, // a serious error, user must be informed about it
87 wxLOG_Warning, // user is normally informed about it but may be ignored
88 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
89 wxLOG_Status, // informational: might go to the status line of GUI app
90 wxLOG_Info, // informational message (a.k.a. 'Verbose')
91 wxLOG_Debug, // never shown to the user, disabled in release mode
92 wxLOG_Trace, // trace messages are also only enabled in debug mode
93 wxLOG_Progress, // used for progress indicator (not yet)
94 wxLOG_User = 100, // user defined levels start here
95 wxLOG_Max = 10000
96};
97
98// symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
99// discarded unless the string "foo" has been added to the list of allowed
100// ones with AddTraceMask()
101
102#define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
103#define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
104#define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
105#define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
106
107#ifdef __WXMSW__
108 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
109#endif
110
111#include "wx/iosfwrap.h"
112
113// ----------------------------------------------------------------------------
114// derive from this class to redirect (or suppress, or ...) log messages
115// normally, only a single instance of this class exists but it's not enforced
116// ----------------------------------------------------------------------------
117
118class WXDLLIMPEXP_BASE wxLog
119{
120public:
121 // ctor
122 wxLog(){}
123
124 // Internal buffer.
125
126 // Allow replacement of the fixed size static buffer with
127 // a user allocated one. Pass in NULL to restore the
128 // built in static buffer.
129 static wxChar *SetLogBuffer( wxChar *buf, size_t size = 0 );
130
131 // these functions allow to completely disable all log messages
132
133 // is logging disabled now?
134 static bool IsEnabled() { return ms_doLog; }
135
136 // change the flag state, return the previous one
137 static bool EnableLogging(bool doIt = true)
138 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
139
140 // static sink function - see DoLog() for function to overload in the
141 // derived classes
142 static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
143 {
144 if ( IsEnabled() && ms_logLevel >= level )
145 {
146 wxLog *pLogger = GetActiveTarget();
147 if ( pLogger )
148 pLogger->DoLog(level, szString, t);
149 }
150 }
151
152 // message buffering
153
154 // flush shows all messages if they're not logged immediately (FILE
155 // and iostream logs don't need it, but wxGuiLog does to avoid showing
156 // 17 modal dialogs one after another)
157 virtual void Flush();
158
159 // flush the active target if any
160 static void FlushActive()
161 {
162 if ( !ms_suspendCount )
163 {
164 wxLog *log = GetActiveTarget();
165 if ( log )
166 log->Flush();
167 }
168 }
169
170 // only one sink is active at each moment
171 // get current log target, will call wxApp::CreateLogTarget() to
172 // create one if none exists
173 static wxLog *GetActiveTarget();
174
175 // change log target, pLogger may be NULL
176 static wxLog *SetActiveTarget(wxLog *pLogger);
177
178 // suspend the message flushing of the main target until the next call
179 // to Resume() - this is mainly for internal use (to prevent wxYield()
180 // from flashing the messages)
181 static void Suspend() { ms_suspendCount++; }
182
183 // must be called for each Suspend()!
184 static void Resume() { ms_suspendCount--; }
185
186 // functions controlling the default wxLog behaviour
187 // verbose mode is activated by standard command-line '-verbose'
188 // option
189 static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; }
190
191 // Set log level. Log messages with level > logLevel will not be logged.
192 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
193
194 // should GetActiveTarget() try to create a new log object if the
195 // current is NULL?
196 static void DontCreateOnDemand();
197
198 // trace mask (see wxTraceXXX constants for details)
199 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
200
201 // add string trace mask
202 static void AddTraceMask(const wxString& str)
203 { ms_aTraceMasks.push_back(str); }
204
205 // add string trace mask
206 static void RemoveTraceMask(const wxString& str);
207
208 // remove all string trace masks
209 static void ClearTraceMasks();
210
211 // get string trace masks
212 static const wxArrayString &GetTraceMasks() { return ms_aTraceMasks; }
213
214 // sets the timestamp string: this is used as strftime() format string
215 // for the log targets which add time stamps to the messages - set it
216 // to NULL to disable time stamping completely.
217 static void SetTimestamp(const wxChar *ts) { ms_timestamp = ts; }
218
219
220 // accessors
221
222 // gets the verbose status
223 static bool GetVerbose() { return ms_bVerbose; }
224
225 // get trace mask
226 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
227
228 // is this trace mask in the list?
229 static bool IsAllowedTraceMask(const wxChar *mask);
230
231 // return the current loglevel limit
232 static wxLogLevel GetLogLevel() { return ms_logLevel; }
233
234 // get the current timestamp format string (may be NULL)
235 static const wxChar *GetTimestamp() { return ms_timestamp; }
236
237
238 // helpers
239
240 // put the time stamp into the string if ms_timestamp != NULL (don't
241 // change it otherwise)
242 static void TimeStamp(wxString *str);
243
244 // make dtor virtual for all derived classes
245 virtual ~wxLog() { }
246
247
248 // this method exists for backwards compatibility only, don't use
249 bool HasPendingMessages() const { return true; }
250
251protected:
252 // the logging functions that can be overriden
253
254 // default DoLog() prepends the time stamp and a prefix corresponding
255 // to the message to szString and then passes it to DoLogString()
256 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
257
258 // default DoLogString does nothing but is not pure virtual because if
259 // you override DoLog() you might not need it at all
260 virtual void DoLogString(const wxChar *szString, time_t t);
261
262private:
263 // static variables
264 // ----------------
265
266 static wxLog *ms_pLogger; // currently active log sink
267 static bool ms_doLog; // false => all logging disabled
268 static bool ms_bAutoCreate; // create new log targets on demand?
269 static bool ms_bVerbose; // false => ignore LogInfo messages
270
271 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
272
273 static size_t ms_suspendCount; // if positive, logs are not flushed
274
275 // format string for strftime(), if NULL, time stamping log messages is
276 // disabled
277 static const wxChar *ms_timestamp;
278
279 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
280 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
281};
282
283// ----------------------------------------------------------------------------
284// "trivial" derivations of wxLog
285// ----------------------------------------------------------------------------
286
287// log everything to a buffer
288class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog
289{
290public:
291 wxLogBuffer() { }
292
293 // get the string contents with all messages logged
294 const wxString& GetBuffer() const { return m_str; }
295
296 // show the buffer contents to the user in the best possible way (this uses
297 // wxMessageOutputMessageBox) and clear it
298 virtual void Flush();
299
300protected:
301 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
302 virtual void DoLogString(const wxChar *szString, time_t t);
303
304private:
305 wxString m_str;
306
307 DECLARE_NO_COPY_CLASS(wxLogBuffer)
308};
309
310
311// log everything to a "FILE *", stderr by default
312class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
313{
314public:
315 // redirect log output to a FILE
316 wxLogStderr(FILE *fp = (FILE *) NULL);
317
318protected:
319 // implement sink function
320 virtual void DoLogString(const wxChar *szString, time_t t);
321
322 FILE *m_fp;
323
324 DECLARE_NO_COPY_CLASS(wxLogStderr)
325};
326
327#if wxUSE_STD_IOSTREAM
328
329// log everything to an "ostream", cerr by default
330class WXDLLIMPEXP_BASE wxLogStream : public wxLog
331{
332public:
333 // redirect log output to an ostream
334 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
335
336protected:
337 // implement sink function
338 virtual void DoLogString(const wxChar *szString, time_t t);
339
340 // using ptr here to avoid including <iostream.h> from this file
341 wxSTD ostream *m_ostr;
342};
343
344#endif // wxUSE_STD_IOSTREAM
345
346// ----------------------------------------------------------------------------
347// /dev/null log target: suppress logging until this object goes out of scope
348// ----------------------------------------------------------------------------
349
350// example of usage:
351/*
352 void Foo()
353 {
354 wxFile file;
355
356 // wxFile.Open() normally complains if file can't be opened, we don't
357 // want it
358 wxLogNull logNo;
359
360 if ( !file.Open("bar") )
361 ... process error ourselves ...
362
363 // ~wxLogNull called, old log sink restored
364 }
365 */
366class WXDLLIMPEXP_BASE wxLogNull
367{
368public:
369 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
370 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
371
372private:
373 bool m_flagOld; // the previous value of the wxLog::ms_doLog
374};
375
376// ----------------------------------------------------------------------------
377// chaining log target: installs itself as a log target and passes all
378// messages to the real log target given to it in the ctor but also forwards
379// them to the previously active one
380//
381// note that you don't have to call SetActiveTarget() with this class, it
382// does it itself in its ctor
383// ----------------------------------------------------------------------------
384
385class WXDLLIMPEXP_BASE wxLogChain : public wxLog
386{
387public:
388 wxLogChain(wxLog *logger);
389 virtual ~wxLogChain();
390
391 // change the new log target
392 void SetLog(wxLog *logger);
393
394 // this can be used to temporarily disable (and then reenable) passing
395 // messages to the old logger (by default we do pass them)
396 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
397
398 // are we passing the messages to the previous log target?
399 bool IsPassingMessages() const { return m_bPassMessages; }
400
401 // return the previous log target (may be NULL)
402 wxLog *GetOldLog() const { return m_logOld; }
403
404 // override base class version to flush the old logger as well
405 virtual void Flush();
406
407protected:
408 // pass the chain to the old logger if needed
409 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
410
411private:
412 // the current log target
413 wxLog *m_logNew;
414
415 // the previous log target
416 wxLog *m_logOld;
417
418 // do we pass the messages to the old logger?
419 bool m_bPassMessages;
420
421 DECLARE_NO_COPY_CLASS(wxLogChain)
422};
423
424// a chain log target which uses itself as the new logger
425class WXDLLIMPEXP_BASE wxLogPassThrough : public wxLogChain
426{
427public:
428 wxLogPassThrough();
429
430private:
431 DECLARE_NO_COPY_CLASS(wxLogPassThrough)
432};
433
434#if wxUSE_GUI
435 // include GUI log targets:
436 #include "wx/generic/logg.h"
437#endif // wxUSE_GUI
438
439// ============================================================================
440// global functions
441// ============================================================================
442
443// ----------------------------------------------------------------------------
444// Log functions should be used by application instead of stdio, iostream &c
445// for log messages for easy redirection
446// ----------------------------------------------------------------------------
447
448// ----------------------------------------------------------------------------
449// get error code/error message from system in a portable way
450// ----------------------------------------------------------------------------
451
452// return the last system error code
453WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
454
455// return the error message for given (or last if 0) error code
456WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
457
458// ----------------------------------------------------------------------------
459// define wxLog<level>
460// ----------------------------------------------------------------------------
461
462#define DECLARE_LOG_FUNCTION(level) \
463extern void WXDLLIMPEXP_BASE wxVLog##level(const wxChar *szFormat, \
464 va_list argptr); \
465extern void WXDLLIMPEXP_BASE wxLog##level(const wxChar *szFormat, \
466 ...) ATTRIBUTE_PRINTF_1
467#define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
468extern void expdecl wxVLog##level(argclass arg, \
469 const wxChar *szFormat, \
470 va_list argptr); \
471extern void expdecl wxLog##level(argclass arg, \
472 const wxChar *szFormat, \
473 ...) ATTRIBUTE_PRINTF_2
474#else // !wxUSE_LOG
475
476// log functions do nothing at all
477#define DECLARE_LOG_FUNCTION(level) \
478inline void wxVLog##level(const wxChar *WXUNUSED(szFormat), \
479 va_list WXUNUSED(argptr)) { } \
480inline void wxLog##level(const wxChar *WXUNUSED(szFormat), \
481 ...) { }
482#define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
483inline void wxVLog##level(argclass WXUNUSED(arg), \
484 const wxChar *WXUNUSED(szFormat), \
485 va_list WXUNUSED(argptr)) {} \
486inline void wxLog##level(argclass WXUNUSED(arg), \
487 const wxChar *WXUNUSED(szFormat), \
488 ...) { }
489
490// Empty Class to fake wxLogNull
491class WXDLLIMPEXP_BASE wxLogNull
492{
493public:
494 wxLogNull() { }
495};
496
497// Dummy macros to replace some functions.
498#define wxSysErrorCode() (unsigned long)0
499#define wxSysErrorMsg( X ) (const wxChar*)NULL
500
501// Fake symbolic trace masks... for those that are used frequently
502#define wxTRACE_OleCalls wxEmptyString // OLE interface calls
503
504#endif // wxUSE_LOG/!wxUSE_LOG
505#define DECLARE_LOG_FUNCTION2(level, argclass, arg) \
506 DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, WXDLLIMPEXP_BASE)
507
508
509// a generic function for all levels (level is passes as parameter)
510DECLARE_LOG_FUNCTION2(Generic, wxLogLevel, level);
511
512// one function per each level
513DECLARE_LOG_FUNCTION(FatalError);
514DECLARE_LOG_FUNCTION(Error);
515DECLARE_LOG_FUNCTION(Warning);
516DECLARE_LOG_FUNCTION(Message);
517DECLARE_LOG_FUNCTION(Info);
518DECLARE_LOG_FUNCTION(Verbose);
519
520// this function sends the log message to the status line of the top level
521// application frame, if any
522DECLARE_LOG_FUNCTION(Status);
523
524#if wxUSE_GUI
525 // this one is the same as previous except that it allows to explicitly
526 class WXDLLEXPORT wxFrame;
527 // specify the frame to which the output should go
528 DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *, pFrame, WXDLLIMPEXP_CORE);
529#endif // wxUSE_GUI
530
531// additional one: as wxLogError, but also logs last system call error code
532// and the corresponding error message if available
533DECLARE_LOG_FUNCTION(SysError);
534
535// and another one which also takes the error code (for those broken APIs
536// that don't set the errno (like registry APIs in Win32))
537DECLARE_LOG_FUNCTION2(SysError, long, lErrCode);
538
539// debug functions do nothing in release mode
540#if wxUSE_LOG_DEBUG
541 DECLARE_LOG_FUNCTION(Debug);
542
543 // there is no more unconditional LogTrace: it is not different from
544 // LogDebug and it creates overload ambiguities
545 //DECLARE_LOG_FUNCTION(Trace);
546
547 // this version only logs the message if the mask had been added to the
548 // list of masks with AddTraceMask()
549 DECLARE_LOG_FUNCTION2(Trace, const wxChar *, mask);
550
551 // and this one does nothing if all of level bits are not set in
552 // wxLog::GetActive()->GetTraceMask() -- it's deprecated in favour of
553 // string identifiers
554 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask, mask);
555#else //!debug
556 // these functions do nothing in release builds
557
558 // note that leaving out "fmt" in the vararg functions provokes a warning
559 // from SGI CC: "the last argument of the varargs function is unnamed"
560 inline void wxVLogDebug(const wxChar *, va_list) { }
561 inline void wxLogDebug(const wxChar *fmt, ...) { wxUnusedVar(fmt); }
562 inline void wxVLogTrace(wxTraceMask, const wxChar *, va_list) { }
563 inline void wxLogTrace(wxTraceMask, const wxChar *fmt, ...) { wxUnusedVar(fmt); }
564 inline void wxVLogTrace(const wxChar *, const wxChar *, va_list) { }
565 inline void wxLogTrace(const wxChar *, const wxChar *fmt, ...) { wxUnusedVar(fmt); }
566#endif // debug/!debug
567
568// wxLogFatalError helper: show the (fatal) error to the user in a safe way,
569// i.e. without using wxMessageBox() for example because it could crash
570void WXDLLIMPEXP_BASE
571wxSafeShowMessage(const wxString& title, const wxString& text);
572
573// ----------------------------------------------------------------------------
574// debug only logging functions: use them with API name and error code
575// ----------------------------------------------------------------------------
576
577#ifdef __WXDEBUG__
578 // make life easier for people using VC++ IDE: clicking on the message
579 // will take us immediately to the place of the failed API
580#ifdef __VISUALC__
581 #define wxLogApiError(api, rc) \
582 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
583 __TFILE__, __LINE__, api, \
584 (long)rc, wxSysErrorMsg(rc))
585#else // !VC++
586 #define wxLogApiError(api, rc) \
587 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
588 wxT("error 0x%08lx (%s)."), \
589 __TFILE__, __LINE__, api, \
590 (long)rc, wxSysErrorMsg(rc))
591#endif // VC++/!VC++
592
593 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
594
595#else //!debug
596 inline void wxLogApiError(const wxChar *, long) { }
597 inline void wxLogLastError(const wxChar *) { }
598#endif //debug/!debug
599
600// wxCocoa has additiional trace masks
601#if defined(__WXCOCOA__)
602#include "wx/cocoa/log.h"
603#endif
604
605#endif // _WX_LOG_H_
606