Cantera  3.2.0a2
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
9namespace Cantera {
10
11//! Logger that delegates to an external source via a callback to produce log output.
12//! @ingroup logGroup
13class ExternalLogger : public Logger
14{
15public:
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
55private:
56 string m_writeBuffer;
57
58 LogCallback m_writer = nullptr;
59};
60
61}
62
63#endif
Base class for exceptions thrown by Cantera classes.
Logger that delegates to an external source via a callback to produce log output.
void writeendl() override
Write an end of line character and flush output.
void write(const string &msg) override
Write a log message.
void error(const string &msg) override
Write an error message and quit.
void warn(const string &warning, const string &msg) override
Write a warning message.
Base class for 'loggers' that write text messages to log files.
Definition logger.h:40
void(* LogCallback)(enum LogLevel logLevel, const char *category, const char *message)
Represents a callback that is invoked to produce log output.
Header for Base class for 'loggers' that write text messages to log files (see Logging and class Logg...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595