-
Notifications
You must be signed in to change notification settings - Fork 31
/
Assert.cc
156 lines (141 loc) · 4.41 KB
/
Assert.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/Assert.cc
//---------------------------------------------------------------------------//
#include "Assert.hh"
#include "Macros.hh"
#if CELERITAS_USE_MPI
# include <mpi.h>
#endif
#include <sstream>
#include <utility>
#include "io/ColorUtils.hh"
#include "sys/Environment.hh" // IWYU pragma: keep
namespace celeritas
{
namespace
{
//---------------------------------------------------------------------------//
/*!
* Construct a debug assertion message for printing.
*/
std::string build_debug_error_msg(DebugErrorDetails const& d)
{
std::ostringstream msg;
// clang-format off
msg << color_code('W') << d.file << ':' << d.line << ':'
<< color_code(' ') << "\nceleritas: "
<< color_code('R') << to_cstring(d.which);
// clang-format on
if (d.which != DebugErrorType::unreachable)
{
msg << ": " << color_code('x') << d.condition;
}
msg << color_code(' ');
return msg.str();
}
//---------------------------------------------------------------------------//
/*!
* Construct a runtime assertion message for printing.
*/
std::string build_runtime_error_msg(RuntimeErrorDetails const& d)
{
static bool const verbose_message = [] {
#if CELERITAS_DEBUG
// Always verbose if debug flags are enabled
return true;
#else
// Verbose if the CELER_LOG environment variable is defined
return !celeritas::getenv("CELER_LOG").empty();
#endif
}();
std::ostringstream msg;
msg << "celeritas: " << color_code('R')
<< (d.which.empty() ? "unknown" : d.which)
<< " error: " << color_code(' ');
if (d.which == "configuration")
{
msg << "required dependency is disabled in this build: ";
}
else if (d.which == "not implemented")
{
msg << "feature is not yet implemented: ";
}
msg << d.what;
if (verbose_message || d.what.empty() || d.which != "runtime")
{
msg << '\n'
<< color_code('W') << (d.file.empty() ? "unknown source" : d.file);
if (d.line)
{
msg << ':' << d.line;
}
if (!d.condition.empty())
{
msg << ':' << color_code(' ') << " '" << d.condition << "' failed";
}
}
return msg.str();
}
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Get a human-readable string describing a debug error.
*/
char const* to_cstring(DebugErrorType which)
{
switch (which)
{
case DebugErrorType::precondition:
return "precondition failed";
case DebugErrorType::internal:
return "internal assertion failed";
case DebugErrorType::unreachable:
return "unreachable code point";
case DebugErrorType::postcondition:
return "postcondition failed";
case DebugErrorType::assumption:
return "assumption failed";
}
return "";
}
//---------------------------------------------------------------------------//
/*!
* Get an MPI error string.
*/
std::string mpi_error_to_string(int errorcode)
{
#if CELERITAS_USE_MPI
std::string error_string;
error_string.resize(MPI_MAX_ERROR_STRING);
int length = 0;
MPI_Error_string(errorcode, &error_string.front(), &length);
error_string.resize(length);
return error_string;
#else
CELER_DISCARD(errorcode);
CELER_NOT_CONFIGURED("MPI");
#endif
}
//---------------------------------------------------------------------------//
/*!
* Construct a debug exception from detailed attributes.
*/
DebugError::DebugError(DebugErrorDetails&& d)
: std::logic_error(build_debug_error_msg(d)), details_(std::move(d))
{
}
//---------------------------------------------------------------------------//
/*!
* Construct a runtime error from detailed descriptions.
*/
RuntimeError::RuntimeError(RuntimeErrorDetails&& d)
: std::runtime_error(build_runtime_error_msg(d)), details_(std::move(d))
{
}
//---------------------------------------------------------------------------//
} // namespace celeritas