Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
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 std::lock_guard<std::mutex> lock(mutex);
49 m_errors.emplace_back(message);
50}
51
53 std::lock_guard<std::mutex> lock(mutex);
54 m_errors.emplace_back(std::move(message));
55}
56
58 const std::string &msg,
59 const char *function, const char *file,
60 const int line) {
61 std::lock_guard<std::mutex> lock(mutex);
62 m_errors.emplace_back(level, m_comm.rank(), msg, std::string(function),
63 std::string(file), line);
64}
65
66void RuntimeErrorCollector::warning(const std::string &msg,
67 const char *function, const char *file,
68 const int line) {
69 std::lock_guard<std::mutex> lock(mutex);
70 m_errors.emplace_back(RuntimeError::ErrorLevel::WARNING, m_comm.rank(), msg,
71 std::string(function), std::string(file), line);
72}
73
74void RuntimeErrorCollector::warning(const char *msg, const char *function,
75 const char *file, const int line) {
76 warning(std::string(msg), function, file, line);
77}
78
79void RuntimeErrorCollector::warning(const std::ostringstream &mstr,
80 const char *function, const char *file,
81 const int line) {
82 warning(mstr.str(), function, file, line);
83}
84
85void RuntimeErrorCollector::error(const std::string &msg, const char *function,
86 const char *file, const int line) {
87 std::lock_guard<std::mutex> lock(mutex);
88 m_errors.emplace_back(RuntimeError::ErrorLevel::ERROR, m_comm.rank(), msg,
89 std::string(function), std::string(file), line);
90}
91
92void RuntimeErrorCollector::error(const char *msg, const char *function,
93 const char *file, const int line) {
94 error(std::string(msg), function, file, line);
95}
96
97void RuntimeErrorCollector::error(const std::ostringstream &mstr,
98 const char *function, const char *file,
99 const int line) {
100 error(mstr.str(), function, file, line);
101}
102
104 std::lock_guard<std::mutex> lock(mutex);
105 return boost::mpi::all_reduce(m_comm, static_cast<int>(m_errors.size()),
106 std::plus<>());
107}
108
110 return static_cast<int>(std::ranges::count_if(
111 m_errors, [level](auto const &e) { return e.level() >= level; }));
112}
113
115 std::lock_guard<std::mutex> lock(mutex);
116 m_errors.clear();
117}
118
120 {
121 std::lock_guard<std::mutex> lock(mutex);
122 for (auto const &e : m_errors) {
123 std::cerr << e.format() << std::endl;
124 }
125 }
126 this->clear();
127}
128
129std::vector<RuntimeError> RuntimeErrorCollector::gather() {
130 std::lock_guard<std::mutex> lock(mutex);
131 std::vector<RuntimeError> all_errors{};
132 std::swap(all_errors, m_errors);
133
134 Utils::Mpi::gather_buffer(all_errors, m_comm);
135
136 return all_errors;
137}
138
140 {
141 std::lock_guard<std::mutex> lock(mutex);
142 Utils::Mpi::gather_buffer(m_errors, m_comm);
143 }
144 this->clear();
145}
146
147} // 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.