ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
RuntimeErrorCollector.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
23
24#include <boost/mpi/collectives.hpp>
25
26#include <algorithm>
27#include <functional>
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace ErrorHandling {
35
37 : m_comm(std::move(comm)) {}
38
40 if (!m_errors.empty()) {
41 /* Print remaining error messages on destruction */
42 std::cerr << "There were unhandled errors.\n";
43 flush();
44 }
45}
46
48 m_errors.emplace_back(message);
49}
50
52 m_errors.emplace_back(std::move(message));
53}
54
56 const std::string &msg,
57 const char *function, const char *file,
58 const int line) {
59 m_errors.emplace_back(level, m_comm.rank(), msg, std::string(function),
60 std::string(file), line);
61}
62
63void RuntimeErrorCollector::warning(const std::string &msg,
64 const char *function, const char *file,
65 const int line) {
66 m_errors.emplace_back(RuntimeError::ErrorLevel::WARNING, m_comm.rank(), msg,
67 std::string(function), std::string(file), line);
68}
69
70void RuntimeErrorCollector::warning(const char *msg, const char *function,
71 const char *file, const int line) {
72 warning(std::string(msg), function, file, line);
73}
74
75void RuntimeErrorCollector::warning(const std::ostringstream &mstr,
76 const char *function, const char *file,
77 const int line) {
78 warning(mstr.str(), function, file, line);
79}
80
81void RuntimeErrorCollector::error(const std::string &msg, const char *function,
82 const char *file, const int line) {
83 m_errors.emplace_back(RuntimeError::ErrorLevel::ERROR, m_comm.rank(), msg,
84 std::string(function), std::string(file), line);
85}
86
87void RuntimeErrorCollector::error(const char *msg, const char *function,
88 const char *file, const int line) {
89 error(std::string(msg), function, file, line);
90}
91
92void RuntimeErrorCollector::error(const std::ostringstream &mstr,
93 const char *function, const char *file,
94 const int line) {
95 error(mstr.str(), function, file, line);
96}
97
99 return boost::mpi::all_reduce(m_comm, static_cast<int>(m_errors.size()),
100 std::plus<>());
101}
102
104 return static_cast<int>(std::count_if(
105 m_errors.begin(), m_errors.end(),
106 [level](const RuntimeError &e) { return e.level() >= level; }));
107}
108
109void RuntimeErrorCollector::clear() { m_errors.clear(); }
110
112 for (auto const &e : m_errors) {
113 std::cerr << e.format() << std::endl;
114 }
115 this->clear();
116}
117
118std::vector<RuntimeError> RuntimeErrorCollector::gather() {
119 std::vector<RuntimeError> all_errors{};
120 std::swap(all_errors, m_errors);
121
122 Utils::Mpi::gather_buffer(all_errors, m_comm);
123
124 return all_errors;
125}
126
128 Utils::Mpi::gather_buffer(m_errors, m_comm);
129
130 this->clear();
131}
132
133} // namespace ErrorHandling
void flush()
Flush error messages to standard error.
int count() const
Get the number of all flying messages on all nodes.
void error(const std::string &msg, const char *function, const char *file, int line)
void warning(const std::string &msg, const char *function, const char *file, int line)
RuntimeErrorCollector(boost::mpi::communicator comm)
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
ErrorLevel
The error level, warnings are only displayed to the user, errors are fatal.