Check-in [b9df324b66]
Overview
Comment:Added support for logging to a file using CACKEY_DEBUG_LOGFILE environment variable
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:b9df324b668adec2ed8011b0bb8298305e54c67c
User & Date: rkeene on 2012-07-19 18:03:46
Other Links: manifest | tags
Context
2012-07-20
00:53
Corrected type-punning issue check-in: 9bed3538e1 user: rkeene tags: trunk
2012-07-19
18:03
Added support for logging to a file using CACKEY_DEBUG_LOGFILE environment variable check-in: b9df324b66 user: rkeene tags: trunk
18:03
Corrected type for debugging certificates check-in: ecfb2af250 user: rkeene tags: trunk
Changes

Modified cackey.c from [f9930320b1] to [556e3f5257].

   170    170   #define MAX_ATR_SIZE 1024
   171    171   #endif
   172    172   
   173    173   #ifdef CACKEY_DEBUG
   174    174   #  ifdef HAVE_TIME_H
   175    175   #    include <time.h>
   176    176   static time_t cackey_debug_start_time = 0;
   177         -#    define CACKEY_DEBUG_PRINTTIME { if (cackey_debug_start_time == 0) { cackey_debug_start_time = time(NULL); }; fprintf(stderr, "[%lu]: ", (unsigned long) (time(NULL) - cackey_debug_start_time)); }
          177  +#    define CACKEY_DEBUG_PRINTTIME { if (cackey_debug_start_time == 0) { cackey_debug_start_time = time(NULL); }; fprintf(cackey_debug_fd(), "[%lu]: ", (unsigned long) (time(NULL) - cackey_debug_start_time)); }
   178    178   #  else
   179    179   #    define CACKEY_DEBUG_PRINTTIME /**/
   180    180   #  endif
   181    181   
   182         -#  define CACKEY_DEBUG_PRINTF(x...) { CACKEY_DEBUG_PRINTTIME; fprintf(stderr, "%s():%i: ", __func__, __LINE__); fprintf(stderr, x); fprintf(stderr, "\n"); fflush(stderr); }
   183         -#  define CACKEY_DEBUG_PRINTBUF(f, x, y) { unsigned char *TMPBUF; unsigned long idx; TMPBUF = (unsigned char *) (x); CACKEY_DEBUG_PRINTTIME; fprintf(stderr, "%s():%i: %s  (%s/%lu = {%02x", __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0]); for (idx = 1; idx < (y); idx++) { fprintf(stderr, ", %02x", TMPBUF[idx]); }; fprintf(stderr, "})\n"); fflush(stderr); }
   184         -#  define CACKEY_DEBUG_PERROR(x) { fprintf(stderr, "%s():%i: ", __func__, __LINE__); CACKEY_DEBUG_PRINTTIME; perror(x); fflush(stderr); }
          182  +#  define CACKEY_DEBUG_PRINTF(x...) { CACKEY_DEBUG_PRINTTIME; fprintf(cackey_debug_fd(), "%s():%i: ", __func__, __LINE__); fprintf(cackey_debug_fd(), x); fprintf(cackey_debug_fd(), "\n"); fflush(cackey_debug_fd()); }
          183  +#  define CACKEY_DEBUG_PRINTBUF(f, x, y) { unsigned char *TMPBUF; unsigned long idx; TMPBUF = (unsigned char *) (x); CACKEY_DEBUG_PRINTTIME; fprintf(cackey_debug_fd(), "%s():%i: %s  (%s/%lu = {%02x", __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0]); for (idx = 1; idx < (y); idx++) { fprintf(cackey_debug_fd(), ", %02x", TMPBUF[idx]); }; fprintf(cackey_debug_fd(), "})\n"); fflush(cackey_debug_fd()); }
          184  +#  define CACKEY_DEBUG_PERROR(x) { fprintf(cackey_debug_fd(), "%s():%i: ", __func__, __LINE__); CACKEY_DEBUG_PRINTTIME; perror(x); fflush(cackey_debug_fd()); }
   185    185   #  define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", x, #x); free(x); }
          186  +
          187  +static FILE *cackey_debug_fd(void) {
          188  +	static FILE *fd = NULL;
          189  +	char *logfile;
          190  +
          191  +	if (fd != NULL) {
          192  +		return(fd);
          193  +	}
          194  +
          195  +	/*
          196  +	 * Log to stderr initially so we can use debugging within
          197  +	 * this function without getting into an infinite loop
          198  +	 */
          199  +	fd = stderr;
          200  +
          201  +	logfile = getenv("CACKEY_DEBUG_LOGFILE");
          202  +	if (logfile != NULL) {
          203  +		CACKEY_DEBUG_PRINTF("Found environment variable: %s", logfile);
          204  +
          205  +		logfile = strchr(logfile, '=');
          206  +		if (logfile == NULL) {
          207  +			logfile = getenv("CACKEY_DEBUG_LOGFILE");
          208  +		} else {
          209  +			logfile++;
          210  +		}
          211  +	}
          212  +
          213  +	if (logfile != NULL) {
          214  +		CACKEY_DEBUG_PRINTF("Found log file: %s", logfile);
          215  +
          216  +		fd = fopen(logfile, "a");
          217  +	}
          218  +
          219  +	if (fd == NULL) {
          220  +		fd = stderr;
          221  +	}
          222  +
          223  +	if (fd == stderr) {
          224  +		CACKEY_DEBUG_PRINTF("Returning stderr");
          225  +	} else {
          226  +		CACKEY_DEBUG_PRINTF("Returning %p", fd);
          227  +	}
          228  +
          229  +	return(fd);
          230  +}
   186    231   
   187    232   static void *CACKEY_DEBUG_FUNC_MALLOC(size_t size, const char *func, int line) {
   188    233   	void *retval;
   189    234   
   190    235   	retval = malloc(size);
   191    236   
   192    237   	CACKEY_DEBUG_PRINTTIME;
   193         -	fprintf(stderr, "%s():%i: ", func, line);
   194         -	fprintf(stderr, "MALLOC() = %p", retval);
   195         -	fprintf(stderr, "\n");
   196         -	fflush(stderr);
          238  +	fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
          239  +	fprintf(cackey_debug_fd(), "MALLOC() = %p", retval);
          240  +	fprintf(cackey_debug_fd(), "\n");
          241  +	fflush(cackey_debug_fd());
   197    242   
   198    243   	return(retval);
   199    244   }
   200    245   
   201    246   static void *CACKEY_DEBUG_FUNC_REALLOC(void *ptr, size_t size, const char *func, int line) {
   202    247   	void *retval;
   203    248   
   204    249   	retval = realloc(ptr, size);
   205    250   
   206    251   	if (retval != ptr) {
   207    252   		CACKEY_DEBUG_PRINTTIME;
   208         -		fprintf(stderr, "%s():%i: ", func, line);
   209         -		fprintf(stderr, "REALLOC(%p) = %p", ptr, retval);
   210         -		fprintf(stderr, "\n");
   211         -		fflush(stderr);
          253  +		fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
          254  +		fprintf(cackey_debug_fd(), "REALLOC(%p) = %p", ptr, retval);
          255  +		fprintf(cackey_debug_fd(), "\n");
          256  +		fflush(cackey_debug_fd());
   212    257   	}
   213    258   
   214    259   	if (retval == NULL) {
   215    260   		CACKEY_DEBUG_PRINTF(" *** ERROR *** realloc returned NULL (size = %lu)", (unsigned long) size);
   216    261   	}
   217    262   
   218    263   	return(retval);
................................................................................
   220    265   
   221    266   static char *CACKEY_DEBUG_FUNC_STRDUP(const char *ptr, const char *func, int line) {
   222    267   	char *retval;
   223    268   
   224    269   	retval = strdup(ptr);
   225    270   
   226    271   	CACKEY_DEBUG_PRINTTIME;
   227         -	fprintf(stderr, "%s():%i: ", func, line);
   228         -	fprintf(stderr, "STRDUP_MALLOC() = %p", retval);
   229         -	fprintf(stderr, "\n");
   230         -	fflush(stderr);
          272  +	fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
          273  +	fprintf(cackey_debug_fd(), "STRDUP_MALLOC() = %p", retval);
          274  +	fprintf(cackey_debug_fd(), "\n");
          275  +	fflush(cackey_debug_fd());
   231    276   
   232    277   	return(retval);
   233    278   }
   234    279   
   235    280   static const char *CACKEY_DEBUG_FUNC_TAG_TO_STR(unsigned char tag) {
   236    281   	switch (tag) {
   237    282   		case GSCIS_TAG_CARDID: