genfio: Splitting gen_template in 2 parts
[fio.git] / log.c
CommitLineData
ac9b9101
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <stdarg.h>
e46d8091 5#include <syslog.h>
3c39a379 6
ac9b9101 7#include "fio.h"
3c39a379 8
e46d8091
JA
9int log_valist(const char *str, va_list args)
10{
734f38cb
JA
11 char buffer[1024];
12 size_t len;
13
14 len = vsnprintf(buffer, sizeof(buffer), str, args);
98ffb8f3 15 len = min(len, sizeof(buffer) - 1);
734f38cb
JA
16
17 if (log_syslog)
18 syslog(LOG_INFO, "%s", buffer);
19 else
20 len = fwrite(buffer, len, 1, f_out);
21
22 return len;
e46d8091
JA
23}
24
13755d94
JA
25int log_local_buf(const char *buf, size_t len)
26{
27 if (log_syslog)
28 syslog(LOG_INFO, "%s", buf);
29 else
30 len = fwrite(buf, len, 1, f_out);
31
32 return len;
33}
34
ac9b9101 35int log_local(const char *format, ...)
3c39a379 36{
ac9b9101
JA
37 char buffer[1024];
38 va_list args;
39 size_t len;
8de8f047 40
ac9b9101 41 va_start(args, format);
734f38cb
JA
42 len = vsnprintf(buffer, sizeof(buffer), format, args);
43 va_end(args);
98ffb8f3 44 len = min(len, sizeof(buffer) - 1);
3c39a379 45
734f38cb
JA
46 if (log_syslog)
47 syslog(LOG_INFO, "%s", buffer);
48 else
49 len = fwrite(buffer, len, 1, f_out);
50
51 return len;
3c39a379
JA
52}
53
ac9b9101 54int log_info(const char *format, ...)
3c39a379 55{
ac9b9101
JA
56 char buffer[1024];
57 va_list args;
58 size_t len;
3c39a379 59
ac9b9101
JA
60 va_start(args, format);
61 len = vsnprintf(buffer, sizeof(buffer), format, args);
62 va_end(args);
98ffb8f3 63 len = min(len, sizeof(buffer) - 1);
f29b25a3 64
ac9b9101 65 if (is_backend)
084d1c6f 66 return fio_server_text_output(FIO_LOG_INFO, buffer, len);
13755d94
JA
67 else if (log_syslog) {
68 syslog(LOG_INFO, "%s", buffer);
69 return len;
70 } else
ac9b9101 71 return fwrite(buffer, len, 1, f_out);
f29b25a3
JA
72}
73
fdd5f15f
VKF
74int log_info_flush(void)
75{
76 if (is_backend || log_syslog)
77 return 0;
78
79 return fflush(f_out);
80}
81
ac9b9101 82int log_err(const char *format, ...)
fb71fbd7 83{
ac9b9101
JA
84 char buffer[1024];
85 va_list args;
86 size_t len;
f29b25a3 87
ac9b9101
JA
88 va_start(args, format);
89 len = vsnprintf(buffer, sizeof(buffer), format, args);
90 va_end(args);
98ffb8f3 91 len = min(len, sizeof(buffer) - 1);
fb71fbd7 92
ac9b9101 93 if (is_backend)
084d1c6f 94 return fio_server_text_output(FIO_LOG_ERR, buffer, len);
13755d94
JA
95 else if (log_syslog) {
96 syslog(LOG_INFO, "%s", buffer);
97 return len;
98 } else {
1f39e555
JA
99 if (f_err != stderr) {
100 int fio_unused ret;
101
102 ret = fwrite(buffer, len, 1, stderr);
103 }
fb71fbd7 104
ac9b9101 105 return fwrite(buffer, len, 1, f_err);
f29b25a3 106 }
e3cedca7 107}
3ad04399
JA
108
109const char *log_get_level(int level)
110{
111 static const char *levels[] = { "Unknown", "Debug", "Info", "Error",
112 "Unknown" };
113
114 if (level >= FIO_LOG_NR)
115 level = FIO_LOG_NR;
116
117 return levels[level];
118}