Unbreak output buffer logging over the network
[fio.git] / lib / output_buffer.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "output_buffer.h"
6 #include "../log.h"
7
8 #define BUF_INC 1024
9
10 void buf_output_init(struct buf_output *out)
11 {
12         out->max_buflen = 0;
13         out->buflen = 0;
14         out->buf = NULL;
15 }
16
17 void buf_output_free(struct buf_output *out)
18 {
19         free(out->buf);
20 }
21
22 size_t buf_output_add(struct buf_output *out, const char *buf, size_t len)
23 {
24         while (out->max_buflen - out->buflen < len) {
25                 size_t old_max = out->max_buflen;
26
27                 out->max_buflen += BUF_INC;
28                 out->buf = realloc(out->buf, out->max_buflen);
29                 memset(&out->buf[old_max], 0, BUF_INC);
30         }
31
32         memcpy(&out->buf[out->buflen], buf, len);
33         out->buflen += len;
34         return len;
35 }
36
37 size_t buf_output_flush(struct buf_output *out)
38 {
39         size_t ret = 0;
40
41         if (out->buflen) {
42                 ret = log_info_buf(out->buf, out->buflen);
43                 memset(out->buf, 0, out->max_buflen);
44                 out->buflen = 0;
45         }
46
47         return ret;
48 }