Index: cackey.c ================================================================== --- cackey.c +++ cackey.c @@ -175,18 +175,54 @@ #ifdef CACKEY_DEBUG # ifdef HAVE_TIME_H # include static time_t cackey_debug_start_time = 0; -# 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)); } +static unsigned long CACKEY_DEBUG_GETTIME(void) { + if (cackey_debug_start_time == 0) { + cackey_debug_start_time = time(NULL); + } + + return(time(NULL) - cackey_debug_start_time); +} # else -# define CACKEY_DEBUG_PRINTTIME /**/ +static unsigned long CACKEY_DEBUG_GETTIME(void) { + return(0); +} # endif -# 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()); } -# 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()); } -# define CACKEY_DEBUG_PERROR(x) { fprintf(cackey_debug_fd(), "%s():%i: ", __func__, __LINE__); CACKEY_DEBUG_PRINTTIME; perror(x); fflush(cackey_debug_fd()); } +# define CACKEY_DEBUG_PRINTF(x...) { \ + static char buf_user[4096] = {0}; \ + snprintf(buf_user, sizeof(buf_user), x); \ + buf_user[sizeof(buf_user) - 1] = '\0'; \ + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, buf_user); \ + fflush(cackey_debug_fd()); \ +} +# define CACKEY_DEBUG_PRINTBUF(f, x, y) { \ + static char buf_user[4096] = {0}, *buf_user_p; \ + unsigned long buf_user_size; \ + unsigned char *TMPBUF; \ + unsigned long idx; \ + int snprintf_ret; \ + TMPBUF = (unsigned char *) (x); \ + buf_user_p = buf_user; \ + buf_user_size = sizeof(buf_user); \ + for (idx = 1; idx < (y); idx++) { \ + if (buf_user_size <= 0) { \ + break; \ + }; \ + snprintf_ret = snprintf(buf_user_p, buf_user_size, ", %02x", TMPBUF[idx]); \ + if (snprintf_ret <= 0) { \ + break; \ + }; \ + buf_user_p += snprintf_ret; \ + buf_user_size -= snprintf_ret; \ + }; \ + buf_user[sizeof(buf_user) - 1] = '\0'; \ + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s (%s/%lu = {%02x%s})\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0], buf_user); \ + fflush(cackey_debug_fd()); \ +} # define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", (void *) x, #x); free(x); } static FILE *cackey_debug_fd(void) { static FILE *fd = NULL; char *logfile; @@ -235,14 +271,11 @@ static void *CACKEY_DEBUG_FUNC_MALLOC(size_t size, const char *func, int line) { void *retval; retval = malloc(size); - CACKEY_DEBUG_PRINTTIME; - fprintf(cackey_debug_fd(), "%s():%i: ", func, line); - fprintf(cackey_debug_fd(), "MALLOC() = %p", retval); - fprintf(cackey_debug_fd(), "\n"); + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: MALLOC() = %p\n", CACKEY_DEBUG_GETTIME(), func, line, retval); fflush(cackey_debug_fd()); return(retval); } @@ -250,14 +283,11 @@ void *retval; retval = realloc(ptr, size); if (retval != ptr) { - CACKEY_DEBUG_PRINTTIME; - fprintf(cackey_debug_fd(), "%s():%i: ", func, line); - fprintf(cackey_debug_fd(), "REALLOC(%p) = %p", ptr, retval); - fprintf(cackey_debug_fd(), "\n"); + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: REALLOC(%p) = %p\n", CACKEY_DEBUG_GETTIME(), func, line, ptr, retval); fflush(cackey_debug_fd()); } if (retval == NULL) { CACKEY_DEBUG_PRINTF(" *** ERROR *** realloc returned NULL (size = %lu)", (unsigned long) size); @@ -269,14 +299,11 @@ static char *CACKEY_DEBUG_FUNC_STRDUP(const char *ptr, const char *func, int line) { char *retval; retval = strdup(ptr); - CACKEY_DEBUG_PRINTTIME; - fprintf(cackey_debug_fd(), "%s():%i: ", func, line); - fprintf(cackey_debug_fd(), "STRDUP_MALLOC() = %p", retval); - fprintf(cackey_debug_fd(), "\n"); + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: STRDUP_MALLOC() = %p\n", CACKEY_DEBUG_GETTIME(), func, line, retval); fflush(cackey_debug_fd()); return(retval); } @@ -654,11 +681,10 @@ # endif # define strdup(x) CACKEY_DEBUG_FUNC_STRDUP(x, __func__, __LINE__) #else # define CACKEY_DEBUG_PRINTF(x...) /**/ # define CACKEY_DEBUG_PRINTBUF(f, x, y) /**/ -# define CACKEY_DEBUG_PERROR(x) /**/ # define CACKEY_DEBUG_FUNC_TAG_TO_STR(x) "DEBUG_DISABLED" # define CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(x) "DEBUG_DISABLED" # define CACKEY_DEBUG_FUNC_OBJID_TO_STR(x) "DEBUG_DISABLED" # define CACKEY_DEBUG_FUNC_APPTYPE_TO_STR(x) "DEBUG_DISABLED" # define CACKEY_DEBUG_FUNC_ATTRIBUTE_TO_STR(x) "DEBUG_DISABLED"