log: unify the logging handlers
[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 "../minmax.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         if (out->max_buflen - out->buflen < len) {
25                 size_t need = len - (out->max_buflen - out->buflen);
26                 size_t old_max = out->max_buflen;
27
28                 need = max((size_t) BUF_INC, need);
29                 out->max_buflen += need;
30                 out->buf = realloc(out->buf, out->max_buflen);
31
32                 old_max = max(old_max, out->buflen + len);
33                 if (old_max + need > out->max_buflen)
34                         need = out->max_buflen - old_max;
35                 memset(&out->buf[old_max], 0, need);
36         }
37
38         memcpy(&out->buf[out->buflen], buf, len);
39         out->buflen += len;
40         return len;
41 }
42
43 void buf_output_clear(struct buf_output *out)
44 {
45         if (out->buflen) {
46                 memset(out->buf, 0, out->max_buflen);
47                 out->buflen = 0;
48         }
49 }