2 * UCW Library -- Printf on Fastbuf Streams
4 * (c) 2002--2013 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/fastbuf.h>
12 #include <ucw/resource.h>
19 vbprintf(struct fastbuf *b, const char *msg, va_list args)
26 remains = bdirect_write_prepare(b, &buf);
27 len = vsnprintf(buf, remains, msg, args2);
32 bdirect_write_commit(b, buf + len);
37 * We need a temporary buffer. If it is small, let's use stack.
38 * Otherwise, we malloc it, but we have to be careful, since we
39 * might be running inside a transaction and bwrite() could
42 * FIXME: This deserves a more systematic solution, the same
43 * problem is likely to happen at other places, too.
45 int bufsize = len + 1;
46 struct resource *res = NULL;
47 byte *extra_buffer = NULL;
49 buf = alloca(bufsize);
50 else if (rp_current())
51 buf = res_malloc(bufsize, &res);
53 buf = extra_buffer = xmalloc(bufsize);
55 vsnprintf(buf, bufsize, msg, args);
60 else if (extra_buffer)
66 bprintf(struct fastbuf *b, const char *msg, ...)
72 res = vbprintf(b, msg, args);
81 struct fastbuf *b = bfdopen_shared(1, 65536);
82 for (int i=0; i<10000; i++)
83 bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");