-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk-analysis-log.c
119 lines (102 loc) · 2.24 KB
/
disk-analysis-log.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <syslog.h>
#include "disk-analysis-log.h"
#define BUF_LEN 2048
#define MAX_LEN (BUF_LEN - 1)
static int msg_threshold;
void print_bin(char *buffer, uint8_t val, int n)
{
int i;
for (i = 0; i < n; i++) {
if ((val >> (n-i-1)) & 0x1) {
buffer[i] = '1';
} else {
buffer[i] = '0';
}
}
}
static void printf_log_func(int level, const char *msg)
{
/* debug < info < err */
if (level <= msg_threshold) {
printf("%s\n", msg);
}
}
//static void syslog_log_func(int level, const char *msg)
//{
// syslog(level, "%s\n", msg);
//}
typedef void (*log_func)(int level, const char *msg);
static log_func _log_func = printf_log_func;
//void log_init(const char *id, int debug)
//{
// _log_func = syslog_log_func;
// msg_threshold = debug;
// openlog(id, LOG_PERROR|LOG_PID|LOG_NDELAY, LOG_DAEMON);
// if (debug) {
// setlogmask(LOG_UPTO(LOG_INFO));
// }
// else {
// setlogmask(LOG_UPTO(LOG_ERR));
// }
// setvbuf(stdout, NULL, _IONBF, 0);
//}
void log_init(const char *id, int debug)
{
id = id;
_log_func = printf_log_func;
msg_threshold = debug;
}
char *_log_msg_fmt(const char *fmt, ...)
{
va_list args;
char *msg = malloc(BUF_LEN);
if (msg) {
va_start(args, fmt);
vsnprintf(msg, MAX_LEN, fmt, args);
va_end(args);
}
return msg;
}
int _log_msg(const char *file, const char * func, int line, char *msg)
{
char buf[BUF_LEN];
buf[0] = '\0';
if (LOG_INFO > msg_threshold) {
snprintf(buf, MAX_LEN, "[%16.16s: %04u,%.14s]\t", file, line, func);
}
int len = strlen(buf);
snprintf(buf+len, MAX_LEN-len, "%s", msg);
if (_log_func) {
_log_func(LOG_INFO, buf);
}
free(msg);
return 0;
}
int _log_dbg(const char *file, const char * func, int line, char *msg)
{
char buf[BUF_LEN];
snprintf(buf, MAX_LEN, "[%16.16s: %04u,%.14s]\t%s", file, line, func, msg);
if (_log_func) {
_log_func(LOG_DEBUG, buf);
}
free(msg);
return 0;
}
int _log_err(const char *file, const char * func, int line, char *msg)
{
char buf[BUF_LEN];
buf[0] = '\0';
if (LOG_INFO > msg_threshold) {
snprintf(buf, MAX_LEN, "[%16.16s: %04u,%.14s]\t", file, line, func);
}
int len = strlen(buf);
snprintf(buf+len, MAX_LEN-len, "%s", msg);
if (_log_func) {
_log_func(LOG_ERR, buf);
}
free(msg);
return 0;
}