Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetScapeLogger.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetScapeLogger.h
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #ifndef JETSCAPELOGGER_H
17 #define JETSCAPELOGGER_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include <cassert>
22 #include <iostream>
23 #include <mutex>
24 #include <memory>
25 
26 #include "JetClass.h"
27 
28 using std::shared_ptr;
29 using std::make_shared;
30 
31 // --------------------------------
32 
33 #define RESET "\033[0m"
34 #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
35 #define BOLDRED "\033[1m\033[31m" /* Bold Red */
36 #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
37 #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
38 #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
39 #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
40 #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
41 #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
42 
43 // define nicer macros to be used for logging ...
44 /*
45 #define JSINFO JetScapeLogger::Instance()->Info()<<" " //<<__PRETTY_FUNCTION__<<" : "
46 #define INFO_NICE JetScapeLogger::Instance()->InfoNice()
47 #define JSDEBUG JetScapeLogger::Instance()->Debug()<<__PRETTY_FUNCTION__<<" : "
48 #define DEBUGTHREAD JetScapeLogger::Instance()->DebugThread()<<__PRETTY_FUNCTION__<<" : "
49 #define REMARK JetScapeLogger::Instance()->Remark()<<__PRETTY_FUNCTION__<<" : "
50 #define VERBOSE(l) JetScapeLogger::Instance()->Verbose(l)<<__PRETTY_FUNCTION__<<" : "
51 #define VERBOSESHOWER(l) JetScapeLogger::Instance()->VerboseShower(l)<<__PRETTY_FUNCTION__<<" : "
52 #define VERBOSEPARTON(l,p) JetScapeLogger::Instance()->VerboseParton(l,p)<<__PRETTY_FUNCTION__<<" : "
53 #define VERBOSEPVERTEX(l,v) JetScapeLogger::Instance()->VerboseVertex(l,v)<<__PRETTY_FUNCTION__<<" : "
54 #define JSWARN JetScapeLogger::Instance()->Warn()<<__PRETTY_FUNCTION__<<" : "
55 */
56 
57 // define nicer macros to be used for logging and check if they should print stuff ...
58 // otherwise quite a performance hit ...
59 #define JSINFO \
60  JetScapeLogger::Instance()->Info() << " " //<<__PRETTY_FUNCTION__<<" : "
61 #define INFO_NICE JetScapeLogger::Instance()->InfoNice()
62 #define JSDEBUG \
63  if (JetScapeLogger::Instance()->GetDebug()) \
64  JetScapeLogger::Instance()->Debug() << __PRETTY_FUNCTION__ << " : "
65 #define DEBUGTHREAD \
66  if (JetScapeLogger::Instance()->GetDebug()) \
67  JetScapeLogger::Instance()->DebugThread() << __PRETTY_FUNCTION__ << " : "
68 #define REMARK \
69  if (JetScapeLogger::Instance()->GetRemark()) \
70  JetScapeLogger::Instance()->Remark() << __PRETTY_FUNCTION__ << " : "
71 #define VERBOSE(l) \
72  if (l < JetScapeLogger::Instance()->GetVerboseLevel()) \
73  JetScapeLogger::Instance()->Verbose(l) << __PRETTY_FUNCTION__ << " : "
74 #define VERBOSESHOWER(l) \
75  if (l < JetScapeLogger::Instance()->GetVerboseLevel()) \
76  JetScapeLogger::Instance()->VerboseShower(l) << __PRETTY_FUNCTION__ << " : "
77 #define VERBOSEPARTON(l, p) \
78  if (l < JetScapeLogger::Instance()->GetVerboseLevel()) \
79  JetScapeLogger::Instance()->VerboseParton(l, p) \
80  << __PRETTY_FUNCTION__ << " : "
81 #define VERBOSEPVERTEX(l, v) \
82  if (l < JetScapeLogger::Instance()->GetVerboseLevel()) \
83  JetScapeLogger::Instance()->VerboseVertex(l, v) \
84  << __PRETTY_FUNCTION__ << " : "
85 #define JSWARN \
86  JetScapeLogger::Instance()->Warn() << __PRETTY_FUNCTION__ << " : "
87 
88 namespace Jetscape {
89 
90 // Forward declarations. Macro implementation is rather cumbersome and hiccups over include guards
91 class Vertex;
92 class Parton;
93 
94 // -------------------------------------------
95 struct SafeOstream {
96  struct GuardedImpl {
97  GuardedImpl() = delete;
98  GuardedImpl(const GuardedImpl &) = delete;
99  void operator=(const GuardedImpl &) = delete;
100  GuardedImpl(std::ostream &ostream, std::mutex &mutex)
101  : Ostream(ostream), Guard(mutex) {}
102  ~GuardedImpl() { Ostream.flush(); }
103  template <typename T> void write(const T &x) { Ostream << x; }
104  std::ostream &Ostream;
105  std::lock_guard<std::mutex> Guard;
106  };
107  struct impl {
108  impl() = delete;
109  void operator=(const impl &) = delete;
110  impl(std::ostream &ostream, std::mutex &mutex)
111  : UniqueImpl(new GuardedImpl(ostream, mutex)) {}
112  impl(const impl &rhs) {
113  assert(rhs.UniqueImpl.get());
114  UniqueImpl.swap(rhs.UniqueImpl);
115  }
116  template <typename T> impl &operator<<(const T &x) {
117  GuardedImpl *p = UniqueImpl.get();
118  assert(p);
119  p->write(x);
120  return *this;
121  }
122  mutable std::unique_ptr<GuardedImpl> UniqueImpl;
123  };
124  explicit SafeOstream(std::ostream &ostream) : Ostream(ostream) {}
125  template <typename T> impl operator<<(const T &x) {
126  return impl(Ostream, mutex_) << x;
127  }
128  std::ostream &Ostream;
129  std::mutex mutex_;
130 };
131 
132 // --------------------------------
133 // Just a helper class to make the interface
134 // consistent with << operator
135 // In principle simple extension to
136 // log into a file ... via m_dest
137 // Think about thread safety ...
138 // << overload in Parton class not working!? Check ...
139 
140 class LogStreamer {
141 
142  shared_ptr<std::ostringstream> m_collector;
143  std::ostream *m_dest;
144 
145 public:
146  LogStreamer(std::ostream &dest) {
147  m_collector = make_shared<std::ostringstream>();
148  m_dest = &dest;
149  };
150 
152  if (m_collector.unique() && m_dest != nullptr) {
153  //*m_dest << m_collector->str() << RESET << std::endl;
154  *m_dest << m_collector->str() << RESET << std::endl;
155  }
156  }
157 
158  template <typename T> LogStreamer &operator<<(T const &value) {
159  *m_collector << value;
160  return *this;
161  }
162 };
163 
165 
166  shared_ptr<std::ostringstream> m_collector;
168 
169 public:
171  m_collector = make_shared<std::ostringstream>();
172  m_dest = &dest;
173  };
174 
176  if (m_collector.unique() && m_dest != nullptr) {
177  *m_dest << m_collector->str() << RESET << "\n";
178  }
179  }
180 
181  template <typename T> LogStreamerThread &operator<<(T const &value) {
182  *m_collector << value;
183  return *this;
184  }
185 };
186 
187 // --------------------------------
188 
190 
191 public:
192  static JetScapeLogger *Instance();
193 
194  LogStreamer Info();
196  LogStreamer Warn();
197  LogStreamer Debug();
200  //LogStreamer Error(); //to be implemented
201  //LogStreamer Fatal(); //to be implemented
202 
203  LogStreamer Verbose(unsigned short m_vlevel);
204  LogStreamer VerboseShower(unsigned short m_vlevel);
205  //Not happy with that fix, still normal << in VERBOSE not working ... follow up.
206  LogStreamer VerboseParton(unsigned short m_vlevel, Parton &p);
207  LogStreamer VerboseVertex(unsigned short m_vlevel, Vertex &v);
208 
209  void SetDebug(bool m_debug) { debug = m_debug; }
210  void SetRemark(bool m_remark) { remark = m_remark; }
211  void SetInfo(bool m_info) { info = m_info; }
212  void SetVerboseLevel(unsigned short m_vlevel) { vlevel = m_vlevel; }
213  bool GetDebug() { return debug; }
214  bool GetRemark() { return remark; }
215  bool GetInfo() { return info; }
216  unsigned short GetVerboseLevel() { return vlevel; }
217 
218 private:
220  info = true;
221  debug = false;
222  remark = false;
223  vlevel = 0;
224  };
226  static JetScapeLogger *m_pInstance;
227 
228  bool debug;
229  bool remark;
230  bool info;
231  unsigned short vlevel;
232 };
233 
234 } // end namespace Jetscape
235 
236 #endif