NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
logger.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2021 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#ifndef LOGGER_HPP
20#define LOGGER_HPP
21
22#include <fstream>
23#include <string>
24#include <vector>
25
32class Logger
33{
34 private:
35 std::ofstream m_logFile;
36 std::string m_sLogFile;
37
38 bool ensure_open();
39
40 public:
41 Logger();
42 Logger(const std::string& sLogFile);
43
44 bool open(const std::string& sLogFile);
45 void close();
46 bool is_open() const;
47
48 void push(const std::string& sMessage);
49 void push_line(const std::string& sMessage);
50
52 {
59 };
60};
61
62
63
69class DetachedLogger : public Logger
70{
71 private:
72 std::vector<std::string> m_buffer;
74
75 public:
78
79 bool is_buffering() const;
80 bool open(const std::string& sLogFile);
82
83 void push_info(const std::string& sInfo);
85 void push_line(Logger::LogLevel lvl, const std::string& sMessage);
86
94 inline void debug(const std::string& sMessage)
95 {
97 }
98
106 inline void info(const std::string& sMessage)
107 {
108 push_line(Logger::LVL_INFO, sMessage);
109 }
110
118 inline void cmdline(const std::string& sMessage)
119 {
121 }
122
130 inline void warning(const std::string& sMessage)
131 {
133 }
134
142 inline void error(const std::string& sMessage)
143 {
144 push_line(Logger::LVL_ERROR, sMessage);
145 }
146
147};
148
149
150// Declaration of the global instance
152
153#endif // LOGGER_HPP
154
This class is a specialisation of the Logger to run detached, i.e. as a global instance usable form e...
Definition: logger.hpp:70
bool is_buffering() const
Determine, whether this instance is currently buffering or directly writing to a file.
Definition: logger.cpp:223
void push_info(const std::string &sInfo)
Push a message to the logger, which is not dependend on the logging level and will be shown without a...
Definition: logger.cpp:276
bool open(const std::string &sLogFile)
Open the log file and push the buffered messages directly to this file.
Definition: logger.cpp:237
void info(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:106
void push_line(Logger::LogLevel lvl, const std::string &sMessage)
Push a message with the corresponding logging level to the logger. The message will be prefixed with ...
Definition: logger.cpp:318
void warning(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:130
void debug(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:94
Logger::LogLevel m_level
Definition: logger.hpp:73
std::vector< std::string > m_buffer
Definition: logger.hpp:72
~DetachedLogger()
DetachedLogger destructor. Appends a terminating message to the current logfile (if any).
Definition: logger.cpp:208
DetachedLogger(Logger::LogLevel lvl=Logger::LVL_INFO)
DetachedLogger constructor. Sets the default logging level.
Definition: logger.cpp:197
void error(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:142
void setLoggingLevel(Logger::LogLevel lvl)
Change the logging level or completely disable the logger.
Definition: logger.cpp:261
void write_system_information()
A helper function to write the current OS's information to the log file.
Definition: logger.cpp:292
void cmdline(const std::string &sMessage)
Convenience member function.
Definition: logger.hpp:118
This class represents a simple logging functionality, which might be extended in the future to handle...
Definition: logger.hpp:33
bool ensure_open()
Ensures that the stream is open and tries to re-open it otherwise.
Definition: logger.cpp:144
void push(const std::string &sMessage)
Push a message to the logger stream. Will automatically re-open a file, if the stream had been closed...
Definition: logger.cpp:162
std::string m_sLogFile
Definition: logger.hpp:36
bool is_open() const
Check, whether the logger stream is currently open.
Definition: logger.cpp:131
void push_line(const std::string &sMessage)
Push a line to the logger stream. The stream will automatically append the line termination character...
Definition: logger.cpp:178
bool open(const std::string &sLogFile)
Open the target logging file for writing.
Definition: logger.cpp:100
Logger()
Empty default constructor.
Definition: logger.cpp:71
std::ofstream m_logFile
Definition: logger.hpp:35
LogLevel
Definition: logger.hpp:52
@ LVL_INFO
Definition: logger.hpp:54
@ LVL_DISABLED
Definition: logger.hpp:58
@ LVL_WARNING
Definition: logger.hpp:56
@ LVL_ERROR
Definition: logger.hpp:57
@ LVL_DEBUG
Definition: logger.hpp:53
@ LVL_CMDLINE
Definition: logger.hpp:55
void close()
Close the logger stream.
Definition: logger.cpp:118
DetachedLogger g_logger
Definition: logger.cpp:23