BSD 4_3 release
[unix-history] / usr / src / ucb / logger.c
CommitLineData
bede97d4
EA
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
95f51977 14static char sccsid[] = "@(#)logger.c 6.2 (Berkeley) 9/19/85";
bede97d4
EA
15#endif not lint
16
17#include <stdio.h>
18#include <syslog.h>
19#include <ctype.h>
20
21/*
22** LOGGER -- read and log utility
23**
24** This routine reads from an input and arranges to write the
25** result on the system log, along with a useful tag.
26*/
27
28main(argc, argv)
29 int argc;
30 char **argv;
31{
32 char buf[200];
33 char *tag;
34 register char *p;
35 int pri = LOG_NOTICE;
36 int logflags = 0;
37 extern char *getlogin();
38
39 /* initialize */
40 tag = getlogin();
41
42 /* crack arguments */
43 while (--argc > 0)
44 {
45 p = *++argv;
46 if (*p != '-')
47 break;
48
49 switch (*++p)
50 {
51 case '\0': /* dummy */
52 /* this can be used to give null parameters */
53 break;
54
55 case 't': /* tag */
56 if (argc > 1 && argv[1][0] != '-')
57 {
58 argc--;
59 tag = *++argv;
60 }
61 else
62 tag = NULL;
63 break;
64
65 case 'p': /* priority */
66 if (argc > 1 && argv[1][0] != '-')
67 {
68 argc--;
69 pri = pencode(*++argv);
70 }
71 break;
72
73 case 'i': /* log process id also */
74 logflags |= LOG_PID;
75 break;
76
77 case 'f': /* file to log */
78 if (argc > 1 && argv[1][0] != '-')
79 {
80 argc--;
81 if (freopen(*++argv, "r", stdin) == NULL)
82 {
83 fprintf("logger: ");
84 perror(*argv);
85 exit(1);
86 }
87 }
88 break;
89
90 default:
91 fprintf(stderr, "logger: unknown flag -%s\n", p);
92 break;
93 }
94 }
95
96 /* setup for logging */
97 openlog(tag, logflags, 0);
98 (void) fclose(stdout);
99
100 /* log input line if appropriate */
101 if (argc > 0)
102 {
103 char buf[120];
104
105 buf[0] = '\0';
106 while (argc-- > 0)
107 {
108 strcat(buf, " ");
109 strcat(buf, *argv++);
110 }
111 syslog(pri, buf + 1);
112 exit(0);
113 }
114
115 /* main loop */
116 while (fgets(buf, sizeof buf, stdin) != NULL)
117 syslog(pri, buf);
118
119 exit(0);
120}
121
122
123struct code {
124 char *c_name;
125 int c_val;
126};
127
128struct code PriNames[] = {
129 "panic", LOG_EMERG,
130 "emerg", LOG_EMERG,
131 "alert", LOG_ALERT,
132 "crit", LOG_CRIT,
133 "err", LOG_ERR,
134 "error", LOG_ERR,
135 "warn", LOG_WARNING,
136 "warning", LOG_WARNING,
137 "notice", LOG_NOTICE,
138 "info", LOG_INFO,
139 "debug", LOG_DEBUG,
140 NULL, -1
141};
142
143struct code FacNames[] = {
144 "kern", LOG_KERN,
145 "user", LOG_USER,
146 "mail", LOG_MAIL,
623d5539 147 "daemon", LOG_DAEMON,
bede97d4
EA
148 "auth", LOG_AUTH,
149 "security", LOG_AUTH,
150 "local0", LOG_LOCAL0,
151 "local1", LOG_LOCAL1,
152 "local2", LOG_LOCAL2,
153 "local3", LOG_LOCAL3,
154 "local4", LOG_LOCAL4,
155 "local5", LOG_LOCAL5,
156 "local6", LOG_LOCAL6,
157 "local7", LOG_LOCAL7,
158 NULL, -1
159};
160
161
162/*
163 * Decode a symbolic name to a numeric value
164 */
165
166pencode(s)
167 register char *s;
168{
169 register char *p;
170 int lev;
171 int fac;
172 char buf[100];
173
174 for (p = buf; *s && *s != '.'; )
175 *p++ = *s++;
176 *p = '\0';
177 if (*s++) {
178 fac = decode(buf, FacNames);
179 if (fac < 0)
180 bailout("unknown facility name: ", buf);
181 for (p = buf; *p++ = *s++; )
182 continue;
183 } else
184 fac = 0;
185 lev = decode(buf, PriNames);
186 if (lev < 0)
187 bailout("unknown priority name: ", buf);
188
189 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
190}
191
192
193decode(name, codetab)
194 char *name;
195 struct code *codetab;
196{
197 register struct code *c;
198 register char *p;
199 char buf[40];
200
201 if (isdigit(*name))
202 return (atoi(name));
203
204 (void) strcpy(buf, name);
205 for (p = buf; *p; p++)
206 if (isupper(*p))
207 *p = tolower(*p);
208 for (c = codetab; c->c_name; c++)
209 if (!strcmp(buf, c->c_name))
210 return (c->c_val);
211
212 return (-1);
213}
214
215bailout(a, b)
216 char *a, *b;
217{
218 fprintf(stderr, "logger: %s%s\n", a, b);
219 exit(1);
220}