LOGO

RestFrames  v1.0.1
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 
58  RFLog(const std::string& source, LogType def_type = LogInfo);
59 
61  RFLog();
62  ~RFLog();
63 
67  void SetSource(const std::string& source);
68 
72  friend void SetLogPrint(bool print);
73 
78  friend void SetLogPrint(LogType type, bool print);
79 
83  friend void SetLogStream(std::ostream* ostr);
84 
88  friend void SetLogColor(bool color);
89 
95  friend void SetLogMaxWidth(int NMAX);
96 
100  static RFLog& EndMessage(RFLog& log);
101 
105  RFLog& operator<< (LogType type);
106 
110  RFLog& operator<< (RFLog& (*_f)( RFLog&));
111 
115  RFLog& operator<< (std::ostream& (*_f)(std::ostream&));
116 
120  RFLog& operator<< (std::ios& (*_f)(std::ios&));
121 
125  template <class T>
127  m_Message << arg;
128  return *this;
129  }
130 
131  private:
132  static std::ostream* m_Ostr;
133  static bool m_Color;
134  static int m_NMAX;
135  static std::map<LogType, bool> m_PrintMap;
136  std::map<LogType, std::string> m_TypeMap;
137  std::map<LogType, std::string> m_ColorMap;
138 
139  void Send();
140  void Init();
141  std::string GetFormattedSource() const;
142  std::string GetFormattedMessage(const std::string& message);
143 
144  void PrintObject(const RFBase* objPtr);
145  template <class T>
146  void PrintList(const RFList<T>* listPtr);
147 
148  LogType m_DefType;
149  LogType m_CurType;
150  std::string m_Source;
151  std::ostringstream m_Message;
152 
153  };
154 
155  template <> RFLog& RFLog::operator<< (const RFBase* arg);
156  template <> RFLog& RFLog::operator<< (const RFBaseList* arg);
157 
158  inline RFLog& RFLog::operator<< (RFLog& (*_f)(RFLog&)){
159  return (_f)(*this);
160  }
161 
162  inline RFLog& RFLog::operator<< (std::ostream& (*_f)(std::ostream&)){
163  (_f)(m_Message);
164  return *this;
165  }
166 
167  inline RFLog& RFLog::operator<< (std::ios& (*_f)(std::ios&)){
168  (_f)(m_Message);
169  return *this;
170  }
171 
173  m_CurType = type;
174  return *this;
175  }
176 
177  extern RFLog g_Log;
178 
179  const RFBase* Log(const RFBase& obj);
180  const RFBase* Log(const RFBase* ptr);
181  template <class T>
182  const RFList<RFBase>* Log(const RFList<T>& list){ return (const RFBaseList*)&list; }
183  template <class T>
184  const RFList<RFBase>* Log(const RFList<T>* ptr){ return (const RFBaseList*)ptr; }
185 
186 #ifndef __MAKECINT__
187  #define LogEnd RFLog::EndMessage
188 #endif
189 
190  class RestFramesException : public std::exception {
191 
192  public:
193  RestFramesException(const std::string& message) : m_Message(message) {}
194 
195  virtual ~RestFramesException() throw() {}
196 
197  virtual const char* what() const throw(){
198  return m_Message.c_str();
199  }
200 
201  private:
202  std::string m_Message;
203 
204  };
205 
206  void SetLogPrint(bool print = true);
207  void SetLogPrint(LogType type, bool print = true);
208  void SetLogStream(std::ostream* ostr);
209  void SetLogColor(bool color = true);
210  void SetLogMaxWidth(int NMAX);
211  std::map<RestFrames::LogType,bool> InitPrintMap();
212 
213 }
214 
215 #endif
RestFrames::RFLog::SetLogPrint
friend void SetLogPrint(bool print)
Sets logging to print (print = true) or to not print (print = false)
Definition: RFLog.cc:190
RestFrames::SetLogStream
void SetLogStream(std::ostream *ostr)
Definition: RFLog.cc:196
RestFrames::RFList
Definition: RFList.hh:43
RestFrames::RFBase
Base class for all RestFrame package objects.
Definition: RFBase.hh:53
RestFrames::RFLog::SetLogColor
friend void SetLogColor(bool color)
Sets color of message.
Definition: RFLog.cc:200
RestFrames::SetLogMaxWidth
void SetLogMaxWidth(int NMAX)
Definition: RFLog.cc:204
RestFrames::LogType
LogType
Type of Log Message.
Definition: RFLog.hh:45
RestFrames::RFLog::SetSource
void SetSource(const std::string &source)
Sets name of instance that is associated with.
Definition: RFLog.cc:182
RestFrames::RFLog
Definition: RFLog.hh:51
RestFrames::RFLog::SetLogStream
friend void SetLogStream(std::ostream *ostr)
Sets output stream.
Definition: RFLog.cc:196
RestFrames::SetLogColor
void SetLogColor(bool color=true)
Definition: RFLog.cc:200
RestFrames::SetLogPrint
void SetLogPrint(bool print=true)
Definition: RFLog.cc:190
RestFrames::RFLog::RFLog
RFLog()
Empty constructor.
Definition: RFLog.cc:52
RFList.hh
RestFrames::RFLog::SetLogMaxWidth
friend void SetLogMaxWidth(int NMAX)
Sets maximum width of message.
Definition: RFLog.cc:204
RestFrames::RFLog::operator<<
RFLog & operator<<(LogType type)
Overloading operator.
Definition: RFLog.hh:172
RestFrames::RestFramesException
Definition: RFLog.hh:190
RestFrames::RFLog::EndMessage
static RFLog & EndMessage(RFLog &log)
Sets end message for log
Definition: RFLog.cc:166