]> git.saurik.com Git - wxWidgets.git/blob - interface/log.h
4e127526da29175af0c3806fde2197caa9f342be
[wxWidgets.git] / interface / log.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: log.h
3 // Purpose: documentation for wxLogWindow class
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxLogWindow
11 @wxheader{log.h}
12
13 This class represents a background log window: to be precise, it collects all
14 log messages in the log frame which it manages but also passes them on to the
15 log target which was active at the moment of its creation. This allows, for
16 example, to show all the log messages in a frame but still continue to process
17 them normally by showing the standard log dialog.
18
19 @library{wxbase}
20 @category{logging}
21
22 @seealso
23 wxLogTextCtrl
24 */
25 class wxLogWindow : public wxLogInterposer
26 {
27 public:
28 /**
29 Creates the log frame window and starts collecting the messages in it.
30
31 @param parent
32 The parent window for the log frame, may be @NULL
33 @param title
34 The title for the log frame
35 @param show
36 @true to show the frame initially (default), otherwise
37 Show() must be called later.
38 @param passToOld
39 @true to process the log messages normally in addition to
40 logging them in the log frame (default), @false to only log them in the
41 log frame.
42 */
43 wxLogWindow(wxFrame parent, const wxChar title, bool show = true,
44 bool passToOld = true);
45
46 /**
47 Returns the associated log frame window. This may be used to position or resize
48 it but use Show() to show or hide it.
49 */
50 wxFrame* GetFrame();
51
52 /**
53 Called if the user closes the window interactively, will not be
54 called if it is destroyed for another reason (such as when program
55 exits).
56 Return @true from here to allow the frame to close, @false to
57 prevent this from happening.
58
59 @see OnFrameDelete()
60 */
61 virtual bool OnFrameClose(wxFrame frame);
62
63 /**
64 Called immediately after the log frame creation allowing for
65 any extra initializations.
66 */
67 virtual void OnFrameCreate(wxFrame frame);
68
69 /**
70 Called right before the log frame is going to be deleted: will
71 always be called unlike OnFrameClose().
72 */
73 virtual void OnFrameDelete(wxFrame frame);
74
75 /**
76 Shows or hides the frame.
77 */
78 void Show(bool show = true);
79 };
80
81
82 /**
83 @class wxLogInterposerTemp
84 @wxheader{log.h}
85
86 A special version of wxLogChain which uses itself as the
87 new log target. It forwards log messages to the previously installed one in
88 addition to
89 processing them itself. Unlike wxLogInterposer, it doesn't
90 delete the old target which means it can be used to temporarily redirect log
91 output.
92
93 As per wxLogInterposer, this class must be derived from to implement
94 wxLog::DoLog
95 and/or wxLog::DoLogString methods.
96
97 @library{wxbase}
98 @category{logging}
99 */
100 class wxLogInterposerTemp : public wxLogChain
101 {
102 public:
103 /**
104 The default constructor installs this object as the current active log target.
105 */
106 };
107
108
109 /**
110 @class wxLogChain
111 @wxheader{log.h}
112
113 This simple class allows to chain log sinks, that is to install a new sink but
114 keep passing log messages to the old one instead of replacing it completely as
115 wxLog::SetActiveTarget does.
116
117 It is especially useful when you want to divert the logs somewhere (for
118 example to a file or a log window) but also keep showing the error messages
119 using the standard dialogs as wxLogGui does by default.
120
121 Example of usage:
122
123 @code
124 wxLogChain *logChain = new wxLogChain(new wxLogStderr);
125
126 // all the log messages are sent to stderr and also processed as usually
127 ...
128
129 // don't delete logChain directly as this would leave a dangling
130 // pointer as active log target, use SetActiveTarget() instead
131 delete wxLog::SetActiveTarget(...something else or @NULL...);
132 @endcode
133
134 @library{wxbase}
135 @category{logging}
136 */
137 class wxLogChain : public wxLog
138 {
139 public:
140 /**
141 Sets the specified @c logger (which may be @NULL) as the default log
142 target but the log messages are also passed to the previous log target if any.
143 */
144 wxLogChain(wxLog* logger);
145
146 /**
147 Destroys the previous log target.
148 */
149 ~wxLogChain();
150
151 /**
152 Detaches the old log target so it won't be destroyed when the wxLogChain object
153 is destroyed.
154 */
155 void DetachOldLog();
156
157 /**
158 Returns the pointer to the previously active log target (which may be @NULL).
159 */
160 wxLog* GetOldLog();
161
162 /**
163 Returns @true if the messages are passed to the previously active log
164 target (default) or @false if PassMessages()
165 had been called.
166 */
167 bool IsPassingMessages();
168
169 /**
170 By default, the log messages are passed to the previously active log target.
171 Calling this function with @false parameter disables this behaviour
172 (presumably temporarily, as you shouldn't use wxLogChain at all otherwise) and
173 it can be reenabled by calling it again with @a passMessages set to @true.
174 */
175 void PassMessages(bool passMessages);
176
177 /**
178 Sets another log target to use (may be @NULL). The log target specified
179 in the @ref ctor() constructor or in a previous call to
180 this function is deleted.
181 This doesn't change the old log target value (the one the messages are
182 forwarded to) which still remains the same as was active when wxLogChain
183 object was created.
184 */
185 void SetLog(wxLog* logger);
186 };
187
188
189 /**
190 @class wxLogGui
191 @wxheader{log.h}
192
193 This is the default log target for the GUI wxWidgets applications. It is passed
194 to wxLog::SetActiveTarget at the program
195 startup and is deleted by wxWidgets during the program shut down.
196
197 @library{wxbase}
198 @category{logging}
199 */
200 class wxLogGui : public wxLog
201 {
202 public:
203 /**
204 Default constructor.
205 */
206 wxLogGui();
207 };
208
209
210 /**
211 @class wxLogStream
212 @wxheader{log.h}
213
214 This class can be used to redirect the log messages to a C++ stream.
215
216 Please note that this class is only available if wxWidgets was compiled with
217 the standard iostream library support (@c wxUSE_STD_IOSTREAM must be on).
218
219 @library{wxbase}
220 @category{logging}
221
222 @seealso
223 wxLogStderr, wxStreamToTextRedirector
224 */
225 class wxLogStream : public wxLog
226 {
227 public:
228 /**
229 Constructs a log target which sends all the log messages to the given
230 output stream. If it is @NULL, the messages are sent to @c cerr.
231 */
232 wxLogStream(std::ostream ostr = NULL);
233 };
234
235
236 /**
237 @class wxLogStderr
238 @wxheader{log.h}
239
240 This class can be used to redirect the log messages to a C file stream (not to
241 be confused with C++ streams). It is the default log target for the non-GUI
242 wxWidgets applications which send all the output to @c stderr.
243
244 @library{wxbase}
245 @category{logging}
246
247 @seealso
248 wxLogStream
249 */
250 class wxLogStderr : public wxLog
251 {
252 public:
253 /**
254 Constructs a log target which sends all the log messages to the given
255 @c FILE. If it is @NULL, the messages are sent to @c stderr.
256 */
257 wxLogStderr(FILE fp = NULL);
258 };
259
260
261 /**
262 @class wxLogBuffer
263 @wxheader{log.h}
264
265 wxLogBuffer is a very simple implementation of log sink which simply collects
266 all the logged messages in a string (except the debug messages which are output
267 in the usual way immediately as we're presumably not interested in collecting
268 them for later). The messages from different log function calls are separated
269 by the new lines.
270
271 All the messages collected so far can be shown to the user (and the current
272 buffer cleared) by calling the overloaded wxLogBuffer::Flush
273 method.
274
275 @library{wxbase}
276 @category{FIXME}
277 */
278 class wxLogBuffer : public wxLog
279 {
280 public:
281 /**
282 Shows all the messages collected so far to the user (using a message box in the
283 GUI applications or by printing them out to the console in text mode) and
284 clears the internal buffer.
285 */
286 virtual void Flush();
287
288 /**
289 Returns the current buffer contains. Messages from different log function calls
290 are separated with the new lines in the buffer.
291 The buffer can be cleared by Flush() which will
292 also show the current contents to the user.
293 */
294 const wxString GetBuffer();
295 };
296
297
298 /**
299 @class wxLogInterposer
300 @wxheader{log.h}
301
302 A special version of wxLogChain which uses itself as the
303 new log target. It forwards log messages to the previously installed one in
304 addition to
305 processing them itself.
306
307 Unlike wxLogChain which is usually used directly as is,
308 this class must be derived from to implement wxLog::DoLog
309 and/or wxLog::DoLogString methods.
310
311 wxLogInterposer destroys the previous log target in its destructor. If you
312 don't want this to happen, use wxLogInterposerTemp instead.
313
314 @library{wxbase}
315 @category{logging}
316 */
317 class wxLogInterposer : public wxLogChain
318 {
319 public:
320 /**
321 The default constructor installs this object as the current active log target.
322 */
323 };
324
325
326 /**
327 @class wxLogTextCtrl
328 @wxheader{log.h}
329
330 Using these target all the log messages can be redirected to a text control.
331 The text control must have been created with @c wxTE_MULTILINE style by the
332 caller previously.
333
334 @library{wxbase}
335 @category{logging}
336
337 @seealso
338 wxTextCtrl, wxStreamToTextRedirector
339 */
340 class wxLogTextCtrl : public wxLog
341 {
342 public:
343 /**
344 Constructs a log target which sends all the log messages to the given text
345 control. The @a textctrl parameter cannot be @NULL.
346 */
347 wxLogTextCtrl(wxTextCtrl textctrl);
348 };
349
350
351 /**
352 @class wxLog
353 @wxheader{log.h}
354
355 wxLog class defines the interface for the @e log targets used by wxWidgets
356 logging functions as explained in the @ref overview_wxlogoverview "wxLog
357 overview".
358 The only situations when you need to directly use this class is when you want
359 to derive your own log target because the existing ones don't satisfy your
360 needs. Another case is if you wish to customize the behaviour of the standard
361 logging classes (all of which respect the wxLog settings): for example, set
362 which trace messages are logged and which are not or change (or even remove
363 completely) the timestamp on the messages.
364
365 Otherwise, it is completely hidden behind the @e wxLogXXX() functions and
366 you may not even know about its existence.
367
368 See @ref overview_wxlogoverview "log overview" for the descriptions of wxWidgets
369 logging facilities.
370
371 @library{wxcore}
372 @category{logging}
373
374 @seealso
375 wxLog::RemoveTraceMask, wxLog::GetTraceMasks
376 */
377 class wxLog
378 {
379 public:
380 /**
381 Add the @a mask to the list of allowed masks for
382 wxLogTrace.
383
384 @see RemoveTraceMask(), GetTraceMasks()
385 */
386 static void AddTraceMask(const wxString& mask);
387
388 /**
389 Removes all trace masks previously set with
390 AddTraceMask().
391
392 @see RemoveTraceMask()
393 */
394 static void ClearTraceMasks();
395
396 /**
397 The functions below allow some limited customization of wxLog behaviour
398 without writing a new log target class (which, aside of being a matter of
399 several minutes, allows you to do anything you want).
400 The verbose messages are the trace messages which are not disabled in the
401 release mode and are generated by wxLogVerbose. They
402 are not normally shown to the user because they present little interest, but
403 may be activated, for example, in order to help the user find some program
404 problem.
405 As for the (real) trace messages, their handling depends on the settings of
406 the (application global) @e trace mask. There are two ways to specify it:
407 either by using SetTraceMask() and
408 GetTraceMask() and using
409 wxLogTrace which takes an integer mask or by using
410 AddTraceMask() for string trace masks.
411 The difference between bit-wise and string trace masks is that a message using
412 integer trace mask will only be logged if all bits of the mask are set in the
413 current mask while a message using string mask will be logged simply if the
414 mask had been added before to the list of allowed ones.
415 For example,
416
417 will do something only if the current trace mask contains both
418 @c wxTraceRefCount and @c wxTraceOle, but
419
420 will log the message if it was preceded by
421
422 Using string masks is simpler and allows to easily add custom ones, so this is
423 the preferred way of working with trace messages. The integer trace mask is
424 kept for compatibility and for additional (but very rarely needed) flexibility
425 only.
426 The standard trace masks are given in wxLogTrace
427 documentation.
428 Finally, the @e wxLog::DoLog() function automatically prepends a time stamp
429 to all the messages. The format of the time stamp may be changed: it can be
430 any string with % specifications fully described in the documentation of the
431 standard @e strftime() function. For example, the default format is
432 "[%d/%b/%y %H:%M:%S] " which gives something like "[17/Sep/98 22:10:16] "
433 (without quotes) for the current date. Setting an empty string as the time
434 format disables timestamping of the messages completely.
435 @b NB: Timestamping is disabled for Visual C++ users in debug builds by
436 default because otherwise it would be impossible to directly go to the line
437 from which the log message was generated by simply clicking in the debugger
438 window on the corresponding error message. If you wish to enable it, please use
439 SetTimestamp() explicitly.
440 AddTraceMask()
441
442 RemoveTraceMask()
443
444 ClearTraceMasks()
445
446 GetTraceMasks()
447
448 IsAllowedTraceMask()
449
450 SetVerbose()
451
452 GetVerbose()
453
454 SetTimestamp()
455
456 GetTimestamp()
457
458 SetTraceMask()
459
460 GetTraceMask()
461
462 SetRepetitionCounting()
463
464 GetRepetitionCounting()
465 */
466
467
468 /**
469 Disables time stamping of the log messages.
470 This function is new since wxWidgets version 2.9
471 */
472 void SetTimestamp(const wxString& format);
473
474 /**
475 Called to process the message of the specified severity. @a msg is the text
476 of the message as specified in the call of @e wxLogXXX() function which
477 generated it and @a timestamp is the moment when the message was generated.
478 The base class version prepends the timestamp to the message, adds a prefix
479 corresponding to the log level and then calls
480 DoLogString() with the resulting string.
481 */
482 virtual void DoLog(wxLogLevel level, const wxString& msg,
483 time_t timestamp);
484
485 /**
486 Called to log the specified string. The timestamp is already included in the
487 string but still passed to this function.
488 A simple implementation may just send the string to @c stdout or, better,
489 @c stderr.
490 */
491 virtual void DoLogString(const wxString& msg, time_t timestamp);
492
493 /**
494 Instructs wxLog to not create new log targets on the fly if there is none
495 currently. (Almost) for internal use only: it is supposed to be called by the
496 application shutdown code.
497 Note that this function also calls
498 ClearTraceMasks().
499 */
500 static void DontCreateOnDemand();
501
502 /**
503 Shows all the messages currently in buffer and clears it. If the buffer
504 is already empty, nothing happens.
505 */
506 virtual void Flush();
507
508 /**
509 Flushes the current log target if any, does nothing if there is none.
510
511 @see Flush()
512 */
513 static void FlushActive();
514
515 /**
516 Returns the pointer to the active log target (may be @NULL).
517 */
518 static wxLog* GetActiveTarget();
519
520 /**
521 Returns the current log level limit.
522 */
523 static wxLogLevel GetLogLevel();
524
525 /**
526 Returns whether the repetition counting mode is enabled.
527 */
528 static bool GetRepetitionCounting();
529
530 /**
531 Returns the current timestamp format string.
532 */
533 static const wxString GetTimestamp();
534
535 /**
536 Returns the current trace mask, see Customization() section
537 for details.
538 */
539 static wxTraceMask GetTraceMask();
540
541 /**
542 Returns the currently allowed list of string trace masks.
543
544 @see AddTraceMask().
545 */
546 static const wxArrayString GetTraceMasks();
547
548 /**
549 Returns whether the verbose mode is currently active.
550 */
551 static bool GetVerbose();
552
553 /**
554 The functions in this section work with and manipulate the active log target.
555 The OnLog() is called by the @e wxLogXXX() functions
556 and invokes the DoLog() of the active log target if any.
557 Get/Set methods are used to install/query the current active target and,
558 finally, DontCreateOnDemand() disables the
559 automatic creation of a standard log target if none actually exists. It is
560 only useful when the application is terminating and shouldn't be used in other
561 situations because it may easily lead to a loss of messages.
562 OnLog()
563
564 GetActiveTarget()
565
566 SetActiveTarget()
567
568 DontCreateOnDemand()
569
570 Suspend()
571
572 Resume()
573 */
574
575
576 /**
577 Returns @true if the @a mask is one of allowed masks for
578 wxLogTrace.
579 See also: AddTraceMask(),
580 RemoveTraceMask()
581 */
582 static bool IsAllowedTraceMask(const wxString& mask);
583
584 /**
585 There are two functions which must be implemented by any derived class to
586 actually process the log messages: DoLog() and
587 DoLogString(). The second function receives a string
588 which just has to be output in some way and the easiest way to write a new log
589 target is to override just this function in the derived class. If more control
590 over the output format is needed, then the first function must be overridden
591 which allows to construct custom messages depending on the log level or even
592 do completely different things depending on the message severity (for example,
593 throw away all messages except warnings and errors, show warnings on the
594 screen and forward the error messages to the user's (or programmer's) cell
595 phone - maybe depending on whether the timestamp tells us if it is day or
596 night in the current time zone).
597 There also functions to support message buffering. Why are they needed?
598 Some of wxLog implementations, most notably the standard wxLogGui class,
599 buffer the messages (for example, to avoid showing the user a zillion of modal
600 message boxes one after another -- which would be really annoying).
601 Flush() shows them all and clears the buffer contents.
602 This function doesn't do anything if the buffer is already empty.
603 Flush()
604
605 FlushActive()
606 */
607
608
609 /**
610 Forwards the message at specified level to the @e DoLog() function of the
611 active log target if there is any, does nothing otherwise.
612 */
613 static void OnLog(wxLogLevel level, const wxString& message);
614
615 /**
616 Remove the @a mask from the list of allowed masks for
617 wxLogTrace.
618 See also: AddTraceMask()
619 */
620 static void RemoveTraceMask(const wxString& mask);
621
622 /**
623 Resumes logging previously suspended by a call to
624 Suspend(). All messages logged in the meanwhile will be
625 flushed soon.
626 */
627 static void Resume();
628
629 /**
630 Sets the specified log target as the active one. Returns the pointer to the
631 previous active log target (may be @NULL). To suppress logging use a new
632 instance of wxLogNull not @NULL. If the active log target is set to @NULL a
633 new default log target will be created when logging occurs.
634 */
635 static wxLog* SetActiveTarget(wxLog* logtarget);
636
637 /**
638 Specifies that log messages with level logLevel should be ignored
639 and not sent to the active log target.
640 */
641 static void SetLogLevel(wxLogLevel logLevel);
642
643 /**
644 Enables logging mode in which a log message is logged once, and in case exactly
645 the same message successively repeats one or more times, only the number of
646 repetitions is logged.
647 */
648 static void SetRepetitionCounting(bool repetCounting = true);
649
650 /**
651 Sets the timestamp format prepended by the default log targets to all
652 messages. The string may contain any normal characters as well as %
653 prefixed format specificators, see @e strftime() manual for details.
654 Passing an empty string to this function disables message time stamping.
655 */
656 static void SetTimestamp(const wxString& format);
657
658 /**
659 Sets the trace mask, see Customization()
660 section for details.
661 */
662 static void SetTraceMask(wxTraceMask mask);
663
664 /**
665 Activates or deactivates verbose mode in which the verbose messages are
666 logged as the normal ones instead of being silently dropped.
667 */
668 static void SetVerbose(bool verbose = true);
669
670 /**
671 Suspends the logging until Resume() is called. Note that
672 the latter must be called the same number of times as the former to undo it,
673 i.e. if you call Suspend() twice you must call Resume() twice as well.
674 Note that suspending the logging means that the log sink won't be be flushed
675 periodically, it doesn't have any effect if the current log target does the
676 logging immediately without waiting for Flush() to be
677 called (the standard GUI log target only shows the log dialog when it is
678 flushed, so Suspend() works as expected with it).
679
680 @see Resume(), wxLogNull
681 */
682 static void Suspend();
683 };
684
685
686 /**
687 @class wxLogNull
688 @wxheader{log.h}
689
690 This class allows to temporarily suspend logging. All calls to the log
691 functions during the life time of an object of this class are just ignored.
692
693 In particular, it can be used to suppress the log messages given by wxWidgets
694 itself but it should be noted that it is rarely the best way to cope with this
695 problem as @b all log messages are suppressed, even if they indicate a
696 completely different error than the one the programmer wanted to suppress.
697
698 For instance, the example of the overview:
699
700 @code
701 wxFile file;
702
703 // wxFile.Open() normally complains if file can't be opened, we don't want it
704 {
705 wxLogNull logNo;
706 if ( !file.Open("bar") )
707 ... process error ourselves ...
708 } // ~wxLogNull called, old log sink restored
709
710 wxLogMessage("..."); // ok
711 @endcode
712
713 would be better written as:
714
715 @code
716 wxFile file;
717
718 // don't try to open file if it doesn't exist, we are prepared to deal with
719 // this ourselves - but all other errors are not expected
720 if ( wxFile::Exists("bar") )
721 {
722 // gives an error message if the file couldn't be opened
723 file.Open("bar");
724 }
725 else
726 {
727 ...
728 }
729 @endcode
730
731
732 @library{wxbase}
733 @category{logging}
734 */
735 class wxLogNull : public wxLog
736 {
737 public:
738 /**
739 Suspends logging.
740 */
741 wxLogNull();
742
743 /**
744 Resumes logging.
745 */
746 };
747
748
749 // ============================================================================
750 // Global functions/macros
751 // ============================================================================
752
753 /**
754 This function shows a message to the user in a safe way and should be safe to
755 call even before the application has been initialized or if it is currently in
756 some other strange state (for example, about to crash). Under Windows this
757 function shows a message box using a native dialog instead of
758 wxMessageBox (which might be unsafe to call), elsewhere
759 it simply prints the message to the standard output using the title as prefix.
760
761 @param title
762 The title of the message box shown to the user or the prefix
763 of the message string
764 @param text
765 The text to show to the user
766
767 @see wxLogFatalError
768 */
769 void wxSafeShowMessage(const wxString& title,
770 const wxString& text);
771