Expand wxString overview and document some problems due to its dual nature.
[wxWidgets.git] / interface / wx / log.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: log.h
3 // Purpose: interface of wxLog* classes
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #if wxUSE_BASE
10
11 /**
12 Different standard log levels (you may also define your own) used with
13 by standard wxLog functions wxLogGeneric(), wxLogError(), wxLogWarning(), etc...
14 */
15 enum wxLogLevelValues
16 {
17 wxLOG_FatalError, //!< program can't continue, abort immediately
18 wxLOG_Error, //!< a serious error, user must be informed about it
19 wxLOG_Warning, //!< user is normally informed about it but may be ignored
20 wxLOG_Message, //!< normal message (i.e. normal output of a non GUI app)
21 wxLOG_Status, //!< informational: might go to the status line of GUI app
22 wxLOG_Info, //!< informational message (a.k.a. 'Verbose')
23 wxLOG_Debug, //!< never shown to the user, disabled in release mode
24 wxLOG_Trace, //!< trace messages are also only enabled in debug mode
25 wxLOG_Progress, //!< used for progress indicator (not yet)
26 wxLOG_User = 100, //!< user defined levels start here
27 wxLOG_Max = 10000
28 };
29
30 /**
31 The type used to specify a log level.
32
33 Default values of ::wxLogLevel used by wxWidgets are contained in the
34 ::wxLogLevelValues enumeration.
35 */
36 typedef unsigned long wxLogLevel;
37
38 /**
39 Information about a log record (unit of the log output).
40 */
41 class wxLogRecordInfo
42 {
43 public:
44 /// The name of the file where this log message was generated.
45 const char *filename;
46
47 /// The line number at which this log message was generated.
48 int line;
49
50 /**
51 The name of the function where the log record was generated.
52
53 This field may be @NULL if the compiler doesn't support @c __FUNCTION__
54 (but most modern compilers do).
55 */
56 const char *func;
57
58 /// Time when the log message was generated.
59 time_t timestamp;
60
61 /**
62 Id of the thread in which the message was generated.
63
64 This field is only available if wxWidgets was built with threads
65 support (<code>wxUSE_THREADS == 1</code>).
66
67 @see wxThread::GetCurrentId()
68 */
69 wxThreadIdType threadId;
70 };
71
72 /**
73 @class wxLogFormatter
74
75 wxLogFormatter class is used to format the log messages. It implements the
76 default formatting and can be derived from to create custom formatters.
77
78 The default implementation formats the message into a string containing
79 the time stamp, level-dependent prefix and the message itself.
80
81 To change it, you can derive from it and override its Format() method. For
82 example, to include the thread id in the log messages you can use
83 @code
84 class LogFormatterWithThread : public wxLogFormatter
85 {
86 virtual wxString Format(wxLogLevel level,
87 const wxString& msg,
88 const wxLogRecordInfo& info) const
89 {
90 return wxString::Format("[%d] %s(%d) : %s",
91 info.threadId, info.filename, info.line, msg);
92 }
93 };
94 @endcode
95 And then associate it with wxLog instance using its SetFormatter(). Then,
96 if you call:
97
98 @code
99 wxLogMessage(_("*** Application started ***"));
100 @endcode
101
102 the log output could be something like:
103
104 @verbatim
105 [7872] d:\testApp\src\testApp.cpp(85) : *** Application started ***
106 @endverbatim
107
108 @library{wxbase}
109 @category{logging}
110
111 @see @ref overview_log
112
113 @since 2.9.4
114 */
115 class wxLogFormatter
116 {
117 public:
118 /**
119 The default ctor does nothing.
120 */
121 wxLogFormatter();
122
123
124 /**
125 This function creates the full log message string.
126
127 Override it to customize the output string format.
128
129 @param level
130 The level of this log record, e.g. ::wxLOG_Error.
131 @param msg
132 The log message itself.
133 @param info
134 All the other information (such as time, component, location...)
135 associated with this log record.
136
137 @return
138 The formated message.
139
140 @note
141 Time stamping is disabled for Visual C++ users in debug builds by
142 default because otherwise it would be impossible to directly go to the line
143 from which the log message was generated by simply clicking in the debugger
144 window on the corresponding error message. If you wish to enable it, override
145 FormatTime().
146 */
147 virtual wxString Format(wxLogLevel level,
148 const wxString& msg,
149 const wxLogRecordInfo& info) const;
150
151 protected:
152 /**
153 This function formats the time stamp part of the log message.
154
155 Override this function if you need to customize just the time stamp.
156
157 @param time
158 Time to format.
159
160 @return
161 The formated time string, may be empty.
162 */
163 virtual wxString FormatTime(time_t time) const;
164 };
165
166
167 /**
168 @class wxLog
169
170 wxLog class defines the interface for the <em>log targets</em> used by wxWidgets
171 logging functions as explained in the @ref overview_log.
172
173 The only situations when you need to directly use this class is when you want
174 to derive your own log target because the existing ones don't satisfy your
175 needs.
176
177 Otherwise, it is completely hidden behind the @ref group_funcmacro_log "wxLogXXX() functions"
178 and you may not even know about its existence.
179
180 @note For console-mode applications, the default target is wxLogStderr, so
181 that all @e wxLogXXX() functions print on @c stderr when @c wxUSE_GUI = 0.
182
183 @library{wxbase}
184 @category{logging}
185
186 @see @ref overview_log, @ref group_funcmacro_log "wxLogXXX() functions"
187 */
188 class wxLog
189 {
190 public:
191 /**
192 @name Trace mask functions
193 */
194 //@{
195
196 /**
197 Add the @a mask to the list of allowed masks for wxLogTrace().
198
199 @see RemoveTraceMask(), GetTraceMasks()
200 */
201 static void AddTraceMask(const wxString& mask);
202
203 /**
204 Removes all trace masks previously set with AddTraceMask().
205
206 @see RemoveTraceMask()
207 */
208 static void ClearTraceMasks();
209
210 /**
211 Returns the currently allowed list of string trace masks.
212
213 @see AddTraceMask().
214 */
215 static const wxArrayString& GetTraceMasks();
216
217 /**
218 Returns @true if the @a mask is one of allowed masks for wxLogTrace().
219
220 See also: AddTraceMask(), RemoveTraceMask()
221 */
222 static bool IsAllowedTraceMask(const wxString& mask);
223
224 /**
225 Remove the @a mask from the list of allowed masks for
226 wxLogTrace().
227
228 @see AddTraceMask()
229 */
230 static void RemoveTraceMask(const wxString& mask);
231
232 //@}
233
234
235
236 /**
237 @name Log target functions
238 */
239 //@{
240
241 /**
242 Instructs wxLog to not create new log targets on the fly if there is none
243 currently (see GetActiveTarget()).
244
245 (Almost) for internal use only: it is supposed to be called by the
246 application shutdown code (where you don't want the log target to be
247 automatically created anymore).
248
249 Note that this function also calls ClearTraceMasks().
250 */
251 static void DontCreateOnDemand();
252
253 /**
254 Returns the pointer to the active log target (may be @NULL).
255
256 Notice that if SetActiveTarget() hadn't been previously explicitly
257 called, this function will by default try to create a log target by
258 calling wxAppTraits::CreateLogTarget() which may be overridden in a
259 user-defined traits class to change the default behaviour. You may also
260 call DontCreateOnDemand() to disable this behaviour.
261
262 When this function is called from threads other than main one,
263 auto-creation doesn't happen. But if the thread has a thread-specific
264 log target previously set by SetThreadActiveTarget(), it is returned
265 instead of the global one. Otherwise, the global log target is
266 returned.
267 */
268 static wxLog* GetActiveTarget();
269
270 /**
271 Sets the specified log target as the active one.
272
273 Returns the pointer to the previous active log target (may be @NULL).
274 To suppress logging use a new instance of wxLogNull not @NULL. If the
275 active log target is set to @NULL a new default log target will be
276 created when logging occurs.
277
278 @see SetThreadActiveTarget()
279 */
280 static wxLog* SetActiveTarget(wxLog* logtarget);
281
282 /**
283 Sets a thread-specific log target.
284
285 The log target passed to this function will be used for all messages
286 logged by the current thread using the usual wxLog functions. This
287 shouldn't be called from the main thread which never uses a thread-
288 specific log target but can be used for the other threads to handle
289 thread logging completely separately; instead of buffering thread log
290 messages in the main thread logger.
291
292 Notice that unlike for SetActiveTarget(), wxWidgets does not destroy
293 the thread-specific log targets when the thread terminates so doing
294 this is your responsibility.
295
296 This method is only available if @c wxUSE_THREADS is 1, i.e. wxWidgets
297 was compiled with threads support.
298
299 @param logger
300 The new thread-specific log target, possibly @NULL.
301 @return
302 The previous thread-specific log target, initially @NULL.
303
304 @since 2.9.1
305 */
306 static wxLog *SetThreadActiveTarget(wxLog *logger);
307
308 /**
309 Flushes the current log target if any, does nothing if there is none.
310
311 When this method is called from the main thread context, it also
312 flushes any previously buffered messages logged by the other threads.
313 When it is called from the other threads it simply calls Flush() on the
314 currently active log target, so it mostly makes sense to do this if a
315 thread has its own logger set with SetThreadActiveTarget().
316 */
317 static void FlushActive();
318
319 /**
320 Resumes logging previously suspended by a call to Suspend().
321 All messages logged in the meanwhile will be flushed soon.
322 */
323 static void Resume();
324
325 /**
326 Suspends the logging until Resume() is called.
327
328 Note that the latter must be called the same number of times as the former
329 to undo it, i.e. if you call Suspend() twice you must call Resume() twice as well.
330
331 Note that suspending the logging means that the log sink won't be flushed
332 periodically, it doesn't have any effect if the current log target does the
333 logging immediately without waiting for Flush() to be called (the standard
334 GUI log target only shows the log dialog when it is flushed, so Suspend()
335 works as expected with it).
336
337 @see Resume(), wxLogNull
338 */
339 static void Suspend();
340
341 //@}
342
343
344
345 /**
346 @name Log level functions
347 */
348 //@{
349
350 /**
351 Returns the current log level limit.
352
353 All messages at levels strictly greater than the value returned by this
354 function are not logged at all.
355
356 @see SetLogLevel(), IsLevelEnabled()
357 */
358 static wxLogLevel GetLogLevel();
359
360 /**
361 Returns true if logging at this level is enabled for the current thread.
362
363 This function only returns @true if logging is globally enabled and if
364 @a level is less than or equal to the maximal log level enabled for the
365 given @a component.
366
367 @see IsEnabled(), SetLogLevel(), GetLogLevel(), SetComponentLevel()
368
369 @since 2.9.1
370 */
371 static bool IsLevelEnabled(wxLogLevel level, wxString component);
372
373 /**
374 Sets the log level for the given component.
375
376 For example, to disable all but error messages from wxWidgets network
377 classes you may use
378 @code
379 wxLog::SetComponentLevel("wx/net", wxLOG_Error);
380 @endcode
381
382 SetLogLevel() may be used to set the global log level.
383
384 @param component
385 Non-empty component name, possibly using slashes (@c /) to separate
386 it into several parts.
387 @param level
388 Maximal level of log messages from this component which will be
389 handled instead of being simply discarded.
390
391 @since 2.9.1
392 */
393 static void SetComponentLevel(const wxString& component, wxLogLevel level);
394
395 /**
396 Specifies that log messages with level greater (numerically) than
397 @a logLevel should be ignored and not sent to the active log target.
398
399 @see SetComponentLevel()
400 */
401 static void SetLogLevel(wxLogLevel logLevel);
402
403 //@}
404
405
406
407 /**
408 @name Enable/disable features functions
409 */
410 //@{
411
412 /**
413 Globally enable or disable logging.
414
415 Calling this function with @false argument disables all log messages
416 for the current thread.
417
418 @see wxLogNull, IsEnabled()
419
420 @return
421 The old state, i.e. @true if logging was previously enabled and
422 @false if it was disabled.
423 */
424 static bool EnableLogging(bool enable = true);
425
426 /**
427 Returns true if logging is enabled at all now.
428
429 @see IsLevelEnabled(), EnableLogging()
430 */
431 static bool IsEnabled();
432
433 /**
434 Returns whether the repetition counting mode is enabled.
435 */
436 static bool GetRepetitionCounting();
437
438 /**
439 Enables logging mode in which a log message is logged once, and in case exactly
440 the same message successively repeats one or more times, only the number of
441 repetitions is logged.
442 */
443 static void SetRepetitionCounting(bool repetCounting = true);
444
445 /**
446 Returns the current timestamp format string.
447
448 Notice that the current time stamp is only used by the default log
449 formatter and custom formatters may ignore this format.
450 */
451 static const wxString& GetTimestamp();
452
453 /**
454 Sets the timestamp format prepended by the default log targets to all
455 messages. The string may contain any normal characters as well as %
456 prefixed format specifiers, see @e strftime() manual for details.
457 Passing an empty string to this function disables message time stamping.
458
459 Notice that the current time stamp is only used by the default log
460 formatter and custom formatters may ignore this format. You can also
461 define a custom wxLogFormatter to customize the time stamp handling
462 beyond changing its format.
463 */
464 static void SetTimestamp(const wxString& format);
465
466 /**
467 Disables time stamping of the log messages.
468
469 Notice that the current time stamp is only used by the default log
470 formatter and custom formatters may ignore calls to this function.
471
472 @since 2.9.0
473 */
474 static void DisableTimestamp();
475
476 /**
477 Returns whether the verbose mode is currently active.
478 */
479 static bool GetVerbose();
480
481 /**
482 Activates or deactivates verbose mode in which the verbose messages are
483 logged as the normal ones instead of being silently dropped.
484
485 The verbose messages are the trace messages which are not disabled in the
486 release mode and are generated by wxLogVerbose().
487
488 @see @ref overview_log
489 */
490 static void SetVerbose(bool verbose = true);
491
492 //@}
493
494
495 /**
496 Sets the specified formatter as the active one.
497
498 @param formatter
499 The new formatter. If @NULL, reset to the default formatter.
500
501 Returns the pointer to the previous formatter. You must delete it
502 if you don't plan to attach it again to a wxLog object later.
503
504 @since 2.9.4
505 */
506 wxLogFormatter *SetFormatter(wxLogFormatter* formatter);
507
508
509 /**
510 Some of wxLog implementations, most notably the standard wxLogGui class,
511 buffer the messages (for example, to avoid showing the user a zillion of modal
512 message boxes one after another -- which would be really annoying).
513 This function shows them all and clears the buffer contents.
514 If the buffer is already empty, nothing happens.
515
516 If you override this method in a derived class, call the base class
517 version first, before doing anything else.
518 */
519 virtual void Flush();
520
521 /**
522 Log the given record.
523
524 This function should only be called from the DoLog() implementations in
525 the derived classes if they need to call DoLogRecord() on another log
526 object (they can, of course, just use wxLog::DoLogRecord() call syntax
527 to call it on the object itself). It should not be used for logging new
528 messages which can be only sent to the currently active logger using
529 OnLog() which also checks if the logging (for this level) is enabled
530 while this method just directly calls DoLog().
531
532 Example of use of this class from wxLogChain:
533 @code
534 void wxLogChain::DoLogRecord(wxLogLevel level,
535 const wxString& msg,
536 const wxLogRecordInfo& info)
537 {
538 // let the previous logger show it
539 if ( m_logOld && IsPassingMessages() )
540 m_logOld->LogRecord(level, msg, info);
541
542 // and also send it to the new one
543 if ( m_logNew && m_logNew != this )
544 m_logNew->LogRecord(level, msg, info);
545 }
546 @endcode
547
548 @since 2.9.1
549 */
550 void LogRecord(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info);
551
552 protected:
553 /**
554 @name Logging callbacks.
555
556 The functions which should be overridden by custom log targets.
557
558 When defining a new log target, you have a choice between overriding
559 DoLogRecord(), which provides maximal flexibility, DoLogTextAtLevel()
560 which can be used if you don't intend to change the default log
561 messages formatting but want to handle log messages of different levels
562 differently or, in the simplest case, DoLogText().
563 */
564 //@{
565
566 /**
567 Called to log a new record.
568
569 Any log message created by wxLogXXX() functions is passed to this
570 method of the active log target. The default implementation prepends
571 the timestamp and, for some log levels (e.g. error and warning), the
572 corresponding prefix to @a msg and passes it to DoLogTextAtLevel().
573
574 You may override this method to implement custom formatting of the
575 log messages or to implement custom filtering of log messages (e.g. you
576 could discard all log messages coming from the given source file).
577 */
578 virtual void DoLogRecord(wxLogLevel level,
579 const wxString& msg,
580 const wxLogRecordInfo& info);
581
582 /**
583 Called to log the specified string at given level.
584
585 The base class versions logs debug and trace messages on the system
586 default debug output channel and passes all the other messages to
587 DoLogText().
588 */
589 virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
590
591 /**
592 Called to log the specified string.
593
594 A simple implementation might just send the string to @c stdout or
595 @c stderr or save it in a file (of course, the already existing
596 wxLogStderr can be used for this).
597
598 The base class version of this function asserts so it must be
599 overridden if you don't override DoLogRecord() or DoLogTextAtLevel().
600 */
601 virtual void DoLogText(const wxString& msg);
602
603 //@}
604 };
605
606
607
608 /**
609 @class wxLogChain
610
611 This simple class allows you to chain log sinks, that is to install a new sink but
612 keep passing log messages to the old one instead of replacing it completely as
613 wxLog::SetActiveTarget does.
614
615 It is especially useful when you want to divert the logs somewhere (for
616 example to a file or a log window) but also keep showing the error messages
617 using the standard dialogs as wxLogGui does by default.
618
619 Example of usage:
620
621 @code
622 wxLogChain *logChain = new wxLogChain(new wxLogStderr);
623
624 // all the log messages are sent to stderr and also processed as usually
625 ...
626
627 // don't delete logChain directly as this would leave a dangling
628 // pointer as active log target, use SetActiveTarget() instead
629 delete wxLog::SetActiveTarget(...something else or NULL...);
630 @endcode
631
632 @library{wxbase}
633 @category{logging}
634 */
635 class wxLogChain : public wxLog
636 {
637 public:
638 /**
639 Sets the specified @c logger (which may be @NULL) as the default log
640 target but the log messages are also passed to the previous log target if any.
641 */
642 wxLogChain(wxLog* logger);
643
644 /**
645 Destroys the previous log target.
646 */
647 virtual ~wxLogChain();
648
649 /**
650 Detaches the old log target so it won't be destroyed when the wxLogChain object
651 is destroyed.
652 */
653 void DetachOldLog();
654
655 /**
656 Returns the pointer to the previously active log target (which may be @NULL).
657 */
658 wxLog* GetOldLog() const;
659
660 /**
661 Returns @true if the messages are passed to the previously active log
662 target (default) or @false if PassMessages() had been called.
663 */
664 bool IsPassingMessages() const;
665
666 /**
667 By default, the log messages are passed to the previously active log target.
668 Calling this function with @false parameter disables this behaviour
669 (presumably temporarily, as you shouldn't use wxLogChain at all otherwise) and
670 it can be reenabled by calling it again with @a passMessages set to @true.
671 */
672 void PassMessages(bool passMessages);
673
674 /**
675 Sets another log target to use (may be @NULL).
676
677 The log target specified in the wxLogChain(wxLog*) constructor or in a
678 previous call to this function is deleted.
679 This doesn't change the old log target value (the one the messages are
680 forwarded to) which still remains the same as was active when wxLogChain
681 object was created.
682 */
683 void SetLog(wxLog* logger);
684 };
685
686
687
688 /**
689 @class wxLogInterposer
690
691 A special version of wxLogChain which uses itself as the new log target.
692 It forwards log messages to the previously installed one in addition to
693 processing them itself.
694
695 Unlike wxLogChain which is usually used directly as is, this class must be
696 derived from to implement wxLog::DoLog and/or wxLog::DoLogString methods.
697
698 wxLogInterposer destroys the previous log target in its destructor.
699 If you don't want this to happen, use wxLogInterposerTemp instead.
700
701 @library{wxbase}
702 @category{logging}
703 */
704 class wxLogInterposer : public wxLogChain
705 {
706 public:
707 /**
708 The default constructor installs this object as the current active log target.
709 */
710 wxLogInterposer();
711 };
712
713
714
715 /**
716 @class wxLogInterposerTemp
717
718 A special version of wxLogChain which uses itself as the new log target.
719 It forwards log messages to the previously installed one in addition to
720 processing them itself. Unlike wxLogInterposer, it doesn't delete the old
721 target which means it can be used to temporarily redirect log output.
722
723 As per wxLogInterposer, this class must be derived from to implement
724 wxLog::DoLog and/or wxLog::DoLogString methods.
725
726 @library{wxbase}
727 @category{logging}
728 */
729 class wxLogInterposerTemp : public wxLogChain
730 {
731 public:
732 /**
733 The default constructor installs this object as the current active log target.
734 */
735 wxLogInterposerTemp();
736 };
737
738
739 /**
740 @class wxLogStream
741
742 This class can be used to redirect the log messages to a C++ stream.
743
744 Please note that this class is only available if wxWidgets was compiled with
745 the standard iostream library support (@c wxUSE_STD_IOSTREAM must be on).
746
747 @library{wxbase}
748 @category{logging}
749
750 @see wxLogStderr, wxStreamToTextRedirector
751 */
752 class wxLogStream : public wxLog
753 {
754 public:
755 /**
756 Constructs a log target which sends all the log messages to the given
757 output stream. If it is @NULL, the messages are sent to @c cerr.
758 */
759 wxLogStream(std::ostream *ostr = NULL);
760 };
761
762
763
764 /**
765 @class wxLogStderr
766
767 This class can be used to redirect the log messages to a C file stream (not to
768 be confused with C++ streams).
769
770 It is the default log target for the non-GUI wxWidgets applications which
771 send all the output to @c stderr.
772
773 @library{wxbase}
774 @category{logging}
775
776 @see wxLogStream
777 */
778 class wxLogStderr : public wxLog
779 {
780 public:
781 /**
782 Constructs a log target which sends all the log messages to the given
783 @c FILE. If it is @NULL, the messages are sent to @c stderr.
784 */
785 wxLogStderr(FILE* fp = NULL);
786 };
787
788
789
790 /**
791 @class wxLogBuffer
792
793 wxLogBuffer is a very simple implementation of log sink which simply collects
794 all the logged messages in a string (except the debug messages which are output
795 in the usual way immediately as we're presumably not interested in collecting
796 them for later). The messages from different log function calls are separated
797 by the new lines.
798
799 All the messages collected so far can be shown to the user (and the current
800 buffer cleared) by calling the overloaded wxLogBuffer::Flush method.
801
802 @library{wxbase}
803 @category{logging}
804 */
805 class wxLogBuffer : public wxLog
806 {
807 public:
808 /**
809 The default ctor does nothing.
810 */
811 wxLogBuffer();
812
813 /**
814 Shows all the messages collected so far to the user (using a message box in the
815 GUI applications or by printing them out to the console in text mode) and
816 clears the internal buffer.
817 */
818 virtual void Flush();
819
820 /**
821 Returns the current buffer contains. Messages from different log function calls
822 are separated with the new lines in the buffer.
823 The buffer can be cleared by Flush() which will also show the current
824 contents to the user.
825 */
826 const wxString& GetBuffer() const;
827 };
828
829
830
831 /**
832 @class wxLogNull
833
834 This class allows you to temporarily suspend logging. All calls to the log
835 functions during the life time of an object of this class are just ignored.
836
837 In particular, it can be used to suppress the log messages given by wxWidgets
838 itself but it should be noted that it is rarely the best way to cope with this
839 problem as @b all log messages are suppressed, even if they indicate a
840 completely different error than the one the programmer wanted to suppress.
841
842 For instance, the example of the overview:
843
844 @code
845 wxFile file;
846
847 // wxFile.Open() normally complains if file can't be opened, we don't want it
848 {
849 wxLogNull logNo;
850 if ( !file.Open("bar") )
851 ... process error ourselves ...
852 } // ~wxLogNull called, old log sink restored
853
854 wxLogMessage("..."); // ok
855 @endcode
856
857 would be better written as:
858
859 @code
860 wxFile file;
861
862 // don't try to open file if it doesn't exist, we are prepared to deal with
863 // this ourselves - but all other errors are not expected
864 if ( wxFile::Exists("bar") )
865 {
866 // gives an error message if the file couldn't be opened
867 file.Open("bar");
868 }
869 else
870 {
871 ...
872 }
873 @endcode
874
875
876 @library{wxbase}
877 @category{logging}
878 */
879 class wxLogNull
880 {
881 public:
882 /**
883 Suspends logging.
884 */
885 wxLogNull();
886
887 /**
888 Resumes logging.
889 */
890 ~wxLogNull();
891 };
892
893 #endif // wxUSE_BASE
894
895 #if wxUSE_GUI
896
897 /**
898 @class wxLogWindow
899
900 This class represents a background log window: to be precise, it collects all
901 log messages in the log frame which it manages but also passes them on to the
902 log target which was active at the moment of its creation. This allows you, for
903 example, to show all the log messages in a frame but still continue to process
904 them normally by showing the standard log dialog.
905
906 @library{wxcore}
907 @category{logging}
908
909 @see wxLogTextCtrl
910 */
911 class wxLogWindow : public wxLogInterposer
912 {
913 public:
914 /**
915 Creates the log frame window and starts collecting the messages in it.
916
917 @param pParent
918 The parent window for the log frame, may be @NULL
919 @param szTitle
920 The title for the log frame
921 @param show
922 @true to show the frame initially (default), otherwise
923 Show() must be called later.
924 @param passToOld
925 @true to process the log messages normally in addition to logging them
926 in the log frame (default), @false to only log them in the log frame.
927 Note that if no targets were set using wxLog::SetActiveTarget() then
928 wxLogWindow simply becomes the active one and messages won't be passed
929 to other targets.
930 */
931 wxLogWindow(wxWindow* pParent, const wxString& szTitle, bool show = true,
932 bool passToOld = true);
933
934 /**
935 Returns the associated log frame window. This may be used to position or resize
936 it but use Show() to show or hide it.
937 */
938 wxFrame* GetFrame() const;
939
940 /**
941 Called if the user closes the window interactively, will not be
942 called if it is destroyed for another reason (such as when program
943 exits).
944
945 Return @true from here to allow the frame to close, @false to
946 prevent this from happening.
947
948 @see OnFrameDelete()
949 */
950 virtual bool OnFrameClose(wxFrame* frame);
951
952 /**
953 Called right before the log frame is going to be deleted: will
954 always be called unlike OnFrameClose().
955 */
956 virtual void OnFrameDelete(wxFrame* frame);
957
958 /**
959 Shows or hides the frame.
960 */
961 void Show(bool show = true);
962 };
963
964
965
966 /**
967 @class wxLogGui
968
969 This is the default log target for the GUI wxWidgets applications.
970
971 Please see @ref overview_log_customize for explanation of how to change the
972 default log target.
973
974 An object of this class is used by default to show the log messages created
975 by using wxLogMessage(), wxLogError() and other logging functions. It
976 doesn't display the messages logged by them immediately however but
977 accumulates all messages logged during an event handler execution and then
978 shows them all at once when its Flush() method is called during the idle
979 time processing. This has the important advantage of showing only a single
980 dialog to the user even if several messages were logged because of a single
981 error as it often happens (e.g. a low level function could log a message
982 because it failed to open a file resulting in its caller logging another
983 message due to the failure of higher level operation requiring the use of
984 this file). If you need to force the display of all previously logged
985 messages immediately you can use wxLog::FlushActive() to force the dialog
986 display.
987
988 Also notice that if an error message is logged when several informative
989 messages had been already logged before, the informative messages are
990 discarded on the assumption that they are not useful -- and may be
991 confusing and hence harmful -- any more after the error. The warning
992 and error messages are never discarded however and any informational
993 messages logged after the first error one are also kept (as they may
994 contain information about the error recovery). You may override DoLog()
995 method to change this behaviour.
996
997 At any rate, it is possible that that several messages were accumulated
998 before this class Flush() method is called. If this is the case, Flush()
999 uses a custom dialog which shows the last message directly and allows the
1000 user to view the previously logged ones by expanding the "Details"
1001 wxCollapsiblePane inside it. This custom dialog also provides the buttons
1002 for copying the log messages to the clipboard and saving them to a file.
1003
1004 However if only a single message is present when Flush() is called, just a
1005 wxMessageBox() is used to show it. This has the advantage of being closer
1006 to the native behaviour but it doesn't give the user any possibility to
1007 copy or save the message (except for the recent Windows versions where @c
1008 Ctrl-C may be pressed in the message box to copy its contents to the
1009 clipboard) so you may want to override DoShowSingleLogMessage() to
1010 customize wxLogGui -- the dialogs sample shows how to do this.
1011
1012 @library{wxcore}
1013 @category{logging}
1014 */
1015 class wxLogGui : public wxLog
1016 {
1017 public:
1018 /**
1019 Default constructor.
1020 */
1021 wxLogGui();
1022
1023 /**
1024 Presents the accumulated log messages, if any, to the user.
1025
1026 This method is called during the idle time and should show any messages
1027 accumulated in wxLogGui#m_aMessages field to the user.
1028 */
1029 virtual void Flush();
1030
1031 protected:
1032 /**
1033 Returns the appropriate title for the dialog.
1034
1035 The title is constructed from wxApp::GetAppDisplayName() and the
1036 severity string (e.g. "error" or "warning") appropriate for the current
1037 wxLogGui#m_bErrors and wxLogGui#m_bWarnings values.
1038 */
1039 wxString GetTitle() const;
1040
1041 /**
1042 Returns wxICON_ERROR, wxICON_WARNING or wxICON_INFORMATION depending on
1043 the current maximal severity.
1044
1045 This value is suitable to be used in the style parameter of
1046 wxMessageBox() function.
1047 */
1048 int GetSeverityIcon() const;
1049
1050 /**
1051 Forgets all the currently stored messages.
1052
1053 If you override Flush() (and don't call the base class version), you
1054 must call this method to avoid messages being logged over and over
1055 again.
1056 */
1057 void Clear();
1058
1059
1060 /**
1061 All currently accumulated messages.
1062
1063 This array may be empty if no messages were logged.
1064
1065 @see m_aSeverity, m_aTimes
1066 */
1067 wxArrayString m_aMessages;
1068
1069 /**
1070 The severities of each logged message.
1071
1072 This array is synchronized with wxLogGui#m_aMessages, i.e. the n-th
1073 element of this array corresponds to the severity of the n-th message.
1074 The possible severity values are @c wxLOG_XXX constants, e.g.
1075 wxLOG_Error, wxLOG_Warning, wxLOG_Message etc.
1076 */
1077 wxArrayInt m_aSeverity;
1078
1079 /**
1080 The time stamps of each logged message.
1081
1082 The elements of this array are time_t values corresponding to the time
1083 when the message was logged.
1084 */
1085 wxArrayLong m_aTimes;
1086
1087 /**
1088 True if there any error messages.
1089 */
1090 bool m_bErrors;
1091
1092 /**
1093 True if there any warning messages.
1094
1095 If both wxLogGui#m_bErrors and this member are false, there are only
1096 informational messages to be shown.
1097 */
1098 bool m_bWarnings;
1099
1100 /**
1101 True if there any messages to be shown to the user.
1102
1103 This variable is used instead of simply checking whether
1104 wxLogGui#m_aMessages array is empty to allow blocking further calls to
1105 Flush() while a log dialog is already being shown, even if the messages
1106 array hasn't been emptied yet.
1107 */
1108 bool m_bHasMessages;
1109
1110 private:
1111 /**
1112 Method called by Flush() to show a single log message.
1113
1114 This function can be overridden to show the message in a different way.
1115 By default a simple wxMessageBox() call is used.
1116
1117 @param message
1118 The message to show (it can contain multiple lines).
1119 @param title
1120 The suggested title for the dialog showing the message, see
1121 GetTitle().
1122 @param style
1123 One of @c wxICON_XXX constants, see GetSeverityIcon().
1124 */
1125 virtual void DoShowSingleLogMessage(const wxString& message,
1126 const wxString& title,
1127 int style);
1128
1129 /**
1130 Method called by Flush() to show multiple log messages.
1131
1132 This function can be overridden to show the messages in a different way.
1133 By default a special log dialog showing the most recent message and
1134 allowing the user to expand it to view the previously logged ones is
1135 used.
1136
1137 @param messages
1138 Array of messages to show; it contains more than one element.
1139 @param severities
1140 Array of message severities containing @c wxLOG_XXX values.
1141 @param times
1142 Array of time_t values indicating when each message was logged.
1143 @param title
1144 The suggested title for the dialog showing the message, see
1145 GetTitle().
1146 @param style
1147 One of @c wxICON_XXX constants, see GetSeverityIcon().
1148 */
1149 virtual void DoShowMultipleLogMessages(const wxArrayString& messages,
1150 const wxArrayInt& severities,
1151 const wxArrayLong& times,
1152 const wxString& title,
1153 int style);
1154 };
1155
1156
1157
1158 /**
1159 @class wxLogTextCtrl
1160
1161 Using these target all the log messages can be redirected to a text control.
1162 The text control must have been created with @c wxTE_MULTILINE style by the
1163 caller previously.
1164
1165 @library{wxcore}
1166 @category{logging}
1167
1168 @see wxTextCtrl, wxStreamToTextRedirector
1169 */
1170 class wxLogTextCtrl : public wxLog
1171 {
1172 public:
1173 /**
1174 Constructs a log target which sends all the log messages to the given text
1175 control. The @a textctrl parameter cannot be @NULL.
1176 */
1177 wxLogTextCtrl(wxTextCtrl* pTextCtrl);
1178 };
1179
1180
1181 #endif // wxUSE_GUI
1182
1183 #if wxUSE_BASE
1184
1185
1186 // ============================================================================
1187 // Global functions/macros
1188 // ============================================================================
1189
1190 /** @addtogroup group_funcmacro_log */
1191 //@{
1192
1193 /**
1194 This function shows a message to the user in a safe way and should be safe
1195 to call even before the application has been initialized or if it is
1196 currently in some other strange state (for example, about to crash). Under
1197 Windows this function shows a message box using a native dialog instead of
1198 wxMessageBox() (which might be unsafe to call), elsewhere it simply prints
1199 the message to the standard output using the title as prefix.
1200
1201 @param title
1202 The title of the message box shown to the user or the prefix of the
1203 message string.
1204 @param text
1205 The text to show to the user.
1206
1207 @see wxLogFatalError()
1208
1209 @header{wx/log.h}
1210 */
1211 void wxSafeShowMessage(const wxString& title, const wxString& text);
1212
1213 /**
1214 Returns the error code from the last system call. This function uses
1215 @c errno on Unix platforms and @c GetLastError under Win32.
1216
1217 @see wxSysErrorMsg(), wxLogSysError()
1218
1219 @header{wx/log.h}
1220 */
1221 unsigned long wxSysErrorCode();
1222
1223 /**
1224 Returns the error message corresponding to the given system error code. If
1225 @a errCode is 0 (default), the last error code (as returned by
1226 wxSysErrorCode()) is used.
1227
1228 @see wxSysErrorCode(), wxLogSysError()
1229
1230 @header{wx/log.h}
1231 */
1232 const wxChar* wxSysErrorMsg(unsigned long errCode = 0);
1233
1234 //@}
1235
1236 /** @addtogroup group_funcmacro_log */
1237 //@{
1238 /**
1239 Logs a message with the given wxLogLevel.
1240 E.g. using @c wxLOG_Message as first argument, this function behaves like wxLogMessage().
1241
1242 @header{wx/log.h}
1243 */
1244 void wxLogGeneric(wxLogLevel level, const char* formatString, ... );
1245 void wxVLogGeneric(wxLogLevel level, const char* formatString, va_list argPtr);
1246 //@}
1247
1248 /** @addtogroup group_funcmacro_log */
1249 //@{
1250 /**
1251 For all normal, informational messages. They also appear in a message box
1252 by default (but it can be changed).
1253
1254 @header{wx/log.h}
1255 */
1256 void wxLogMessage(const char* formatString, ... );
1257 void wxVLogMessage(const char* formatString, va_list argPtr);
1258 //@}
1259
1260 /** @addtogroup group_funcmacro_log */
1261 //@{
1262 /**
1263 For verbose output. Normally, it is suppressed, but might be activated if
1264 the user wishes to know more details about the program progress (another,
1265 but possibly confusing name for the same function could be @c wxLogInfo).
1266
1267 @header{wx/log.h}
1268 */
1269 void wxLogVerbose(const char* formatString, ... );
1270 void wxVLogVerbose(const char* formatString, va_list argPtr);
1271 //@}
1272
1273 /** @addtogroup group_funcmacro_log */
1274 //@{
1275 /**
1276 For warnings - they are also normally shown to the user, but don't
1277 interrupt the program work.
1278
1279 @header{wx/log.h}
1280 */
1281 void wxLogWarning(const char* formatString, ... );
1282 void wxVLogWarning(const char* formatString, va_list argPtr);
1283 //@}
1284
1285 /** @addtogroup group_funcmacro_log */
1286 //@{
1287 /**
1288 Like wxLogError(), but also terminates the program with the exit code 3.
1289 Using @e abort() standard function also terminates the program with this
1290 exit code.
1291
1292 @header{wx/log.h}
1293 */
1294 void wxLogFatalError(const char* formatString, ... );
1295 void wxVLogFatalError(const char* formatString, va_list argPtr);
1296 //@}
1297
1298 /** @addtogroup group_funcmacro_log */
1299 //@{
1300 /**
1301 The functions to use for error messages, i.e. the messages that must be
1302 shown to the user. The default processing is to pop up a message box to
1303 inform the user about it.
1304
1305 @header{wx/log.h}
1306 */
1307 void wxLogError(const char* formatString, ... );
1308 void wxVLogError(const char* formatString, va_list argPtr);
1309 //@}
1310
1311 /** @addtogroup group_funcmacro_log */
1312 //@{
1313 /**
1314 Log a message at @c wxLOG_Trace log level (see ::wxLogLevelValues enum).
1315
1316 Notice that the use of trace masks is not recommended any more as setting
1317 the log components (please see @ref overview_log_enable) provides a way to
1318 do the same thing for log messages of any level, and not just the tracing
1319 ones.
1320
1321 Like wxLogDebug(), trace functions only do something in debug builds and
1322 expand to nothing in the release one. The reason for making it a separate
1323 function is that usually there are a lot of trace messages, so it might
1324 make sense to separate them from other debug messages.
1325
1326 Trace messages can be separated into different categories; these functions in facts
1327 only log the message if the given @a mask is currently enabled in wxLog.
1328 This lets you selectively trace only some operations and not others by enabling the
1329 desired trace masks with wxLog::AddTraceMask() or by setting the
1330 @ref overview_envvars "@c WXTRACE environment variable".
1331
1332 The predefined string trace masks used by wxWidgets are:
1333
1334 @beginDefList
1335 @itemdef{ wxTRACE_MemAlloc, Trace memory allocation (new/delete) }
1336 @itemdef{ wxTRACE_Messages, Trace window messages/X callbacks }
1337 @itemdef{ wxTRACE_ResAlloc, Trace GDI resource allocation }
1338 @itemdef{ wxTRACE_RefCount, Trace various ref counting operations }
1339 @itemdef{ wxTRACE_OleCalls, Trace OLE method calls (Win32 only) }
1340 @endDefList
1341
1342 @header{wx/log.h}
1343 */
1344 void wxLogTrace(const char* mask, const char* formatString, ... );
1345 void wxVLogTrace(const char* mask, const char* formatString, va_list argPtr);
1346 //@}
1347
1348 /** @addtogroup group_funcmacro_log */
1349 //@{
1350 /**
1351 Like wxLogDebug(), trace functions only do something in debug builds and
1352 expand to nothing in the release one. The reason for making it a separate
1353 function is that usually there are a lot of trace messages, so it might
1354 make sense to separate them from other debug messages.
1355
1356 @deprecated
1357 This version of wxLogTrace() only logs the message if all the bits
1358 corresponding to the @a mask are set in the wxLog trace mask which can be
1359 set by calling wxLog::SetTraceMask(). This version is less flexible than
1360 wxLogTrace(const char*,const char*,...) because it doesn't allow defining
1361 the user trace masks easily. This is why it is deprecated in favour of
1362 using string trace masks.
1363
1364 The following bitmasks are defined for wxTraceMask:
1365
1366 @beginDefList
1367 @itemdef{ wxTraceMemAlloc, Trace memory allocation (new/delete) }
1368 @itemdef{ wxTraceMessages, Trace window messages/X callbacks }
1369 @itemdef{ wxTraceResAlloc, Trace GDI resource allocation }
1370 @itemdef{ wxTraceRefCount, Trace various ref counting operations }
1371 @itemdef{ wxTraceOleCalls, Trace OLE method calls (Win32 only) }
1372 @endDefList
1373
1374 @header{wx/log.h}
1375 */
1376 void wxLogTrace(wxTraceMask mask, const char* formatString, ... );
1377 void wxVLogTrace(wxTraceMask mask, const char* formatString, va_list argPtr);
1378 //@}
1379
1380 /** @addtogroup group_funcmacro_log */
1381 //@{
1382 /**
1383 The right functions for debug output. They only do something in debug mode
1384 (when the preprocessor symbol @c __WXDEBUG__ is defined) and expand to
1385 nothing in release mode (otherwise).
1386
1387 @header{wx/log.h}
1388 */
1389 void wxLogDebug(const char* formatString, ... );
1390 void wxVLogDebug(const char* formatString, va_list argPtr);
1391 //@}
1392
1393 /** @addtogroup group_funcmacro_log */
1394 //@{
1395 /**
1396 Messages logged by this function will appear in the statusbar of the
1397 @a frame or of the top level application window by default (i.e. when using
1398 the second version of the functions).
1399
1400 If the target frame doesn't have a statusbar, the message will be lost.
1401
1402 @header{wx/log.h}
1403 */
1404 void wxLogStatus(wxFrame* frame, const char* formatString, ... );
1405 void wxVLogStatus(wxFrame* frame, const char* formatString, va_list argPtr);
1406 void wxLogStatus(const char* formatString, ... );
1407 void wxVLogStatus(const char* formatString, va_list argPtr);
1408 //@}
1409
1410 /** @addtogroup group_funcmacro_log */
1411 //@{
1412 /**
1413 Mostly used by wxWidgets itself, but might be handy for logging errors
1414 after system call (API function) failure. It logs the specified message
1415 text as well as the last system error code (@e errno or @e GetLastError()
1416 depending on the platform) and the corresponding error message. The second
1417 form of this function takes the error code explicitly as the first
1418 argument.
1419
1420 @see wxSysErrorCode(), wxSysErrorMsg()
1421
1422 @header{wx/log.h}
1423 */
1424 void wxLogSysError(const char* formatString, ... );
1425 void wxVLogSysError(const char* formatString, va_list argPtr);
1426 //@}
1427
1428 /** @addtogroup group_funcmacro_debug */
1429 //@{
1430
1431 /**
1432 @def wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD()
1433
1434 Use this macro to disable logging at debug and trace levels in release
1435 build when not using wxIMPLEMENT_APP().
1436
1437 @see wxDISABLE_DEBUG_SUPPORT(),
1438 wxDISABLE_ASSERTS_IN_RELEASE_BUILD(),
1439 @ref overview_debugging
1440
1441 @since 2.9.1
1442
1443 @header{wx/log.h}
1444 */
1445 #define wxDISABLE_DEBUG_LOGGING_IN_RELEASE_BUILD()
1446
1447 //@}
1448
1449 #endif // wxUSE_BASE