Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
mrtlog.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
23 #pragma once
24 #include <fstream>
25 
26 namespace mty::log {
27 
28  class LogStream {
29 
30  public:
31 
32  LogStream(std::string const& fileName)
33  :out(fileName)
34  {
35 
36  }
37 
38  void setLogFile(std::string const& fileName)
39  {
40  if (out)
41  out.close();
42  out.open(fileName);
43  }
44 
45  virtual ~LogStream() { if (out) out.close(); };
46 
47  template<class Printable>
48  LogStream& operator<<(Printable var) {
49  if (out)
50  out << var;
51  return *this;
52  }
53 
54  LogStream& operator<<(std::ostream&(*f)(std::ostream&)) {
55  f(out);
56  return *this;
57  }
58 
59  private:
60 
61  std::ofstream out;
62  };
63 
64  inline std::ofstream out;
65 
66  inline void print()
67  {
68 
69  }
70 
71  template<class T, class ...Args>
72  void print(T first, Args&&... next)
73  {
74  if (out) {
75  out << first << " ";
76  print(std::forward<Args>(next)...);
77  out << '\n';
78  }
79  }
80 }
Definition: mrtlog.h:26
Definition: mrtlog.h:28