summaryrefslogtreecommitdiffstats
path: root/tests/syslog_caller.c
blob: 1a0b04f8478f3599641c5a41c15ab6d301ba8cc3 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* A testing tool that just emits a number of
 * messages to the system log socket.
 *
 * Options
 *
 * -s severity (0..7 accoding to syslog spec, r "rolling", default 6)
 * -m number of messages to generate (default 500)
 * -C liblognorm-stdlog channel description
 * -f message format to use
 *
 * Part of the testbench for rsyslog.
 *
 * Copyright 2010-2018 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       -or-
 *       see COPYING.ASL20 in the source distribution
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(_AIX)
	#include  <unistd.h>
#else
	#include <getopt.h>
#endif
#include <errno.h>
#include <string.h>
#include <syslog.h>
#ifdef HAVE_LIBLOGGING_STDLOG
#include <liblogging/stdlog.h>
#endif

static enum { FMT_NATIVE, FMT_SYSLOG_INJECT_L, FMT_SYSLOG_INJECT_C
	} fmt = FMT_NATIVE;

static void usage(void)
{
	fprintf(stderr, "usage: syslog_caller num-messages\n");
	exit(1);
}


#ifdef HAVE_LIBLOGGING_STDLOG
/* buffer must be large "enough" [4K?] */
static void
genMsg(char *buf, const int sev, const int iRun)
{
	switch(fmt) {
	case FMT_NATIVE:
		sprintf(buf, "test message nbr %d, severity=%d", iRun, sev);
		break;
	case FMT_SYSLOG_INJECT_L:
		sprintf(buf, "test\n");
		break;
	case FMT_SYSLOG_INJECT_C:
		sprintf(buf, "test 1\t2");
		break;
	}
}
#endif

int main(int argc, char *argv[])
{
	int i;
	int opt;
	int bRollingSev = 0;
	int sev = 6;
	int msgs = 500;
#ifdef HAVE_LIBLOGGING_STDLOG
	stdlog_channel_t logchan = NULL;
	const char *chandesc = "syslog:";
	char msgbuf[4096];
#endif

#ifdef HAVE_LIBLOGGING_STDLOG
	stdlog_init(STDLOG_USE_DFLT_OPTS);
	while((opt = getopt(argc, argv, "m:s:C:f:")) != -1) {
#else
	while((opt = getopt(argc, argv, "m:s:")) != -1) {
#endif
		switch (opt) {
		case 's':	if(*optarg == 'r') {
					bRollingSev = 1;
					sev = 0;
				} else
#ifdef HAVE_LIBLOGGING_STDLOG
					sev = atoi(optarg) % 8;
#else
					sev = atoi(optarg);
#endif
				break;
		case 'm':	msgs = atoi(optarg);
				break;
#ifdef HAVE_LIBLOGGING_STDLOG
		case 'C':	chandesc = optarg;
				break;
		case 'f':	if(!strcmp(optarg, "syslog_inject-l"))
					fmt = FMT_SYSLOG_INJECT_L;
				else if(!strcmp(optarg, "syslog_inject-c"))
					fmt = FMT_SYSLOG_INJECT_C;
				else
					usage();
				break;
#endif
		default:	usage();
#ifdef HAVE_LIBLOGGING_STDLOG
				exit(1);
#endif
				break;
		}
	}

#ifdef HAVE_LIBLOGGING_STDLOG
	if((logchan = stdlog_open(argv[0], 0, STDLOG_LOCAL1, chandesc)) == NULL) {
		fprintf(stderr, "error opening logchannel '%s': %s\n",
			chandesc, strerror(errno));
		exit(1);
	}
#endif
	for(i = 0 ; i < msgs ; ++i) {
#ifdef HAVE_LIBLOGGING_STDLOG
		genMsg(msgbuf, sev, i);
		if(stdlog_log(logchan, sev, "%s", msgbuf) != 0) {
			perror("error writing log record");
			exit(1);
		}
#else
		syslog(sev % 8, "test message nbr %d, severity=%d", i, sev % 8);
#endif
		if(bRollingSev)
#ifdef HAVE_LIBLOGGING_STDLOG
			sev = (sev + 1) % 8;
#else
		sev++;
#endif
	}
	return(0);
}