cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Message.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2022 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_Message_h
20#define CepGen_Utils_Message_h
21
22#include <set>
23#include <sstream>
24#include <unordered_map>
25
26#include "CepGen/Utils/Logger.h"
27
28namespace cepgen {
32 struct Message {
34 explicit inline Message() = default;
35 virtual ~Message() = default;
38 virtual void dump(std::ostream* os = nullptr) const = 0;
39 static std::string now();
40 };
41
44 class LoggedMessage : public Message {
45 public:
47 enum class MessageType {
48 undefined = -1,
49 debug,
50 verbatim,
51 info,
52 warning,
53 };
60 explicit LoggedMessage(const char* mod,
61 const char* from = "",
63 const char* file = "",
64 short lineno = 0) noexcept;
66 LoggedMessage(const LoggedMessage&) noexcept;
68 virtual ~LoggedMessage() noexcept override;
69
71 friend std::ostream& operator<<(std::ostream&, const MessageType&);
72
73 //----- Overloaded stream operators
74
76 template <typename T>
77 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const T& var) noexcept {
78 auto& nc_except = const_cast<LoggedMessage&>(exc);
79 nc_except.message_ << var;
80 return exc;
81 }
83 friend const LoggedMessage& operator<<(const LoggedMessage&, const bool&) noexcept;
85 friend const LoggedMessage& operator<<(const LoggedMessage&, const std::wstring&) noexcept;
87 template <typename T, typename U>
88 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const std::pair<T, U>& pair_var) noexcept {
89 return exc << "(" << pair_var.first << ", " << pair_var.second << ")";
90 }
92 template <typename T>
93 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const std::set<T>& set_var) noexcept {
94 exc << "[";
95 std::string sep;
96 if (!set_var.empty())
97 for (const auto& var : set_var)
98 exc << sep << var, sep = ", ";
99 return exc << "]";
100 }
102 template <typename T>
103 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const std::vector<T>& vec_var) noexcept {
104 exc << "{";
105 std::string sep;
106 if (!vec_var.empty())
107 for (const auto& var : vec_var)
108 exc << sep << var, sep = ", ";
109 return exc << "}";
110 }
112 template <typename T, std::size_t N>
113 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const std::array<T, N>& vec_var) noexcept {
114 exc << "{";
115 std::string sep;
116 if (!vec_var.empty())
117 for (const auto& var : vec_var)
118 exc << sep << var, sep = ", ";
119 return exc << "}";
120 }
122 template <typename T, typename U>
123 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc, const std::map<T, U>& map_var) noexcept {
124 exc << "{";
125 std::string sep;
126 if (!map_var.empty())
127 for (const auto& var : map_var)
128 exc << sep << "{" << var.first << " -> " << var.second << "}", sep = ", ";
129 return exc << "}";
130 }
132 template <typename T, typename U>
133 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc,
134 const std::unordered_map<T, U>& map_var) noexcept {
135 exc << "{";
136 std::string sep;
137 if (!map_var.empty())
138 for (const auto& var : map_var)
139 exc << sep << "{" << var.first << " -> " << var.second << "}", sep = ", ";
140 return exc << "}";
141 }
143 inline friend const LoggedMessage& operator<<(const LoggedMessage& exc,
144 std::ios_base& (*f)(std::ios_base&)) noexcept {
145 LoggedMessage& nc_except = const_cast<LoggedMessage&>(exc);
146 f(nc_except.message_);
147 return exc;
148 }
149
151 template <typename T>
152 inline const LoggedMessage& log(T&& lam) noexcept {
153 lam(*this);
154 return *this;
155 }
156
158 std::string message() const { return message_.str(); }
159
161 const std::string& from() const { return from_; }
163 const std::string& file() const { return file_; }
165 short lineNumber() const { return line_num_; }
167 const MessageType& type() const { return type_; }
169 void dump(std::ostream* os = nullptr) const noexcept override;
171 std::ostream& stream() { return message_; }
172
173 protected:
174 std::ostringstream message_;
175 std::string from_;
176 std::string file_;
177 short line_num_;
178
179 private:
180 MessageType type_;
181 std::string module_;
182 };
183
187 using Message::Message;
189 inline NullStream() = default;
191 inline NullStream(const LoggedMessage&) {}
192 void dump(std::ostream* os = nullptr) const override { (void)(os); }
194 template <class T>
196 return *this;
197 }
199 template <typename T>
201 return *this;
202 }
203 };
204} // namespace cepgen
205
206#ifdef _WIN32
207#define __FUNC__ __FUNCSIG__
208#else
209#define __FUNC__ __PRETTY_FUNCTION__
210#endif
211
212#define CG_LOG \
213 (cepgen::utils::Logger::get().level() <= cepgen::utils::Logger::Level::nothing) \
214 ? cepgen::NullStream() \
215 : cepgen::LoggedMessage("Logging", __FUNC__, cepgen::LoggedMessage::MessageType::verbatim, __FILE__, __LINE__)
216#define CG_INFO(mod) \
217 (!CG_LOG_MATCH(mod, information)) \
218 ? cepgen::NullStream() \
219 : cepgen::LoggedMessage(mod, __FUNC__, cepgen::LoggedMessage::MessageType::info, __FILE__, __LINE__)
220#define CG_DEBUG(mod) \
221 (!CG_LOG_MATCH(mod, debug)) \
222 ? cepgen::NullStream() \
223 : cepgen::LoggedMessage(mod, __FUNC__, cepgen::LoggedMessage::MessageType::debug, __FILE__, __LINE__)
224#define CG_DEBUG_LOOP(mod) \
225 (!CG_LOG_MATCH(mod, debugInsideLoop)) \
226 ? cepgen::NullStream() \
227 : cepgen::LoggedMessage(mod, __FUNC__, cepgen::LoggedMessage::MessageType::debug, __FILE__, __LINE__)
228#define CG_WARNING(mod) \
229 (!CG_LOG_MATCH(mod, warning)) \
230 ? cepgen::NullStream() \
231 : cepgen::LoggedMessage(mod, __FUNC__, cepgen::LoggedMessage::MessageType::warning, __FILE__, __LINE__)
232
233#endif
A simple exception handler.
Definition Message.h:44
const std::string & from() const
Origin of the exception.
Definition Message.h:161
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::unordered_map< T, U > &map_var) noexcept
Generic templated mapping-variables feeder operator.
Definition Message.h:133
short lineNumber() const
Line number where the exception occured.
Definition Message.h:165
friend std::ostream & operator<<(std::ostream &, const MessageType &)
Printout operator for message type.
Definition Message.cpp:97
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::vector< T > &vec_var) noexcept
Generic templated vector-variables feeder operator.
Definition Message.h:103
std::ostringstream message_
Message to throw.
Definition Message.h:174
const LoggedMessage & log(T &&lam) noexcept
Lambda function handler.
Definition Message.h:152
friend const LoggedMessage & operator<<(const LoggedMessage &exc, std::ios_base &(*f)(std::ios_base &)) noexcept
Pipe modifier operator.
Definition Message.h:143
const MessageType & type() const
Message type.
Definition Message.h:167
std::string from_
Origin of the exception.
Definition Message.h:175
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::map< T, U > &map_var) noexcept
Generic templated mapping-variables feeder operator.
Definition Message.h:123
short line_num_
Line number.
Definition Message.h:177
std::ostream & stream()
Output stream object.
Definition Message.h:171
std::string message() const
Human-readable message.
Definition Message.h:158
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::set< T > &set_var) noexcept
Generic templated vector-variables feeder operator.
Definition Message.h:93
std::string file_
File.
Definition Message.h:176
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::array< T, N > &vec_var) noexcept
Generic templated vector-variables feeder operator.
Definition Message.h:113
MessageType
Enumeration of message type.
Definition Message.h:47
@ warning
Casual non-stopping warning.
@ debug
Debugging information to be enabled.
@ info
Prettified information.
void dump(std::ostream *os=nullptr) const noexcept override
Human-readable dump of the message.
Definition Message.cpp:55
friend const LoggedMessage & operator<<(const LoggedMessage &exc, const std::pair< T, U > &pair_var) noexcept
Generic templated pair-variables feeder operator.
Definition Message.h:88
const std::string & file() const
File where the exception occured.
Definition Message.h:163
Common namespace for this Monte Carlo generator.
A generic message type.
Definition Message.h:32
static std::string now()
Human-readable date/time.
Definition Message.cpp:23
Message()=default
Generic message constructor.
virtual void dump(std::ostream *os=nullptr) const =0
Dump the full exception information in a given output stream.
virtual ~Message()=default
Placeholder for debugging messages if logging threshold is not reached.
Definition Message.h:186
void dump(std::ostream *os=nullptr) const override
Dump the full exception information in a given output stream.
Definition Message.h:192
NullStream(const LoggedMessage &)
Empty constructor.
Definition Message.h:191
NullStream()=default
Empty constructor.
NullStream & log(T &&)
Lambda function handler (null and void)
Definition Message.h:200
NullStream & operator<<(const T &)
Stream operator (null and void)
Definition Message.h:195