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