829
|
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
|
|
22 GLOBALS(
|
|
23 char *priority_arg;
|
|
24 char *ident;
|
|
25 )
|
|
26
|
|
27 struct mapping {
|
973
|
28 char *key;
|
829
|
29 int value;
|
|
30 };
|
|
31
|
973
|
32 static struct mapping facilities[] = {
|
829
|
33 {"user", LOG_USER}, {"main", LOG_MAIL}, {"news", LOG_NEWS},
|
|
34 {"uucp", LOG_UUCP}, {"daemon", LOG_DAEMON}, {"auth", LOG_AUTH},
|
|
35 {"cron", LOG_CRON}, {"lpr", LOG_LPR}, {"local0", LOG_LOCAL0},
|
|
36 {"local1", LOG_LOCAL1}, {"local2", LOG_LOCAL2}, {"local3", LOG_LOCAL3},
|
|
37 {"local4", LOG_LOCAL4}, {"local5", LOG_LOCAL5}, {"local6", LOG_LOCAL6},
|
|
38 {"local7", LOG_LOCAL7},
|
|
39 {NULL, 0}
|
|
40 };
|
|
41
|
973
|
42 static struct mapping priorities[] = {
|
829
|
43 {"emerg", LOG_EMERG}, {"alert", LOG_ALERT}, {"crit", LOG_CRIT},
|
|
44 {"err", LOG_ERR}, {"warning", LOG_WARNING}, {"notice", LOG_NOTICE},
|
|
45 {"info", LOG_INFO}, {"debug", LOG_DEBUG},
|
|
46 {NULL, 0}
|
|
47 };
|
|
48
|
973
|
49 static int lookup(struct mapping *where, char *key)
|
829
|
50 {
|
973
|
51 for (; where->key; where++)
|
|
52 if (!strcasecmp(key, where->key)) return where->value;
|
829
|
53
|
|
54 return -1;
|
|
55 }
|
|
56
|
973
|
57 void logger_main(void)
|
829
|
58 {
|
973
|
59 int facility = LOG_USER, priority = LOG_NOTICE;
|
|
60 char *message = NULL;
|
|
61
|
|
62 if (toys.optflags & FLAG_p) {
|
|
63 char *sep = strchr(TT.priority_arg, '.');
|
829
|
64
|
973
|
65 if (sep) {
|
|
66 *sep = '\0';
|
|
67 if ((facility = lookup(facilities, TT.priority_arg)) == -1)
|
|
68 error_exit("bad facility: %s", TT.priority_arg);
|
|
69 TT.priority_arg = sep+1;
|
|
70 }
|
|
71
|
|
72 if ((priority = lookup(priorities, TT.priority_arg)) == -1)
|
|
73 error_exit("bad priority: %s", TT.priority_arg);
|
829
|
74 }
|
|
75
|
973
|
76 if (!(toys.optflags & FLAG_t)) {
|
|
77 struct passwd *pw = getpwuid(geteuid());
|
829
|
78
|
973
|
79 if (!pw) perror_exit("getpwuid");
|
829
|
80 TT.ident = xstrdup(pw->pw_name);
|
|
81 }
|
|
82
|
|
83 if (toys.optc) {
|
973
|
84 int length = 0, pos = 0;
|
829
|
85
|
973
|
86 for (;*toys.optargs; toys.optargs++) {
|
829
|
87 length += strlen(*(toys.optargs)) + 1; // plus one for the args spacing
|
|
88 message = xrealloc(message, length + 1); // another one for the null byte
|
|
89
|
|
90 sprintf(message + pos, "%s ", *toys.optargs);
|
|
91 pos = length;
|
|
92 }
|
|
93 } else {
|
|
94 toybuf[readall(0, toybuf, 4096-1)] = '\0';
|
|
95 message = toybuf;
|
|
96 }
|
|
97
|
973
|
98 openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , facility);
|
|
99 syslog(priority, "%s", message);
|
829
|
100 closelog();
|
|
101 }
|