parse_monitor: replace printf printf_P
[protos/xbee-avr.git] / error.h
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: error.h,v 1.11.4.3 2007-12-31 16:25:00 zer0 Exp $
19  *
20  */
21
22 #ifndef _ERROR_H_
23 #define _ERROR_H_
24
25 #ifndef _AVERSIVE_ERROR_H_
26 #error "Don't include <error.h>, include <aversive/error.h> instead"
27 #endif
28
29 #include <aversive/pgmspace.h>
30 #include <aversive.h>
31 #include <general_errors.h>
32
33 #include "error_config.h"
34
35 #define ERROR_SEVERITY_EMERG    0
36 #define ERROR_SEVERITY_ERROR    1
37 #define ERROR_SEVERITY_WARNING  2
38 #define ERROR_SEVERITY_NOTICE   3
39 #define ERROR_SEVERITY_DEBUG    4
40
41 /** The error structure, which is given as a parameter in log funcs */ 
42 struct error {
43         uint8_t err_num;
44         uint8_t severity;
45         PGM_P text;
46         PGM_P file;
47         uint16_t line;
48 };
49
50
51 struct error_fct {
52         void (*emerg)(struct error *, ...);
53         void (*error)(struct error *, ...);
54         void (*warning)(struct error *, ...);
55         void (*notice)(struct error *, ...);
56         void (*debug)(struct error *, ...);
57 } ;
58
59 extern struct error_fct g_error_fct;
60
61
62 struct error error_generate(uint8_t num, uint8_t severity, PGM_P t, PGM_P f, uint16_t l);
63
64 /** Register log function for EMERG level */
65 void error_register_emerg(void (*f)(struct error *, ...));
66
67 /** Register log function for ERROR level */
68 void error_register_error(void (*f)(struct error *, ...));
69
70 /** Register log function for WARNING level */
71 void error_register_warning(void (*f)(struct error *, ...));
72
73 /** Register log function for NOTICE level */
74 void error_register_notice(void (*f)(struct error *, ...));
75
76 /** Register log function for DEBUG level */
77 void error_register_debug(void (*f)(struct error *, ...));
78
79
80
81
82 /** Call this macro to log EMERG events */
83 #define EMERG(num, text, ...)  do {                                            \
84         if(g_error_fct.emerg) {                                                \
85                 struct error e = error_generate(num, ERROR_SEVERITY_EMERG,     \
86                                                                 PSTR(text),    \
87                                                                 PSTR(__FILE__),\
88                                                                 __LINE__);     \
89                 g_error_fct.emerg(&e, ##__VA_ARGS__);                          \
90         }                                                                      \
91 } while(0)
92
93 /** Call this macro to log ERROR events */
94 #define ERROR(num, text, ...)  do {                                            \
95         if(g_error_fct.error) {                                                \
96                 struct error e = error_generate(num, ERROR_SEVERITY_ERROR,     \
97                                                                 PSTR(text),    \
98                                                                 PSTR(__FILE__),\
99                                                                 __LINE__);     \
100                 g_error_fct.error(&e, ##__VA_ARGS__);                          \
101         }                                                                      \
102 } while(0)
103
104 /** Call this macro to log WARNING events */
105 #define WARNING(num, text, ...)  do {                                          \
106         if(g_error_fct.warning) {                                              \
107                 struct error e = error_generate(num, ERROR_SEVERITY_WARNING,   \
108                                                                 PSTR(text),    \
109                                                                 PSTR(__FILE__),\
110                                                                 __LINE__);     \
111                 g_error_fct.warning(&e, ##__VA_ARGS__);                        \
112         }                                                                      \
113 } while(0)
114
115 /** Call this macro to log NOTICE events */
116 #define NOTICE(num, text, ...)  do {                                           \
117         if(g_error_fct.notice) {                                               \
118                 struct error e = error_generate(num, ERROR_SEVERITY_NOTICE,    \
119                                                                 PSTR(text),    \
120                                                                 PSTR(__FILE__),\
121                                                                 __LINE__);     \
122                 g_error_fct.notice(&e, ##__VA_ARGS__);                         \
123         }                                                                      \
124 } while(0)
125
126 /** Call this macro to log DEBUG events */
127 #define DEBUG(num, text, ...)  do {                                            \
128         if(g_error_fct.debug) {                                                \
129                 struct error e = error_generate(num, ERROR_SEVERITY_DEBUG,     \
130                                                                 PSTR(text),    \
131                                                                 PSTR(__FILE__),\
132                                                                 __LINE__);     \
133                 g_error_fct.debug(&e, ##__VA_ARGS__);                          \
134         }                                                                      \
135 } while(0)
136
137
138
139
140 #endif /* _ERROR_H_ */