From a9e05fe5c0911a8ca5ad0746e54a5c38a8b832a4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 1 Jan 2018 16:32:13 -0600 Subject: Promote logger, and fluff up help text a bit. --- toys/pending/logger.c | 64 ---------------------------------------------- toys/posix/logger.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 64 deletions(-) delete mode 100644 toys/pending/logger.c create mode 100644 toys/posix/logger.c (limited to 'toys') diff --git a/toys/pending/logger.c b/toys/pending/logger.c deleted file mode 100644 index 8a458b58..00000000 --- a/toys/pending/logger.c +++ /dev/null @@ -1,64 +0,0 @@ -/* logger.c - Log messages. - * - * Copyright 2013 Ilya Kuzmich - * - * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/logger.html - -USE_LOGGER(NEWTOY(logger, "st:p:", TOYFLAG_USR|TOYFLAG_BIN)) - -config LOGGER - bool "logger" - default n - help - usage: logger [-s] [-t tag] [-p [facility.]priority] [message] - - Log message (or stdin) to syslog. -*/ - -#define FOR_logger -#include "toys.h" - -GLOBALS( - char *priority; - char *ident; -) - -void logger_main(void) -{ - int facility = LOG_USER, priority = LOG_NOTICE, len; - char *s1, *s2, **arg; - CODE *code; - - if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name); - if (toys.optflags & FLAG_p) { - if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority; - else { - *s1++ = 0; - for (code = facilitynames; code->c_name; code++) - if (!strcasecmp(TT.priority, code->c_name)) break; - if (!code->c_name) error_exit("bad facility: %s", TT.priority); - facility = code->c_val; - } - - for (code = prioritynames; code->c_name; code++) - if (!strcasecmp(s1, code->c_name)) break; - if (!code->c_name) error_exit("bad priority: %s", s1); - } - - - if (toys.optc) { - for (len = 0, arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1; - s1 = s2 = xmalloc(len); - for (arg = toys.optargs; *arg; arg++) { - if (arg != toys.optargs) *s2++ = ' '; - s2 = stpcpy(s2, *arg); - } - } else { - toybuf[readall(0, toybuf, sizeof(toybuf)-1)] = 0; - s1 = toybuf; - } - - openlog(TT.ident, LOG_PERROR*!!(toys.optflags&FLAG_s), facility); - syslog(priority, "%s", s1); - closelog(); -} diff --git a/toys/posix/logger.c b/toys/posix/logger.c new file mode 100644 index 00000000..16262c9f --- /dev/null +++ b/toys/posix/logger.c @@ -0,0 +1,70 @@ +/* logger.c - Log messages. + * + * Copyright 2013 Ilya Kuzmich + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/logger.html + * + * Deviations from posix: specified manner and format, defined implementation. + +USE_LOGGER(NEWTOY(logger, "st:p:", TOYFLAG_USR|TOYFLAG_BIN)) + +config LOGGER + bool "logger" + default y + help + usage: logger [-s] [-t TAG] [-p [FACILITY.]PRIORITY] [message...] + + Log message (or stdin) to syslog. + + -s Also write message to stderr + -t Use TAG instead of username to identify message source + -p Specify PRIORITY with optional FACILITY. Default is "user.notice" +*/ + +#define FOR_logger +#include "toys.h" + +GLOBALS( + char *priority; + char *ident; +) + +void logger_main(void) +{ + int facility = LOG_USER, priority = LOG_NOTICE, len; + char *s1, *s2, **arg; + CODE *code; + + if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name); + if (toys.optflags & FLAG_p) { + if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority; + else { + *s1++ = 0; + for (code = facilitynames; code->c_name; code++) + if (!strcasecmp(TT.priority, code->c_name)) break; + if (!code->c_name) error_exit("bad facility: %s", TT.priority); + facility = code->c_val; + } + + for (code = prioritynames; code->c_name; code++) + if (!strcasecmp(s1, code->c_name)) break; + if (!code->c_name) error_exit("bad priority: %s", s1); + } + + + if (toys.optc) { + for (len = 0, arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1; + s1 = s2 = xmalloc(len); + for (arg = toys.optargs; *arg; arg++) { + if (arg != toys.optargs) *s2++ = ' '; + s2 = stpcpy(s2, *arg); + } + } else { + toybuf[readall(0, toybuf, sizeof(toybuf)-1)] = 0; + s1 = toybuf; + } + + openlog(TT.ident, LOG_PERROR*!!(toys.optflags&FLAG_s), facility); + syslog(priority, "%s", s1); + closelog(); +} -- cgit v1.2.3