Commit | Line | Data |
---|---|---|
bede97d4 EA |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
7dc01867 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
5e8b0e60 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bede97d4 EA |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
20 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
21 | All rights reserved.\n"; | |
7dc01867 | 22 | #endif /* not lint */ |
bede97d4 EA |
23 | |
24 | #ifndef lint | |
cdb01b0c | 25 | static char sccsid[] = "@(#)logger.c 6.9 (Berkeley) %G%"; |
7dc01867 | 26 | #endif /* not lint */ |
bede97d4 EA |
27 | |
28 | #include <stdio.h> | |
29 | #include <syslog.h> | |
30 | #include <ctype.h> | |
31 | ||
32 | /* | |
33 | ** LOGGER -- read and log utility | |
34 | ** | |
35 | ** This routine reads from an input and arranges to write the | |
36 | ** result on the system log, along with a useful tag. | |
37 | */ | |
38 | ||
39 | main(argc, argv) | |
40 | int argc; | |
41 | char **argv; | |
42 | { | |
7dc01867 KB |
43 | extern char *optarg; |
44 | extern int optind; | |
bede97d4 | 45 | int pri = LOG_NOTICE; |
7dc01867 KB |
46 | int ch, logflags = 0; |
47 | char *tag, buf[200], *getlogin(); | |
48 | ||
49 | tag = NULL; | |
50 | while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF) | |
51 | switch((char)ch) { | |
52 | case 'f': /* file to log */ | |
53 | if (freopen(optarg, "r", stdin) == NULL) { | |
54 | fprintf("logger: "); | |
55 | perror(optarg); | |
56 | exit(1); | |
bede97d4 | 57 | } |
bede97d4 | 58 | break; |
7dc01867 | 59 | case 'i': /* log process id also */ |
bede97d4 EA |
60 | logflags |= LOG_PID; |
61 | break; | |
7dc01867 KB |
62 | case 'p': /* priority */ |
63 | pri = pencode(optarg); | |
bede97d4 | 64 | break; |
7dc01867 KB |
65 | case 't': /* tag */ |
66 | tag = optarg; | |
bede97d4 | 67 | break; |
7dc01867 KB |
68 | case '?': |
69 | default: | |
70 | usage(); | |
bede97d4 | 71 | } |
7dc01867 KB |
72 | argc -= optind; |
73 | argv += optind; | |
bede97d4 EA |
74 | |
75 | /* setup for logging */ | |
7dc01867 | 76 | openlog(tag ? tag : getlogin(), logflags, 0); |
bede97d4 EA |
77 | (void) fclose(stdout); |
78 | ||
79 | /* log input line if appropriate */ | |
7dc01867 KB |
80 | if (argc > 0) { |
81 | register char *p, *endp; | |
82 | int len; | |
83 | ||
84 | for (p = buf, endp = buf + sizeof(buf) - 1;;) { | |
85 | len = strlen(*argv); | |
71ed3b14 MK |
86 | if (p + len < endp && p > buf) { |
87 | *--p = '\0'; | |
cdb01b0c | 88 | syslog(pri, "%s", buf); |
71ed3b14 MK |
89 | p = buf; |
90 | } | |
91 | if (len > sizeof(buf) - 1) { | |
cdb01b0c | 92 | syslog(pri, "%s", *argv++); |
71ed3b14 MK |
93 | if (!--argc) |
94 | break; | |
95 | } else { | |
7dc01867 KB |
96 | bcopy(*argv++, p, len); |
97 | p += len; | |
98 | if (!--argc) | |
99 | break; | |
100 | *p++ = ' '; | |
7dc01867 | 101 | *--p = '\0'; |
7dc01867 KB |
102 | } |
103 | } | |
104 | if (p != buf) { | |
105 | *p = '\0'; | |
cdb01b0c | 106 | syslog(pri, "%s", buf); |
bede97d4 | 107 | } |
bede97d4 EA |
108 | exit(0); |
109 | } | |
110 | ||
111 | /* main loop */ | |
7dc01867 | 112 | while (fgets(buf, sizeof(buf), stdin) != NULL) |
cdb01b0c | 113 | syslog(pri, "%s", buf); |
bede97d4 EA |
114 | |
115 | exit(0); | |
116 | } | |
117 | ||
118 | ||
119 | struct code { | |
120 | char *c_name; | |
121 | int c_val; | |
122 | }; | |
123 | ||
124 | struct code PriNames[] = { | |
125 | "panic", LOG_EMERG, | |
126 | "emerg", LOG_EMERG, | |
127 | "alert", LOG_ALERT, | |
128 | "crit", LOG_CRIT, | |
129 | "err", LOG_ERR, | |
130 | "error", LOG_ERR, | |
131 | "warn", LOG_WARNING, | |
132 | "warning", LOG_WARNING, | |
133 | "notice", LOG_NOTICE, | |
134 | "info", LOG_INFO, | |
135 | "debug", LOG_DEBUG, | |
136 | NULL, -1 | |
137 | }; | |
138 | ||
139 | struct code FacNames[] = { | |
140 | "kern", LOG_KERN, | |
141 | "user", LOG_USER, | |
142 | "mail", LOG_MAIL, | |
623d5539 | 143 | "daemon", LOG_DAEMON, |
bede97d4 EA |
144 | "auth", LOG_AUTH, |
145 | "security", LOG_AUTH, | |
3f980dd2 EA |
146 | "syslog", LOG_SYSLOG, |
147 | "lpr", LOG_LPR, | |
148 | "news", LOG_NEWS, | |
09fa8a60 | 149 | "uucp", LOG_UUCP, |
bede97d4 EA |
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 | ||
166 | pencode(s) | |
167 | register char *s; | |
168 | { | |
7dc01867 KB |
169 | char *save; |
170 | int fac, lev; | |
171 | ||
172 | for (save = s; *s && *s != '.'; ++s); | |
173 | if (*s) { | |
174 | *s = '\0'; | |
175 | fac = decode(save, FacNames); | |
bede97d4 | 176 | if (fac < 0) |
7dc01867 KB |
177 | bailout("unknown facility name: ", save); |
178 | *s++ = '.'; | |
179 | } | |
180 | else { | |
bede97d4 | 181 | fac = 0; |
7dc01867 KB |
182 | s = save; |
183 | } | |
184 | lev = decode(s, PriNames); | |
bede97d4 | 185 | if (lev < 0) |
7dc01867 | 186 | bailout("unknown priority name: ", save); |
bede97d4 EA |
187 | return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); |
188 | } | |
189 | ||
190 | ||
191 | decode(name, codetab) | |
192 | char *name; | |
193 | struct code *codetab; | |
194 | { | |
195 | register struct code *c; | |
bede97d4 EA |
196 | |
197 | if (isdigit(*name)) | |
198 | return (atoi(name)); | |
199 | ||
bede97d4 | 200 | for (c = codetab; c->c_name; c++) |
7dc01867 | 201 | if (!strcasecmp(name, c->c_name)) |
bede97d4 EA |
202 | return (c->c_val); |
203 | ||
204 | return (-1); | |
205 | } | |
206 | ||
7dc01867 KB |
207 | bailout(msg, arg) |
208 | char *msg, *arg; | |
209 | { | |
210 | fprintf(stderr, "logger: %s%s\n", msg, arg); | |
211 | exit(1); | |
212 | } | |
213 | ||
214 | usage() | |
bede97d4 | 215 | { |
7dc01867 KB |
216 | fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n", |
217 | stderr); | |
bede97d4 EA |
218 | exit(1); |
219 | } |