| Cantera 3.0.0

Loading...
Searching...
No Matches
ExternalLogger.h
1// This file is part of Cantera. See License.txt in the top-level directory or
2// at https://cantera.org/license.txt for license and copyright information.
3
4#ifndef CT_EXTERNAL_LOGGER_H
5#define CT_EXTERNAL_LOGGER_H
6
7#include "logger.h"
8
9 namespace Cantera {
10
11//! Logger that delegates to an external source via a callback to produce log output.
12//! @ingroup logGroup
13 class ExternalLogger : public Logger
14{
15 public:
16 explicit ExternalLogger(LogCallback writer) {
17 if (writer == nullptr) {
18 throw CanteraError("ExternalLogger::ExternalLogger",
19 "Argument “writer” must not be null!");
20 }
21
22 m_writer = writer;
23 }
24
25 void write(const string& msg) override {
26 m_writeBuffer.append(msg);
27
28 if (!m_writeBuffer.empty() && m_writeBuffer.back() == '\n') {
29 // This is a bit strange, but the terminal new line is interpreted to mean
30 // “end of message”, so we want to pop it from the message itself.
31 // The other side of the logger will be in charge of deciding whether
32 // “messages” will have a terminal new line or not.
33 m_writeBuffer.pop_back();
34
35 m_writer(LogLevel::INFO, "Info", m_writeBuffer.c_str());
36
37 m_writeBuffer.erase();
38 }
39 }
40
41 void writeendl() override {
42 m_writer(LogLevel::INFO, "Info", m_writeBuffer.c_str());
43
44 m_writeBuffer.erase();
45 }
46
47 void warn(const string& warning, const string& msg) override {
48 m_writer(LogLevel::WARN, warning.c_str(), msg.c_str());
49 }
50
51 void error(const string& msg) override {
52 m_writer(LogLevel::ERROR, "Error", msg.c_str());
53 }
54
55 private:
56 string m_writeBuffer;
57
58 LogCallback m_writer = nullptr;
59};
60
61}
62
63#endif
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition ctexceptions.h:66
Cantera::ExternalLogger
Logger that delegates to an external source via a callback to produce log output.
Definition ExternalLogger.h:14
Cantera::ExternalLogger::writeendl
void writeendl() override
Write an end of line character and flush output.
Definition ExternalLogger.h:41
Cantera::ExternalLogger::write
void write(const string &msg) override
Write a log message.
Definition ExternalLogger.h:25
Cantera::ExternalLogger::error
void error(const string &msg) override
Write an error message and quit.
Definition ExternalLogger.h:51
Cantera::ExternalLogger::warn
void warn(const string &warning, const string &msg) override
Write a warning message.
Definition ExternalLogger.h:47
Cantera::Logger
Base class for 'loggers' that write text messages to log files.
Definition logger.h:41
LogCallback
void(* LogCallback)(enum LogLevel logLevel, const char *category, const char *message)
Represents a callback that is invoked to produce log output.
Definition clib_defs.h:33
logger.h
Header for Base class for 'loggers' that write text messages to log files (see Logging and class Logg...
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564