PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/error/error.h

https://bitbucket.org/cvra/cvra-nios2
C++ Header | 145 lines | 81 code | 29 blank | 35 comment | 5 complexity | 821d3cb3d3319f7cc2f58f2a4a8909da MD5 | raw file
  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.h>
 30#include <general_errors.h>
 31
 32#define ERROR_SEVERITY_EMERG    0
 33#define ERROR_SEVERITY_ERROR    1
 34#define ERROR_SEVERITY_WARNING  2
 35#define ERROR_SEVERITY_NOTICE   3
 36#define ERROR_SEVERITY_DEBUG    4
 37
 38
 39/** enable the dump of the comment */
 40#define ERROR_DUMP_TEXTLOG 
 41
 42/** enable the dump of filename and line number */
 43#define ERROR_DUMP_FILE_LINE
 44
 45/** The error structure, which is given as a parameter in log funcs */ 
 46struct error {
 47	uint8_t err_num;        /**< Error number */
 48	uint8_t severity;       /**< Error severity */
 49	const char * text;      /**< Error text */
 50	const char * file;      /**< File in which the error occurred */
 51	uint16_t line;          /**< Line number in which the error occurred */
 52};
 53
 54/** Structure of pointers to functions which are called on errors
 55 */
 56struct error_fct {
 57	void (*emerg)(struct error *, ...);     /**< Pointer to emergency func */
 58	void (*error)(struct error *, ...);     /**< Pointer to error func */
 59	void (*warning)(struct error *, ...);   /**< Pointer to warning func */
 60	void (*notice)(struct error *, ...);    /**< Pointer to notice func */
 61	void (*debug)(struct error *, ...);     /**< Pointer to debug func */
 62} ;
 63
 64extern struct error_fct g_error_fct;
 65
 66
 67struct error error_generate(uint8_t num, uint8_t severity, const char * t, const char * f, uint16_t l);
 68
 69/** Register log function for EMERG level */
 70void error_register_emerg(void (*f)(struct error *, ...));
 71
 72/** Register log function for ERROR level */
 73void error_register_error(void (*f)(struct error *, ...));
 74
 75/** Register log function for WARNING level */
 76void error_register_warning(void (*f)(struct error *, ...));
 77
 78/** Register log function for NOTICE level */
 79void error_register_notice(void (*f)(struct error *, ...));
 80
 81/** Register log function for DEBUG level */
 82void error_register_debug(void (*f)(struct error *, ...));
 83
 84
 85
 86
 87/** Call this macro to log EMERG events */
 88#define EMERG(num, text, ...)  do {                                            \
 89	if(g_error_fct.emerg) {                                                \
 90		struct error e = error_generate(num, ERROR_SEVERITY_EMERG,     \
 91								(text),    \
 92								(__FILE__),\
 93								__LINE__);     \
 94		g_error_fct.emerg(&e, ##__VA_ARGS__);                          \
 95	}                                                                      \
 96} while(0)
 97
 98/** Call this macro to log ERROR events */
 99#define ERROR(num, text, ...)  do {                                            \
100	if(g_error_fct.error) {                                                \
101		struct error e = error_generate(num, ERROR_SEVERITY_ERROR,     \
102								(text),    \
103								(__FILE__),\
104								__LINE__);     \
105		g_error_fct.error(&e, ##__VA_ARGS__);                          \
106	}                                                                      \
107} while(0)
108
109/** Call this macro to log WARNING events */
110#define WARNING(num, text, ...)  do {                                          \
111	if(g_error_fct.warning) {                                              \
112		struct error e = error_generate(num, ERROR_SEVERITY_WARNING,   \
113								(text),    \
114								(__FILE__),\
115								__LINE__);     \
116		g_error_fct.warning(&e, ##__VA_ARGS__);                        \
117	}                                                                      \
118} while(0)
119
120/** Call this macro to log NOTICE events */
121#define NOTICE(num, text, ...)  do {                                           \
122	if(g_error_fct.notice) {                                               \
123		struct error e = error_generate(num, ERROR_SEVERITY_NOTICE,    \
124								(text),    \
125								(__FILE__),\
126								__LINE__);     \
127		g_error_fct.notice(&e, ##__VA_ARGS__);                         \
128	}                                                                      \
129} while(0)
130
131/** Call this macro to log DEBUG events */
132#define DEBUG(num, text, ...)  do {                                            \
133	if(g_error_fct.debug) {                                                \
134		struct error e = error_generate(num, ERROR_SEVERITY_DEBUG,     \
135								(text),    \
136								(__FILE__),\
137								__LINE__);     \
138		g_error_fct.debug(&e, ##__VA_ARGS__);                          \
139	}                                                                      \
140} while(0)
141
142
143
144
145#endif /* _ERROR_H_ */