forked from axboe/fio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
142 lines (110 loc) · 2.22 KB
/
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include "fio.h"
#define LOG_START_SZ 512
size_t log_info_buf(const char *buf, size_t len)
{
/*
* buf could be NULL (not just "").
*/
if (!buf)
return 0;
if (is_backend) {
size_t ret = fio_server_text_output(FIO_LOG_INFO, buf, len);
if (ret != -1)
return ret;
}
if (log_syslog) {
syslog(LOG_INFO, "%s", buf);
return len;
} else
return fwrite(buf, len, 1, f_out);
}
static size_t valist_to_buf(char **buffer, const char *fmt, va_list src_args)
{
size_t len, cur = LOG_START_SZ;
va_list args;
do {
*buffer = calloc(1, cur);
va_copy(args, src_args);
len = vsnprintf(*buffer, cur, fmt, args);
va_end(args);
if (len < cur)
break;
cur = len + 1;
free(*buffer);
} while (1);
return len;
}
size_t log_valist(const char *fmt, va_list args)
{
char *buffer;
size_t len;
len = valist_to_buf(&buffer, fmt, args);
len = log_info_buf(buffer, len);
free(buffer);
return len;
}
size_t log_info(const char *format, ...)
{
va_list args;
size_t ret;
va_start(args, format);
ret = log_valist(format, args);
va_end(args);
return ret;
}
size_t __log_buf(struct buf_output *buf, const char *format, ...)
{
char *buffer;
va_list args;
size_t len;
va_start(args, format);
len = valist_to_buf(&buffer, format, args);
va_end(args);
len = buf_output_add(buf, buffer, len);
free(buffer);
return len;
}
int log_info_flush(void)
{
if (is_backend || log_syslog)
return 0;
return fflush(f_out);
}
size_t log_err(const char *format, ...)
{
size_t ret, len;
char *buffer;
va_list args;
va_start(args, format);
len = valist_to_buf(&buffer, format, args);
va_end(args);
if (is_backend) {
ret = fio_server_text_output(FIO_LOG_ERR, buffer, len);
if (ret != -1)
goto done;
}
if (log_syslog) {
syslog(LOG_INFO, "%s", buffer);
ret = len;
} else {
if (f_err != stderr)
ret = fwrite(buffer, len, 1, stderr);
ret = fwrite(buffer, len, 1, f_err);
}
done:
free(buffer);
return ret;
}
const char *log_get_level(int level)
{
static const char *levels[] = { "Unknown", "Debug", "Info", "Error",
"Unknown" };
if (level >= FIO_LOG_NR)
level = FIO_LOG_NR;
return levels[level];
}