votca 2024.1-dev
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
21
22#pragma once
23#ifndef VOTCA_XTP_LOGGER_H
24#define VOTCA_XTP_LOGGER_H
25
26// Standard includes
27#include <chrono>
28#include <iostream>
29#include <sstream>
30
31// VOTCA includes
32#include <votca/tools/globals.h>
33
34namespace votca {
35namespace xtp {
36
37/*
38 * Macros to use the Logger: XTP_LOG(level,logger) << message
39 */
40#define XTP_LOG(level, log) \
41 if (level > (log).getReportLevel()) \
42 ; \
43 else \
44 (log)(level)
45
46/*
47 * Custom buffer to store messages
48 */
49class LogBuffer final : public std::stringbuf {
50
51 public:
52 LogBuffer() : std::stringbuf() { LogLevel_ = Log::current_level; }
53
54 // sets the log level (needed for output)
55 void setLogLevel(Log::Level LogLevel) { LogLevel_ = LogLevel; }
56
57 // sets Multithreading (buffering required)
58 void setMultithreading(bool maverick) { maverick_ = maverick; }
59
60 // sets preface strings for Log::error, Log::warning, ...
61 void setPreface(Log::Level level, std::string preface) {
62 switch (level) {
63
65 errorPreface_ = preface;
66 break;
68 warnPreface_ = preface;
69 break;
71 infoPreface_ = preface;
72 break;
74 dbgPreface_ = preface;
75 break;
76 }
77 }
78
79 void EnablePreface() { writePreface_ = true; }
80 void DisablePreface() { writePreface_ = false; }
81
82 // flushes all collected messages
83 void FlushBuffer() {
84 std::cout << stringStream_.str();
85 stringStream_.str("");
86 }
87
88 // returns the pointer to the collected messages
89 std::string Messages() {
90 std::string messages_ = stringStream_.str();
91 stringStream_.str("");
92 return messages_;
93 }
94
95 private:
96 // Log Level (WARNING, INFO, etc)
98
99 // temporary buffer to store messages
100 std::ostringstream stringStream_;
101
102 // Multithreading
103 bool maverick_ = true;
104
105 std::string errorPreface_ = "\n ERROR ";
106 std::string warnPreface_ = "\n WARNING ";
107 std::string infoPreface_ = "\n ";
108 std::string dbgPreface_ = "\n DEBUG ";
109 bool writePreface_ = true;
110
111 protected:
112 int sync() {
113
114 std::ostringstream message_;
115
116 if (writePreface_) {
117 switch (LogLevel_) {
119 message_ << errorPreface_;
120 break;
122 message_ << warnPreface_;
123 break;
124 case Log::Level::info:
125 message_ << infoPreface_;
126 break;
128 message_ << dbgPreface_;
129 break;
130 }
131 }
132
133 if (!maverick_) {
134 // collect all messages of one thread
135 stringStream_ << message_.str() << " " << str();
136 } else {
137 // if only one thread outputs, flush immediately
138 std::cout << message_.str() << " " << str() << std::flush;
139 }
140 message_.str("");
141 str("");
142 return 0;
143 }
144};
145
164class Logger final : public std::ostream {
165
166 friend std::ostream &operator<<(std::ostream &log_out, Logger &logger) {
167 log_out << logger.Messages();
168 return log_out;
169 }
170
171 public:
172 Logger() : std::ostream(&buffer_), ReportLevel_(Log::current_level) {
174 }
175 Logger(Log::Level ReportLevel)
176 : std::ostream(&buffer_), ReportLevel_(ReportLevel) {
178 }
179
181 buffer_.setLogLevel(LogLevel);
182 return *this;
183 }
184
185 void setReportLevel(Log::Level ReportLevel) { ReportLevel_ = ReportLevel; }
186 void setMultithreading(bool maverick) {
187 maverick_ = maverick;
189 }
190 bool isMaverick() const { return maverick_; }
191
193
194 void setPreface(Log::Level level, const std::string &preface) {
195 buffer_.setPreface(level, preface);
196 }
197
198 void setCommonPreface(const std::string &preface) {
199 setPreface(Log::info, preface);
200 setPreface(Log::error, preface);
201 setPreface(Log::debug, preface);
202 setPreface(Log::warning, preface);
203 }
204
206
208
209 private:
211 // at what level of detail output messages
213
214 // if true, only a single processor job is executed
215 bool maverick_ = false;
216
217 std::string Messages() { return buffer_.Messages(); }
218};
219
225 public:
226 friend std::ostream &operator<<(std::ostream &os, const TimeStamp &) {
227 std::time_t now_time =
228 std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
229 std::tm *timeinfo = std::localtime(&now_time);
230 os << timeinfo->tm_year + 1900 << "-" << timeinfo->tm_mon + 1 << "-"
231 << timeinfo->tm_mday << " " << timeinfo->tm_hour << ":"
232 << timeinfo->tm_min << ":" << timeinfo->tm_sec;
233 return os;
234 }
235
236 explicit TimeStamp() = default;
237};
238
239} // namespace xtp
240} // namespace votca
241
242#endif // VOTCA_XTP_LOGGER_H
Log::Level LogLevel_
Definition logger.h:97
std::ostringstream stringStream_
Definition logger.h:100
void DisablePreface()
Definition logger.h:80
void setMultithreading(bool maverick)
Definition logger.h:58
std::string Messages()
Definition logger.h:89
void setPreface(Log::Level level, std::string preface)
Definition logger.h:61
std::string warnPreface_
Definition logger.h:106
std::string dbgPreface_
Definition logger.h:108
std::string errorPreface_
Definition logger.h:105
void setLogLevel(Log::Level LogLevel)
Definition logger.h:55
std::string infoPreface_
Definition logger.h:107
Logger is used for thread-safe output of messages.
Definition logger.h:164
void setPreface(Log::Level level, const std::string &preface)
Definition logger.h:194
void setReportLevel(Log::Level ReportLevel)
Definition logger.h:185
Log::Level ReportLevel_
Definition logger.h:212
Log::Level getReportLevel() const
Definition logger.h:192
Logger & operator()(Log::Level LogLevel)
Definition logger.h:180
void setMultithreading(bool maverick)
Definition logger.h:186
std::string Messages()
Definition logger.h:217
void setCommonPreface(const std::string &preface)
Definition logger.h:198
void DisablePreface()
Definition logger.h:207
friend std::ostream & operator<<(std::ostream &log_out, Logger &logger)
Definition logger.h:166
Logger(Log::Level ReportLevel)
Definition logger.h:175
void EnablePreface()
Definition logger.h:205
LogBuffer buffer_
Definition logger.h:210
bool isMaverick() const
Definition logger.h:190
Timestamp returns the current time as a string Example: cout << TimeStamp()
Definition logger.h:224
friend std::ostream & operator<<(std::ostream &os, const TimeStamp &)
Definition logger.h:226
STL namespace.
base class for all analysis tools
Definition basebead.h:33
static Level current_level
Definition globals.h:30
Level
be loud and noisy
Definition globals.h:28