cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.3
A generic central exclusive processes event generator
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2015-2023 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 <iostream> // for cout
20
21#include "CepGen/Utils/Logger.h"
23
24namespace cepgen {
25 namespace utils {
26 Logger::Logger(Logger::StreamHandler os) : output_(std::move(os)) {
27 if (output_.get() != &std::cout)
28 CG_INFO("Logger") << "New logger initialised for output@0x" << output_.get() << ".";
29 }
30
31 Logger& Logger::get(std::ostream* os) {
32 static Logger log(os ? std::unique_ptr<std::ostream, std::default_delete<std::ostream> >(os)
33 : StreamHandler(&std::cout, [](std::ostream*) -> void {}));
34 return log;
35 }
36
37 void Logger::addExceptionRule(const std::string& rule) {
38 allowed_exc_.emplace_back(rule, std::regex_constants::extended);
39 }
40
41 bool Logger::passExceptionRule(const std::string& tmpl, const Level& lev) const {
42 if (level_ >= lev)
43 return true;
44 if (allowed_exc_.empty())
45 return false;
46 for (const auto& rule : allowed_exc_)
47 try {
48 if (std::regex_match(tmpl, rule))
49 return true;
50 } catch (const std::regex_error& err) {
51 throw std::runtime_error("Failed to evaluate regex for logging tool.\n" + std::string(err.what()));
52 }
53 return false;
54 }
55
56 void Logger::setOutput(std::ostream* os) { output_.reset(os); }
57
58 Logger::StreamHandler& Logger::output() {
59 static StreamHandler empty_output{nullptr};
60 if (level_ == Level::nothing)
61 return empty_output;
62 return output_;
63 }
64
65 bool Logger::isTTY() const { return output_.get() == &std::cout || output_.get() == &std::cerr; }
66 } // namespace utils
67
68 std::ostream& operator<<(std::ostream& os, const utils::Logger::Level& lvl) {
69 switch (lvl) {
70 case utils::Logger::Level::nothing:
71 return os << "None";
72 case utils::Logger::Level::error:
73 return os << "Errors";
74 case utils::Logger::Level::warning:
75 return os << "Warnings";
76 case utils::Logger::Level::information:
77 return os << "Infos";
78 case utils::Logger::Level::debug:
79 return os << "Debug";
80 case utils::Logger::Level::debugInsideLoop:
81 return os << "Debug (in loops)";
82 }
83 return os;
84 }
85} // namespace cepgen
#define CG_INFO(mod)
Definition Message.h:216
General purposes logger.
Definition Logger.h:30
Level
Logging threshold for the output stream.
Definition Logger.h:44
std::unique_ptr< std::ostream, std::function< void(std::ostream *)> > StreamHandler
Definition Logger.h:41
Common namespace for this Monte Carlo generator.
std::ostream & operator<<(std::ostream &os, const Exception::Type &type)
Definition Exception.cpp:59