Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
debuglog.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/>.
15 
23 #pragma once
24 
25 #include <iostream>
26 #include <fstream>
27 
28 namespace sgl {
29 
30  class DebugLog {
31 
32  public:
33 
34  DebugLog() = default;
35  DebugLog(std::string_view name): fout(&name[0]) {}
36 
37  void activate(std::string_view name) {
38  close();
39  fout.open(&name[0]);
40  }
41 
42  void out_impl()
43  {}
44 
45  template<class First>
46  void out_impl(First const &first)
47  {
48  if (fout) {
49  fout << first;
50  }
51  }
52 
53  template<class First, class Second, class ...Args>
54  void out_impl(First const &first, Second const &second, Args &&...args)
55  {
56  if (fout) {
57  fout << first << ' ';
58  out_impl(second, std::forward<Args>(args)...);
59  }
60  }
61 
62  template<class ...Args>
63  void out(Args &&...args)
64  {
65  if (fout) {
66  out_impl(std::forward<Args>(args)...);
67  fout << std::endl;
68  }
69  }
70 
71  void close() { if (fout) fout.close(); }
72 
73  ~DebugLog() { close(); }
74 
75  private:
76 
77  std::ofstream fout;
78  };
79 
80 
81 #ifdef DEBUGLOG
82  inline DebugLog log("log.txt");
83  #define LOG(...) sgl::log.out(__VA_ARGS__);
84  #define SCOPELOGNAME(name) ScopeLog _scope_log_name_;
85  #define SCOPELOG ScopeLog _scope_log_;
86 #else
87  inline DebugLog log;
88  #define LOG(...)
89  #define SCOPELOGNAME(name)
90  #define SCOPELOG
91 #endif
92 
93  struct ScopeLog {
94  static inline size_t r_general = 0;
95  size_t r;
96  ScopeLog(): r(r_general++) { LOG() LOG("******************************** R", r) LOG() }
97  ~ScopeLog() { /*--r_general;*/ LOG() LOG("******************************** R", r) LOG() }
98  };
99 }
Definition: debuglog.h:93
Definition: debuglog.h:30
Definition: abstractgammasym.h:32