add a diagnostic function debug_print_buffer, which might be useful in the future.

This commit is contained in:
Mike Brady
2024-07-18 15:26:55 +01:00
parent 5827792081
commit 10b965a7aa
2 changed files with 35 additions and 0 deletions
+32
View File
@@ -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
+3
View File
@@ -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);