Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <stdarg.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 5 | #include <syslog.h> |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 6 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 7 | #include "fio.h" |
| 8 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 9 | int log_valist(const char *str, va_list args) |
| 10 | { |
Jens Axboe | 734f38c | 2011-10-04 23:46:22 +0200 | [diff] [blame] | 11 | char buffer[1024]; |
| 12 | size_t len; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 13 | |
Jens Axboe | 734f38c | 2011-10-04 23:46:22 +0200 | [diff] [blame] | 14 | len = vsnprintf(buffer, sizeof(buffer), str, args); |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 15 | len = min(len, sizeof(buffer) - 1); |
Jens Axboe | 734f38c | 2011-10-04 23:46:22 +0200 | [diff] [blame] | 16 | |
Jens Axboe | 03a0825 | 2013-09-11 08:23:45 -0600 | [diff] [blame] | 17 | if (is_backend) |
| 18 | len = fio_server_text_output(FIO_LOG_INFO, buffer, len); |
Jens Axboe | 734f38c | 2011-10-04 23:46:22 +0200 | [diff] [blame] | 19 | if (log_syslog) |
| 20 | syslog(LOG_INFO, "%s", buffer); |
| 21 | else |
| 22 | len = fwrite(buffer, len, 1, f_out); |
| 23 | |
| 24 | return len; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 27 | int 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 Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 37 | int log_info(const char *format, ...) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 38 | { |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 39 | char buffer[1024]; |
| 40 | va_list args; |
| 41 | size_t len; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 42 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 43 | va_start(args, format); |
| 44 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
| 45 | va_end(args); |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 46 | len = min(len, sizeof(buffer) - 1); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 47 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 48 | if (is_backend) |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 49 | return fio_server_text_output(FIO_LOG_INFO, buffer, len); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 50 | else if (log_syslog) { |
| 51 | syslog(LOG_INFO, "%s", buffer); |
| 52 | return len; |
| 53 | } else |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 54 | return fwrite(buffer, len, 1, f_out); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Vincent Kang Fu | fdd5f15 | 2013-04-26 16:56:01 -0600 | [diff] [blame] | 57 | int log_info_flush(void) |
| 58 | { |
| 59 | if (is_backend || log_syslog) |
| 60 | return 0; |
| 61 | |
| 62 | return fflush(f_out); |
| 63 | } |
| 64 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 65 | int log_err(const char *format, ...) |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 66 | { |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 67 | char buffer[1024]; |
| 68 | va_list args; |
| 69 | size_t len; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 70 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 71 | va_start(args, format); |
| 72 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
| 73 | va_end(args); |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 74 | len = min(len, sizeof(buffer) - 1); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 75 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 76 | if (is_backend) |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 77 | return fio_server_text_output(FIO_LOG_ERR, buffer, len); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 78 | else if (log_syslog) { |
| 79 | syslog(LOG_INFO, "%s", buffer); |
| 80 | return len; |
| 81 | } else { |
Jens Axboe | 1f39e55 | 2011-10-05 11:50:47 +0200 | [diff] [blame] | 82 | if (f_err != stderr) { |
| 83 | int fio_unused ret; |
| 84 | |
| 85 | ret = fwrite(buffer, len, 1, stderr); |
| 86 | } |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 87 | |
| 88 | return fwrite(buffer, len, 1, f_err); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 89 | } |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 90 | } |
Jens Axboe | 3ad0439 | 2012-03-26 21:08:46 +0200 | [diff] [blame] | 91 | |
| 92 | const 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 | } |