NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
logger.cpp
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#include "logger.hpp"
20#include "../utils/stringtools.hpp"
21#include <windows.h>
22
24
25typedef BOOL (WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
26
27
35bool IsWow64()
36{
37 BOOL bIsWow64 = false;
38
39 //IsWow64Process is not available on all supported versions of Windows.
40 //Use GetModuleHandle to get a handle to the DLL that contains the function
41 //and GetProcAddress to get a pointer to the function if available.
42
43 LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
44 GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
45
46 if (NULL != fnIsWow64Process)
47 {
48 if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
49 {
50 return false;
51 }
52 }
53 return (bool)bIsWow64;
54}
55
56
57
58
59
60
61
62
63
64
65
66
67
72{
73 //
74}
75
76
84Logger::Logger(const std::string& sLogFile)
85{
86 open(sLogFile);
87}
88
89
100bool Logger::open(const std::string& sLogFile)
101{
102 if (m_logFile.is_open())
103 m_logFile.close();
104
105 m_logFile.open(sLogFile, std::ios_base::out | std::ios_base::app | std::ios_base::ate);
106 m_sLogFile = sLogFile;
107
108 return m_logFile.good();
109}
110
111
119{
120 m_logFile.close();
121}
122
123
131bool Logger::is_open() const
132{
133 return m_logFile.is_open();
134}
135
136
145{
146 if (!m_logFile.is_open() && m_sLogFile.length())
147 m_logFile.open(m_sLogFile, std::ios_base::out | std::ios_base::app | std::ios_base::ate);
148
149 return m_logFile.is_open();
150}
151
152
162void Logger::push(const std::string& sMessage)
163{
164 if (ensure_open())
165 m_logFile << sMessage;
166}
167
168
178void Logger::push_line(const std::string& sMessage)
179{
180 if (ensure_open())
181 m_logFile << sMessage << std::endl;
182}
183
184
185
186
187
188
189
198{
199 //
200}
201
202
209{
210 // Write an information to the log file
211 push_info("SESSION WAS TERMINATED SUCCESSFULLY\n");
212}
213
214
224{
225 return !is_open();
226}
227
228
237bool DetachedLogger::open(const std::string& sLogFile)
238{
239 bool good = Logger::open(sLogFile);
240
241 if (!good)
242 return false;
243
244 for (size_t i = 0; i < m_buffer.size(); i++)
246
247 m_buffer.clear();
248
249 return good;
250}
251
252
262{
263 m_level = lvl;
264}
265
266
276void DetachedLogger::push_info(const std::string& sInfo)
277{
278 if (is_buffering())
279 m_buffer.push_back(sInfo);
280 else
281 Logger::push_line(sInfo);
282}
283
284
293{
294 std::string sysInfo;
295 // Get the version of the operating system
296 OSVERSIONINFOA _osversioninfo;
297 _osversioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
298 GetVersionExA(&_osversioninfo);
299
300 // Create system information to the log file
301 sysInfo = "OS: Windows v " + toString((int)_osversioninfo.dwMajorVersion) + "." + toString((int)_osversioninfo.dwMinorVersion) + "." + toString((int)_osversioninfo.dwBuildNumber) + (IsWow64() ? " (64 Bit)" : "");
302
303 push_info(sysInfo);
304}
305
306
318void DetachedLogger::push_line(Logger::LogLevel lvl, const std::string& sMessage)
319{
320 if (lvl < m_level)
321 return;
322
323 std::string sLevel = toString(sys_time_now(), GET_MILLISECONDS) + " ";
324
325 switch (lvl)
326 {
328 sLevel += "DEBUG: ";
329 break;
330 case Logger::LVL_INFO:
331 sLevel += "INFO: ";
332 break;
334 sLevel += "CMDLINE: ";
335 break;
337 sLevel += "WARNING: ";
338 break;
340 sLevel += "ERROR: ";
341 break;
343 return;
344 }
345
346 push_info(sLevel + sMessage);
347}
348
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 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
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 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
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
sys_time_point sys_time_now()
Returns the current time as a sys_time_point (i.e. a std::chrono::time_point with microseconds precis...
bool IsWow64()
This function returns true, if we're currently running on Win x64.
Definition: logger.cpp:35
BOOL(WINAPI * LPFN_ISWOW64PROCESS)(HANDLE, PBOOL)
Definition: logger.cpp:25
DetachedLogger g_logger
Definition: logger.cpp:23
@ GET_MILLISECONDS
Definition: stringtools.hpp:37
std::string toString(int)
Converts an integer to a string without the Settings bloat.