KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
err.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : errors.h
11  * Created : 21 mrt. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #ifndef ERRORS_H_
17 #define ERRORS_H_
18 
19 /**
20  * @file
21  *
22  * @ingroup kernel
23  *
24  * Manages the global system error.
25  */
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include "util/macro.h"
30 
31 // #define TRACE_ERROR
32 
33 extern const char * _errName;
34 
35 
36 /**
37  * Sets an error.
38  *
39  * @param code The error code
40  * @param error The error description
41  * @param name The module where the error was generated
42  *
43  * @return Always false. For compact code: `return errSet(...)`
44  */
45 bool errSet(uint32_t code, const char * error, const char * name);
46 
47 
48 // For tracing errors -> increases .cdata section.
49 // -> displays the location in the software for each error.
50 #ifdef TRACE_ERROR
51 #define _errSet(CODE, ERROR) \
52  errSet(CODE, ERROR __FILE__ "(" STR(__LINE__) ")" )
53 
54 #define errSet(...) \
55  _errSet(__VA_ARGS__)
56 #endif
57 
58 
59 /**
60  * Clears the current error.
61  */
62 void errClear();
63 
64 /**
65  * Returns whether there is an error pending.
66  */
67 bool errHas();
68 
69 
70 /**
71  * Returns the last error description, if any, else null.
72  */
73 const char * errGetDescr();
74 
75 /**
76  * Returns the last error cause name, or null.
77  */
78 const char * errGetName();
79 
80 /**
81  * Returns the last error code, or null.
82  */
83 uint32_t errGet();
84 
85 /**
86  * Prints the last error.
87  *
88  * @param clear When true, clears the error state.
89  */
90 void errPrint(bool clear);
91 
92 /**
93  * Prints the last error and halts the system. If there is no error, the system will still be
94  * halted with an unknown error.
95  */
96 void errFatal();
97 
98 /**
99  * Rebases the cause of the error message.
100  *
101  * @param name New module reporting the error
102  * @return Always returns false
103  */
104 static inline bool errRebase(const char * name)
105 {
106  _errName = name;
107  return false;
108 }
109 
110 /**
111  * Transparent conditional error rebase.
112  *
113  * @param err The result of the error function
114  * @param name The name of the new error base
115  * @return Same as err
116  */
117 static inline bool errCondRebase(bool err, const char * name)
118 {
119  if (err) return true;
120  return errRebase(name);
121 }
122 
123 
124 
125 #endif /* ERRORS_H_ */
bool errHas()
Returns whether there is an error pending.
Definition: err.c:52
const char * errGetDescr()
Returns the last error description, if any, else null.
Definition: err.c:57
void errFatal()
Prints the last error and halts the system.
Definition: err.c:97
static bool errCondRebase(bool err, const char *name)
Transparent conditional error rebase.
Definition: err.h:117
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
void errClear()
Clears the current error.
Definition: err.c:46
uint32_t errGet()
Returns the last error code, or null.
Definition: err.c:74
static bool errRebase(const char *name)
Rebases the cause of the error message.
Definition: err.h:104
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
const char * errGetName()
Returns the last error cause name, or null.
Definition: err.c:65
Provides common macros.