Aversive/Modules/debug/error

De Wikidroids

The error modules provides an efficient way to signal error, warning, errors, etc... the error modules provides error numbers, verbosity levels, etc...

Module initialisation

First of all, you have to register the function you will use on error, warnings, etc... to print something on the UART, display a message on a LCD, or what you want. To do this, use the follwing functions : [to do, see error.h]

Using the error module

To generate an error message, use the following macros : EMERG(num, text, ...); ERROR(num, text, ...); WARNING(num, text, ...); NOTICE(num, text, ...); DEBUG(num, text, ...);

For example, doing WARNING(0, "Invalid command : %d", 7); will raise a warning with error code 0, and text "Invalid command : 7". This warning will be catched by your logging function. An example function is provided below.

Log function example

This function will just print the severity of the error, its number, its text, its file & number information.

void mylog(struct error * e, ...)
{
	va_list ap;          /* For the variable number of arguments. */
	va_start(ap, e);     /* Starts the list of arguments. */
	printf_P(PSTR("[%d]: E%d "), e->severity, e->err_num); /* prints the severity and the error no. */
	vfprintf_P(stdout, e->text, ap);  /* prints the test and the variable arguments like values */
	printf_P(PSTR(", "));
	printf_P(e->file);  /* Prints the file & line infos. */
	printf_P(PSTR(", L%d\n"), e->line);
	va_end(ap);
}

To register it in the error module, you can usen the following code :

	error_register_emerg(mylog);
	error_register_error(mylog);
        error_register_warning(mylog);
        error_register_notice(mylog);
        //error_register_debug(mylog); /* To decrease verbosity level, just comment out the corresponding lines. */
Boîte à outils
LANGUAGES