Cantera  3.0.0
Loading...
Searching...
No Matches
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 logGroup 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
17namespace 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 logGroup
39//!
40class Logger
41{
42public:
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 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 a warning message.
71 /*!
72 * The default behavior is to write to the logging output.
73 * @param warning String specifying type of warning
74 * @param msg String message to be written to cout
75 */
76 virtual void warn(const string& warning, const string& msg) {
77 std::clog << warning << "Warning: " << msg << std::endl;
78 }
79
80 //! Write an error message and quit.
81 /*!
82 * The default behavior is to write to the standard error stream, and then
83 * call exit(). Note that no end-of-line character is appended to the
84 * message, and so if one is desired it must be included in the string. Note
85 * that this default behavior will terminate the application %Cantera is
86 * invoked from (MATLAB, Excel, etc.) If this is not desired, then derive a
87 * class and reimplement this method.
88 *
89 * @param msg Error message to be written to cerr.
90 */
91 virtual void error(const string& msg) {
92 std::cerr << msg << std::endl;
93 exit(EXIT_FAILURE);
94 }
95};
96
97}
98#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 string &msg)
Write a log message.
Definition logger.h:57
virtual ~Logger()
Destructor - empty.
Definition logger.h:47
virtual void error(const string &msg)
Write an error message and quit.
Definition logger.h:91
virtual void warn(const string &warning, const string &msg)
Write a warning message.
Definition logger.h:76
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564