summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2014-07-26 17:32:27 +0200
committerGravatar Martin Szulecki2014-07-26 17:32:27 +0200
commit06a26dbc0c9fc7e96d211481bc6dd2944d7aeb36 (patch)
treecc4789377ed7b2cb001c26d9dd2bdcea3fa1707f
parentaede4d06c49a8446e8011ab569c765a4f4ba32e7 (diff)
downloadusbmuxd-06a26dbc0c9fc7e96d211481bc6dd2944d7aeb36.tar.gz
usbmuxd-06a26dbc0c9fc7e96d211481bc6dd2944d7aeb36.tar.bz2
Add usbmuxd_log_buffer helper for easier debugging of binary payloadslograw
-rw-r--r--src/log.c57
-rw-r--r--src/log.h2
2 files changed, 53 insertions, 6 deletions
diff --git a/src/log.c b/src/log.c
index 2d22e94..6122c0a 100644
--- a/src/log.c
+++ b/src/log.c
@@ -61,6 +61,24 @@ static int level_to_syslog_level(int level)
return result;
}
+static void usbmuxd_vlog_raw(enum loglevel level, const char *fmt, va_list ap)
+{
+ if (log_syslog) {
+ vsyslog(level_to_syslog_level(level), fmt, ap);
+ } else {
+ vfprintf(stderr, fmt, ap);
+ }
+}
+
+static void usbmuxd_log_raw(enum loglevel level, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ usbmuxd_vlog_raw(level, fmt, ap);
+ va_end(ap);
+}
+
void usbmuxd_log(enum loglevel level, const char *fmt, ...)
{
va_list ap;
@@ -84,12 +102,41 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...)
}
va_start(ap, fmt);
- if (log_syslog) {
- vsyslog(level_to_syslog_level(level), fs, ap);
- } else {
- vfprintf(stderr, fs, ap);
- }
+ usbmuxd_vlog_raw(level, fs, ap);
va_end(ap);
free(fs);
}
+
+void usbmuxd_log_buffer(enum loglevel level, const unsigned char *data, const size_t length)
+{
+ if(level > log_level)
+ return;
+
+ size_t i;
+ int j;
+ unsigned char c;
+
+ for (i = 0; i < length; i += 16) {
+ usbmuxd_log_raw(level, "%04x: ", i);
+ for (j = 0; j < 16; j++) {
+ if (i + j >= length) {
+ usbmuxd_log_raw(level, " ");
+ continue;
+ }
+ usbmuxd_log_raw(level, "%02x ", *(data + i + j) & 0xff);
+ }
+ usbmuxd_log_raw(level, " | ");
+ for (j = 0; j < 16; j++) {
+ if (i + j >= length)
+ break;
+ c = *(data + i + j);
+ if ((c < 32) || (c > 127)) {
+ usbmuxd_log_raw(level, ".");
+ continue;
+ }
+ usbmuxd_log_raw(level, "%c", c);
+ }
+ usbmuxd_log_raw(level, "\n");
+ }
+} \ No newline at end of file
diff --git a/src/log.h b/src/log.h
index 1fb77ee..af0e359 100644
--- a/src/log.h
+++ b/src/log.h
@@ -39,6 +39,6 @@ void log_enable_syslog();
void log_disable_syslog();
void usbmuxd_log(enum loglevel level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
-
+void usbmuxd_log_buffer(enum loglevel level, const unsigned char *data, const size_t length);
#endif