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>
18 vbprintf(struct fastbuf *b, const char *msg, va_list args)
25 remains = bdirect_write_prepare(b, &buf);
26 len = vsnprintf(buf, remains, msg, args2);
31 bdirect_write_commit(b, buf + len);
36 * We need a temporary buffer. If it is small, let's use stack.
37 * Otherwise, we malloc it, but we have to be careful, since we
38 * might be running inside a transaction and bwrite() could
41 * FIXME: This deserves a more systematic solution, the same
42 * problem is likely to happen at other places, too.
44 int bufsize = len + 1;
45 struct resource *res = NULL;
46 byte *extra_buffer = NULL;
48 buf = alloca(bufsize);
49 else if (rp_current())
50 buf = res_malloc(bufsize, &res);
52 buf = extra_buffer = xmalloc(bufsize);
54 vsnprintf(buf, bufsize, msg, args);
59 else if (extra_buffer)
65 bprintf(struct fastbuf *b, const char *msg, ...)
71 res = vbprintf(b, msg, args);
80 struct fastbuf *b = bfdopen_shared(1, 65536);
81 for (int i=0; i<10000; i++)
82 bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");