QNANO
Debug_Logger.h
1 #ifndef QNANO_NEW_DEBUG_LOGGER_DEFINED_H
2 #define QNANO_NEW_DEBUG_LOGGER_DEFINED_H
3 #include "tools/Reader.h"
4 #include "tools/Has_Print.h"
5 
6 
8 public:
9  bool use_debug;
10  std::string debug_fname;
11 
12 
13  void setup(const std::string &fname){
14  debug_fname=fname;
15  use_debug=(fname!="");
16  }
17 
18  template<typename T>
19  Debug_Logger& operator<<(const T &x){
20  if(use_debug){
21  std::ofstream ofs(debug_fname.c_str(), std::ios::app);
22  ofs<<x;
23  }
24  return *this;
25  }
26  Debug_Logger& operator<<(std::ostream&(*x)(std::ostream&)){
27  if(use_debug){
28  std::ofstream ofs(debug_fname.c_str(), std::ios::app);
29  ofs<<x;
30  }
31  return *this;
32  }
33  void print(const Has_Print &p) const{
34  if(use_debug){
35  std::ofstream ofs(debug_fname.c_str(), std::ios::app);
36  p.print(ofs);
37  }
38  }
39 
40  void copy(const Debug_Logger& other){
41  use_debug=other.use_debug;
42  debug_fname=other.debug_fname;
43  }
44  Debug_Logger &operator=(const Debug_Logger& other){
45  copy(other);
46  return *this;
47  }
48  Debug_Logger(const Debug_Logger& other){
49  copy(other);
50  }
51  Debug_Logger(const std::string &fname=""){
52  setup(fname);
53  }
54 };
55 
56 /*
57 template<typename T>
58 Debug_Logger& operator<<(Debug_Logger& log, T &x){
59  std::ofstream ofs(log.debug_fname.c_str(), std::ios::app);
60  ofs<<x;
61  return log;
62 }
63 */
64 
65 #endif
Interface for classes that have a "print" function.
Definition: Has_Print.h:7
Definition: Debug_Logger.h:7