From 92b8fe6e949f08308d237e87441e066a19a9eda6 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Sat, 2 May 2015 00:37:45 +0200 Subject: Provide data debugging API --- pcilib/debug.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'pcilib/debug.c') diff --git a/pcilib/debug.c b/pcilib/debug.c index f07e1e6..6d7d8af 100644 --- a/pcilib/debug.c +++ b/pcilib/debug.c @@ -1,8 +1,15 @@ +#define _ISOC99_SOURCE #include #include #include +#include +#include +#include "config.h" #include "error.h" +#include "debug.h" + +#define PCILIB_MAX_DEBUG_FILENAME_LENGTH 1023 void pcilib_debug_message(const char *function, const char *file, int line, const char *format, ...) { va_list va; @@ -14,3 +21,68 @@ void pcilib_debug_message(const char *function, const char *file, int line, cons va_end(va); } + +void pcilib_debug_data_buffer(const char *function, size_t size, void *buffer, pcilib_debug_buffer_flags_t flags, const char *file, ...) { + va_list va; + + FILE *f; + size_t prefix_len; + const char *prefix; + char fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1]; + + + prefix = getenv(function); + if (!prefix) return; + + if ((!prefix[0])||(prefix[0] == '1')) + prefix = PCILIB_DEBUG_DIR; + + prefix_len = strlen(prefix); + if (prefix_len >= PCILIB_MAX_DEBUG_FILENAME_LENGTH) + return; + + if (prefix_len) { + strncpy(fname, prefix, PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1); + fname[prefix_len++] = '/'; + } + + fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH] = -1; + va_start(va, file); + vsnprintf(fname + prefix_len, PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1 - prefix_len, file, va); + va_end(va); + + // file name is too long, skipping... + if (!fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH]) + return; + + + if (flags&PCILIB_DEBUG_BUFFER_MKDIR) { + char *slash; + if (prefix_len) slash = fname + prefix_len - 1; + else slash = strchr(fname, '/'); + + while (slash) { + size_t len; + + *slash = 0; + mkdir(fname, 0755); + len = strlen(fname); + *slash = '/'; + + slash = strchr(fname + len + 1, '/'); + } + } + + if (flags&PCILIB_DEBUG_BUFFER_APPEND) + f = fopen(fname, "a+"); + else + f = fopen(fname, "w"); + + if (!f) + return; + + if (size&&buffer) + fwrite(buffer, 1, size, f); + + fclose(f); +} -- cgit v1.2.3