1 /* $NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: logger.c,v 1.17 2012/04/27 06:30:48 wiz Exp $");
56 static int decode(const char *, const CODE
*);
57 static int pencode(char *);
58 __dead
static void usage(void);
61 * logger -- read and log utility
63 * Reads from an input and arranges to write the result on the system
67 main(int argc
, char *argv
[])
69 int ch
, logflags
, pri
;
72 const char *msgid
= "-";
78 while ((ch
= getopt(argc
, argv
, "cd:f:im:np:st:")) != -1)
80 case 'c': /* log to console */
83 case 'd': /* structured data field */
86 case 'f': /* file to log */
87 if (freopen(optarg
, "r", stdin
) == NULL
)
88 err(EXIT_FAILURE
, "%s", optarg
);
90 case 'i': /* log process id also */
93 case 'm': /* msgid field */
96 case 'n': /* open log file immediately */
97 logflags
|= LOG_NDELAY
;
99 case 'p': /* priority */
100 pri
= pencode(optarg
);
102 case 's': /* log to standard error */
103 logflags
|= LOG_PERROR
;
115 /* setup for logging */
116 openlog(tag
!= NULL
? tag
: getlogin(), logflags
, 0);
117 (void)fclose(stdout
);
119 /* log input line if appropriate */
124 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
!= NULL
;) {
126 if (p
+ len
> endp
&& p
> buf
) {
127 syslogp(pri
, msgid
, sd
, "%s", buf
);
130 if (len
> sizeof(buf
) - 1)
131 syslogp(pri
, msgid
, sd
, "%s", *argv
++);
135 memmove(p
, *argv
++, len
);
140 syslogp(pri
, msgid
, sd
, "%s", buf
);
141 } else /* TODO: allow syslog-protocol messages from file/stdin
142 * but that will require parsing the line to split
143 * it into three fields.
145 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
)
146 syslogp(pri
, msgid
, sd
, "%s", buf
);
153 * Decode a symbolic name to a numeric value
161 for (save
= s
; *s
!= '\0' && *s
!= '.'; ++s
)
165 fac
= decode(save
, facilitynames
);
167 errx(EXIT_FAILURE
, "unknown facility name: %s", save
);
173 lev
= decode(s
, prioritynames
);
175 errx(EXIT_FAILURE
, "unknown priority name: %s", s
);
176 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
180 decode(const char *name
, const CODE
*codetab
)
184 if (isdigit((unsigned char)*name
))
187 for (c
= codetab
; c
->c_name
!= NULL
; c
++)
188 if (strcasecmp(name
, c
->c_name
) == 0)
198 (void)fprintf(stderr
,
199 "Usage: %s [-cins] [-d SD] [-f file] [-m msgid] "
200 "[-p pri] [-t tag] [message ...]\n",