blob: 4822c33526d29fd4b7d9c0731f5030c75c982909 [file] [log] [blame]
Jens Axboeac9b9102011-10-01 15:01:08 -04001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <stdarg.h>
Jens Axboee46d8092011-10-03 09:11:02 +02005#include <syslog.h>
Jens Axboeac9b9102011-10-01 15:01:08 -04006
Jens Axboe3c39a372006-06-06 20:56:12 +02007#include "fio.h"
8
Jens Axboee46d8092011-10-03 09:11:02 +02009int log_valist(const char *str, va_list args)
10{
Jens Axboe734f38c2011-10-04 23:46:22 +020011 char buffer[1024];
12 size_t len;
Jens Axboee46d8092011-10-03 09:11:02 +020013
Jens Axboe734f38c2011-10-04 23:46:22 +020014 len = vsnprintf(buffer, sizeof(buffer), str, args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010015 len = min(len, sizeof(buffer) - 1);
Jens Axboe734f38c2011-10-04 23:46:22 +020016
17 if (log_syslog)
18 syslog(LOG_INFO, "%s", buffer);
19 else
20 len = fwrite(buffer, len, 1, f_out);
21
22 return len;
Jens Axboee46d8092011-10-03 09:11:02 +020023}
24
Jens Axboe13755d92011-10-10 19:51:26 +020025int 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
Jens Axboeac9b9102011-10-01 15:01:08 -040035int log_local(const char *format, ...)
Jens Axboe691c8fb2008-03-07 14:26:26 +010036{
Jens Axboeac9b9102011-10-01 15:01:08 -040037 char buffer[1024];
38 va_list args;
39 size_t len;
40
41 va_start(args, format);
Jens Axboe734f38c2011-10-04 23:46:22 +020042 len = vsnprintf(buffer, sizeof(buffer), format, args);
43 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010044 len = min(len, sizeof(buffer) - 1);
Jens Axboeac9b9102011-10-01 15:01:08 -040045
Jens Axboe734f38c2011-10-04 23:46:22 +020046 if (log_syslog)
47 syslog(LOG_INFO, "%s", buffer);
48 else
49 len = fwrite(buffer, len, 1, f_out);
50
51 return len;
Jens Axboe691c8fb2008-03-07 14:26:26 +010052}
53
Jens Axboeac9b9102011-10-01 15:01:08 -040054int log_info(const char *format, ...)
Jens Axboe3c39a372006-06-06 20:56:12 +020055{
Jens Axboeac9b9102011-10-01 15:01:08 -040056 char buffer[1024];
57 va_list args;
58 size_t len;
Jens Axboef29b25a2007-07-23 08:56:43 +020059
Jens Axboeac9b9102011-10-01 15:01:08 -040060 va_start(args, format);
61 len = vsnprintf(buffer, sizeof(buffer), format, args);
62 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010063 len = min(len, sizeof(buffer) - 1);
Jens Axboef29b25a2007-07-23 08:56:43 +020064
Jens Axboeac9b9102011-10-01 15:01:08 -040065 if (is_backend)
Jens Axboe084d1c62012-03-03 20:28:07 +010066 return fio_server_text_output(FIO_LOG_INFO, buffer, len);
Jens Axboe13755d92011-10-10 19:51:26 +020067 else if (log_syslog) {
68 syslog(LOG_INFO, "%s", buffer);
69 return len;
70 } else
Jens Axboeac9b9102011-10-01 15:01:08 -040071 return fwrite(buffer, len, 1, f_out);
Jens Axboef29b25a2007-07-23 08:56:43 +020072}
73
Vincent Kang Fufdd5f152013-04-26 16:56:01 -060074int log_info_flush(void)
75{
76 if (is_backend || log_syslog)
77 return 0;
78
79 return fflush(f_out);
80}
81
Jens Axboeac9b9102011-10-01 15:01:08 -040082int log_err(const char *format, ...)
Jens Axboef29b25a2007-07-23 08:56:43 +020083{
Jens Axboeac9b9102011-10-01 15:01:08 -040084 char buffer[1024];
85 va_list args;
86 size_t len;
Jens Axboef29b25a2007-07-23 08:56:43 +020087
Jens Axboeac9b9102011-10-01 15:01:08 -040088 va_start(args, format);
89 len = vsnprintf(buffer, sizeof(buffer), format, args);
90 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010091 len = min(len, sizeof(buffer) - 1);
Jens Axboef29b25a2007-07-23 08:56:43 +020092
Jens Axboeac9b9102011-10-01 15:01:08 -040093 if (is_backend)
Jens Axboe084d1c62012-03-03 20:28:07 +010094 return fio_server_text_output(FIO_LOG_ERR, buffer, len);
Jens Axboe13755d92011-10-10 19:51:26 +020095 else if (log_syslog) {
96 syslog(LOG_INFO, "%s", buffer);
97 return len;
98 } else {
Jens Axboe1f39e552011-10-05 11:50:47 +020099 if (f_err != stderr) {
100 int fio_unused ret;
101
102 ret = fwrite(buffer, len, 1, stderr);
103 }
Jens Axboeac9b9102011-10-01 15:01:08 -0400104
105 return fwrite(buffer, len, 1, f_err);
Jens Axboef29b25a2007-07-23 08:56:43 +0200106 }
Jens Axboee3cedca2008-11-19 19:57:52 +0100107}
Jens Axboe3ad04392012-03-26 21:08:46 +0200108
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}