]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tvhlog: added hexdump routine.
authorAdam Sutton <dev@adamsutton.me.uk>
Wed, 10 Apr 2013 10:30:11 +0000 (11:30 +0100)
committerAdam Sutton <dev@adamsutton.me.uk>
Wed, 10 Apr 2013 10:30:11 +0000 (11:30 +0100)
src/tvhlog.c
src/tvhlog.h

index ce243077f6ee5d47070113307ee134b4dd0ef612..e0466e940c69ec49d69e57865c2b9319755afa5e 100644 (file)
@@ -195,7 +195,51 @@ void _tvhlog ( const char *file, int line,
                const char *subsys, const char *fmt, ... )
 {
   va_list args;
+  pthread_mutex_lock(&tvhlog_mutex);
   va_start(args, fmt);
   tvhlogv(file, line, notify, severity, subsys, fmt, args);
   va_end(args);
+  pthread_mutex_unlock(&tvhlog_mutex);
 }
+
+/*
+ * Log a hexdump
+ */
+#define HEXDUMP_WIDTH 16
+void
+_tvhlog_hexdump(const char *file, int line,
+                int notify, int severity,
+                const char *subsys,
+                const uint8_t *data, ssize_t len)
+{
+  int i, c;
+  char str[1024];
+  va_list args;
+
+  pthread_mutex_lock(&tvhlog_mutex);
+  while (len > 0) {
+    c = 0;
+    for (i = 0; i < HEXDUMP_WIDTH; i++) {
+      if (i >= len)
+        c += snprintf(str+c, sizeof(str)-c, "   ");
+      else
+        c += snprintf(str+c, sizeof(str)-c, "%02X ", data[i]);
+    }
+    for (i = 0; i < HEXDUMP_WIDTH; i++) {
+      if (i < len) {
+        if (data[i] < ' ' || data[i] > '~')
+          str[c] = '.';
+        else
+          str[c] = data[i];
+      } else
+        str[c] = ' ';
+      c++;
+    }
+    str[c] = '\0';
+    tvhlogv(file, line, notify, severity, subsys, str, args);
+    len  -= HEXDUMP_WIDTH;
+    data += HEXDUMP_WIDTH;
+  }
+  pthread_mutex_unlock(&tvhlog_mutex);
+}
+
index a00a0dad16170e9c270eb1de7338d81bde7fea66..69587e0d6ad59268c7552e1626392fc4364af572 100644 (file)
@@ -41,6 +41,10 @@ void _tvhlog           ( const char *file, int line,
                          int notify, int severity,
                          const char *subsys, const char *fmt, ... )
   __attribute__((format(printf,6,7)));
+void _tvhlog_hexdump   ( const char *file, int line,
+                         int notify, int severity,
+                         const char *subsys,
+                         const uint8_t *data, ssize_t len );
 
 
 /* Options */
@@ -67,8 +71,11 @@ void _tvhlog           ( const char *file, int line,
 #if ENABLE_TRACE
 #define tvhtrace(subsys, fmt, ...)\
   _tvhlog(__FILE__, __LINE__, 0, LOG_TRACE, subsys, fmt, ##__VA_ARGS__)
+#define tvhlog_hexdump(subsys, data, len)\
+  _tvhlog_hexdump(__FILE__, __LINE__, 0, LOG_TRACE, subsys, (uint8_t*)data, len)
 #else
 #define tvhtrace(...) (void)0
+#define tvhlog_hexdump(...) (void)0
 #endif
 
 #endif /* __TVH_LOGGING_H__ */