blob: c4a3b52075367919b191219f84d14a9b46936338 [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
Jens Axboe03a08252013-09-11 08:23:45 -060017 if (is_backend)
18 len = fio_server_text_output(FIO_LOG_INFO, buffer, len);
Jens Axboe734f38c2011-10-04 23:46:22 +020019 if (log_syslog)
20 syslog(LOG_INFO, "%s", buffer);
21 else
22 len = fwrite(buffer, len, 1, f_out);
23
24 return len;
Jens Axboee46d8092011-10-03 09:11:02 +020025}
26
Jens Axboe13755d92011-10-10 19:51:26 +020027int 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
Jens Axboeac9b9102011-10-01 15:01:08 -040037int log_info(const char *format, ...)
Jens Axboe3c39a372006-06-06 20:56:12 +020038{
Jens Axboeac9b9102011-10-01 15:01:08 -040039 char buffer[1024];
40 va_list args;
41 size_t len;
Jens Axboef29b25a2007-07-23 08:56:43 +020042
Jens Axboeac9b9102011-10-01 15:01:08 -040043 va_start(args, format);
44 len = vsnprintf(buffer, sizeof(buffer), format, args);
45 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010046 len = min(len, sizeof(buffer) - 1);
Jens Axboef29b25a2007-07-23 08:56:43 +020047
Jens Axboeac9b9102011-10-01 15:01:08 -040048 if (is_backend)
Jens Axboe084d1c62012-03-03 20:28:07 +010049 return fio_server_text_output(FIO_LOG_INFO, buffer, len);
Jens Axboe13755d92011-10-10 19:51:26 +020050 else if (log_syslog) {
51 syslog(LOG_INFO, "%s", buffer);
52 return len;
53 } else
Jens Axboeac9b9102011-10-01 15:01:08 -040054 return fwrite(buffer, len, 1, f_out);
Jens Axboef29b25a2007-07-23 08:56:43 +020055}
56
Vincent Kang Fufdd5f152013-04-26 16:56:01 -060057int log_info_flush(void)
58{
59 if (is_backend || log_syslog)
60 return 0;
61
62 return fflush(f_out);
63}
64
Jens Axboeac9b9102011-10-01 15:01:08 -040065int log_err(const char *format, ...)
Jens Axboef29b25a2007-07-23 08:56:43 +020066{
Jens Axboeac9b9102011-10-01 15:01:08 -040067 char buffer[1024];
68 va_list args;
69 size_t len;
Jens Axboef29b25a2007-07-23 08:56:43 +020070
Jens Axboeac9b9102011-10-01 15:01:08 -040071 va_start(args, format);
72 len = vsnprintf(buffer, sizeof(buffer), format, args);
73 va_end(args);
Ken Raeburn98ffb8f2013-01-30 22:31:09 +010074 len = min(len, sizeof(buffer) - 1);
Jens Axboef29b25a2007-07-23 08:56:43 +020075
Jens Axboeac9b9102011-10-01 15:01:08 -040076 if (is_backend)
Jens Axboe084d1c62012-03-03 20:28:07 +010077 return fio_server_text_output(FIO_LOG_ERR, buffer, len);
Jens Axboe13755d92011-10-10 19:51:26 +020078 else if (log_syslog) {
79 syslog(LOG_INFO, "%s", buffer);
80 return len;
81 } else {
Jens Axboe1f39e552011-10-05 11:50:47 +020082 if (f_err != stderr) {
83 int fio_unused ret;
84
85 ret = fwrite(buffer, len, 1, stderr);
86 }
Jens Axboeac9b9102011-10-01 15:01:08 -040087
88 return fwrite(buffer, len, 1, f_err);
Jens Axboef29b25a2007-07-23 08:56:43 +020089 }
Jens Axboee3cedca2008-11-19 19:57:52 +010090}
Jens Axboe3ad04392012-03-26 21:08:46 +020091
92const 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}