LOGO

RestFrames  v1.0.0
RestFrames HEP Event Analysis Software Library
RFLog.hh
Go to the documentation of this file.
1 // RestFrames: particle physics event analysis library
3 // --------------------------------------------------------------------
4 // Copyright (c) 2014-2016, Christopher Rogan
14 // This file is part of RestFrames.
15 //
16 // RestFrames is free software; you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation; either version 2 of the License, or
19 // (at your option) any later version.
20 //
21 // RestFrames is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with RestFrames. If not, see <http://www.gnu.org/licenses/>.
29 
30 #ifndef RFLog_HH
31 #define RFLog_HH
32 #include <string>
33 #include <sstream>
34 #include <iostream>
35 #include <map>
36 #include <exception>
37 
38 #include "RestFrames/RFList.hh"
39 
40 namespace RestFrames {
41 
42  class RFBase;
43 
45  enum LogType { LogError, LogWarning, LogInfo,
46  LogDebug, LogVerbose };
47 
49  // RFLog class
51  class RFLog {
52  public:
53  RFLog(const std::string& source, LogType def_type = LogInfo);
54  RFLog();
55  ~RFLog();
56 
57  void SetSource(const std::string& source);
58 
59  friend void SetLogPrint(bool print);
60  friend void SetLogPrint(LogType type, bool print);
61  friend void SetLogStream(std::ostream* ostr);
62  friend void SetLogColor(bool color);
63  friend void SetLogMaxWidth(int NMAX);
64 
65  static RFLog& EndMessage(RFLog& log);
66 
67  RFLog& operator<< (LogType type);
68  RFLog& operator<< (RFLog& (*_f)( RFLog&));
69  RFLog& operator<< (std::ostream& (*_f)(std::ostream&));
70  RFLog& operator<< (std::ios& (*_f)(std::ios&));
71 
72  template <class T>
73  RFLog& operator<< (T arg){
74  m_Message << arg;
75  return *this;
76  }
77 
78  private:
79  static std::ostream* m_Ostr;
80  static bool m_Color;
81  static int m_NMAX;
82  static std::map<LogType, bool> m_PrintMap;
83  std::map<LogType, std::string> m_TypeMap;
84  std::map<LogType, std::string> m_ColorMap;
85 
86  void Send();
87  void Init();
88  std::string GetFormattedSource() const;
89  std::string GetFormattedMessage(const std::string& message);
90 
91  void PrintObject(const RFBase* objPtr);
92  template <class T>
93  void PrintList(const RFList<T>* listPtr);
94 
95  LogType m_DefType;
96  LogType m_CurType;
97  std::string m_Source;
98  std::ostringstream m_Message;
99 
100  };
101 
102  template <> RFLog& RFLog::operator<< (const RFBase* arg);
103  template <> RFLog& RFLog::operator<< (const RFBaseList* arg);
104 
105  inline RFLog& RFLog::operator<< (RFLog& (*_f)(RFLog&)){
106  return (_f)(*this);
107  }
108 
109  inline RFLog& RFLog::operator<< (std::ostream& (*_f)(std::ostream&)){
110  (_f)(m_Message);
111  return *this;
112  }
113 
114  inline RFLog& RFLog::operator<< (std::ios& (*_f)(std::ios&)){
115  (_f)(m_Message);
116  return *this;
117  }
118 
119  inline RFLog& RFLog::operator<< (LogType type){
120  m_CurType = type;
121  return *this;
122  }
123 
124  extern RFLog g_Log;
125 
126  const RFBase* Log(const RFBase& obj);
127  const RFBase* Log(const RFBase* ptr);
128  template <class T>
129  const RFList<RFBase>* Log(const RFList<T>& list){ return (const RFBaseList*)&list; }
130  template <class T>
131  const RFList<RFBase>* Log(const RFList<T>* ptr){ return (const RFBaseList*)ptr; }
132 
133 #ifndef __MAKECINT__
134  #define LogEnd RFLog::EndMessage
135 #endif
136 
137  class RestFramesException : public std::exception {
138 
139  public:
140  RestFramesException(const std::string& message) : m_Message(message) {}
141 
142  virtual ~RestFramesException() throw() {}
143 
144  virtual const char* what() const throw(){
145  return m_Message.c_str();
146  }
147 
148  private:
149  std::string m_Message;
150 
151  };
152 
153  void SetLogPrint(bool print = true);
154  void SetLogPrint(LogType type, bool print = true);
155  void SetLogStream(std::ostream* ostr);
156  void SetLogColor(bool color = true);
157  void SetLogMaxWidth(int NMAX);
158  std::map<RestFrames::LogType,bool> InitPrintMap();
159 
160 }
161 
162 #endif
Base class for all RestFrame package objects.
Definition: RFBase.hh:53