NuTo
Numerics Tool
Logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 
7 namespace NuTo
8 {
12 class Logger
13 {
14 
15 public:
17  Logger(std::string prefix = "", bool isQuiet = true);
18 
21  void OpenFile(std::string filename);
22 
24  void CloseFile();
25 
28  void SetQuiet(bool isQuiet);
29 
31  template <typename T>
32  void Out(const T& rObject, bool withNewline = false)
33  {
34  std::string optionalPrefix = OptionalPrefix();
35  if (!mIsQuiet)
36  {
37  std::cout << optionalPrefix << rObject;
38  std::cout.flush();
39  }
40  if (mLogFile.is_open())
41  {
42  mLogFile << optionalPrefix << rObject;
43  mLogFile.flush();
44  }
45  mPrintPrefix = withNewline;
46  }
47 
48  std::string OptionalPrefix();
49 
51  void Info() const
52  {
53  std::cout << "LogFileName " << mLogFileName << " is quiet " << mIsQuiet << std::endl;
54  }
55 
56 
57 protected:
58  std::ofstream mLogFile;
59  std::string mLogFileName;
60  bool mIsQuiet = false;
61 
62  std::string mPrefix;
63  bool mPrintPrefix = true;
64 };
65 
67 template <typename T>
68 inline Logger& operator<<(Logger& rLogger, const T& t)
69 {
70  rLogger.Out(t);
71  return rLogger;
72 }
73 
74 Logger& operator<<(Logger& rLogger, const char& t);
75 Logger& operator<<(Logger& rLogger, const std::string& t);
76 Logger& operator<<(Logger& rLogger, const char* t);
77 
79 struct Log
80 {
81  static Logger Debug; // prefix "Debug| "
82  static Logger Info; // prefix "Info| "
83  static Logger Error; // prefix "Error| "
84 };
85 } // namespace NuTo
std::ofstream mLogFile
Logfile for output.
Definition: Logger.h:58
Logger(std::string prefix="", bool isQuiet=true)
...constructor
Definition: Logger.cpp:4
NuTo::Logger & operator<<(NuTo::Logger &rLogger, const char &t)
Definition: Logger.cpp:48
bool mPrintPrefix
Definition: Logger.h:63
static Logger Info
Definition: Logger.h:82
void OpenFile(std::string filename)
..open file
Definition: Logger.cpp:12
set of predefined global loggers
Definition: Logger.h:79
void CloseFile()
..Close file
Definition: Logger.cpp:22
void Info() const
Info routine that prints general information about the object (detail according to verbose level) ...
Definition: Logger.h:51
void Out(const T &rObject, bool withNewline=false)
..Writes a message to the log and to console
Definition: Logger.h:32
void SetQuiet(bool isQuiet)
..sets the output to be forwarded to the console (false) or only to the file (true) ...
Definition: Logger.cpp:27
static Logger Debug
Definition: Logger.h:81
logger class for redirecting output to different locations/files
Definition: Logger.h:12
Definition: Exception.h:6
std::string mPrefix
prefix written on the beginning of the log line
Definition: Logger.h:62
static Logger Error
Definition: Logger.h:83
bool mIsQuiet
If true, no writing to console.
Definition: Logger.h:60
std::string mLogFileName
LogfileName for output.
Definition: Logger.h:59
std::string OptionalPrefix()
Definition: Logger.cpp:32