Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
logger.h
Go to the documentation of this file.
1 /**
2  * @file logger.h
3  * Header for Base class for 'loggers' that write text messages to log files
4  * (see \ref textlogs and class \link Cantera::Logger Logger\endlink).
5  */
6 #ifndef CT_LOGGER_H
7 #define CT_LOGGER_H
8 
9 #include "ct_defs.h"
10 
11 #include <iostream>
12 
13 namespace Cantera
14 {
15 
16 ///
17 /// Base class for 'loggers' that write text messages to log files.
18 ///
19 /// This class is used to direct log messages to application- or
20 /// environment-specific output. The default is to simply print
21 /// the messages to the standard output stream or standard error
22 /// stream, but classes may be derived from Logger that implement
23 /// other output options. This is important when Cantera is used
24 /// in applications that do not display the standard output, such
25 /// as MATLAB. The Cantera MATLAB interface derives a class from
26 /// Logger that implements these methods with MATLAB-specific
27 /// procedures, insuring that the messages will be passed through
28 /// to the user. It would also be possible to derive a class that
29 /// displayed the messages in a pop-up window, or redirected them
30 /// to a file, etc.
31 ///
32 /// To install a logger, call function setLogger (global.h / misc.cpp).
33 ///
34 /// See the files Cantera/python/src/pylogger.h and
35 /// Cantera/matlab/cantera/private/mllogger.h for examples of
36 /// deriving logger classes.
37 /// @ingroup textlogs
38 ///
39 class Logger
40 {
41 public:
42 
43  //! Constructor - empty
44  Logger() {}
45 
46  //! Destructor - empty
47  virtual ~Logger() {}
48 
49  //! Write a log message.
50  /*!
51  * The default behavior is to write to
52  * the standard output. Note that no end-of-line character is
53  * appended to the message, and so if one is desired it must
54  * be included in the string.
55  *
56  * @param msg String message to be written to cout
57  */
58  virtual void write(const std::string& msg) {
59  std::cout << msg;
60  }
61 
62  //! Write an end of line character and flush output.
63  /*!
64  * Some systems treat endl and \n differently. The endl
65  * statement causes a flushing of stdout to the screen.
66  */
67  virtual void writeendl() {
68  std::cout << std::endl;
69  }
70 
71  //! Write an error message and quit.
72  /*!
73  * The default behavior is
74  * to write to the standard error stream, and then call
75  * exit(). Note that no end-of-line character is appended to
76  * the message, and so if one is desired it must be included
77  * in the string. Note that this default behavior will
78  * terminate the application Cantera is invoked from (MATLAB,
79  * Excel, etc.) If this is not desired, then derive a class
80  * and reimplement this method.
81  *
82  * @param msg Error message to be written to cerr.
83  */
84  virtual void error(const std::string& msg) {
85  std::cerr << msg << std::endl;
86  exit(EXIT_FAILURE);
87  }
88 };
89 
90 }
91 #endif
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:39
virtual ~Logger()
Destructor - empty.
Definition: logger.h:47
virtual void write(const std::string &msg)
Write a log message.
Definition: logger.h:58
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:84
Logger()
Constructor - empty.
Definition: logger.h:44
virtual void writeendl()
Write an end of line character and flush output.
Definition: logger.h:67