mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
mxError.cpp
Go to the documentation of this file.
1/** \file mxError.cpp
2 * \author Jared R. Males (jaredmales@gmail.com)
3 * \brief Function definitions for the mxlib error reporting system.
4 * \ingroup error_handling_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2020 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#include "mxError.hpp"
28#include <format>
29
30namespace mx
31{
32
33std::string MXE_CodeToName( int ec )
34{
35
36 switch( ec )
37 {
38 case MXE_INVALIDARG:
39 return MXE_INVALIDARG_NAME;
41 return MXE_INVALIDCONFIG_NAME;
42 case MXE_NOTIMPL:
43 return MXE_NOTIMPL_NAME;
44 case MXE_PARAMNOTSET:
45 return MXE_PARAMNOTSET_NAME;
46 case MXE_ENVNOTSET:
47 return MXE_ENVNOTSET_NAME;
48 case MXE_NOTFOUND:
49 return MXE_NOTFOUND_NAME;
50 case MXE_SIZEERR:
51 return MXE_SIZEERR_NAME;
52 case MXE_ALLOCERR:
53 return MXE_ALLOCERR_NAME;
54 case MXE_FREEERR:
55 return MXE_FREEERR_NAME;
56 case MXE_PARSEERR:
57 return MXE_PARSEERR_NAME;
58 case MXE_FILEOERR:
59 return MXE_FILEOERR_NAME;
60 case MXE_FILEWERR:
61 return MXE_FILEWERR_NAME;
62 case MXE_FILERERR:
63 return MXE_FILERERR_NAME;
64 case MXE_FILECERR:
65 return MXE_FILECERR_NAME;
67 return MXE_FILENOTFOUND_NAME;
68 case MXE_PROCERR:
69 return MXE_PROCERR_NAME;
70 case MXE_TIMEOUT:
71 return MXE_TIMEOUT_NAME;
72 case MXE_LIBERR:
73 return MXE_LIBERR_NAME;
74 case MXE_GNUPLOTERR:
75 return MXE_GNUPLOTERR_NAME;
76 case MXE_LAPACKERR:
77 return MXE_LAPACKERR_NAME;
78 default:
79 return "?";
80 }
81}
82
83std::string MXE_CodeToDescription( int ec )
84{
85
86 switch( ec )
87 {
88 case MXE_INVALIDARG:
89 return MXE_INVALIDARG_MSG;
91 return MXE_INVALIDCONFIG_MSG;
92 case MXE_NOTIMPL:
93 return MXE_NOTIMPL_MSG;
94 case MXE_PARAMNOTSET:
95 return MXE_PARAMNOTSET_MSG;
96 case MXE_ENVNOTSET:
97 return MXE_ENVNOTSET_MSG;
98 case MXE_NOTFOUND:
99 return MXE_NOTFOUND_MSG;
100 case MXE_SIZEERR:
101 return MXE_SIZEERR_MSG;
102 case MXE_ALLOCERR:
103 return MXE_ALLOCERR_MSG;
104 case MXE_FREEERR:
105 return MXE_FREEERR_MSG;
106 case MXE_PARSEERR:
107 return MXE_PARSEERR_MSG;
108 case MXE_FILEOERR:
109 return MXE_FILEOERR_MSG;
110 case MXE_FILEWERR:
111 return MXE_FILEWERR_MSG;
112 case MXE_FILERERR:
113 return MXE_FILERERR_MSG;
114 case MXE_FILECERR:
115 return MXE_FILECERR_MSG;
116 case MXE_FILENOTFOUND:
117 return MXE_FILENOTFOUND_MSG;
118 case MXE_PROCERR:
119 return MXE_PROCERR_MSG;
120 case MXE_TIMEOUT:
121 return MXE_TIMEOUT_MSG;
122 case MXE_LIBERR:
123 return MXE_LIBERR_MSG;
124 case MXE_GNUPLOTERR:
125 return MXE_GNUPLOTERR_MSG;
126 case MXE_LAPACKERR:
127 return MXE_LAPACKERR_MSG;
128 default:
129 return "Unknown mxlib error code.";
130 }
131}
132
133std::string errno_CodeToName( int ec )
134{
135 switch( ec )
136 {
137#ifdef E2BIG
138 case E2BIG:
139 return "E2BIG";
140#endif
141#ifdef EACCES
142 case EACCES:
143 return "EACCES";
144#endif
145#ifdef EADDRINUSE
146 case EADDRINUSE:
147 return "EADDRINUSE";
148#endif
149#ifdef EADDRNOTAVAIL
150 case EADDRNOTAVAIL:
151 return "EADDRNOTAVAIL";
152#endif
153#ifdef EAFNOSUPPORT
154 case EAFNOSUPPORT:
155 return "EAFNOSUPPORT";
156#endif
157#ifdef EAGAIN
158 case EAGAIN:
159 #if ( EWOULDBLOCK == EAGAIN )
160 return "EAGIAN / EWOULDBLOCK";
161 #else
162 return "EAGAIN";
163 #endif
164#endif
165#ifdef EALREADY
166 case EALREADY:
167 return "EALREADY";
168#endif
169#ifdef EBADF
170 case EBADF:
171 return "EBADF";
172#endif
173#ifdef EBADMSG
174 case EBADMSG:
175 return "EBADMSG";
176#endif
177#ifdef EBUSY
178 case EBUSY:
179 return "EBUSY";
180#endif
181#ifdef ECANCELED
182 case ECANCELED:
183 return "ECANCELED";
184#endif
185#ifdef ECHILD
186 case ECHILD:
187 return "ECHILD";
188#endif
189#ifdef ECONNABORTED
190 case ECONNABORTED:
191 return "ECONNABORTED";
192#endif
193#ifdef ECONNREFUSED
194 case ECONNREFUSED:
195 return "ECONNREFUSED";
196#endif
197#ifdef ECONNRESET
198 case ECONNRESET:
199 return "ECONNRESET";
200#endif
201#ifdef EDESTADDRREQ
202 case EDESTADDRREQ:
203 return "EDESTADDRREQ";
204#endif
205#ifdef EDOM
206 case EDOM:
207 return "EDOM";
208#endif
209#ifdef EEXIST
210 case EEXIST:
211 return "EEXIST";
212#endif
213#ifdef EFAULT
214 case EFAULT:
215 return "EFAULT";
216#endif
217#ifdef EFBIG
218 case EFBIG:
219 return "EFBIG";
220#endif
221#ifdef EHOSTUNREACH
222 case EHOSTUNREACH:
223 return "EHOSTUNREACH";
224#endif
225#ifdef EIDRM
226 case EIDRM:
227 return "EIDRM";
228#endif
229#ifdef EILSEQ
230 case EILSEQ:
231 return "EILSEQ";
232#endif
233#ifdef EINPROGRESS
234 case EINPROGRESS:
235 return "EINPROGRESS";
236#endif
237#ifdef EINTR
238 case EINTR:
239 return "EINTR";
240#endif
241#ifdef EINVAL
242 case EINVAL:
243 return "EINVAL";
244#endif
245#ifdef EIO
246 case EIO:
247 return "EIO";
248#endif
249#ifdef EISCONN
250 case EISCONN:
251 return "EISCONN";
252#endif
253#ifdef EISDIR
254 case EISDIR:
255 return "EISDIR";
256#endif
257#ifdef ELOOP
258 case ELOOP:
259 return "ELOOP";
260#endif
261#ifdef EMFILE
262 case EMFILE:
263 return "EMFILE";
264#endif
265#ifdef EMLINK
266 case EMLINK:
267 return "EMLINK";
268#endif
269#ifdef EMSGSIZE
270 case EMSGSIZE:
271 return "EMSGSIZE";
272#endif
273#ifdef ENAMETOOLONG
274 case ENAMETOOLONG:
275 return "ENAMETOOLONG";
276#endif
277#ifdef ENETDOWN
278 case ENETDOWN:
279 return "ENETDOWN";
280#endif
281#ifdef ENETRESET
282 case ENETRESET:
283 return "ENETRESET";
284#endif
285#ifdef ENETUNREACH
286 case ENETUNREACH:
287 return "ENETUNREACH";
288#endif
289#ifdef ENFILE
290 case ENFILE:
291 return "ENFILE";
292#endif
293#ifdef ENOBUFS
294 case ENOBUFS:
295 return "ENOBUFS";
296#endif
297#ifdef ENODATA
298 case ENODATA:
299 return "ENODATA";
300#endif
301#ifdef ENODEV
302 case ENODEV:
303 return "ENODEV";
304#endif
305#ifdef ENOENT
306 case ENOENT:
307 return "ENOENT";
308#endif
309#ifdef ENOEXEC
310 case ENOEXEC:
311 return "ENOEXEC";
312#endif
313#ifdef ENOLCK
314 case ENOLCK:
315 return "ENOLCK";
316#endif
317#ifdef ENOLINK
318 case ENOLINK:
319 return "ENOLINK";
320#endif
321#ifdef ENOMEM
322 case ENOMEM:
323 return "ENOMEM";
324#endif
325#ifdef ENOMSG
326 case ENOMSG:
327 return "ENOMSG";
328#endif
329#ifdef ENOPROTOOPT
330 case ENOPROTOOPT:
331 return "ENOPROTOOPT";
332#endif
333#ifdef ENOSPC
334 case ENOSPC:
335 return "ENOSPC";
336#endif
337#ifdef ENOSR
338 case ENOSR:
339 return "ENOSR";
340#endif
341#ifdef ENOSTR
342 case ENOSTR:
343 return "ENOSTR";
344#endif
345#ifdef ENOSYS
346 case ENOSYS:
347 return "ENOSYS";
348#endif
349#ifdef ENOTCONN
350 case ENOTCONN:
351 return "ENOTCONN";
352#endif
353#ifdef ENOTDIR
354 case ENOTDIR:
355 return "ENOTDIR";
356#endif
357#ifdef ENOTEMPTY
358 case ENOTEMPTY:
359 return "ENOTEMPTY";
360#endif
361#ifdef ENOTRECOVERABLE
362 case ENOTRECOVERABLE:
363 return "ENOTRECOVERABLE";
364#endif
365#ifdef ENOTSOCK
366 case ENOTSOCK:
367 return "ENOTSOCK";
368#endif
369#ifdef ENOTSUP
370 #if ( ENOTSUP != EOPNOTSUPP )
371 case ENOTSUP:
372 return "ENOTSUP";
373 #endif
374#endif
375#ifdef ENOTTY
376 case ENOTTY:
377 return "ENOTTY";
378#endif
379#ifdef ENXIO
380 case ENXIO:
381 return "ENXIO";
382#endif
383#ifdef EOPNOTSUPP
384 case EOPNOTSUPP:
385 return "EOPNOTSUPP";
386#endif
387#ifdef EOVERFLOW
388 case EOVERFLOW:
389 return "EOVERFLOW";
390#endif
391#ifdef EOWNERDEAD
392 case EOWNERDEAD:
393 return "EOWNERDEAD";
394#endif
395#ifdef EPERM
396 case EPERM:
397 return "EPERM";
398#endif
399#ifdef EPIPE
400 case EPIPE:
401 return "EPIPE";
402#endif
403#ifdef EPROTO
404 case EPROTO:
405 return "EPROTO";
406#endif
407#ifdef EPROTONOSUPPORT
408 case EPROTONOSUPPORT:
409 return "EPROTONOSUPPORT";
410#endif
411#ifdef EPROTOTYPE
412 case EPROTOTYPE:
413 return "EPROTOTYPE";
414#endif
415#ifdef ERANGE
416 case ERANGE:
417 return "ERANGE";
418#endif
419#ifdef EROFS
420 case EROFS:
421 return "EROFS";
422#endif
423#ifdef ESPIPE
424 case ESPIPE:
425 return "ESPIPE";
426#endif
427#ifdef ESRCH
428 case ESRCH:
429 return "ESRCH";
430#endif
431#ifdef ETIME
432 case ETIME:
433 return "ETIME";
434#endif
435#ifdef ETIMEDOUT
436 case ETIMEDOUT:
437 return "ETIMEDOUT";
438#endif
439#ifdef ETXTBSY
440 case ETXTBSY:
441 return "ETXTBSY";
442#endif
443#ifdef EWOULDBLOCK
444 #if ( EWOULDBLOCK != EAGAIN )
445 case EWOULDBLOCK:
446 return "EWOULDBLOCK";
447 #endif
448#endif
449#ifdef EXDEV
450 case EXDEV:
451 return "EXDEV";
452#endif
453 default:
454 return "?";
455 }
456}
457
458std::string error_report(
459 const std::string &source, const int &code, const std::string &file, const int &line, const std::string &expl )
460{
461
462 std::string codeName, codeMessage;
463
464 codeName = MXE_CodeToName( code );
465 codeMessage = MXE_CodeToDescription( code );
466
467 std::ostringstream s;
468 s.str( "" );
469
470 s << "An error has occured in an mxlib component.\n";
471 s << " source: " << source << "\n";
472 s << " code: " << codeName << "(" << code << ")\n";
473 s << " code msg: " << codeMessage << "\n";
474 s << " in file: " << file << "\n";
475 s << " at line: " << line << "\n";
476 if( expl != "" )
477 s << " explanation: " << expl << "\n";
478
479 return s.str();
480}
481
482std::string
483errno_report( const std::string &source, int ec, const std::string &file, const int &line, const std::string &expl )
484{
485 std::string codeName, codeMessage;
486
487 codeName = errno_CodeToName( ec );
488 codeMessage = strerror( ec );
489
490 std::ostringstream s;
491 s.str( "" );
492
493 s << "An error has occured in an mxlib component.\n";
494 s << " source: " << source << "\n";
495 s << " errno code: " << codeName << "(" << ec << ")\n";
496 s << " code msg: " << codeMessage << "\n";
497 s << " in file: " << file << "\n";
498 s << " at line: " << line << "\n";
499 if( expl != "" )
500 s << " explanation: " << expl << "\n";
501
502 return s.str();
503}
504
505namespace internal
506{
507
508template <>
509std::string mxlib_error_message<verbose::o>( [[maybe_unused]] const error_t &code,
510 [[maybe_unused]] const std::string &expl,
511 [[maybe_unused]] const std::source_location &loc )
512{
513 return "";
514}
515
516template <>
518 const std::string &expl,
519 [[maybe_unused]] const std::source_location &loc )
520{
521 return std::format( "{}: {}.", errorName( code ), expl );
522}
523
524template <>
525std::string
526mxlib_error_message<verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc )
527{
528 return std::format( "{} ({}): {}. [{} {}]",
529 errorMessage( code ),
530 errorName( code ),
531 expl,
532 loc.file_name(),
533 loc.line() );
534}
535
536template <>
537std::string
538mxlib_error_message<verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc )
539{
540 return std::format( "An error has occurred in mxlib:\n"
541 " error: {} ({})\n"
542 " explanation: {}\n"
543 " in file: {}\n"
544 " at line: {}\n"
545 " source: {}\n",
546 errorMessage( code ),
547 errorName( code ),
548 expl,
549 loc.file_name(),
550 loc.line(),
551 loc.function_name() );
552}
553
554template <>
555std::string mxlib_error_message<verbose::o>( const error_t &code, [[maybe_unused]] const std::source_location &loc )
556{
557 return "";
558}
559
560template <>
561std::string mxlib_error_message<verbose::v>( const error_t &code, [[maybe_unused]] const std::source_location &loc )
562{
563 return errorName( code );
564}
565
566template <>
567std::string mxlib_error_message<verbose::vv>( const error_t &code, const std::source_location &loc )
568{
569 return std::format( "{} ({}). [{} {}]", errorMessage( code ), errorName( code ), loc.file_name(), loc.line() );
570}
571
572template <>
573std::string mxlib_error_message<verbose::vvv>( const error_t &code, const std::source_location &loc )
574{
575 return std::format( "An error has occurred in mxlib:\n"
576 " error: {} ({})\n"
577 " in file: {}\n"
578 " at line: {}\n"
579 " source: {}\n",
580 errorMessage( code ),
581 errorName( code ),
582 loc.file_name(),
583 loc.line(),
584 loc.function_name() );
585}
586
587template <>
589 [[maybe_unused]] const std::string &expl,
590 [[maybe_unused]] const std::source_location &loc )
591{
592 return code;
593}
594
595template <>
596error_t mxlib_error_report<verbose::o>( const error_t &code, [[maybe_unused]] const std::source_location &loc )
597{
598 return code;
599}
600
601
602} // namespace internal
603
604template <>
605std::string error_message<verbose::o>( const error_t &code,
606 [[maybe_unused]] const std::string &expl,
607 [[maybe_unused]] const std::source_location &loc )
608{
609 return "";
610}
611
612template <>
613std::string error_message<verbose::v>( const error_t &code, const std::string &expl, const std::source_location &loc )
614{
615 return internal::mxlib_error_message<verbose::v>( code, expl, loc );
616}
617
618template <>
619std::string error_message<verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc )
620{
621 return internal::mxlib_error_message<verbose::vv>( code, expl, loc );
622}
623
624template <>
625std::string error_message<verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc )
626{
627 return std::format( "An error has occurred:\n"
628 " error: {} ({})\n"
629 " explanation: {}\n"
630 " in file: {}\n"
631 " at line: {}\n"
632 " source: {}\n",
633 errorMessage( code ),
634 errorName( code ),
635 expl,
636 loc.file_name(),
637 loc.line(),
638 loc.function_name() );
639}
640
641template <>
642std::string error_message<verbose::o>( [[maybe_unused]] const error_t &code,
643 [[maybe_unused]] const std::source_location &loc )
644{
645 return "";
646}
647
648template <>
649std::string error_message<verbose::v>( const error_t &code, const std::source_location &loc )
650{
652}
653
654template <>
655std::string error_message<verbose::vv>( const error_t &code, const std::source_location &loc )
656{
658}
659
660template <>
661std::string error_message<verbose::vvv>( const error_t &code, const std::source_location &loc )
662{
663 return std::format( "An error has occurred:\n"
664 " error: {} ({})\n"
665 " in file: {}\n"
666 " at line: {}\n"
667 " source: {}\n",
668 errorMessage( code ),
669 errorName( code ),
670 loc.file_name(),
671 loc.line(),
672 loc.function_name() );
673}
674
675template <>
677 [[maybe_unused]] const std::string &expl,
678 [[maybe_unused]] const std::source_location &loc )
679{
680 return code;
681}
682
683template <>
684error_t error_report<verbose::o>( const error_t &code, [[maybe_unused]] const std::source_location &loc )
685{
686 return code;
687}
688
689} // namespace mx
error_t
The mxlib error codes.
Definition error_t.hpp:20
static constexpr const char * errorName(const error_t &errc)
Convert an error_t code to its name.
Definition error_t.hpp:140
static constexpr const char * errorMessage(const error_t &errc)
Get the descriptive message for an error_t code.
Definition error_t.hpp:377
std::string MXE_CodeToName(int ec)
Return the name for an mxlib error code.
Definition mxError.cpp:33
std::string errno_CodeToName(int ec)
Return the macro name and a message for a standard errno code.
Definition mxError.cpp:133
std::string MXE_CodeToDescription(int ec)
Return the description for an mxlib error code.
Definition mxError.cpp:83
std::string errno_report(const std::string &source, int ec, const std::string &file, const int &line, const std::string &expl="")
Construct a rich error report given a standard errno error code.
Definition mxError.cpp:483
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< 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
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
#define MXE_PROCERR
An error occrred while starting a process.
#define MXE_FILERERR
An error occurred while reading from a file.
#define MXE_INVALIDCONFIG
A config setting was invalid.
#define MXE_INVALIDARG
An argument was invalid.
#define MXE_FILENOTFOUND
The file was not found.
#define MXE_LAPACKERR
An error was returned by Lapack.
#define MXE_LIBERR
An error was returned by a library.
#define MXE_ENVNOTSET
An environment variable is not set.
#define MXE_GNUPLOTERR
An error was returned by gnuplot.
#define MXE_ALLOCERR
An error occurred during memory allocation.
#define MXE_SIZEERR
A size was invalid or calculated incorrectly.
#define MXE_FILEWERR
An error occurred while writing to a file.
#define MXE_PARAMNOTSET
A parameter was not set.
#define MXE_FILECERR
An error occurred while closing a file.
#define MXE_NOTIMPL
A component or technique is not implemented.
#define MXE_PARSEERR
A parsing error occurred.
#define MXE_FILEOERR
An error occurred while opening a file.
#define MXE_TIMEOUT
A timeout occurred.
#define MXE_FREEERR
An error occurred during memory de-allocation.
#define MXE_NOTFOUND
An item was not found.
The mxlib error reporting system.
The mxlib c++ namespace.
Definition mxError.hpp:40