#ifndef __mydebug_h__
#define __mydebug_h__

/*for debugging print statments and timers
*	Troy Rosin TJR760 11200680
*	compile with -Ddebug and the prints will be there 
* otherwise nothing wil appear
*
* to use with makefile add the following
*
* debug ?=off
* ifeq ($(debug),on)
* CPPFLAGS += -Ddebug
* endif
*
* then use "make debug=on" to make if you want the debug messages
* just use make to build normally
*
*/

#include <stdio.h>
#include <time.h>

#ifdef debug
/*prints the name of the function that was called passed as an arg */
#define PRINT_C(FUNC)\
	puts(#FUNC" was called");


/*prints the message wanted followed by a \n no "" needed*/
#define PRINT_M(MSG)\
	puts(#MSG);


/*prints the name of a variable holding a pointer and its value*/
#define PRINT_P(PTR)\
	printf(#PTR" has value %p\n", (void*)PTR);


/*prints the varilble name holding the int and the value
* Type is how you want it to print ie l, d, ld, u, f... 
* just pass the field type printf expects without the '%'
*/
#define PRINT_INT(VAR, TYPE)\
	printf(#VAR" has value %"#TYPE"\n", VAR);


/*starts a timer so you can see how long fuctions can run for
* you must end the timer within the same scope as the start
* the id can be anything its only used to differentiate between 
* timers
* T_START must be placed at the end of variable definitions to comply with C90
* standards. if not using C90 go nuts 
*/
#define T_START(ID)\
	struct timespec ID ## __d_timer_s__;\
	struct timespec ID ## __d_timer_e__;\
	clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_s__);



/*checks how long since the timer began and prints the seconds and nanoseconds
* since. Can be called multiple times if needed
*/
#define T_END(ID)\
	clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_e__);\
	printf("timer "#ID" took:\n%ld second(s) total\n%ld nanoseconds total\n",\
		((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec - \
		ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) - \
		((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec-\
		ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) \
	  % 1000000000)) / 1000000000 , \
		ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec -\
		ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec );



#else
/*if debug was not defined we insert nothing there*/
#define PRINT_C(X)			{}
#define PRINT_M(X)			{}
#define PRINT_P(X)			{}
#define PRINT_INT(X,Y)	{}
#define T_START(X)			{}
#define T_END(X)				{}

#endif

#endif
