Merge branch 'gfio' of ssh://brick.kernel.dk/data/git/fio into gfio
[fio.git] / log.c
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
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
16         if (log_syslog)
17                 syslog(LOG_INFO, "%s", buffer);
18         else
19                 len = fwrite(buffer, len, 1, f_out);
20
21         return len;
22 }
23
24 int log_local_buf(const char *buf, size_t len)
25 {
26         if (log_syslog)
27                 syslog(LOG_INFO, "%s", buf);
28         else
29                 len = fwrite(buf, len, 1, f_out);
30
31         return len;
32 }
33
34 int log_local(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         if (log_syslog)
45                 syslog(LOG_INFO, "%s", buffer);
46         else
47                 len = fwrite(buffer, len, 1, f_out);
48
49         return len;
50 }
51
52 int log_info(const char *format, ...)
53 {
54         char buffer[1024];
55         va_list args;
56         size_t len;
57
58         va_start(args, format);
59         len = vsnprintf(buffer, sizeof(buffer), format, args);
60         va_end(args);
61
62         if (is_backend)
63                 return fio_server_text_output(FIO_LOG_INFO, buffer, len);
64         else if (log_syslog) {
65                 syslog(LOG_INFO, "%s", buffer);
66                 return len;
67         } else
68                 return fwrite(buffer, len, 1, f_out);
69 }
70
71 int log_err(const char *format, ...)
72 {
73         char buffer[1024];
74         va_list args;
75         size_t len;
76
77         va_start(args, format);
78         len = vsnprintf(buffer, sizeof(buffer), format, args);
79         va_end(args);
80
81         if (is_backend)
82                 return fio_server_text_output(FIO_LOG_ERR, buffer, len);
83         else if (log_syslog) {
84                 syslog(LOG_INFO, "%s", buffer);
85                 return len;
86         } else {
87                 if (f_err != stderr) {
88                         int fio_unused ret;
89
90                         ret = fwrite(buffer, len, 1, stderr);
91                 }
92
93                 return fwrite(buffer, len, 1, f_err);
94         }
95 }
96
97 const char *log_get_level(int level)
98 {
99         static const char *levels[] = { "Unknown", "Debug", "Info", "Error",
100                                                 "Unknown" };
101
102         if (level >= FIO_LOG_NR)
103                 level = FIO_LOG_NR;
104
105         return levels[level];
106 }