]> git.ipfire.org Git - thirdparty/shairport-sync.git/commitdiff
add a diagnostic function debug_print_buffer, which might be useful in the future.
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Thu, 18 Jul 2024 14:26:55 +0000 (15:26 +0100)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Thu, 18 Jul 2024 14:26:55 +0000 (15:26 +0100)
common.c
common.h

index 0e2a07243bfccac2956d31daa8c52d7b904f79e8..42411c5fefcd0a6fd290e7324d2dec7f7b605d1e 100644 (file)
--- a/common.c
+++ b/common.c
@@ -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
index a1d50d448131dad45b25d89a3ddf58a91a2eec23..9de26f6366044ad00618127c211f626333ba3ba5 100644 (file)
--- a/common.h
+++ b/common.h
@@ -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);