summaryrefslogtreecommitdiffstats
path: root/pcilib/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'pcilib/debug.c')
-rw-r--r--pcilib/debug.c72
1 files changed, 72 insertions, 0 deletions
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 <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <string.h>
+#include <sys/stat.h>
+#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);
+}