filesetup: create zbd_info before jumping to done label
[fio.git] / log.c
CommitLineData
ac9b9101 1#include <unistd.h>
ac9b9101
JA
2#include <string.h>
3#include <stdarg.h>
e46d8091 4#include <syslog.h>
3c39a379 5
ac9b9101 6#include "fio.h"
9274160e 7#include "oslib/asprintf.h"
7d64aa48 8
830814d4
JA
9size_t log_info_buf(const char *buf, size_t len)
10{
3dc3aa41
TK
11 /*
12 * buf could be NULL (not just "").
13 */
14 if (!buf)
15 return 0;
16
408e0b90 17 if (is_backend) {
34a3a855 18 ssize_t ret = fio_server_text_output(FIO_LOG_INFO, buf, len);
408e0b90
JA
19 if (ret != -1)
20 return ret;
21 }
22
23 if (log_syslog) {
830814d4
JA
24 syslog(LOG_INFO, "%s", buf);
25 return len;
26 } else
27 return fwrite(buf, len, 1, f_out);
28}
29
fc97d6ae 30size_t log_valist(const char *fmt, va_list args)
3c39a379 31{
7d64aa48 32 char *buffer;
9274160e 33 int len;
7d64aa48 34
9274160e
BVA
35 len = vasprintf(&buffer, fmt, args);
36 if (len < 0)
37 return 0;
fc97d6ae
JA
38 len = log_info_buf(buffer, len);
39 free(buffer);
f29b25a3 40
fc97d6ae
JA
41 return len;
42}
7d64aa48 43
e5f9a813 44/* add prefix for the specified type in front of the valist */
4065c6a4 45#ifdef FIO_INC_DEBUG
e5f9a813
RE
46void log_prevalist(int type, const char *fmt, va_list args)
47{
9274160e
BVA
48 char *buf1, *buf2;
49 int len;
e5f9a813
RE
50 pid_t pid;
51
52 pid = gettid();
53 if (fio_debug_jobp && *fio_debug_jobp != -1U
54 && pid != *fio_debug_jobp)
55 return;
56
9274160e
BVA
57 len = vasprintf(&buf1, fmt, args);
58 if (len < 0)
59 return;
60 len = asprintf(&buf2, "%-8s %-5u %s", debug_levels[type].name,
61 (int) pid, buf1);
62 free(buf1);
63 if (len < 0)
64 return;
402859b3 65 log_info_buf(buf2, len);
9274160e 66 free(buf2);
e5f9a813 67}
4065c6a4 68#endif
e5f9a813 69
34a3a855 70ssize_t log_info(const char *format, ...)
fc97d6ae
JA
71{
72 va_list args;
34a3a855 73 ssize_t ret;
7d64aa48 74
fc97d6ae
JA
75 va_start(args, format);
76 ret = log_valist(format, args);
77 va_end(args);
7d64aa48 78
fc97d6ae 79 return ret;
f29b25a3
JA
80}
81
5768cc2b 82size_t __log_buf(struct buf_output *buf, const char *format, ...)
a666cab8 83{
7d64aa48 84 char *buffer;
a666cab8 85 va_list args;
9274160e 86 int len;
a666cab8 87
fc97d6ae 88 va_start(args, format);
9274160e 89 len = vasprintf(&buffer, format, args);
fc97d6ae 90 va_end(args);
9274160e
BVA
91 if (len < 0)
92 return 0;
fc97d6ae 93 len = buf_output_add(buf, buffer, len);
7d64aa48
JA
94 free(buffer);
95
fc97d6ae 96 return len;
a666cab8
JA
97}
98
fdd5f15f
VKF
99int log_info_flush(void)
100{
101 if (is_backend || log_syslog)
102 return 0;
103
104 return fflush(f_out);
105}
106
34a3a855 107ssize_t log_err(const char *format, ...)
fb71fbd7 108{
34a3a855 109 ssize_t ret;
9274160e 110 int len;
7d64aa48 111 char *buffer;
ac9b9101 112 va_list args;
f29b25a3 113
fc97d6ae 114 va_start(args, format);
9274160e 115 len = vasprintf(&buffer, format, args);
fc97d6ae 116 va_end(args);
9274160e
BVA
117 if (len < 0)
118 return len;
fb71fbd7 119
408e0b90 120 if (is_backend) {
7d64aa48 121 ret = fio_server_text_output(FIO_LOG_ERR, buffer, len);
408e0b90 122 if (ret != -1)
7d64aa48 123 goto done;
408e0b90
JA
124 }
125
126 if (log_syslog) {
13755d94 127 syslog(LOG_INFO, "%s", buffer);
7d64aa48 128 ret = len;
13755d94 129 } else {
7d64aa48 130 if (f_err != stderr)
1f39e555 131 ret = fwrite(buffer, len, 1, stderr);
fb71fbd7 132
7d64aa48 133 ret = fwrite(buffer, len, 1, f_err);
f29b25a3 134 }
7d64aa48
JA
135
136done:
137 free(buffer);
138 return ret;
e3cedca7 139}
3ad04399
JA
140
141const char *log_get_level(int level)
142{
143 static const char *levels[] = { "Unknown", "Debug", "Info", "Error",
144 "Unknown" };
145
146 if (level >= FIO_LOG_NR)
147 level = FIO_LOG_NR;
148
149 return levels[level];
150}