KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
err.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : err.c
11  * Created : 21 mrt. 2013
12  * Author : Vincent van Beveren
13  */
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <stdio.h>
18 
19 #include <util/log.h>
20 #include <kernel/err.h>
21 
22 LOG_DEF(Error)
23 
24 #define _ERR_MAX_BUF_SIZE 128
25 
26 static uint32_t _errCode;
27 static const char * _errDescr;
28 const char * _errName;
29 
30 #ifdef TRACE_ERROR
31 #undef errSet
32 #endif
33 
34 bool errSet(uint32_t code, const char * descr, const char * name)
35 {
36  if (_errCode != 0) return false;
37  if (descr != 0) _errDescr = descr;
38  else _errDescr = "";
39  _errName = name;
40  _errCode = code;
41 
42  return false;
43 }
44 
45 
46 void errClear()
47 {
48  _errCode = 0;
49  _errDescr = "";
50 }
51 
52 bool errHas()
53 {
54  return _errCode != 0;
55 }
56 
57 const char * errGetDescr()
58 {
59  if (_errCode != 0) {
60  return _errDescr;
61  }
62  return NULL;
63 }
64 
65 const char * errGetName()
66 {
67  if (_errName != 0) {
68  return _errName;
69  }
70  return NULL;
71 }
72 
73 
74 uint32_t errGet()
75 {
76  return _errCode;
77 }
78 
79 void errPrint(bool clear)
80 {
81  if (_errCode == 0) {
82  return;
83  }
84  if (strlen(_errDescr) == 0) {
85  logError("!! Error %8x: <no description> %s", _errCode, _errName);
86  } else {
87 
88  if (_errName == 0 || strlen(_errName) == 0) {
89  logError("!! Error %8x: %s", _errCode, _errDescr);
90  } else {
91  logError("!! Error %8x: %s [%s]", _errCode, _errDescr, _errName);
92  }
93  }
94  if (clear) errClear();
95 }
96 
97 void errFatal()
98 {
99  if (errHas()) {
100  errPrint(false);
101  } else {
102  logError("!! Error ??: Unknown cause");
103  }
104  while (1) {};
105 }
106 
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
Manages the global system error.
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
#define logError(MSG,...)
Format a log message with fatal level.
Definition: log.h:232
#define LOG_DEF(NAME,...)
Define a logger for a module.
Definition: log.h:129
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
Implements a generic logger facility.