SPH
Logger.cpp
Go to the documentation of this file.
1 #include "io/Logger.h"
2 #include "common/Assert.h"
3 #include "io/FileSystem.h"
4 #include "objects/Exceptions.h"
7 #include <fstream>
8 #include <iostream>
9 
11 
13  std::cout << console;
14 }
15 
18 }
19 
20 void StdOutLogger::writeString(const std::string& s) {
21  std::cout << s << std::flush;
22 }
23 
24 void StringLogger::writeString(const std::string& s) {
25  ss << s << std::flush;
26 }
27 
29  // clear internal storage
30  ss.str(std::string());
31  // reset flags
32  ss.clear();
33 }
34 
35 std::string StringLogger::toString() const {
36  return ss.str();
37 }
38 
39 
40 FileLogger::FileLogger(const Path& path, const Flags<Options> flags)
41  : path(path)
42  , flags(flags) {
43  stream = makeAuto<std::ofstream>();
44  if (!flags.has(Options::OPEN_WHEN_WRITING)) {
45  auto mode = flags.has(Options::APPEND) ? std::ostream::app : std::ostream::out;
47  stream->open(path.native(), mode);
48  if (!*stream) {
49  throw IoError("Error opening FileLogger at " + path.native());
50  }
51  }
52 }
53 
54 FileLogger::~FileLogger() = default;
55 
56 void FileLogger::writeString(const std::string& s) {
57  auto write = [this](const std::string& s) {
58  if (flags.has(Options::ADD_TIMESTAMP)) {
59  std::time_t t = std::time(nullptr);
60  *stream << std::put_time(std::localtime(&t), "%b %d, %H:%M:%S -- ");
61  }
62  *stream << s << std::flush;
63  };
64 
65  if (flags.has(Options::OPEN_WHEN_WRITING)) {
66  stream->open(path.native(), std::ostream::app);
67  if (*stream) {
68  write(s);
69  stream->close();
70  }
71  } else {
72  SPH_ASSERT(stream->is_open());
73  write(s);
74  }
75 }
76 
78  AutoPtr<ILogger> logger = makeAuto<NullLogger>();
79  int indent = 0;
80 };
81 
82 static VerboseLogThreadContext context;
83 
84 VerboseLogGuard::VerboseLogGuard(const std::string& functionName) {
85  if (!context.logger) {
86  // quick exit in case no logger is used
87  return;
88  }
89  // remove unneeded parts of the pretty name
90  std::string printedName = functionName;
91  std::size_t n = printedName.find('(');
92  if (n != std::string::npos) {
93  // no need for the list of parameters
94  printedName = printedName.substr(0, n);
95  }
96  // remove unnecessary return types, common namespaces, ...
97  printedName = replaceAll(printedName, "Sph::", "");
98  printedName = replaceFirst(printedName, "virtual ", "");
99  printedName = replaceFirst(printedName, "void ", "");
100  printedName = replaceFirst(printedName, "int ", "");
101  printedName = replaceFirst(printedName, "auto ", "");
102 
103  const std::string prefix = std::string(4 * context.indent, ' ') + std::to_string(context.indent);
104  context.logger->writeString(prefix + "-" + printedName + "\n");
105  context.indent++;
106 }
107 
109  if (!context.logger) {
110  // quick exit in case no logger is used
111  return;
112  }
113 
114  --context.indent;
115  const std::string prefix = std::string(4 * context.indent, ' ');
116  context.logger->writeString(
117  prefix + " took " + std::to_string(int(timer.elapsed(TimerUnit::MILLISECOND))) + "ms\n");
118  SPH_ASSERT(context.indent >= 0);
119 }
120 
122  SPH_ASSERT(context.indent == 0);
123  context.logger = std::move(logger);
124  context.indent = 0;
125 }
126 
127 
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
void setVerboseLogger(AutoPtr< ILogger > &&logger)
Creates a global verbose logger.
Definition: Logger.cpp:121
Logging routines of the run.
#define NAMESPACE_SPH_END
Definition: Object.h:12
std::string replaceFirst(const std::string &source, const std::string &old, const std::string &s)
Replaces first occurence of string with a new string.
std::string replaceAll(const std::string &source, const std::string &old, const std::string &s)
Replaces all occurences of string with a new string.
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.
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
Definition: Flags.h:77
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
Exception thrown when file cannot be read, it has invalid format, etc.
Definition: Exceptions.h:31
Object representing a path on a filesystem.
Definition: Path.h:17
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
Path parentPath() const
Returns the parent directory. If the path is empty or root, return empty path.
Definition: Path.cpp:35
virtual void writeString(const std::string &s) override
Logs a string message.
Definition: Logger.cpp:20
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
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
Definition: Timer.cpp:55
VerboseLogGuard(const std::string &functionName)
Creates a guard, should be at the very beginning of a function/scope.
Definition: Logger.cpp:84
Outcome createDirectory(const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
Creates a directory with given path. Creates all parent directories as well.
Definition: FileSystem.cpp:119
Definition: Logger.h:70
ScopedConsole(const Console console)
Definition: Logger.cpp:12
~ScopedConsole()
Definition: Logger.cpp:16
AutoPtr< ILogger > logger
Definition: Logger.cpp:78