blob: 08509b32b0150f85a55d76c38776d00d856be159 [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)
66 return fio_server_text_output(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
Jens Axboeac9b9102011-10-01 15:01:08 -040074int log_err(const char *format, ...)
Jens Axboef29b25a2007-07-23 08:56:43 +020075{
Jens Axboeac9b9102011-10-01 15:01:08 -040076 char buffer[1024];
77 va_list args;
78 size_t len;
Jens Axboef29b25a2007-07-23 08:56:43 +020079
Jens Axboeac9b9102011-10-01 15:01:08 -040080 va_start(args, format);
81 len = vsnprintf(buffer, sizeof(buffer), format, args);
82 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010083 len = min(len, sizeof(buffer) - 1);
Jens Axboef29b25a2007-07-23 08:56:43 +020084
Jens Axboeac9b9102011-10-01 15:01:08 -040085 if (is_backend)
86 return fio_server_text_output(buffer, len);
Jens Axboe13755d92011-10-10 19:51:26 +020087 else if (log_syslog) {
88 syslog(LOG_INFO, "%s", buffer);
89 return len;
90 } else {
Jens Axboe1f39e552011-10-05 11:50:47 +020091 if (f_err != stderr) {
92 int fio_unused ret;
93
94 ret = fwrite(buffer, len, 1, stderr);
95 }
Jens Axboeac9b9102011-10-01 15:01:08 -040096
97 return fwrite(buffer, len, 1, f_err);
Jens Axboef29b25a2007-07-23 08:56:43 +020098 }
Jens Axboee3cedca2008-11-19 19:57:52 +010099}