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