Skip to content

Commit

Permalink
add a diagnostic function debug_print_buffer, which might be useful i…
Browse files Browse the repository at this point in the history
…n the future.
  • Loading branch information
mikebrady committed Jul 18, 2024
1 parent 5827792 commit 10b965a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,38 @@ void _inform(const char *thefilename, const int linenumber, const char *format,
pthread_setcancelstate(oldState, NULL);
}

void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *vbuf,
size_t buf_len) {
if (level > debuglev)
return;
char *buf = (char *)vbuf;
char *obf =
malloc(buf_len * 4 + 1); // to be on the safe side -- 4 characters on average for each byte
if (obf != NULL) {
char *obfp = obf;
unsigned int obfc;
for (obfc = 0; obfc < buf_len; obfc++) {
snprintf(obfp, 3, "%02X", buf[obfc]);
obfp += 2;
if (obfc != buf_len - 1) {
if (obfc % 32 == 31) {
snprintf(obfp, 5, " || ");
obfp += 4;
} else if (obfc % 16 == 15) {
snprintf(obfp, 4, " | ");
obfp += 3;
} else if (obfc % 4 == 3) {
snprintf(obfp, 2, " ");
obfp += 1;
}
}
};
*obfp = 0;
_debug(thefilename, linenumber, level, "%s", obf);
free(obf);
}
}

// The following two functions are adapted slightly and with thanks from Jonathan Leffler's sample
// code at
// https://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux
Expand Down
3 changes: 3 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,14 @@ void _die(const char *filename, const int linenumber, const char *format, ...);
void _warn(const char *filename, const int linenumber, const char *format, ...);
void _inform(const char *filename, const int linenumber, const char *format, ...);
void _debug(const char *filename, const int linenumber, int level, const char *format, ...);
void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *buf,
size_t buf_len);

#define die(...) _die(__FILE__, __LINE__, __VA_ARGS__)
#define debug(...) _debug(__FILE__, __LINE__, __VA_ARGS__)
#define warn(...) _warn(__FILE__, __LINE__, __VA_ARGS__)
#define inform(...) _inform(__FILE__, __LINE__, __VA_ARGS__)
#define debug_print_buffer(...) _debug_print_buffer(__FILE__, __LINE__, __VA_ARGS__)

uint8_t *base64_dec(char *input, int *outlen);
char *base64_enc(uint8_t *input, int length);
Expand Down

0 comments on commit 10b965a

Please sign in to comment.