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