mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
mxError.hpp
Go to the documentation of this file.
1/** \file mxError.hpp
2 * \brief The mxlib error reporting system.
3 * \ingroup error_handling_files
4 *
5 */
6
7//***********************************************************************//
8// Copyright 2025 Jared R. Males (jaredmales@gmail.com)
9//
10// This file is part of mxlib.
11//
12// mxlib is free software: you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation, either version 3 of the License, or
15// (at your option) any later version.
16//
17// mxlib is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
24//***********************************************************************//
25
26#ifndef __mxError__
27#define __mxError__
28
29#include <cerrno>
30#include <cstring>
31#include <sstream>
32#include <source_location>
33#include <iostream>
34#include <string>
35
36#include "error/mxErrorOld.hpp"
37#include "error/error_t.hpp"
38
39namespace mx
40{
41
42/** \defgroup error_verbosity Error Report Verbosity
43 * \ingroup error_handling
44 *
45 * Error reports can be controlled with a verbosity template parameter,
46 * usually called `verboseT`. The types in this namespace are the standard
47 * ones supported by `mxlib`.
48 *
49 */
50
51/// Namespace for the error reporting verbosity levels
52/**
53 * \ingroup error_verbosity
54 */
55namespace verbose
56{
57
58/// Verbosity level 0, no reports are generated or printed to stderr.
59/** o stands for off
60 *
61 * \ingroup error_verbosity
62 */
63struct o
64{
65 static constexpr int level = 0;
66};
67
68/// Verbosity level 1. Minimal reports with no source location information.
69/** A typical example:
70 * \verbatim
71 dirnotfound: /tmp/fileUtils_test/dirnf was not found.
72 * \endverbatim
73 *
74 * \ingroup error_verbosity
75 */
76struct v
77{
78 static constexpr int level = 1;
79};
80
81/// Verbosity level 2. Additional information is provided, including source file and line
82/** A typical example:
83 * \verbatim
84 The directory was not found (dirnotfound): /tmp/fileUtils_test/dirnf was not found. [ioutils/fileUtils.cpp 186]
85 * \endverbatim
86 *
87 * \ingroup error_verbosity
88 */
89struct vv
90{
91 static constexpr int level = 2;
92};
93
94/// Verbosity level 3. A full report is provided.
95/** A typical example:
96 * \verbatim
97 An error has occurred in mxlib:
98 error: The directory was not found (dirnotfound)
99 explanation: /tmp/fileUtils_test/dirnf was not found
100 in file: ioutils/fileUtils.cpp
101 at line: 186
102 source: mx::error_t mx::ioutils::impl::getFileNames(std::vector<std::__cxx11::basic_string<char> >&,
103 const std::string&, const std::string&, const std::string&, const std::string&) [with verboseT =
104 mx::verbose::vvv; std::string = std::__cxx11::basic_string<char>]
105 \endverbatim
106 *
107 * \ingroup error_verbosity
108 */
109struct vvv
110{
111 static constexpr int level = 3;
112};
113
114} // namespace verbose
115
116/** \defgroup error_internal Internal Error Reporting
117 * \ingroup error_handling
118 *
119 * These functions are meant to be used by mxlib itself. You should use the non-`internal` versions,
120 * which don't start with `mxlib_`, in most
121 * other cases.
122 */
123
124/// Namespace for the internal error reporting functions.
125/**
126 * \ingroup error_internal
127 */
128namespace internal
129{
130
131/// Format a report given an mxlib \ref error_t code and explanation.
132/** What is included depends on the verbosity level set by the template parameter
133 * This is for internal `mxlib` use, it includes `mxlib` in the vvv report.
134 *
135 * \tparam verboseT sets the verbosity level based on its `level` member.
136 *
137 * \returns the formatted message
138 *
139 * \ingroup error_internal
140 */
141template <class verboseT>
142std::string mxlib_error_message( const error_t &code, /**< [in] is an mx::error_t error code*/
143 const std::string &expl, /**< [in] [optional] if more information can be provided,
144 use this to inform the user.*/
145 const std::source_location &loc /**< [in] [optional] source location */
146 = std::source_location::current() );
147
148/// Specialization of \ref mxlib_error_message for \ref verbose::o
149/**
150 * \ingroup error_internal
151 */
152template <>
153std::string
154mxlib_error_message<verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
155
156/// Specialization of \ref mxlib_error_message for \ref verbose::v
157/**
158 * \ingroup error_internal
159 */
160template <>
161std::string
162mxlib_error_message<verbose::v>( const error_t &code, const std::string &expl, const std::source_location &loc );
163
164/// Specialization of \ref mxlib_error_message for \ref verbose::vv
165/**
166 * \ingroup error_internal
167 */
168template <>
169std::string
170mxlib_error_message<verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc );
171
172/// Specialization of \ref mxlib_error_message for \ref verbose::vvv
173/**
174 * \ingroup error_internal
175 */
176template <>
177std::string
178mxlib_error_message<verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc );
179
180/// Format a report given an mxlib \ref error_t code.
181/** What is included depends on the verbosity level set by the template parameter
182 * This is for internal mxlib use, it includes `mxlib` in the vvv report.
183 *
184 * \tparam verboseT sets the verbosity level based on its `level` member.
185 *
186 * \returns the formatted message
187 *
188 * \ingroup error_internal
189 */
190template <class verboseT>
191std::string mxlib_error_message( const error_t &code, /**< [in] is an mx::error_t error code*/
192 const std::source_location &loc /**< [in] [optional] source location */
193 = std::source_location::current() );
194
195/** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
196 * "mxlib_error_message" for
197 * \ref verbose::o
198 *
199 * \ingroup error_internal
200 */
201template <>
202std::string mxlib_error_message<verbose::o>( const error_t &code, const std::source_location &loc );
203
204/** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
205 * "mxlib_error_message" for \ref verbose::v
206 *
207 * \ingroup error_internal
208 */
209template <>
210std::string mxlib_error_message<verbose::v>( const error_t &code, const std::source_location &loc );
211
212/** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
213 * "mxlib_error_message" for \ref verbose::vv
214 *
215 * \ingroup error_internal
216 */
217template <>
218std::string mxlib_error_message<verbose::vv>( const error_t &code, const std::source_location &loc );
219
220/** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
221 * "mxlib_error_message" for \ref verbose::vvv
222 *
223 * \ingroup error_internal
224 */
225template <>
226std::string mxlib_error_message<verbose::vvv>( const error_t &code, const std::source_location &loc );
227
228/// Print a report to stderr given an mxlib \ref error_t \p code and explanation and return the \p code.
229/** What is printed depends on the verbosity level set by the template parameter
230 * This is for internal mxlib use, it includes `mxlib` in the vvv report.
231 *
232 * \tparam verboseT sets the verbosity level based on its `level` member.
233 *
234 * \returns the provided \ref error_t \p code
235 *
236 * \ingroup error_internal
237 */
238template <class verboseT>
239error_t mxlib_error_report( const error_t &code, /**< [in] is an mx::error_t error code*/
240 const std::string &expl, /**< [in] [optional] if more information can be provided,
241 use this to inform the user.*/
242 const std::source_location &loc /**< [in] [optional] source location */
243 = std::source_location::current() )
244{
245 if( verboseT::level > 0 )
246 {
247 std::cerr << mxlib_error_message<verboseT>( code, expl, loc ) << '\n';
248 }
249
250 return code;
251}
252
253/// Specialization of \ref mxlib_error_report for \ref verbose::o
254/**
255 * \ingroup error_internal
256 */
257template <>
258error_t mxlib_error_report<verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
259
260/// Print a report to stderr given an mxlib \ref error_t \p code and return the \p code.
261/** What is printed depends on the verbosity level set by the template parameter
262 * This is for internal mxlib use, it includes `mxlib` in the vvv report.
263 *
264 * \tparam verboseT sets the verbosity level based on its `level` member.
265 *
266 * \returns the provided \ref error_t \p code
267 *
268 * \ingroup error_internal
269 */
270template <class verboseT>
271error_t mxlib_error_report( const error_t &code /**< [in] is an mx::error_t error code*/,
272 const std::source_location &loc /**< [in] [optional] source location */
273 = std::source_location::current() )
274{
275 if( verboseT::level > 0 )
276 {
277 std::cerr << mxlib_error_message<verboseT>( code, loc ) << '\n';
278 }
279
280 return code;
281}
282
283/** \brief Specialization of \ref mxlib_error_report(const error_t &, const std::source_location&) "mxlib_error_report"
284 * for \ref verbose::o
285 *
286 * \ingroup error_internal
287 */
288template <>
289error_t mxlib_error_report<verbose::o>( const error_t &code, const std::source_location &loc );
290
291} // namespace internal
292
293/// Format a report given an mxlib \ref error_t \p code and explanation.
294/** What is included depends on the verbosity level set by the template parameter
295 *
296 * \tparam verboseT sets the verbosity level based on its `level` member.
297 *
298 * \returns the formatted message
299 *
300 * \ingroup error_handling
301 */
302template <class verboseT>
303std::string error_message( const error_t &code, /**< [in] is an mx::error_t error code*/
304 const std::string &expl, /**< [in] [optional] if more information can be provided,
305 use this to inform the user.*/
306 const std::source_location &loc /**< [in] [optional] source location */
307 = std::source_location::current() );
308
309/// Specialization of \ref error_message for \ref verbose::o
310/**
311 * \ingroup error_handling
312 */
313template <>
314std::string error_message<verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
315
316/// Specialization of \ref error_message for \ref verbose::v
317/**
318 * \ingroup error_handling
319 */
320template <>
321std::string error_message<verbose::v>( const error_t &code, const std::string &expl, const std::source_location &loc );
322
323/// Specialization of \ref error_message for \ref verbose::vv
324/**
325 * \ingroup error_handling
326 */
327template <>
328std::string error_message<verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc );
329
330/// Specialization of \ref error_message for \ref verbose::vvv
331/**
332 * \ingroup error_handling
333 */
334template <>
335std::string
336error_message<verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc );
337
338/// Format a report given an mxlib \ref error_t \p code.
339/** What is included depends on the verbosity level set by the template parameter
340 *
341 * \tparam verboseT sets the verbosity level based on its `level` member.
342 *
343 * \returns the formatted message
344 *
345 * \ingroup error_handling
346 */
347template <class verboseT>
348std::string error_message( const error_t &code, /**< [in] is an mx::error_t error code*/
349 const std::source_location &loc /**< [in] [optional] source location */
350 = std::source_location::current() );
351
352/** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
353 * verbose::o
354 *
355 * \ingroup error_handling
356 */
357template <>
358std::string error_message<verbose::o>( const error_t &code, const std::source_location &loc );
359
360/** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
361 * verbose::v
362 *
363 * \ingroup error_handling
364 */
365template <>
366std::string error_message<verbose::v>( const error_t &code, const std::source_location &loc );
367
368/** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
369 * verbose::vv
370 *
371 * \ingroup error_handling
372 */
373template <>
374std::string error_message<verbose::vv>( const error_t &code, const std::source_location &loc );
375
376/** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
377 * verbose::vvv
378 *
379 * \ingroup error_handling
380 */
381template <>
382std::string error_message<verbose::vvv>( const error_t &code, const std::source_location &loc );
383
384/// Print a report to stderr given an mxlib \ref error_t \p code and explanation and return the \p code.
385/** What is printed depends on the verbosity level set by the template parameter
386 *
387 * \tparam verboseT sets the verbosity level based on its `level` member.
388 *
389 * \returns the provided \ref error_t \p code
390 *
391 * \ingroup error_handling
392 */
393template <class verboseT>
394error_t error_report( const error_t &code, /**< [in] is an mx::error_t error code*/
395 const std::string &expl, /**< [in] [optional] if more information can be provided,
396 use this to inform the user.*/
397 const std::source_location &loc /**< [in] [optional] source location */
398 = std::source_location::current() )
399{
400 if( verboseT::level > 0 )
401 {
402 std::cerr << error_message<verboseT>( code, expl, loc ) << '\n';
403 }
404
405 return code;
406}
407
408/** \brief Specialization of \ref error_report for \ref verbose::o
409 *
410 * \ingroup error_handling
411 */
412template <>
413error_t error_report<verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
414
415/// Print a report to stderr given an mxlib \ref error_t \p code and return the \p code.
416/** What is printed depends on the verbosity level set by the template parameter
417 *
418 * \tparam verboseT sets the verbosity level based on its `level` member.
419 *
420 * \returns the provided \ref error_t \p code
421 *
422 * \ingroup error_handling
423 */
424template <class verboseT>
425error_t error_report( const error_t &code, /**< [in] is an mx::error_t error code*/
426 const std::source_location &loc /**< [in] [optional] source location */
427 = std::source_location::current() )
428{
429 if( verboseT::level > 0 )
430 {
431 std::cerr << error_message<verboseT>( code, loc ) << '\n';
432 }
433
434 return code;
435}
436
437/** \brief Specialization of \ref error_report(const error_t &, const std::source_location&) "error_report"
438 * for \ref verbose::o
439 *
440 * \ingroup error_handling
441 */
442template <>
443error_t error_report<verbose::o>( const error_t &code, const std::source_location &loc );
444
445} // namespace mx
446
447#endif //__mxError__
The mxlib error_t type and utilities.
error_t
The mxlib error codes.
Definition error_t.hpp:20
std::string error_message< verbose::v >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::v.
Definition mxError.cpp:613
std::string error_message< verbose::vv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::vv.
Definition mxError.cpp:619
error_t error_report< verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_report for verbose::o.
Definition mxError.cpp:676
std::string error_message< verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::o.
Definition mxError.cpp:605
error_t error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition mxError.hpp:394
std::string error_message(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Format a report given an mxlib error_t code and explanation.
std::string error_message< verbose::vvv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of error_message for verbose::vvv.
Definition mxError.cpp:625
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition mxError.hpp:239
std::string mxlib_error_message< verbose::v >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::v.
Definition mxError.cpp:517
std::string mxlib_error_message< verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::o.
Definition mxError.cpp:509
std::string mxlib_error_message< verbose::vv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::vv.
Definition mxError.cpp:526
std::string mxlib_error_message< verbose::vvv >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_message for verbose::vvv.
Definition mxError.cpp:538
error_t mxlib_error_report< verbose::o >(const error_t &code, const std::string &expl, const std::source_location &loc)
Specialization of mxlib_error_report for verbose::o.
Definition mxError.cpp:588
std::string mxlib_error_message(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Format a report given an mxlib error_t code and explanation.
Old version. Deprecated. Declares and defines the mxlib error reporting system.
The mxlib c++ namespace.
Definition mxError.hpp:40
Verbosity level 0, no reports are generated or printed to stderr.
Definition mxError.hpp:64
Verbosity level 1. Minimal reports with no source location information.
Definition mxError.hpp:77
Verbosity level 2. Additional information is provided, including source file and line.
Definition mxError.hpp:90
Verbosity level 3. A full report is provided.
Definition mxError.hpp:110