#include <assert.h>
/* 
 * Define some nice color stuff
 */
#define  MOVE_TO_COL(col) "\033["#col"G"
#define      SETCOLOR_SUCCESS "\033[1;32m"
#define      SETCOLOR_FAILURE "\033[1;31m"
#define      SETCOLOR_WARNING "\033[1;33m"
#define      SETCOLOR_NORMAL "\033[0;39m"

#undef __assert_fail
# define __assert_fail(exp,file,line,fun)	\
({rtl_printf(SETCOLOR_FAILURE ": %s:%d: %s:assert(%s) failed\n"SETCOLOR_NORMAL,file,line,fun,exp);\
global_error++;})
//#define NSEC_PER_SEC 1000000000
#define timerplusnsec(c,d) (c)->tv_nsec +=(d);             \
                           if ((c)->tv_nsec >NSEC_PER_SEC){ \
                           (c)->tv_nsec -= NSEC_PER_SEC;(c)->tv_sec++;}

#define timerdiff(a,b) (((a)->tv_sec - (b)->tv_sec)*NSEC_PER_SEC + \
                         (a)->tv_nsec - (b)->tv_nsec)

#define timersum(c,a,b) (c)->tv_sec = ((a)->tv_sec + (b)->tv_sec); \
                       (c)->tv_nsec = ((a)->tv_nsec + (b)->tv_nsec); \
                       if ((c)->tv_nsec > NSEC_PER_SEC){ \
                           (c)->tv_nsec -= NSEC_PER_SEC;(c)->tv_sec++;}
#define timer_gt(a,b) (((a)->tv_sec > (b)->tv_sec) ? 1 :  \
                      (((a)->tv_sec < (b)->tv_sec) ? 0 :  \
                      ((a)->tv_nsec > (b)->tv_nsec)))
#define timespec_to_timeval(a,b) (b)->tv_sec = (a)->tv_sec; \
                                 (b)->tv_nsec = (a)->tv_usec * 1000

int global_error;
#define by_now() { char * excolor = (global_error) ? SETCOLOR_FAILURE : \
                         SETCOLOR_WARNING;                     \
                         fprintf(stderr, \
                         "%sEnd of test, %d error(s) found.\n"  \
                         SETCOLOR_NORMAL,                      \
                         excolor,global_error);                       \
                         exit(global_error);}
#ifdef debug
#undef debug
#define debug(a) do {a}while (0)
#else
#define debug(a) do {} while (0)
#endif
#define MASK(x) (1<<(x))
#define myperror(s) MYperror(__BASE_FILE__,__LINE__,s)
int MYperror(char * where,int line,char *what)
{
        rtl_printf(SETCOLOR_FAILURE"%s,%d:",where,line);
        //perror(what);
        rtl_printf(SETCOLOR_NORMAL);
	//exit depending if you are in a th or in init_module
	return 0;
}
#define my_c_perror(e,s) MY_c_perror(e,__BASE_FILE__,__LINE__,s)
int MY_c_perror(int e,char * where,int line,char *what)
{
        char *fmt;
        fmt = e ? "expected" :SETCOLOR_FAILURE"UNEXPECTED";
        rtl_printf("%s %s,%d:",fmt,where,line);
	//        perror(what);
        if ( ! e) {
                global_error++;
                rtl_printf(SETCOLOR_NORMAL);
        }

        return e ? 0:-1;
}
#define no_error(e,s) No_error(e,__BASE_FILE__,__LINE__,s)
int No_error(int e,char * where,int line,char *what)
{
        char *fmt;
        fmt = e ? SETCOLOR_FAILURE"ERROR expected but not found" : "Cool";
        rtl_printf("%s %s,%d:%s\n"SETCOLOR_NORMAL,fmt,where,line,what);
        if ( e) {
                global_error++;
                rtl_printf(SETCOLOR_NORMAL);
        }

        return e ? -1 : 0;
}


