cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.3
A generic central exclusive processes event generator
Loading...
Searching...
No Matches
Message.cpp
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
20#include "CepGen/Utils/String.h"
21
22namespace cepgen {
23 std::string Message::now() { return utils::timeAs("%H:%M:%S"); }
24
26 const char* mod, const char* from, MessageType type, const char* file, short lineno) noexcept
27 : from_(from), file_(file), line_num_(lineno), type_(type), module_(mod) {}
28
30 : message_(rhs.message_.str()), // only reason to customise the copy constructor
31 from_(rhs.from_),
32 file_(rhs.file_),
33 line_num_(rhs.line_num_),
34 type_(rhs.type_),
35 module_(rhs.module_) {}
36
38 if (type_ != MessageType::undefined)
39 dump();
40 }
41
42 const LoggedMessage& operator<<(const LoggedMessage& exc, const bool& var) noexcept {
43 LoggedMessage& nc_except = const_cast<LoggedMessage&>(exc);
44 nc_except.message_ << (var ? utils::colourise("true", utils::Colour::green)
46 return exc;
47 }
48
49 const LoggedMessage& operator<<(const LoggedMessage& exc, const std::wstring& var) noexcept {
50 LoggedMessage& nc_except = const_cast<LoggedMessage&>(exc);
51 nc_except.message_ << utils::toString(var);
52 return exc;
53 }
54
55 void LoggedMessage::dump(std::ostream* os) const noexcept {
56 if (!os)
57 os = utils::Logger::get().output().get();
58 if (!os)
59 return;
60
61 if (type_ == MessageType::verbatim || type_ == MessageType::undefined) {
62 (*os) << message_.str() << "\n";
63 return;
64 }
65 (*os) << type_;
66 if (type_ == MessageType::info) {
67 if (utils::Logger::get().extended())
68 (*os) << utils::colourise(
70 else
71 (*os) << ":\t";
72 (*os) << message_.str() << "\n";
73 return;
74 }
76 if (utils::Logger::get().extended())
77 (*os) << " "
78 << utils::colourise(file_,
81 << ":"
84 << "\n";
85 else
86 (*os) << ": ";
87 if (type_ == MessageType::debug) {
88 (*os) << utils::colourise(message_.str(), utils::Colour::none, utils::Modifier::dimmed) << "\n";
89 return;
90 }
91 if (type_ == MessageType::warning) {
92 (*os) << message_.str() << "\n";
93 return;
94 }
95 }
96
97 std::ostream& operator<<(std::ostream& os, const LoggedMessage::MessageType& type) {
98 switch (type) {
108 return os << utils::colourise("Undef'd exception", utils::Colour::none, utils::Modifier::reverse);
109 }
110 return os;
111 }
112} // namespace cepgen
A simple exception handler.
Definition Message.h:44
std::ostringstream message_
Message to throw.
Definition Message.h:174
virtual ~LoggedMessage() noexcept override
Default destructor.
Definition Message.cpp:37
LoggedMessage(const char *mod, const char *from="", MessageType type=MessageType::undefined, const char *file="", short lineno=0) noexcept
Generic constructor.
Definition Message.cpp:25
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
static Logger & get(std::ostream *=nullptr)
Retrieve the running instance of the logger.
Definition Logger.cpp:31
StreamHandler & output()
Output stream to use for all logging operations.
Definition Logger.cpp:58
std::string timeAs(const std::string &fmt)
Return the formatted date/time now.
Definition String.cpp:110
std::string toString(const std::wstring &str)
Convert a wide characters to a standard characters string.
Definition String.cpp:151
std::string colourise(const std::string &str, const Colour &col, const Modifier &mod)
Colourise a string for TTY-type output streams.
Definition String.cpp:70
Common namespace for this Monte Carlo generator.
std::ostream & operator<<(std::ostream &os, const Exception::Type &type)
Definition Exception.cpp:59
static std::string now()
Human-readable date/time.
Definition Message.cpp:23