/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
252 by Suren A. Chilingaryan
Provide data debugging API
1
#define _ISOC99_SOURCE 
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <stdarg.h>
252 by Suren A. Chilingaryan
Provide data debugging API
5
#include <string.h>
6
#include <sys/stat.h>
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
7
252 by Suren A. Chilingaryan
Provide data debugging API
8
#include "config.h"
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
9
#include "error.h"
252 by Suren A. Chilingaryan
Provide data debugging API
10
#include "debug.h"
11
12
#define PCILIB_MAX_DEBUG_FILENAME_LENGTH 1023
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
13
271 by Suren A. Chilingaryan
Prevent excessive calling of getenv by debugging code for better performance
14
263 by Suren A. Chilingaryan
Support pcilib_log_once calls
15
void pcilib_debug_message(const char *function, const char *file, int line, pcilib_log_flags_t flags, const char *format, ...) {
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
16
    va_list va;
17
271 by Suren A. Chilingaryan
Prevent excessive calling of getenv by debugging code for better performance
18
//    if (!getenv(function)) return;
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
19
20
    va_start(va, format);
263 by Suren A. Chilingaryan
Support pcilib_log_once calls
21
    pcilib_log_vmessage(file, line, PCILIB_LOG_DEBUG, flags, format, va);
250 by Suren A. Chilingaryan
Provide an interface for logging debug messages
22
    va_end(va);
23
}
24
252 by Suren A. Chilingaryan
Provide data debugging API
25
26
void pcilib_debug_data_buffer(const char *function, size_t size, void *buffer, pcilib_debug_buffer_flags_t flags, const char *file, ...) {
27
    va_list va;
28
29
    FILE *f;
30
    size_t prefix_len;
31
    const char *prefix;
32
    char fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1];
33
34
    prefix = getenv(function);
35
    if (!prefix) return;
36
37
    if ((!prefix[0])||(prefix[0] == '1'))
38
	prefix = PCILIB_DEBUG_DIR;
39
40
    prefix_len = strlen(prefix);
41
    if (prefix_len >= PCILIB_MAX_DEBUG_FILENAME_LENGTH)
42
	return;
43
44
    if (prefix_len) {
45
	strncpy(fname, prefix, PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1);
46
	fname[prefix_len++] = '/';
47
    }
48
    
49
    fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH] = -1;
50
    va_start(va, file);
51
    vsnprintf(fname + prefix_len, PCILIB_MAX_DEBUG_FILENAME_LENGTH + 1 - prefix_len, file, va);
52
    va_end(va);
53
54
	// file name is too long, skipping...
55
    if (!fname[PCILIB_MAX_DEBUG_FILENAME_LENGTH])
56
	return;
57
58
59
    if (flags&PCILIB_DEBUG_BUFFER_MKDIR) {
60
	char *slash;
61
	if (prefix_len) slash = fname + prefix_len - 1;
62
	else slash = strchr(fname, '/');
63
64
	while (slash) {
65
	    size_t len;
66
67
	    *slash = 0;
68
	    mkdir(fname, 0755);
69
	    len = strlen(fname);
70
	    *slash = '/';
71
72
	    slash = strchr(fname + len + 1, '/');
73
	}	    
74
    }
75
76
    if (flags&PCILIB_DEBUG_BUFFER_APPEND)
77
	f = fopen(fname, "a+");
78
    else 
79
	f = fopen(fname, "w");
80
81
    if (!f)
82
	return;
83
    
84
    if (size&&buffer)
85
	fwrite(buffer, 1, size, f);
86
87
    fclose(f);
88
}