annotate toys/pending/logger.c @ 1276:d48bdc1cb017 draft

Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
author Rob Landley <rob@landley.net>
date Tue, 06 May 2014 06:31:28 -0500
parents f9271a80fedc
children
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"
1019
f9271a80fedc In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents: 973
diff changeset
11 depends on SYSLOGD
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 default n
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
1019
f9271a80fedc In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents: 973
diff changeset
14 usage: logger [-s] [-t tag] [-p [facility.]priority] [message]
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Log message (or stdin) to syslog.
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
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #define FOR_logger
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #include "toys.h"
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 GLOBALS(
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 char *priority_arg;
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 char *ident;
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 )
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
1019
f9271a80fedc In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents: 973
diff changeset
27 extern int logger_lookup(int where, char *key);
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
29 void logger_main(void)
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 {
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
31 int facility = LOG_USER, priority = LOG_NOTICE;
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
32 char *message = NULL;
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
33
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
34 if (toys.optflags & FLAG_p) {
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
35 char *sep = strchr(TT.priority_arg, '.');
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
37 if (sep) {
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
38 *sep = '\0';
1019
f9271a80fedc In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents: 973
diff changeset
39 if ((facility = logger_lookup(0, TT.priority_arg)) == -1)
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
40 error_exit("bad facility: %s", TT.priority_arg);
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
41 TT.priority_arg = sep+1;
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
42 }
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
43
1019
f9271a80fedc In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents: 973
diff changeset
44 if ((priority = logger_lookup(1, TT.priority_arg)) == -1)
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
45 error_exit("bad priority: %s", TT.priority_arg);
829
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
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
48 if (!(toys.optflags & FLAG_t)) {
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
49 struct passwd *pw = getpwuid(geteuid());
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
50
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
51 if (!pw) perror_exit("getpwuid");
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 TT.ident = xstrdup(pw->pw_name);
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
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 if (toys.optc) {
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
56 int length = 0, pos = 0;
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
58 for (;*toys.optargs; toys.optargs++) {
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 length += strlen(*(toys.optargs)) + 1; // plus one for the args spacing
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 message = xrealloc(message, length + 1); // another one for the null byte
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 sprintf(message + pos, "%s ", *toys.optargs);
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 pos = length;
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 }
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 } else {
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 toybuf[readall(0, toybuf, 4096-1)] = '\0';
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 message = toybuf;
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 }
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
69
973
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
70 openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , facility);
d1768175ce1d logger: Some cleanup
Felix Janda <felix.janda@posteo.de>
parents: 829
diff changeset
71 syslog(priority, "%s", message);
829
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 closelog();
792d510b8fef Logger, by Ilya Kuzmich.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 }