VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
UbLogger.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef UBLOGGER_H
35#define UBLOGGER_H
36
37#include <fstream>
38#include <iomanip>
39#include <iostream>
40#include <cstdio>
41#include <sstream>
42#include <string>
43
44#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__WIN64__)
45#include <windows.h>
46#else
47#include <sys/time.h>
48#endif
49
50#if defined(CAB_BOOST) && !defined(NO_THREADSAFE_LOGGING)
51#include <boost/thread.hpp>
52#endif // CAB_BOOST
53
55
57// template <typename OutputPolicy> class Log - declaration
110template <typename OutputPolicy>
112{
113public:
115
116public:
118 virtual ~UbLogger();
119 std::ostringstream &get(const LogLevel &level = logINFO);
120
121public:
122 // static, weil man so spaeter die ObjErstellunge ersparen kann,
123 // falls level kleiner als Level
125
126 static std::string logLevelToString(const LogLevel &level);
127 static LogLevel logLevelFromString(const std::string &level);
128
129 static std::string logTimeString();
130
131protected:
132 std::ostringstream os;
133
134private:
135 UbLogger(const UbLogger &);
136 UbLogger &operator=(const UbLogger &);
137};
138
140// template <typename OutputPolicy> class Log - implementation
142template <typename OutputPolicy>
144/*==========================================================*/
145template <typename OutputPolicy>
146std::ostringstream &UbLogger<OutputPolicy>::get(const LogLevel &level)
147{
148 os << logTimeString() << " " << std::setw(6)
149#if defined(CAB_BOOST) && !defined(NO_MT_LOGGING)
150 << boost::this_thread::get_id() << " "
151#endif
152 << std::setw(8) << std::left << UbLogger<OutputPolicy>::logLevelToString(level) << ": "
153 << std::string(level > logDEBUG ? 3 * (level - logDEBUG) : 0, ' '); //<baumartiger output :D
154
155 return os;
156}
157/*==========================================================*/
158template <typename OutputPolicy>
160{
161 os << std::endl;
162 OutputPolicy::output(os.str());
163}
164/*==========================================================*/
165template <typename OutputPolicy>
171/*==========================================================*/
172template <typename OutputPolicy>
174{
175 static std::string const buffer[] = { "ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1",
176 "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5" };
177 return buffer[level];
178}
179/*==========================================================*/
180template <typename OutputPolicy>
182{
183 if (level == "DEBUG5")
184 return logDEBUG5;
185 if (level == "DEBUG4")
186 return logDEBUG4;
187 if (level == "DEBUG3")
188 return logDEBUG3;
189 if (level == "DEBUG2")
190 return logDEBUG2;
191 if (level == "DEBUG1")
192 return logDEBUG1;
193 if (level == "DEBUG")
194 return logDEBUG;
195 if (level == "INFO")
196 return logINFO;
197 if (level == "WARNING")
198 return logWARNING;
199 if (level == "ERROR")
200 return logERROR;
201
203 << "UbLogger<OutputPolicy>::logLevelFromString(level) - unknown logging level '" << level
204 << "'. Using INFO level as default.";
205 return logINFO;
206}
207
209// logTimeString
211#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__WIN64__)
212template <typename OutputPolicy>
213inline std::string UbLogger<OutputPolicy>::logTimeString()
214{
215 const int MAX_LEN = 200;
216 char buffer[MAX_LEN];
217 if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0, "HH':'mm':'ss", buffer, MAX_LEN) == 0) {
218 return "Error in std::string UbLogger<OutputPolicy>::logTimeString()";
219 }
220
221 char result[100] = { 0 };
222 static DWORD first = (DWORD)GetTickCount64();
223 std::snprintf(result, sizeof(result), "%s.%03ld", buffer, (long)(GetTickCount64() - first) % 1000);
224 return result;
225}
226#else
227template <typename OutputPolicy>
229{
230 char buffer[11];
231 time_t t;
232 time(&t);
233 tm r; // = { 0 };
234 strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r));
235 struct timeval tv;
236 gettimeofday(&tv, 0);
237 char result[100] = { 0 };
238 std::snprintf(result, sizeof(result), "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
239 return result;
240}
241#endif
242
246class Output2Stream // implementation of OutputPolicy
247{
248public:
249 static std::ostream *&getStream();
250 static void output(const std::string &msg);
251
253 static void setStream(const std::string &filename);
254
256 static void setStream(std::ostream *pStream, const bool &gcControl = false);
257
258protected:
259#if defined(CAB_BOOST) && !defined(NO_MT_LOGGING)
260 static boost::mutex mtx;
261#endif
262};
263/*==========================================================*/
264inline std::ostream *&Output2Stream::getStream()
265{
266 static std::ostream *pStream = &std::clog;
267 return pStream;
268}
269/*==========================================================*/
270inline void Output2Stream::setStream(std::ostream *pFile, const bool &gcControl)
271{
272#if defined(CAB_BOOST) && !defined(NO_MT_LOGGING)
273 boost::mutex::scoped_lock lock(mtx);
274#endif
275 static bool s_gcControl = false;
276
279 }
280
282
284}
285/*==========================================================*/
286inline void Output2Stream::setStream(const std::string &filename)
287{
288 std::ofstream *file = new std::ofstream(filename.c_str());
289 if (!(*file)) {
290 delete file;
291 Output2Stream::setStream(&std::cerr, false);
293 << " Output2Stream::setStream(const std::string& filename) could not open file " << filename
294 << " -> std::cerr is used instead " << std::endl;
295 return;
296 }
297 std::cout << "UbLog writes to " << filename << std::endl;
298 Output2Stream::setStream(file, true);
299}
300/*==========================================================*/
301inline void Output2Stream::output(const std::string &msg)
302{
303#if defined(CAB_BOOST) && !defined(NO_MT_LOGGING)
304 boost::mutex::scoped_lock lock(mtx);
305#endif
306 std::ostream *pStream = getStream();
307 if (!pStream)
308 return;
309 (*pStream) << msg << std::flush;
310}
311
313// UbLog
315class UbLog : public UbLogger<Output2Stream>
316{
317};
318
319// Macro to limit compiler-side maxLevel
320#ifndef UBLOG_MAX_LEVEL
321#define UBLOG_MAX_LEVEL logDEBUG5
322#endif
323
325// example UBLOG(logINFO) << "das ist ein log eintrag";
327#define UBLOG(level, logtext) \
328 if (level <= UBLOG_MAX_LEVEL && level <= UbLog::reportingLevel() && Output2Stream::getStream()) { \
329 UbLog().get(level) << logtext; \
330 }
331
333// makro 2 fuer korrekten MultiLineOutput (teuer!!)
334// example1: UBLOGML(logINFO, "line1"<<endl<<"line2"<<endl<<"line3" )
335// example2: UBLOGML(logINFO, "line1\nline2\nendl\nline3" )
337#define UBLOGML(level, multiline) \
338 if (level > UBLOG_MAX_LEVEL || level > UbLog::reportingLevel() || !Output2Stream::getStream()) \
339 ; \
340 else { \
341 std::ostringstream output; \
342 output << multiline; \
343 std::istringstream input(output.str()); \
344 while (!input.eof()) { \
345 std::string dummy; \
346 getline(input, dummy, '\n'); \
347 UbLog().get(level) << dummy; \
348 } \
349 }
351// makro3, falls auch bildschirmausgabe erwuenscht
352// -> es wird sowohl ins logfile als auch auf den "stream" geschrieben
353// wenn reporting level und level passen :D
354// example1: UBLOG2ML(logINFO, std::cout, "line1"<<endl<<"line2"<<endl<<"line3" )
355// example2: UBLOG2ML(logINFO, std::cout, "line1\nline2\nendl\nline3" )
357#define UBLOG2(level, stream, text) \
358 if (level > UBLOG_MAX_LEVEL || level > UbLog::reportingLevel() || !Output2Stream::getStream()) \
359 ; \
360 else { \
361 stream << text << std::endl; \
362 UbLog().get(level) << text; \
363 }
364
366// makro4, wie 3 nur mit multiline
367// example: UBLOG2(logINFO, std::cout, "test" )
369#define UBLOG2ML(level, stream, multiline) \
370 if (level > UBLOG_MAX_LEVEL || level > UbLog::reportingLevel() || !Output2Stream::getStream()) \
371 ; \
372 else { \
373 stream << multiline << std::endl; \
374 std::ostringstream output; \
375 output << multiline; \
376 std::istringstream input(output.str()); \
377 while (!input.eof()) { \
378 std::string dummy; \
379 getline(input, dummy, '\n'); \
380 UbLog().get(level) << dummy; \
381 } \
382 }
383
385// example 2
387// try
388// {
389// UbLog::reportingLevel() = UbLog::logLevelFromString("DEBUG3");
390// //UbLog::output_policy::setStream(&std::cerr); //<- clog ist stdandard
391// UbLog::output_policy::setStream("c:/temp/out.txt"); //kann man diese nicht oeffnen -> fehlermeldung -> Log wird
392// in cerr ausgegben
393//
394// int count = 3;
395// UBLOG(logINFO, "A loop with " << count << " iterations");
396// for (int i = 0; i != count; ++i)
397// {
398// UBLOG(logERROR , "error - the counter i = " << i );
399// UBLOG(logDEBUG1, "debug1 - the counter i = " << i );
400// UBLOG(logDEBUG2, "debug2 - the counter i = " << i );
401// UBLOG(logDEBUG3, "debug3 - the counter i = " << i );
402// //fuer MultiLine Eintraege: --> koerrekte formatierung im logfile
403// UBLOGML(logDEBUG3, "debug3 - the counter i = "<<endl<<" 2 zeile "<< "3. Zeile" << i);
404// UBLOGML(logDEBUG3, "debug3 - the counter i = "<<endl<<" 2 zeile "<< "3. Zeile" << i);
405// UBLOG2ML(logDEBUG3,std:cout,"debug3 - the counter i = "<<endl<<" 2 zeile "<< "3. Zeile" << i);
406// }
407// return 0;
408// }
409// catch(const std::exception& e)
410// {
411// UBLOG(logERROR) << e.what();
412// }
413
414#endif // UBLOGGER_H
415
Implementation of OutputPolicy)
Definition UbLogger.h:247
C++ Logger.
Definition UbLogger.h:112
OutputPolicy output_policy
Definition UbLogger.h:114
std::ostringstream os
Definition UbLogger.h:132
std::shared_ptr< T > SPtr
static std::string logTimeString()
Definition UbLogger.h:228
virtual ~UbLogger()
Definition UbLogger.h:159
static std::string logLevelToString(const LogLevel &level)
Definition UbLogger.h:173
std::ostringstream & get(const LogLevel &level=logINFO)
Definition UbLogger.h:146
static void output(const std::string &msg)
Definition UbLogger.h:301
static LogLevel & reportingLevel()
Definition UbLogger.h:166
static LogLevel logLevelFromString(const std::string &level)
Definition UbLogger.h:181
LogLevel
Definition UbLogger.h:54
static std::ostream *& getStream()
Definition UbLogger.h:264
static void setStream(const std::string &filename)
creates output-file-stream (of file opening fails -> stream is set to std::cerr)
Definition UbLogger.h:286
@ logDEBUG1
Definition UbLogger.h:54
@ logDEBUG5
Definition UbLogger.h:54
@ logDEBUG
Definition UbLogger.h:54
@ logDEBUG4
Definition UbLogger.h:54
@ logWARNING
Definition UbLogger.h:54
@ logINFO
Definition UbLogger.h:54
@ logDEBUG2
Definition UbLogger.h:54
@ logDEBUG3
Definition UbLogger.h:54
@ logERROR
Definition UbLogger.h:54