1 /* vi: set sw=4 ts=4: */
3 * Mini logger implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
32 #if !defined BB_SYSLOGD
35 #include <sys/syslog.h>
38 /* We have to do this since the header file defines static
39 * structues. Argh.... bad libc, bad, bad...
41 #include <sys/syslog.h>
42 typedef struct _code {
46 extern CODE prioritynames[];
47 extern CODE facilitynames[];
50 static const char logger_usage[] =
51 "logger [OPTION]... [MESSAGE]\n\n"
52 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n"
54 "\t-s\tLog to stderr as well as the system log.\n"
55 "\t-t\tLog using the specified tag (defaults to user name).\n"
57 "\t-p\tEnter the message with the specified priority.\n"
58 "\t\tThis may be numerical or a ``facility.level'' pair.\n";
61 /* Decode a symbolic name to a numeric value
62 * this function is based on code
63 * Copyright (c) 1983, 1993
64 * The Regents of the University of California. All rights reserved.
66 static int decode(char *name, CODE * codetab)
72 for (c = codetab; c->c_name; c++) {
73 if (!strcasecmp(name, c->c_name)) {
81 /* Decode a symbolic name to a numeric value
82 * this function is based on code
83 * Copyright (c) 1983, 1993
84 * The Regents of the University of California. All rights reserved.
86 static int pencode(char *s)
89 int lev, fac = LOG_USER;
91 for (save = s; *s && *s != '.'; ++s);
94 fac = decode(save, facilitynames);
96 fprintf(stderr, "unknown facility name: %s\n", save);
103 lev = decode(s, prioritynames);
105 fprintf(stderr, "unknown priority name: %s\n", save);
108 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
112 extern int logger_main(int argc, char **argv)
114 int pri = LOG_USER | LOG_NOTICE;
116 int fromStdinFlag = FALSE;
117 int stopLookingAtMeLikeThat = FALSE;
118 char *message, buf[1024], name[128];
120 /* Fill out the name string early (may be overwritten later */
121 my_getpwuid(name, geteuid());
123 /* Parse any options */
124 while (--argc > 0 && **(++argv) == '-') {
125 if (*((*argv) + 1) == '\0') {
126 fromStdinFlag = TRUE;
128 stopLookingAtMeLikeThat = FALSE;
129 while (*(++(*argv)) && stopLookingAtMeLikeThat == FALSE) {
132 option |= LOG_PERROR;
138 pri = pencode(*(++argv));
139 stopLookingAtMeLikeThat = TRUE;
145 strncpy(name, *(++argv), sizeof(name));
146 stopLookingAtMeLikeThat = TRUE;
154 if (fromStdinFlag == TRUE) {
155 /* read from stdin */
158 while ((c = getc(stdin)) != EOF && i < sizeof(buf)) {
166 fprintf(stderr, "No message\n");
171 openlog(name, option, (pri | LOG_FACMASK));
172 syslog(pri, message);