1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <syslog.h>
6
7 #include "fio.h"
8
log_valist(const char * str,va_list args)9 int log_valist(const char *str, va_list args)
10 {
11 char buffer[1024];
12 size_t len;
13
14 len = vsnprintf(buffer, sizeof(buffer), str, args);
15 len = min(len, sizeof(buffer) - 1);
16
17 if (is_backend)
18 len = fio_server_text_output(FIO_LOG_INFO, buffer, len);
19 if (log_syslog)
20 syslog(LOG_INFO, "%s", buffer);
21 else
22 len = fwrite(buffer, len, 1, f_out);
23
24 return len;
25 }
26
log_local_buf(const char * buf,size_t len)27 int log_local_buf(const char *buf, size_t len)
28 {
29 if (log_syslog)
30 syslog(LOG_INFO, "%s", buf);
31 else
32 len = fwrite(buf, len, 1, f_out);
33
34 return len;
35 }
36
log_info(const char * format,...)37 int log_info(const char *format, ...)
38 {
39 char buffer[1024];
40 va_list args;
41 size_t len;
42
43 va_start(args, format);
44 len = vsnprintf(buffer, sizeof(buffer), format, args);
45 va_end(args);
46 len = min(len, sizeof(buffer) - 1);
47
48 if (is_backend)
49 return fio_server_text_output(FIO_LOG_INFO, buffer, len);
50 else if (log_syslog) {
51 syslog(LOG_INFO, "%s", buffer);
52 return len;
53 } else
54 return fwrite(buffer, len, 1, f_out);
55 }
56
log_info_flush(void)57 int log_info_flush(void)
58 {
59 if (is_backend || log_syslog)
60 return 0;
61
62 return fflush(f_out);
63 }
64
log_err(const char * format,...)65 int log_err(const char *format, ...)
66 {
67 char buffer[1024];
68 va_list args;
69 size_t len;
70
71 va_start(args, format);
72 len = vsnprintf(buffer, sizeof(buffer), format, args);
73 va_end(args);
74 len = min(len, sizeof(buffer) - 1);
75
76 if (is_backend)
77 return fio_server_text_output(FIO_LOG_ERR, buffer, len);
78 else if (log_syslog) {
79 syslog(LOG_INFO, "%s", buffer);
80 return len;
81 } else {
82 if (f_err != stderr) {
83 int fio_unused ret;
84
85 ret = fwrite(buffer, len, 1, stderr);
86 }
87
88 return fwrite(buffer, len, 1, f_err);
89 }
90 }
91
log_get_level(int level)92 const char *log_get_level(int level)
93 {
94 static const char *levels[] = { "Unknown", "Debug", "Info", "Error",
95 "Unknown" };
96
97 if (level >= FIO_LOG_NR)
98 level = FIO_LOG_NR;
99
100 return levels[level];
101 }
102