cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.3
A generic 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
22#include "CepGen/Utils/String.h"
23
24namespace 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
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) {
64 return os << utils::colourise("Fatal error", utils::Colour::red, utils::Modifier::bold);
66 return os << utils::colourise("Undef'd exception", utils::Colour::none, utils::Modifier::reverse);
67 }
68 return os;
69 }
70} // namespace cepgen
Type
Enumeration of exception severities.
Definition Exception.h:28
@ fatal
Critical and stopping error.
Definition Exception.h:31
@ error
General non-stopping error.
Definition Exception.h:30
@ undefined
Irregular message.
Definition Exception.h:29
Exception(const char *mod, const char *from="", Type type=Type::undefined, const char *file="", short lineno=0) noexcept
Definition Exception.cpp:25
virtual ~Exception() noexcept override
Destructor (potentially killing the process)
Definition Exception.cpp:30
const char * what() const noexcept override
Definition Exception.cpp:36
void dump(std::ostream *os=nullptr) const noexcept override
Human-readable dump of the exception.
Definition Exception.cpp:41
A simple exception handler.
Definition Message.h:44
std::ostringstream message_
Message to throw.
Definition Message.h:174
std::string from_
Origin of the exception.
Definition Message.h:175
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 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