NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
MSGTool.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9
10namespace NAGASH
11{
13 enum class MSGLevel
14 {
15 SILENT = 7,
16 FATAL = 6,
17 ERROR = 5,
18 WARNING = 4,
19 INFO = 3,
20 FOCUS = 2,
21 DEBUG = 1
22 };
23
27 class MSGTool : public std::enable_shared_from_this<MSGTool>
28 {
29 private:
30 friend class LoopEvent;
31#ifdef NAGASH_HEPMC
32 friend class LoopHepMC;
33#endif
34 friend class ConfigTool;
35 friend class Analysis;
36
39 TString FocusName = "UNKNOWN";
40 std::vector<TString> FuncTitle;
41 std::ofstream outfile;
42 bool tolog = false;
43 bool isCreateLog = false;
44 bool isOpenLog = false;
45 TString LogFileName;
46
47 mutable std::shared_mutex MSGMutex; // for using MSGTool concurrently
48
49 void PrintTitle();
50 void PrintSpace();
51 void PrintEnd();
52 void PrintLevel(MSGLevel level);
53
54 template <typename... Args>
55 void Print(Args &&...args);
56
57 public:
58 MSGTool(MSGLevel level, const TString &name = "UNKNOWN");
59 MSGTool(const MSGTool &msg, int ThreadNumber);
60 MSGTool() = default;
61 MSGTool(const MSGTool &msg) = delete;
62 MSGTool(MSGTool &&msg) = delete;
63 MSGTool &operator=(const MSGTool &msg) = delete;
64 MSGTool &operator=(MSGTool &&msg) = delete;
65 ~MSGTool();
66
67 template <typename... Args>
68 void MSG(MSGLevel level, Args &&...args);
69 template <typename... Args>
70 void MSG_ERROR(Args &&...args);
71 template <typename... Args>
72 void MSG_WARNING(Args &&...args);
73 template <typename... Args>
74 void MSG_INFO(Args &&...args);
75 template <typename... Args>
76 void MSG_DEBUG(Args &&...args);
77
79 MSGLevel Level() const;
80 void CreateLog(const TString &name);
81 void OpenLog(const TString &name);
82 void CloseLog();
83 void StartFocusRegion(const TString &name);
84 void EndFocusRegion(const TString &name);
85 void StartTitle(const TString &name);
86 void EndTitle();
87
99 {
100 friend class MSGTool;
101
102 private:
103 MSGTitleGuard(std::shared_ptr<MSGTool> tempmsg) : msg(tempmsg) {}
104 std::shared_ptr<MSGTool> msg;
105
106 public:
107 MSGTitleGuard() = delete;
108 MSGTitleGuard(const MSGTitleGuard &) = delete;
112
114 {
115 msg->EndTitle();
116 }
117 };
118
119 std::unique_ptr<MSGTitleGuard> StartTitleWithGuard(const TString &title);
120 };
121
123 template <typename... Args>
124 inline void MSGTool::MSG(MSGLevel level, Args &&...args)
125 {
126 if (level >= OutputLevel)
127 {
128 PrintTitle();
129 PrintSpace();
130 PrintLevel(level);
131 PrintSpace();
132 Print(args...);
133 PrintEnd();
134 }
135 }
136
138 template <typename... Args>
139 inline void MSGTool::MSG_ERROR(Args &&...args)
140 {
141 MSG(MSGLevel::ERROR, std::forward<Args>(args)...);
142 }
143
145 template <typename... Args>
146 inline void MSGTool::MSG_WARNING(Args &&...args)
147 {
148 MSG(MSGLevel::WARNING, std::forward<Args>(args)...);
149 }
150
152 template <typename... Args>
153 inline void MSGTool::MSG_INFO(Args &&...args)
154 {
155 MSG(MSGLevel::INFO, std::forward<Args>(args)...);
156 }
157
159 template <typename... Args>
160 inline void MSGTool::MSG_DEBUG(Args &&...args)
161 {
162 MSG(MSGLevel::DEBUG, std::forward<Args>(args)...);
163 }
164
165 template <typename... Args>
166 inline void MSGTool::Print(Args &&...args)
167 {
168 if (tolog)
169 (outfile << ... << args);
170 else
171 (std::cout << ... << args);
172 }
173
175 {
176 Print(" ");
177 }
178
179 inline void MSGTool::PrintEnd()
180 {
181 if (tolog)
182 outfile << std::endl;
183 else
184 std::cout << std::endl;
185 }
186
187} // namespace NAGASH
Some global definitions.
Provide multi-thread interface to manipulate with Job.
Definition Analysis.h:235
provide interface to config objects in NAGASH.
Definition ConfigTool.h:14
Virtual base class for event loops.
Definition LoopEvent.h:26
Used to call MSGTool::EndTitle() automatically.
Definition MSGTool.h:99
std::shared_ptr< MSGTool > msg
Definition MSGTool.h:104
MSGTitleGuard(MSGTitleGuard &&)=delete
MSGTitleGuard(std::shared_ptr< MSGTool > tempmsg)
Definition MSGTool.h:103
MSGTitleGuard & operator=(MSGTitleGuard &&)=delete
MSGTitleGuard & operator=(const MSGTitleGuard &)=delete
MSGTitleGuard(const MSGTitleGuard &)=delete
Maniplulate all messages of NAGASH.
Definition MSGTool.h:28
void MSG_ERROR(Args &&...args)
Output with ERROR level. Messages below the level in MSGTool will be ignored.
Definition MSGTool.h:139
MSGLevel OutputLevel
Definition MSGTool.h:37
void OpenLog(const TString &name)
Open log file with given file name.
Definition MSGTool.cxx:67
bool isCreateLog
Definition MSGTool.h:43
MSGLevel DefinedLevel
Definition MSGTool.h:38
void PrintTitle()
Definition MSGTool.cxx:142
void MSG_INFO(Args &&...args)
Output with INFO level. Messages below the level in MSGTool will be ignored.
Definition MSGTool.h:153
TString FocusName
Definition MSGTool.h:39
void MSG(MSGLevel level, Args &&...args)
Output with given message level. Messages below the level in MSGTool will be ignored.
Definition MSGTool.h:124
std::unique_ptr< MSGTitleGuard > StartTitleWithGuard(const TString &title)
Start a title with given name.
Definition MSGTool.cxx:127
MSGLevel SetOutputLevel(MSGLevel level)
Set the output message level.
Definition MSGTool.cxx:176
void CloseLog()
Close the log file.
Definition MSGTool.cxx:80
MSGTool(MSGTool &&msg)=delete
void MSG_WARNING(Args &&...args)
Output with WARNING level. Messages below the level in MSGTool will be ignored.
Definition MSGTool.h:146
void Print(Args &&...args)
Definition MSGTool.h:166
MSGTool & operator=(const MSGTool &msg)=delete
void EndTitle()
End the current title.
Definition MSGTool.cxx:135
MSGTool & operator=(MSGTool &&msg)=delete
void PrintSpace()
Definition MSGTool.h:174
std::vector< TString > FuncTitle
Definition MSGTool.h:40
void StartFocusRegion(const TString &name)
Start focus region, inside this region the message level would be set to debug.
Definition MSGTool.cxx:95
void CreateLog(const TString &name)
Create log file with given file name.
Definition MSGTool.cxx:53
MSGLevel Level() const
Get the current message level.
Definition MSGTool.cxx:184
void StartTitle(const TString &name)
Start a title with given name.
Definition MSGTool.cxx:118
MSGTool()=default
void PrintEnd()
Definition MSGTool.h:179
void PrintLevel(MSGLevel level)
Definition MSGTool.cxx:159
TString LogFileName
Definition MSGTool.h:45
std::ofstream outfile
Definition MSGTool.h:41
void MSG_DEBUG(Args &&...args)
Output with DEBUG level. Messages below the level in MSGTool will be ignored.
Definition MSGTool.h:160
std::shared_mutex MSGMutex
Definition MSGTool.h:47
void EndFocusRegion(const TString &name)
End focus region.
Definition MSGTool.cxx:103
MSGTool(const MSGTool &msg)=delete
MSGLevel
class to define different message level
Definition MSGTool.h:14