chiark / gitweb /
tone down logging
[disorder] / lib / sink.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2004 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <stdio.h>
25#include <stdarg.h>
26#include <string.h>
27#include <errno.h>
28
29#include "mem.h"
30#include "vector.h"
31#include "sink.h"
32#include "log.h"
33#include "printf.h"
34
35int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
36 return byte_vsinkprintf(s, fmt, ap);
37}
38
39int sink_printf(struct sink *s, const char *fmt, ...) {
40 va_list ap;
41 int n;
42
43 va_start(ap, fmt);
44 n = byte_vsinkprintf(s, fmt, ap);
45 va_end(ap);
46 return n;
47}
48
49/* stdio sink *****************************************************************/
50
51struct stdio_sink {
52 struct sink s;
53 const char *name;
54 FILE *fp;
55};
56
57#define S(s) ((struct stdio_sink *)s)
58
59static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
60 int n = fwrite(buffer, 1, nbytes, S(s)->fp);
61 if(n < nbytes) {
62 if(S(s)->name)
63 fatal(errno, "error writing to %s", S(s)->name);
64 else
65 return -1;
66 }
67 return n;
68}
69
70struct sink *sink_stdio(const char *name, FILE *fp) {
71 struct stdio_sink *s = xmalloc(sizeof *s);
72
73 s->s.write = sink_stdio_write;
74 s->name = name;
75 s->fp = fp;
76 return (struct sink *)s;
77}
78
79/* dynstr sink ****************************************************************/
80
81struct dynstr_sink {
82 struct sink s;
83 struct dynstr *d;
84};
85
86static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
87 dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
88 return nbytes;
89}
90
91struct sink *sink_dynstr(struct dynstr *output) {
92 struct dynstr_sink *s = xmalloc(sizeof *s);
93
94 s->s.write = sink_dynstr_write;
95 s->d = output;
96 return (struct sink *)s;
97}
98
99/*
100Local Variables:
101c-basic-offset:2
102comment-column:40
103End:
104*/