aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/fifolog/fifolog_writer/fifolog_writer.c
blob: 77776b860fe4592aa3b76f2c46e3a5cd8f003eb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*-
 * Copyright (c) 2005-2008 Poul-Henning Kamp
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <err.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
#include <poll.h>
#include <string.h>
#include <zlib.h>

#include "libfifolog.h"

static void
usage(void)
{
	fprintf(stderr,
	    "Usage: fifolog_writer [-w write-rate] [-s sync-rate] "
	    "[-z compression] file\n");
	exit(EX_USAGE);
}

int
main(int argc, char * const *argv)
{
	struct fifolog_writer *f;
	const char *es;
	struct pollfd pfd[1];
	char buf[BUFSIZ], *p;
	int i, c;
	unsigned w_opt = 10;
	unsigned s_opt = 60;
	unsigned z_opt = Z_BEST_COMPRESSION;

	while ((c = getopt(argc, argv, "w:s:z:")) != -1) {
		switch(c) {
		case 'w':
			w_opt = strtoul(optarg, NULL, 0);
			break;
		case 's':
			s_opt = strtoul(optarg, NULL, 0);
			break;
		case 'z':
			z_opt = strtoul(optarg, NULL, 0);
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if (argc != 1)
		usage();

	if (z_opt > 9)
		usage();

	if (w_opt > s_opt)
		usage();

	f = fifolog_write_new();
	assert(f != NULL);

	es = fifolog_write_open(f, argv[0], w_opt, s_opt, z_opt);
	if (es)
		err(1, "Error: %s", es);

	while (1) {
		pfd[0].fd = 0;
		pfd[0].events = POLLIN;
		i = poll(pfd, 1, 1000);
		if (i == 1) {
			if (fgets(buf, sizeof buf, stdin) == NULL)
				break;
			p = strchr(buf, '\0');
			assert(p != NULL);
			while (p > buf && isspace(p[-1]))
				p--;
			*p = '\0';
			if (*buf != '\0')
				fifolog_write_record_poll(f, 0, 0, buf, 0);
		} else if (i == 0)
			fifolog_write_poll(f, 0);
	}
	fifolog_write_close(f);
	return (0);
}