SPH
Logger.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Globals.h"
9 #include "io/Path.h"
12 #include "objects/wrappers/Flags.h"
13 #include "system/Timer.h"
14 #include <iomanip>
15 
17 
22 class ILogger : public Polymorphic, public Noncopyable {
23 private:
24  Size precision = PRECISION;
25  bool scientific = true;
26 
27 public:
31  virtual void writeString(const std::string& s) = 0;
32 
36  template <typename... TArgs>
37  void write(TArgs&&... args) {
38  std::stringstream ss;
39  writeImpl(ss, std::forward<TArgs>(args)...);
40  ss << std::endl;
41  this->writeString(ss.str());
42  }
43 
47  void setPrecision(const Size newPrecision) {
48  precision = newPrecision;
49  }
50 
52  void setScientific(const bool newScientific) {
53  scientific = newScientific;
54  }
55 
56 private:
57  template <typename T0, typename... TArgs>
58  void writeImpl(std::stringstream& ss, T0&& first, TArgs&&... rest) {
59  ss << std::setprecision(precision);
60  if (scientific) {
61  ss << std::scientific;
62  }
63  ss << first;
64  writeImpl(ss, std::forward<TArgs>(rest)...);
65  }
66  void writeImpl(std::stringstream& UNUSED(ss)) {}
67 };
68 
69 
70 struct Console {
71  enum class Foreground {
72  BLACK = 30,
73  RED = 31,
74  GREEN = 32,
75  YELLOW = 33,
76  BLUE = 34,
77  MAGENTA = 35,
78  CYAN = 36,
79  LIGHT_GRAY = 37,
80  DARK_GRAY = 90,
81  LIGHT_RED = 91,
82  LIGHT_GREEN = 92,
83  LIGHT_YELLOW = 93,
84  LIGHT_BLUE = 94,
85  LIGHT_MAGENTA = 95,
86  LIGHT_CYAN = 96,
87  WHITE = 97,
88  DEFAULT = 39,
89  UNCHANGED = 0,
91 
92  enum class Background {
93  RED = 41,
94  GREEN = 42,
95  BLUE = 44,
96  DEFAULT = 49,
97  UNCHANGED = 0
99 
100  enum class Series {
101  NORMAL = 0,
102  BOLD = 1,
104 
105  Console() = default;
106 
108  : fg(fg) {}
109 
111  : bg(bg) {}
112 
114  : series(series) {}
115 
116  friend std::ostream& operator<<(std::ostream& stream, const Console& mod) {
117  if (mod.bg != Background::UNCHANGED) {
118  stream << "\033[" << int(mod.bg) << "m";
119  }
120  if (mod.fg != Foreground::UNCHANGED) {
121  stream << "\033[" << int(mod.fg) << "m";
122  }
123  stream << "\e[" << int(mod.series) << "m";
124  return stream;
125  }
126 };
127 
129  ScopedConsole(const Console console);
130  ~ScopedConsole();
131 };
132 
133 
138 class StdOutLogger : public ILogger {
139 public:
140  virtual void writeString(const std::string& s) override;
141 };
142 
143 
145 class StringLogger : public ILogger {
146 private:
147  std::stringstream ss;
148 
149 public:
150  virtual void writeString(const std::string& s) override;
151 
153  void clean();
154 
156  std::string toString() const;
157 };
158 
160 class FileLogger : public ILogger {
161 public:
162  enum class Options {
166  KEEP_OPENED = 0,
167 
171  OPEN_WHEN_WRITING = 1 << 0,
172 
175  APPEND = 1 << 1,
176 
178  ADD_TIMESTAMP = 1 << 2,
179  };
180 
181 private:
182  AutoPtr<std::ofstream> stream;
183  Path path;
184  Flags<Options> flags;
185 
186 public:
187  FileLogger(const Path& path, const Flags<Options> flags = EMPTY_FLAGS);
188 
190 
191  virtual void writeString(const std::string& s) override;
192 };
193 
197 class MultiLogger : public ILogger {
198 private:
199  Array<AutoPtr<ILogger>> loggers;
200 
201 public:
202  Size getLoggerCnt() const {
203  return loggers.size();
204  }
205 
206  void add(AutoPtr<ILogger>&& logger) {
207  loggers.push(std::move(logger));
208  }
209 
210  virtual void writeString(const std::string& s) override {
211  for (auto& l : loggers) {
212  l->writeString(s);
213  }
214  }
215 };
216 
218 class NullLogger : public ILogger {
219 public:
220  virtual void writeString(const std::string& UNUSED(s)) override {}
221 };
222 
224 class VerboseLogGuard : public Noncopyable {
225 private:
226  Timer timer;
227 
228 public:
230  VerboseLogGuard(const std::string& functionName);
231 
233 };
234 
239 void setVerboseLogger(AutoPtr<ILogger>&& logger);
240 
242 #define VERBOSE_LOG VerboseLogGuard __verboseLogGuard(__PRETTY_FUNCTION__);
243 
Generic dynamically allocated resizable storage.
Simplified implementation of std::unique_ptr, using only default deleter.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Wrapper over enum allowing setting (and querying) individual bits of the stored value.
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
Global parameters of the code.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
constexpr int PRECISION
Number of valid digits of numbers on output.
Definition: Globals.h:25
void setVerboseLogger(AutoPtr< ILogger > &&logger)
Creates a global verbose logger.
Definition: Logger.cpp:121
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Measuring time intervals and executing periodic events.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter size() const noexcept
Definition: Array.h:193
File output logger.
Definition: Logger.h:160
virtual void writeString(const std::string &s) override
Logs a string message.
Definition: Logger.cpp:56
FileLogger(const Path &path, const Flags< Options > flags=EMPTY_FLAGS)
Definition: Logger.cpp:40
@ ADD_TIMESTAMP
Adds a time of writing before each message.
Interface providing generic (text, human readable) output of the program.
Definition: Logger.h:22
void setPrecision(const Size newPrecision)
Changes the precision of printed numbers.
Definition: Logger.h:47
virtual void writeString(const std::string &s)=0
Logs a string message.
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Definition: Logger.h:37
void setScientific(const bool newScientific)
Sets/unsets scientific notation.
Definition: Logger.h:52
Class holding multiple loggers and writing messages to all of them.
Definition: Logger.h:197
void add(AutoPtr< ILogger > &&logger)
Definition: Logger.h:206
Size getLoggerCnt() const
Definition: Logger.h:202
virtual void writeString(const std::string &s) override
Logs a string message.
Definition: Logger.h:210
Helper logger that does not write anything.
Definition: Logger.h:218
virtual void writeString(const std::string &UNUSED(s)) override
Definition: Logger.h:220
Object representing a path on a filesystem.
Definition: Path.h:17
Standard output logger.
Definition: Logger.h:138
virtual void writeString(const std::string &s) override
Logs a string message.
Definition: Logger.cpp:20
Logger writing messages to string stream.
Definition: Logger.h:145
virtual void writeString(const std::string &s) override
Logs a string message.
Definition: Logger.cpp:24
void clean()
Removes all written messages from the string.
Definition: Logger.cpp:28
std::string toString() const
Returns all written messages as a string. Messages are not erased from the logger by this.
Definition: Logger.cpp:35
Basic time-measuring tool. Starts automatically when constructed.
Definition: Timer.h:27
RAII guard writing called functions and their durations to a special verbose logger.
Definition: Logger.h:224
VerboseLogGuard(const std::string &functionName)
Creates a guard, should be at the very beginning of a function/scope.
Definition: Logger.cpp:84
Definition: Logger.h:70
friend std::ostream & operator<<(std::ostream &stream, const Console &mod)
Definition: Logger.h:116
Console(const Background bg)
Definition: Logger.h:110
enum Console::Foreground fg
Series
Definition: Logger.h:100
enum Console::Series series
Background
Definition: Logger.h:92
enum Console::Background bg
Console(const Foreground fg)
Definition: Logger.h:107
Console(const Series series)
Definition: Logger.h:113
Console()=default
Foreground
Definition: Logger.h:71
Object with deleted copy constructor and copy operator.
Definition: Object.h:54
Base class for all polymorphic objects.
Definition: Object.h:88
ScopedConsole(const Console console)
Definition: Logger.cpp:12
~ScopedConsole()
Definition: Logger.cpp:16