Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
ErrorHandling.cpp
1 /*===-- serialbox-c/ErrorHandling.cpp -----------------------------------------------*- C++ -*-===*\
2  *
3  * S E R I A L B O X
4  *
5  * This file is distributed under terms of BSD license.
6  * See LICENSE.txt for more information
7  *
8  *===------------------------------------------------------------------------------------------===//
9  *
10  *! \file
11  *! This file contains the error handling of the C Interface of Serialbox.
12  *
13 \*===------------------------------------------------------------------------------------------===*/
14 
15 #include "serialbox-c/ErrorHandling.h"
17 #include <cassert>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <string>
22 
23 #include <iostream>
24 
26 
28  assert(handler);
29  FatalErrorHandler = handler;
30 }
31 
33  FatalErrorHandler = serialboxDefaultFatalErrorHandler;
34 }
35 
36 void serialboxFatalError(const char* reason) {
37  assert(FatalErrorHandler);
38  (*FatalErrorHandler)(reason);
39 }
40 
41 /*===------------------------------------------------------------------------------------------===*\
42  * Error Handler Callbacks
43 \*===------------------------------------------------------------------------------------------===*/
44 
45 void serialboxDefaultFatalErrorHandler(const char* reason) {
46  std::fprintf(stderr, "Serialbox: ERROR: %s\n", reason);
47  std::fflush(stderr);
48  std::exit(1);
49 }
50 
51 namespace internal {
52 
53 struct ErrorState {
54  bool hasError = false;
55  std::string errMsg = "";
56 };
57 
58 static ErrorState errorState;
59 
60 } // namespace internal
61 
62 void serialboxStateErrorHandler(const char* reason) {
63  internal::errorState.hasError = true;
64  internal::errorState.errMsg = reason;
65 }
66 
67 int serialboxStateErrorHandlerHasError(void) { return internal::errorState.hasError; }
68 
70  std::size_t size = internal::errorState.errMsg.size() + 1;
71  char* errorMessage = (char*)std::malloc(size * sizeof(char));
72  std::memcpy(errorMessage, internal::errorState.errMsg.c_str(), size);
73  return errorMessage;
74 }
75 
77  internal::errorState.hasError = false;
78  internal::errorState.errMsg.clear();
79 }
void serialboxResetFatalErrorHandler(void)
Reset the fatal error handler.
int serialboxStateErrorHandlerHasError(void)
Check the current error state.
char * serialboxStateErrorHandlerGetErrorMessage(void)
Query the current error state.
void serialboxInstallFatalErrorHandler(serialboxFatalErrorHandler_t handler)
Install a fatal error handler.
void serialboxStateErrorHandler(const char *reason)
Store the the current state of the error which can be queried via serialboxStateErrorHandlerGetLastEr...
void serialboxStateErrorHandlerResetState(void)
Reset the current error state.
void serialboxFatalError(const char *reason)
Report a fatal error.
void serialboxDefaultFatalErrorHandler(const char *reason)
Emit the last error string to stderr and call exit(1)
SERIALBOX_API typedef void(* serialboxFatalErrorHandler_t)(const char *reason)
Error handler callback.
Definition: ErrorHandling.h:44