Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:19:56

0001 /*******************************************************************************
0002  * Copyright (c) The JETSCAPE Collaboration, 2018
0003  *
0004  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
0005  * 
0006  * For the list of contributors see AUTHORS.
0007  *
0008  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
0009  *
0010  * or via email to bugs.jetscape@gmail.com
0011  *
0012  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
0013  * See COPYING for details.
0014  ******************************************************************************/
0015 
0016 #ifndef JETSCAPELOGGER_H
0017 #define JETSCAPELOGGER_H
0018 
0019 #include <iostream>
0020 #include <sstream>
0021 #include <cassert>
0022 #include <iostream>
0023 #include <mutex>
0024 #include <memory>
0025 
0026 #include "JetClass.h"
0027 
0028 using std::shared_ptr;
0029 using std::make_shared;
0030 
0031 // --------------------------------
0032 
0033 #define RESET "\033[0m"
0034 #define BOLDBLACK "\033[1m\033[30m"   /* Bold Black */
0035 #define BOLDRED "\033[1m\033[31m"     /* Bold Red */
0036 #define BOLDGREEN "\033[1m\033[32m"   /* Bold Green */
0037 #define BOLDYELLOW "\033[1m\033[33m"  /* Bold Yellow */
0038 #define BOLDBLUE "\033[1m\033[34m"    /* Bold Blue */
0039 #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
0040 #define BOLDCYAN "\033[1m\033[36m"    /* Bold Cyan */
0041 #define BOLDWHITE "\033[1m\033[37m"   /* Bold White */
0042 
0043 // define nicer macros to be used for logging ...
0044 /*
0045 #define JSINFO  JetScapeLogger::Instance()->Info()<<" " //<<__PRETTY_FUNCTION__<<" : "
0046 #define INFO_NICE  JetScapeLogger::Instance()->InfoNice()
0047 #define JSDEBUG JetScapeLogger::Instance()->Debug()<<__PRETTY_FUNCTION__<<" : "
0048 #define DEBUGTHREAD JetScapeLogger::Instance()->DebugThread()<<__PRETTY_FUNCTION__<<" : "
0049 #define REMARK JetScapeLogger::Instance()->Remark()<<__PRETTY_FUNCTION__<<" : "
0050 #define VERBOSE(l) JetScapeLogger::Instance()->Verbose(l)<<__PRETTY_FUNCTION__<<" : "
0051 #define VERBOSESHOWER(l) JetScapeLogger::Instance()->VerboseShower(l)<<__PRETTY_FUNCTION__<<" : "
0052 #define VERBOSEPARTON(l,p) JetScapeLogger::Instance()->VerboseParton(l,p)<<__PRETTY_FUNCTION__<<" : "
0053 #define VERBOSEPVERTEX(l,v) JetScapeLogger::Instance()->VerboseVertex(l,v)<<__PRETTY_FUNCTION__<<" : "
0054 #define JSWARN JetScapeLogger::Instance()->Warn()<<__PRETTY_FUNCTION__<<" : "
0055 */
0056 
0057 // define nicer macros to be used for logging and check if they should print stuff ...
0058 // otherwise quite a performance hit ...
0059 #define JSINFO                                                                 \
0060   JetScapeLogger::Instance()->Info() << " " //<<__PRETTY_FUNCTION__<<" : "
0061 #define INFO_NICE JetScapeLogger::Instance()->InfoNice()
0062 #define JSDEBUG                                                                \
0063   if (JetScapeLogger::Instance()->GetDebug())                                  \
0064   JetScapeLogger::Instance()->Debug() << __PRETTY_FUNCTION__ << " : "
0065 #define DEBUGTHREAD                                                            \
0066   if (JetScapeLogger::Instance()->GetDebug())                                  \
0067   JetScapeLogger::Instance()->DebugThread() << __PRETTY_FUNCTION__ << " : "
0068 #define REMARK                                                                 \
0069   if (JetScapeLogger::Instance()->GetRemark())                                 \
0070   JetScapeLogger::Instance()->Remark() << __PRETTY_FUNCTION__ << " : "
0071 #define VERBOSE(l)                                                             \
0072   if (l < JetScapeLogger::Instance()->GetVerboseLevel())                       \
0073   JetScapeLogger::Instance()->Verbose(l) << __PRETTY_FUNCTION__ << " : "
0074 #define VERBOSESHOWER(l)                                                       \
0075   if (l < JetScapeLogger::Instance()->GetVerboseLevel())                       \
0076   JetScapeLogger::Instance()->VerboseShower(l) << __PRETTY_FUNCTION__ << " : "
0077 #define VERBOSEPARTON(l, p)                                                    \
0078   if (l < JetScapeLogger::Instance()->GetVerboseLevel())                       \
0079   JetScapeLogger::Instance()->VerboseParton(l, p)                              \
0080       << __PRETTY_FUNCTION__ << " : "
0081 #define VERBOSEPVERTEX(l, v)                                                   \
0082   if (l < JetScapeLogger::Instance()->GetVerboseLevel())                       \
0083   JetScapeLogger::Instance()->VerboseVertex(l, v)                              \
0084       << __PRETTY_FUNCTION__ << " : "
0085 #define JSWARN                                                                 \
0086   JetScapeLogger::Instance()->Warn() << __PRETTY_FUNCTION__ << " : "
0087 
0088 namespace Jetscape {
0089 
0090 // Forward declarations. Macro implementation is rather cumbersome and hiccups over include guards
0091 class Vertex;
0092 class Parton;
0093 
0094 // -------------------------------------------
0095 struct SafeOstream {
0096   struct GuardedImpl {
0097     GuardedImpl() = delete;
0098     GuardedImpl(const GuardedImpl &) = delete;
0099     void operator=(const GuardedImpl &) = delete;
0100     GuardedImpl(std::ostream &ostream, std::mutex &mutex)
0101         : Ostream(ostream), Guard(mutex) {}
0102     ~GuardedImpl() { Ostream.flush(); }
0103     template <typename T> void write(const T &x) { Ostream << x; }
0104     std::ostream &Ostream;
0105     std::lock_guard<std::mutex> Guard;
0106   };
0107   struct impl {
0108     impl() = delete;
0109     void operator=(const impl &) = delete;
0110     impl(std::ostream &ostream, std::mutex &mutex)
0111         : UniqueImpl(new GuardedImpl(ostream, mutex)) {}
0112     impl(const impl &rhs) {
0113       assert(rhs.UniqueImpl.get());
0114       UniqueImpl.swap(rhs.UniqueImpl);
0115     }
0116     template <typename T> impl &operator<<(const T &x) {
0117       GuardedImpl *p = UniqueImpl.get();
0118       assert(p);
0119       p->write(x);
0120       return *this;
0121     }
0122     mutable std::unique_ptr<GuardedImpl> UniqueImpl;
0123   };
0124   explicit SafeOstream(std::ostream &ostream) : Ostream(ostream) {}
0125   template <typename T> impl operator<<(const T &x) {
0126     return impl(Ostream, mutex_) << x;
0127   }
0128   std::ostream &Ostream;
0129   std::mutex mutex_;
0130 };
0131 
0132 // --------------------------------
0133 // Just a helper class to make the interface
0134 // consistent with << operator
0135 // In principle simple extension to
0136 // log into a file ... via m_dest
0137 // Think about thread safety ...
0138 // << overload in Parton class not working!? Check ...
0139 
0140 class LogStreamer {
0141 
0142   shared_ptr<std::ostringstream> m_collector;
0143   std::ostream *m_dest;
0144 
0145 public:
0146   LogStreamer(std::ostream &dest) {
0147     m_collector = make_shared<std::ostringstream>();
0148     m_dest = &dest;
0149   };
0150 
0151   ~LogStreamer() {
0152     if (m_collector.unique() && m_dest != nullptr) {
0153       //*m_dest << m_collector->str() << RESET << std::endl;
0154       *m_dest << m_collector->str() << RESET << std::endl;
0155     }
0156   }
0157 
0158   template <typename T> LogStreamer &operator<<(T const &value) {
0159     *m_collector << value;
0160     return *this;
0161   }
0162 };
0163 
0164 class LogStreamerThread {
0165 
0166   shared_ptr<std::ostringstream> m_collector;
0167   SafeOstream *m_dest;
0168 
0169 public:
0170   LogStreamerThread(SafeOstream &dest) {
0171     m_collector = make_shared<std::ostringstream>();
0172     m_dest = &dest;
0173   };
0174 
0175   ~LogStreamerThread() {
0176     if (m_collector.unique() && m_dest != nullptr) {
0177       *m_dest << m_collector->str() << RESET << "\n";
0178     }
0179   }
0180 
0181   template <typename T> LogStreamerThread &operator<<(T const &value) {
0182     *m_collector << value;
0183     return *this;
0184   }
0185 };
0186 
0187 // --------------------------------
0188 
0189 class JetScapeLogger {
0190 
0191 public:
0192   static JetScapeLogger *Instance();
0193 
0194   LogStreamer Info();
0195   LogStreamer InfoNice();
0196   LogStreamer Warn();
0197   LogStreamer Debug();
0198   LogStreamerThread DebugThread();
0199   LogStreamer Remark();
0200   //LogStreamer Error(); //to be implemented
0201   //LogStreamer Fatal(); //to be implemented
0202 
0203   LogStreamer Verbose(unsigned short m_vlevel);
0204   LogStreamer VerboseShower(unsigned short m_vlevel);
0205   //Not happy with that fix, still normal << in VERBOSE not working ... follow up.
0206   LogStreamer VerboseParton(unsigned short m_vlevel, Parton &p);
0207   LogStreamer VerboseVertex(unsigned short m_vlevel, Vertex &v);
0208 
0209   void SetDebug(bool m_debug) { debug = m_debug; }
0210   void SetRemark(bool m_remark) { remark = m_remark; }
0211   void SetInfo(bool m_info) { info = m_info; }
0212   void SetVerboseLevel(unsigned short m_vlevel) { vlevel = m_vlevel; }
0213   bool GetDebug() { return debug; }
0214   bool GetRemark() { return remark; }
0215   bool GetInfo() { return info; }
0216   unsigned short GetVerboseLevel() { return vlevel; }
0217 
0218 private:
0219   JetScapeLogger() {
0220     info = true;
0221     debug = false;
0222     remark = false;
0223     vlevel = 0;
0224   };
0225   JetScapeLogger(JetScapeLogger const &){};
0226   static JetScapeLogger *m_pInstance;
0227 
0228   bool debug;
0229   bool remark;
0230   bool info;
0231   unsigned short vlevel;
0232 };
0233 
0234 } // end namespace Jetscape
0235 
0236 #endif