ATLAS Offline Software
MLogging.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ISF_FASTCALOSIMEVENT_MLogging_h
6 #define ISF_FASTCALOSIMEVENT_MLogging_h
7 
8 #include <TNamed.h> //for ClassDef
10 
11 #if defined(__FastCaloSimStandAlone__)
12 #include <iomanip>
13 #include <iostream>
14 namespace MSG {
15 enum Level {
16  NIL = 0,
17  VERBOSE,
18  DEBUG,
19  INFO,
20  WARNING,
21  ERROR,
22  FATAL,
23  ALWAYS,
25 }; // enum Level
26 } // end namespace MSG
27 
28 // Macro for use outside classes.
29 // Use this in standalone functions or static methods.
30 #define ATH_MSG_NOCLASS(logger_name, x) \
31  do { \
32  logger_name.msg() << logger_name.startMsg(MSG::ALWAYS, __FILE__, __LINE__) \
33  << x << std::endl; \
34  } while (0)
35 #else // not __FastCaloSimStandAlone__ We get some things from AthenaKernal.
36 // STL includes
37 #include <iosfwd>
38 #include <string>
39 #include <atomic>
40 
41 // framework includes
42 #include "GaudiKernel/MsgStream.h"
43 #include "Gaudi/Property.h"
46 
47 #include <boost/thread/tss.hpp>
48 
49 // Macro for use outside classes.
50 // Use this in standalone functions or static methods.
51 // Differs, becuase it must call doOutput
52 #define ATH_MSG_NOCLASS(logger_name, x) \
53  do { \
54  logger_name.msg(MSG::ALWAYS) << x << std::endl; \
55  logger_name.msg().doOutput(); \
56  } while (0)
57 #endif // end not __FastCaloSimStandAlone__
58 
59 // Declare the class accessories in a namespace
60 // namespace ISF_FCS {
61 //__attribute__((unused)) static const char *LevelNames[MSG::NUM_LEVELS] = {
62 // "NIL", "VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL", "ALWAYS"};
63 //} // end namespace ISF_FCS
64 
65 // Declare the class in a namespace
66 namespace ISF_FCS {
67 
68 // Define Athena like message macros such that they work stand alone
69 #if defined(__FastCaloSimStandAlone__)
70 
71 // We can define a number of macros here to replace standard ATH_MSG
72 // macros. This can only be done outside Athena or the compiler complains.
73 typedef std::ostream MsgStream;
74 
75 #define ATH_MSG_LVL(enum_lvl, x) \
76  do { \
77  if (this->msgLvl(enum_lvl)) \
78  this->msg() << this->startMsg(enum_lvl, __FILE__, __LINE__) << x \
79  << std::endl; \
80  } while (0)
81 
82 #define ATH_MSG_LVL_NOCHK(enum_lvl, x) \
83  do { \
84  this->msg() << this->startMsg(enum_lvl, __FILE__, __LINE__) << x \
85  << std::endl; \
86  } while (0)
87 
88 #define ATH_MSG_VERBOSE(x) ATH_MSG_LVL(MSG::VERBOSE, x)
89 #define ATH_MSG_DEBUG(x) ATH_MSG_LVL(MSG::DEBUG, x)
90 #define ATH_MSG_INFO(x) ATH_MSG_LVL_NOCHK(MSG::INFO, x)
91 #define ATH_MSG_WARNING(x) ATH_MSG_LVL_NOCHK(MSG::WARNING, x)
92 #define ATH_MSG_ERROR(x) ATH_MSG_LVL_NOCHK(MSG::ERROR, x)
93 #define ATH_MSG_FATAL(x) ATH_MSG_LVL_NOCHK(MSG::FATAL, x)
94 
95 // Set up a stream that can be used like: ATH_MSG(INFO) << "hello" <<
96 // END_MSG(INFO); It needs to only write the left columns once, until it is fed
97 // another END_MSG
98 
99 // Provide a stream
100 #define ATH_MSG(lvl) this->stream(MSG::lvl, __FILE__, __LINE__)
101 // Add a new line if the level is in use, and end any stream
102 #define END_MSG(lvl) this->streamerEndLine(MSG::lvl)
103 // Force a new line, and end any stream
104 #define endmsg this->streamerEndLine(MSG::INFO)
105 
106 class MLogging {
107 
108 public:
110  MLogging(){};
112  MLogging(const MLogging &other) : m_level(other.m_level){};
115  m_level = other.m_level;
116  return *this;
117  };
118 
120  virtual ~MLogging(){};
121 
123  MSG::Level level() const { return m_level; }
125  virtual void setLevel(int level);
126 
128  static std::string startMsg(MSG::Level lvl, const std::string& file, int line);
129 
131  MsgStream &msg() const { return *m_msg; }
133  MsgStream &msg(const MSG::Level lvl) const;
135  MsgStream &stream(MSG::Level lvl, std::string file, int line) const;
137  bool msgLvl(const MSG::Level lvl) const;
138 
140  void print(MSG::Level lvl, std::string file, int line,
141  std::string message) const;
142 
144  std::string streamerEndLine(MSG::Level lvl) const;
145 
146 private:
148  bool streamerInLine() const { return m_streamer_in_line; }
150  void streamerInLine(bool is_in_line) const;
152  bool streamerNeedStart(MSG::Level lvl, std::string file) const;
153 
154  MSG::Level m_level = MSG::INFO;
155 
156  MsgStream *m_msg = &std::cout;
157  MsgStream m_null_msg = MsgStream(nullptr);
158  MsgStream *m_null_msg_ptr = &m_null_msg;
159 
160  mutable bool m_streamer_in_line = false;
161  mutable MSG::Level m_streamer_has_lvl = MSG::NIL;
162  mutable std::string m_streamer_from_file = "";
163 
164  // Version number 0 to tell ROOT not to store this.
165  ClassDef(MLogging, 0)
166 };
167 
168 #else // end __FastCaloSimStandAlone__
169 // For inside Athena
170 
171 #define END_MSG(lvl) endmsg
172 
173 // Note that we also cannot use AthMessaging as a base class as this
174 // creates problems when storing these objects in ROOT files (ATLASSIM-5854).
176 class MLogging {
177 
178 public:
180  MLogging(const std::string &name = "ISF_FastCaloSimEvent");
181 
183  // in the AthMessaging, these were disabled, but I dont' see why.
184  MLogging(const MLogging &rhs);
185  MLogging &operator=(const MLogging &rhs);
186 
188  virtual ~MLogging();
189 
191  bool msgLvl(const MSG::Level lvl) const;
192 
194  MsgStream &msg() const;
195 
197  MsgStream &msg(const MSG::Level lvl) const;
198 
200  // This isn't in AthMessaging, but it's useful
201  MSG::Level level() const { return msg().level(); }
203  virtual void setLevel(MSG::Level lvl);
204 
206  // This isn't in AthMessaging, but it's useful outside classes
207  static std::string startMsg(MSG::Level lvl, const std::string& file, int line);
208 
209 private:
211  std::string m_nm;
212 
214  inline static boost::thread_specific_ptr<MsgStream> m_msg_tls
216 
217  ClassDef(MLogging, 0)
218 };
219 
220 // Inline methods:
221 
222 inline bool MLogging::msgLvl(const MSG::Level lvl) const {
223  if (msg().level() <= lvl) {
224  msg() << lvl;
225  return true;
226  } else {
227  return false;
228  }
229 }
230 
231 inline MsgStream &MLogging::msg() const {
232  MsgStream *ms = m_msg_tls.get();
233  if (!ms) {
234  ms = new MsgStream(Athena::getMessageSvc(), m_nm);
235  m_msg_tls.reset(ms);
236  }
237  return *ms;
238 }
239 
240 inline MsgStream &MLogging::msg(const MSG::Level lvl) const {
241  return msg() << lvl;
242 }
243 
244 #endif // end not __FastCaloSimStandAlone__
245 
246 } // namespace ISF_FCS
247 
248 #endif // End header guard
ISF_FCS::MLogging
Cut down AthMessaging.
Definition: MLogging.h:176
checkFileSG.line
line
Definition: checkFileSG.py:75
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
AthMsgStreamMacros.h
ISF_FCS::MLogging::level
MSG::Level level() const
Retrieve output level.
Definition: MLogging.h:201
ISF_FCS::MLogging::operator=
MLogging & operator=(const MLogging &rhs)
Definition: MLogging.cxx:99
ISF_FCS::MLogging::~MLogging
virtual ~MLogging()
Destructor:
Definition: MLogging.cxx:93
python.Constants.ERROR
int ERROR
Definition: Control/AthenaCommon/python/Constants.py:18
python.SystemOfUnits.ms
int ms
Definition: SystemOfUnits.py:132
ReweightUtils.message
message
Definition: ReweightUtils.py:15
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
ISF_FCS::MLogging::ATLAS_THREAD_SAFE
static boost::thread_specific_ptr< MsgStream > m_msg_tls ATLAS_THREAD_SAFE
Do not persistify!
Definition: MLogging.h:215
ISF_FCS
Definition: MLogging.h:66
ISF_FCS::MLogging::MLogging
MLogging(const std::string &name="ISF_FastCaloSimEvent")
Constructor.
Definition: MLogging.cxx:91
ISF_FCS::MLogging::msg
MsgStream & msg() const
Return a stream for sending messages directly (no decoration)
Definition: MLogging.h:231
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
TrigConf::MSGTC::ALWAYS
@ ALWAYS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:29
MSG
Definition: MsgLevel.h:28
ISF_FCS::MLogging::m_nm
std::string m_nm
Message source name.
Definition: MLogging.h:211
file
TFile * file
Definition: tile_monitor.h:29
python.Constants.WARNING
int WARNING
Definition: Control/AthenaCommon/python/Constants.py:17
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TrigConf::MSGTC::NUM_LEVELS
@ NUM_LEVELS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:30
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ISF_FCS::MLogging::setLevel
virtual void setLevel(MSG::Level lvl)
Update outputlevel.
Definition: MLogging.cxx:105
ISF_FCS::MLogging::startMsg
static std::string startMsg(MSG::Level lvl, const std::string &file, int line)
Make a message to decorate the start of logging.
Definition: MLogging.cxx:116
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:16
DEBUG
#define DEBUG
Definition: page_access.h:11
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
TrigConf::MSGTC::NIL
@ NIL
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:22
checker_macros.h
Define macros for attributes used to control the static checker.
ISF_FCS::MLogging::msgLvl
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition: MLogging.h:222