Cantera  3.1.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//! Python. The %Cantera Python interface derives a class from Logger that
28//! implements these methods with Python-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 file Cantera/python/src/pylogger.h for examples of
36//! deriving logger classes.
37//! @ingroup logGroup
38//!
39class Logger
40{
41public:
42 //! Constructor - empty
43 Logger() {}
44
45 //! Destructor - empty
46 virtual ~Logger() {}
47
48 //! Write a log message.
49 /*!
50 * The default behavior is to write to the standard output. Note that no
51 * end-of-line character is appended to the message, and so if one is
52 * desired it must be included in the string.
53 *
54 * @param msg String message to be written to cout
55 */
56 virtual void write(const string& msg) {
57 std::cout << msg;
58 }
59
60 //! Write an end of line character and flush output.
61 /*!
62 * Some systems treat endl and \n differently. The endl statement causes a
63 * flushing of stdout to the screen.
64 */
65 virtual void writeendl() {
66 std::cout << std::endl;
67 }
68
69 //! Write a warning message.
70 /*!
71 * The default behavior is to write to the logging output.
72 * @param warning String specifying type of warning
73 * @param msg String message to be written to cout
74 */
75 virtual void warn(const string& warning, const string& msg) {
76 std::clog << warning << "Warning: " << msg << std::endl;
77 }
78
79 //! Write an error message and quit.
80 /*!
81 * The default behavior is to write to the standard error stream, and then
82 * call exit(). Note that no end-of-line character is appended to the
83 * message, and so if one is desired it must be included in the string. Note
84 * that this default behavior will terminate the application %Cantera is
85 * invoked from (MATLAB, Excel, etc.) If this is not desired, then derive a
86 * class and reimplement this method.
87 *
88 * @param msg Error message to be written to cerr.
89 */
90 virtual void error(const string& msg) {
91 std::cerr << msg << std::endl;
92 exit(EXIT_FAILURE);
93 }
94};
95
96}
97#endif
Base class for 'loggers' that write text messages to log files.
Definition logger.h:40
virtual void writeendl()
Write an end of line character and flush output.
Definition logger.h:65
Logger()
Constructor - empty.
Definition logger.h:43
virtual void write(const string &msg)
Write a log message.
Definition logger.h:56
virtual ~Logger()
Destructor - empty.
Definition logger.h:46
virtual void error(const string &msg)
Write an error message and quit.
Definition logger.h:90
virtual void warn(const string &warning, const string &msg)
Write a warning message.
Definition logger.h:75
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:595