Cantera  2.0
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 #include <string>
13 #include <cstdlib>
14 
15 namespace Cantera
16 {
17 
18 ///
19 /// Base class for 'loggers' that write text messages to log files.
20 ///
21 /// This class is used to direct log messages to application- or
22 /// environment-specific output. The default is to simply print
23 /// the messages to the standard output stream or standard error
24 /// stream, but classes may be derived from Logger that implement
25 /// other output options. This is important when Cantera is used
26 /// in applications that do not display the standard output, such
27 /// as MATLAB. The Cantera MATLAB interface derives a class from
28 /// Logger that implements these methods with MATLAB-specific
29 /// procedures, insuring that the messages will be passed through
30 /// to the user. It would also be possible to derive a class that
31 /// displayed the messages in a pop-up window, or redirected them
32 /// to a file, etc.
33 ///
34 /// To install a logger, call function setLogger (global.h / misc.cpp).
35 ///
36 /// See the files Cantera/python/src/pylogger.h and
37 /// Cantera/matlab/cantera/private/mllogger.h for examples of
38 /// deriving logger classes.
39 /// @ingroup textlogs
40 ///
41 class Logger
42 {
43 public:
44 
45  //! Constructor - empty
46  Logger() {}
47 
48  //! Destructor - empty
49  virtual ~Logger() {}
50 
51  //! Write a log message.
52  /*!
53  * The default behavior is to write to
54  * the standard output. Note that no end-of-line character is
55  * appended to the message, and so if one is desired it must
56  * be included in the string.
57  *
58  * @param msg String message to be written to cout
59  */
60  virtual void write(const std::string& msg) {
61  std::cout << msg;
62  }
63 
64  //! Write an end of line character and flush output.
65  /*!
66  * Some systems treat endl and \n differently. The endl
67  * statement causes a flushing of stdout to the screen.
68  */
69  virtual void writeendl() {
70  std::cout << std::endl;
71  }
72 
73  //! Write an error message and quit.
74  /*!
75  * The default behavior is
76  * to write to the standard error stream, and then call
77  * exit(). Note that no end-of-line character is appended to
78  * the message, and so if one is desired it must be included
79  * in the string. Note that this default behavior will
80  * terminate the application Cantera is invoked from (MATLAB,
81  * Excel, etc.) If this is not desired, then derive a class
82  * and reimplement this method.
83  *
84  * @param msg Error message to be written to cerr.
85  */
86  virtual void error(const std::string& msg) {
87  std::cerr << msg << std::endl;
88  exit(EXIT_FAILURE);
89  }
90 
91  /// Return an integer specifying the application environment.
92  /// @deprecated
93  DEPRECATED(virtual int env()) {
94  return 0;
95  }
96 };
97 
98 }
99 #endif