From: Adam Sutton Date: Wed, 10 Apr 2013 10:30:11 +0000 (+0100) Subject: tvhlog: added hexdump routine. X-Git-Tag: v3.9~104 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd7c5dd9b9e5a61dfdb69ec406c572434b00de91;p=thirdparty%2Ftvheadend.git tvhlog: added hexdump routine. --- diff --git a/src/tvhlog.c b/src/tvhlog.c index ce243077f..e0466e940 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -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); +} + diff --git a/src/tvhlog.h b/src/tvhlog.h index a00a0dad1..69587e0d6 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -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__ */