1 #ifndef CONSOLE_LOGGER_CLASS
2 #define CONSOLE_LOGGER_CLASS
4 /*****************************************************************************\
6 * Name : console_logger *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2000-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 //! A logger that sends to the console screen using the standard output device.
20 The object can optionally be used to log to the standard error device
24 #include "standard_log_base.h"
26 #include <basis/astring.h>
27 #include <basis/contracts.h>
28 #include <basis/enhance_cpp.h>
32 class console_logger : public virtual standard_log_base
35 enum stream_choices { TO_STDOUT, TO_STDERR }; //!< where to send the logging.
37 console_logger(stream_choices log_target = TO_STDOUT);
38 //!< if "standard_error" is true, than stderr is used instead of stdout.
40 virtual ~console_logger();
42 DEFINE_CLASS_NAME("console_logger");
44 bool to_standard_output() const { return c_target == TO_STDOUT; }
45 //!< reports if the logger goes to standard output (default).
47 bool to_standard_error() const { return c_target == TO_STDERR; }
48 //!< reports if the logger goes to standard error instead.
50 void set_output(stream_choices target) { c_target = target; }
51 //!< enables the target of logging to be changed after construction.
53 virtual basis::outcome log(const basis::base_string &info, int filter);
54 //!< sends the string "info" to the standard output device.
55 /*!< if the "filter" is not in the current filter set, then the
56 information will be dropped. otherwise the information is displayed,
57 where appropriate. for some environments, this will essentially throw
58 it away. for example, in ms-windows, a windowed program will only save
59 standard out if one redirects standard output to a file, whereas a
60 console mode program will output the text to its parent prompt. */
63 stream_choices c_target; //!< records whether stdout or stderr is used.
68 //!< a macro that retasks the program-wide logger as a console_logger.
69 #define SETUP_CONSOLE_LOGGER { \
70 loggers::standard_log_base *old_log = loggers::program_wide_logger::set \
71 (new loggers::console_logger); \
72 /* assumes we are good to entirely remove the old logger. */ \