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