sisi4s
Loading...
Searching...
No Matches
Log.hpp
Go to the documentation of this file.
1#ifndef LOG_DEFINED
2#define LOG_DEFINED
3
4#include <util/Time.hpp>
5
6#include <string>
7#include <iostream>
8#include <streambuf>
9#include <fstream>
10
11// A nice handy macro to do formatting
12#define _FORMAT(_fmt, ...) \
13 ([&](void) -> std::string { \
14 int _sz = std::snprintf(nullptr, 0, _fmt, __VA_ARGS__); \
15 std::vector<char> _out(_sz + 1); \
16 std::snprintf(&_out[0], _out.size(), _fmt, __VA_ARGS__); \
17 return std::string(_out.data()); \
18 })()
19
20namespace sisi4s {
21class LogBuffer : public std::streambuf {
22public:
23 LogBuffer(std::streambuf *log_, std::streambuf *out_)
24 : log(log_)
25 , out(out_) {}
26
27protected:
28 virtual int overflow(int c) {
29 if (c == EOF) {
30 return !EOF;
31 } else {
32 int const logPut(log->sputc(c));
33 int const outPut(out->sputc(c));
34 return logPut == EOF || outPut == EOF ? EOF : c;
35 }
36 }
37
38 virtual int sync() {
39 int const logSync(log->pubsync());
40 int const outSync(out->pubsync());
41 return logSync == 0 && outSync == 0 ? 0 : -1;
42 }
43 std::streambuf *log, *out;
44};
45
46class LogStream : public std::ostream {
47public:
48 LogStream(std::string const &logFileName,
49 int const logLevel = 0,
50 std::string const &indent = "\t");
51
52 std::ostream &prepare(int const rank,
53 std::string const &sourceFileName,
54 int const level,
55 std::string const &category = "");
56
57protected:
58 std::ofstream logFile;
60
71 std::string indent;
72
74};
75
80class Log {
81public:
82 static void setRank(const int rank);
83 static int getRank();
84 static void setFileName(const std::string &fileName);
85 static std::string getFileName();
86 static void setLogLevel(const int logLevel);
87 static int getLogLevel();
88
89 static LogStream &getLogStream();
90
91protected:
92 static int rank;
93 static std::string fileName;
94 static int logLevel;
96};
97} // namespace sisi4s
98
99// TODO: return output stream for all processes, including those
100// who shouldn't print, so that formating functions can be used.
107#define OUT() \
108 if (sisi4s::Log::getRank() != 0) { \
109 } else std::cout
110#define WARN() \
111 if (sisi4s::Log::getRank() != 0) { \
112 } else std::cout << "WARNING: "
113#define NEW_FILE(NAME) \
114 if (sisi4s::Log::getRank() != 0) { \
115 } else std::ofstream(NAME, std::ofstream::out)
116#define FILE(NAME) \
117 if (sisi4s::Log::getRank() != 0) { \
118 } else std::ofstream(NAME, std::ofstream::app)
119#define LOG(...) \
120 if (sisi4s::Log::getRank() != 0) { \
121 } else sisi4s::Log::getLogStream().prepare(0, __FILE__, __VA_ARGS__)
122#define LOG_RANK(...) \
123 sisi4s::Log::getLogStream().prepare(sisi4s::Log::getRank(), \
124 __FILE__, \
125 __VA_ARGS__)
126
127#endif
Definition Log.hpp:21
LogBuffer(std::streambuf *log_, std::streambuf *out_)
Definition Log.hpp:23
virtual int sync()
Definition Log.hpp:38
std::streambuf * log
Definition Log.hpp:43
std::streambuf * out
Definition Log.hpp:43
virtual int overflow(int c)
Definition Log.hpp:28
Definition Log.hpp:46
std::string indent
Indentation string used for each log level. By default a tab character will be used.
Definition Log.hpp:71
int logLevel
The log level to use for subsequent LOG messages. A log message will only be written if its log level...
Definition Log.hpp:66
std::ostream & prepare(int const rank, std::string const &sourceFileName, int const level, std::string const &category="")
Definition Log.cxx:19
std::ofstream logFile
Definition Log.hpp:58
LogBuffer logBuffer
Definition Log.hpp:59
Time startTime
Definition Log.hpp:73
Class with static members offering control over logging. Log entries are created with the macro LOG.
Definition Log.hpp:80
static void setFileName(const std::string &fileName)
Definition Log.cxx:49
static LogStream * logStream
Definition Log.hpp:95
static LogStream & getLogStream()
Definition Log.cxx:57
static int getRank()
Definition Log.cxx:47
static int getLogLevel()
Definition Log.cxx:55
static int logLevel
Definition Log.hpp:94
static void setRank(const int rank)
Definition Log.cxx:45
static void setLogLevel(const int logLevel)
Definition Log.cxx:53
static std::string fileName
Definition Log.hpp:93
static std::string getFileName()
Definition Log.cxx:51
static int rank
Definition Log.hpp:92
Definition Time.hpp:17
Definition Algorithm.hpp:10