cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Exception.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 <csignal>
20
21
#include "
CepGen/Core/Exception.h
"
22
#include "
CepGen/Utils/String.h
"
23
24
namespace
cepgen
{
25
Exception::Exception
(
const
char
* mod,
const
char
* from,
Type
type,
const
char
* file,
short
lineno) noexcept
26
:
LoggedMessage
(mod, from, MessageType::undefined, file, lineno),
27
std::runtime_error(
"cepgen::Exception"
),
28
type_(type) {}
29
30
Exception::~Exception
() noexcept {
31
// we stop this process' execution on fatal exception
32
if
(type_ ==
Type::fatal
&& raise(SIGINT) != 0)
33
exit(0);
34
}
35
36
const
char
*
Exception::what
() const noexcept {
37
sprintf(what_,
"cepgen::Exception from %s:\n\t%s"
,
from_
.data(),
message_
.str().data());
38
return
what_;
39
}
40
41
void
Exception::dump
(std::ostream* os)
const
noexcept
{
42
if
(!os)
43
os =
utils::Logger::get
().
output
().get();
44
if
(!os)
45
return
;
46
47
const
std::string sep(80,
'-'
);
48
(*os) << sep <<
"\n"
<< type_ <<
" occurred at "
<<
Message::now
() <<
"\n"
;
49
if
(!from_.empty())
50
(*os) <<
" raised by: "
<<
utils::colourise
(from_,
utils::Colour::none
,
utils::Modifier::underline
) <<
"\n"
;
51
if
(
utils::Logger::get
().extended() && !file_.empty()) {
52
(*os) <<
" file: "
<<
utils::colourise
(file_,
utils::Colour::none
,
utils::Modifier::dimmed
) <<
"\n"
;
53
if
(line_num_ != 0)
54
(*os) <<
" line #"
<< line_num_ <<
"\n"
;
55
}
56
(*os) <<
"\n"
<< message_.str() <<
"\n"
<< sep <<
"\n"
;
57
}
58
59
std::ostream&
operator<<
(std::ostream& os,
const
Exception::Type
& type) {
60
switch
(type) {
61
case
Exception::Type::error
:
62
return
os <<
utils::colourise
(
"Error"
,
utils::Colour::red
,
utils::Modifier::bold
);
63
case
Exception::Type::fatal
:
64
return
os <<
utils::colourise
(
"Fatal error"
,
utils::Colour::red
,
utils::Modifier::bold
);
65
case
Exception::Type::undefined
:
66
return
os <<
utils::colourise
(
"Undef'd exception"
,
utils::Colour::none
,
utils::Modifier::reverse
);
67
}
68
return
os;
69
}
70
}
// namespace cepgen
Exception.h
String.h
cepgen::Exception::Type
Type
Enumeration of exception severities.
Definition
Exception.h:28
cepgen::Exception::fatal
@ fatal
Critical and stopping error.
Definition
Exception.h:31
cepgen::Exception::error
@ error
General non-stopping error.
Definition
Exception.h:30
cepgen::Exception::undefined
@ undefined
Irregular message.
Definition
Exception.h:29
cepgen::Exception::Exception
Exception(const char *mod, const char *from="", Type type=Type::undefined, const char *file="", short lineno=0) noexcept
Definition
Exception.cpp:25
cepgen::Exception::~Exception
virtual ~Exception() noexcept override
Destructor (potentially killing the process)
Definition
Exception.cpp:30
cepgen::Exception::what
const char * what() const noexcept override
Definition
Exception.cpp:36
cepgen::Exception::dump
void dump(std::ostream *os=nullptr) const noexcept override
Human-readable dump of the exception.
Definition
Exception.cpp:41
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::from_
std::string from_
Origin of the exception.
Definition
Message.h:175
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::Modifier::dimmed
@ dimmed
cepgen::utils::Modifier::reverse
@ reverse
cepgen::utils::Modifier::bold
@ bold
cepgen::utils::Modifier::underline
@ underline
cepgen::utils::Colour::none
@ none
cepgen::utils::Colour::red
@ red
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
Core
Exception.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7