comparison toys/pending/logger.c @ 829:792d510b8fef

Logger, by Ilya Kuzmich.
author Rob Landley <rob@landley.net>
date Sun, 24 Mar 2013 17:20:47 -0500
parents
children d1768175ce1d
comparison
equal deleted inserted replaced
828:1fdaba9a7124 829:792d510b8fef
1 /* logger.c - Log messages.
2 *
3 * Copyright 2013 Ilya Kuzmich <ilya.kuzmich@gmail.com>
4 *
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/logger.html
6
7 USE_LOGGER(NEWTOY(logger, "st:p:", TOYFLAG_USR|TOYFLAG_BIN))
8
9 config LOGGER
10 bool "logger"
11 default n
12 help
13 usage: hello [-s] [-t tag] [-p [facility.]priority] [message]
14
15 Log message (or stdin) to syslog.
16 */
17
18 #define FOR_logger
19 #include "toys.h"
20 #include <syslog.h>
21 #include <strings.h>
22 #include <string.h>
23
24 GLOBALS(
25 char *priority_arg;
26 char *ident;
27
28 int facility;
29 int priority;
30 )
31
32 struct mapping {
33 const char *key;
34 int value;
35 };
36
37 static const struct mapping facilities[] = {
38 {"user", LOG_USER}, {"main", LOG_MAIL}, {"news", LOG_NEWS},
39 {"uucp", LOG_UUCP}, {"daemon", LOG_DAEMON}, {"auth", LOG_AUTH},
40 {"cron", LOG_CRON}, {"lpr", LOG_LPR}, {"local0", LOG_LOCAL0},
41 {"local1", LOG_LOCAL1}, {"local2", LOG_LOCAL2}, {"local3", LOG_LOCAL3},
42 {"local4", LOG_LOCAL4}, {"local5", LOG_LOCAL5}, {"local6", LOG_LOCAL6},
43 {"local7", LOG_LOCAL7},
44 {NULL, 0}
45 };
46
47 static const struct mapping priorities[] = {
48 {"emerg", LOG_EMERG}, {"alert", LOG_ALERT}, {"crit", LOG_CRIT},
49 {"err", LOG_ERR}, {"warning", LOG_WARNING}, {"notice", LOG_NOTICE},
50 {"info", LOG_INFO}, {"debug", LOG_DEBUG},
51 {NULL, 0}
52 };
53
54 static int lookup(const struct mapping *where, const char *key)
55 {
56 int i;
57 for (i = 0; where[i].key; i++)
58 if (!strcasecmp(key, where[i].key))
59 return where[i].value;
60
61 return -1;
62 }
63
64 static void parse_priority()
65 {
66 char *sep = strchr(TT.priority_arg, '.');
67
68 if (sep)
69 {
70 *sep = '\0';
71 if ((TT.facility = lookup(facilities, TT.priority_arg)) == -1)
72 error_exit("bad facility: %s", TT.priority_arg);
73 TT.priority_arg = sep+1;
74 }
75
76 if ((TT.priority = lookup(priorities, TT.priority_arg)) == -1)
77 error_exit("bad priority: %s", TT.priority_arg);
78 }
79
80 void logger_main(void)
81 {
82 if (toys.optflags & FLAG_p)
83 parse_priority();
84 else
85 {
86 TT.facility = LOG_USER;
87 TT.priority = LOG_NOTICE;
88 }
89
90 if (!(toys.optflags & FLAG_t))
91 {
92 struct passwd *pw = getpwuid(geteuid());
93 if (!pw)
94 perror_exit("getpwuid");
95 TT.ident = xstrdup(pw->pw_name);
96 }
97
98 char *message = NULL;
99 if (toys.optc) {
100 int length = 0;
101 int pos = 0;
102
103 for (;*toys.optargs; (void) *(toys.optargs)++) // shut up gcc
104 {
105 length += strlen(*(toys.optargs)) + 1; // plus one for the args spacing
106 message = xrealloc(message, length + 1); // another one for the null byte
107
108 sprintf(message + pos, "%s ", *toys.optargs);
109 pos = length;
110 }
111 } else {
112 toybuf[readall(0, toybuf, 4096-1)] = '\0';
113 message = toybuf;
114 }
115
116 openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , TT.facility);
117 syslog(TT.priority, "%s", message);
118 closelog();
119 }