summaryrefslogtreecommitdiffstats
path: root/pcilib/debug.c
blob: 5192dc2936bfebf107c8b137f19e94ac916bde73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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, pcilib_log_flags_t flags, const char *format, ...) {
    va_list va;

//    if (!getenv(function)) return;

    va_start(va, format);
    pcilib_log_vmessage(file, line, PCILIB_LOG_DEBUG, flags, format, va);
    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);
}