cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
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
19
#include "
CepGen/Utils/Message.h
"
20
#include "
CepGen/Utils/String.h
"
21
22
namespace
cepgen
{
23
std::string
Message::now
() {
return
utils::timeAs
(
"%H:%M:%S"
); }
24
25
LoggedMessage::LoggedMessage
(
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
29
LoggedMessage::LoggedMessage
(
const
LoggedMessage
& rhs) noexcept
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
37
LoggedMessage::~LoggedMessage
() noexcept {
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
)
45
:
utils::colourise
(
"false"
,
utils::Colour::red
));
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
(
69
"{"
+ from_ +
"}\n\t"
,
utils::Colour::none
,
utils::Modifier::dimmed
|
utils::Modifier::italic
);
70
else
71
(*os) <<
":\t"
;
72
(*os) << message_.str() <<
"\n"
;
73
return
;
74
}
75
(*os) <<
" "
<<
utils::colourise
(from_,
utils::Colour::none
,
utils::Modifier::underline
|
utils::Modifier::dimmed
);
76
if
(
utils::Logger::get
().extended())
77
(*os) <<
" "
78
<<
utils::colourise
(file_,
79
utils::Colour::none
,
80
utils::Modifier::bold
|
utils::Modifier::italic
|
utils::Modifier::dimmed
)
81
<<
":"
82
<<
utils::colourise
(
83
std::to_string(line_num_),
utils::Colour::none
,
utils::Modifier::italic
|
utils::Modifier::dimmed
)
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) {
99
case
LoggedMessage::MessageType::info
:
100
return
os <<
utils::colourise
(
"Info"
,
utils::Colour::green
,
utils::Modifier::bold
);
101
case
LoggedMessage::MessageType::debug
:
102
return
os <<
utils::colourise
(
"Debug"
,
utils::Colour::yellow
,
utils::Modifier::bold
);
103
case
LoggedMessage::MessageType::warning
:
104
return
os <<
utils::colourise
(
"Warning"
,
utils::Colour::blue
,
utils::Modifier::bold
);
105
case
LoggedMessage::MessageType::verbatim
:
106
return
os <<
utils::colourise
(
"Verbatim"
,
utils::Colour::none
,
utils::Modifier::bold
);
107
case
LoggedMessage::MessageType::undefined
:
108
return
os <<
utils::colourise
(
"Undef'd exception"
,
utils::Colour::none
,
utils::Modifier::reverse
);
109
}
110
return
os;
111
}
112
}
// namespace cepgen
Message.h
String.h
cepgen::LoggedMessage
A simple exception handler.
Definition
Message.h:44
cepgen::LoggedMessage::message_
std::ostringstream message_
Message to throw.
Definition
Message.h:174
cepgen::LoggedMessage::~LoggedMessage
virtual ~LoggedMessage() noexcept override
Default destructor.
Definition
Message.cpp:37
cepgen::LoggedMessage::LoggedMessage
LoggedMessage(const char *mod, const char *from="", MessageType type=MessageType::undefined, const char *file="", short lineno=0) noexcept
Generic constructor.
Definition
Message.cpp:25
cepgen::LoggedMessage::MessageType
MessageType
Enumeration of message type.
Definition
Message.h:47
cepgen::LoggedMessage::MessageType::undefined
@ undefined
Irregular message.
cepgen::LoggedMessage::MessageType::warning
@ warning
Casual non-stopping warning.
cepgen::LoggedMessage::MessageType::debug
@ debug
Debugging information to be enabled.
cepgen::LoggedMessage::MessageType::info
@ info
Prettified information.
cepgen::LoggedMessage::MessageType::verbatim
@ verbatim
Raw information.
cepgen::LoggedMessage::dump
void dump(std::ostream *os=nullptr) const noexcept override
Human-readable dump of the message.
Definition
Message.cpp:55
cepgen::utils::Logger::get
static Logger & get(std::ostream *=nullptr)
Retrieve the running instance of the logger.
Definition
Logger.cpp:31
cepgen::utils::Logger::output
StreamHandler & output()
Output stream to use for all logging operations.
Definition
Logger.cpp:58
cepgen::utils::timeAs
std::string timeAs(const std::string &fmt)
Return the formatted date/time now.
Definition
String.cpp:110
cepgen::utils::Modifier::italic
@ italic
cepgen::utils::Modifier::dimmed
@ dimmed
cepgen::utils::Modifier::reverse
@ reverse
cepgen::utils::Modifier::bold
@ bold
cepgen::utils::Modifier::underline
@ underline
cepgen::utils::toString
std::string toString(const std::wstring &str)
Convert a wide characters to a standard characters string.
Definition
String.cpp:151
cepgen::utils::Colour::none
@ none
cepgen::utils::Colour::blue
@ blue
cepgen::utils::Colour::green
@ green
cepgen::utils::Colour::red
@ red
cepgen::utils::Colour::yellow
@ yellow
cepgen::utils::colourise
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
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
cepgen::operator<<
std::ostream & operator<<(std::ostream &os, const Exception::Type &type)
Definition
Exception.cpp:59
cepgen::Message::now
static std::string now()
Human-readable date/time.
Definition
Message.cpp:23
CepGen
Utils
Message.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7