]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: add function pointer to choose how logging is done
authorSami Kerola <kerolasa@iki.fi>
Thu, 26 Jun 2014 20:48:25 +0000 (21:48 +0100)
committerSami Kerola <kerolasa@iki.fi>
Mon, 28 Jul 2014 20:15:17 +0000 (21:15 +0100)
This change paves way to adding support for both RFC 3164 and RFC 5424
syslog protocols.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
misc-utils/logger.c

index 9f9650c317ae706b35fafe3b72f9cb37e62a8b37..ed45f93cab6d2424e9e8cabd22398006c9674489 100644 (file)
@@ -77,6 +77,14 @@ enum {
        OPT_JOURNALD
 };
 
+struct logger_ctl {
+       int fd;
+       int logflags;
+       int pri;
+       char *tag;
+       void (*syslogfp)(struct logger_ctl *ctl, char *msg);
+};
+
 static char *get_prio_prefix(char *msg, int *prio)
 {
        int p;
@@ -268,29 +276,34 @@ static char *xgetlogin()
        return cp;
 }
 
-static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
+static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
 {
        char buf[1000], pid[30], *cp, *tp;
        time_t now;
 
-       if (fd < 0)
+       if (ctl->fd < 0)
                return;
-       if (logflags & LOG_PID)
+       if (ctl->logflags & LOG_PID)
                snprintf(pid, sizeof(pid), "[%d]", getpid());
        else
                pid[0] = 0;
-       if (tag)
-               cp = tag;
+       if (ctl->tag)
+               cp = ctl->tag;
        else
                cp = xgetlogin();
        time(&now);
        tp = ctime(&now) + 4;
        snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
-                pri, tp, cp, pid, msg);
-       if (write_all(fd, buf, strlen(buf) + 1) < 0)
+                ctl->pri, tp, cp, pid, msg);
+       if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
                warn(_("write failed"));
 }
 
+static void syslog_local(struct logger_ctl *ctl, char *msg)
+{
+       syslog(ctl->pri, "%s", msg);
+}
+
 static void __attribute__ ((__noreturn__)) usage(FILE *out)
 {
        fputs(USAGE_HEADER, out);
@@ -328,12 +341,18 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
  */
 int main(int argc, char **argv)
 {
-       int ch, logflags, pri, prio_prefix;
-       char *tag, buf[1024];
+       struct logger_ctl ctl = {
+               .fd = -1,
+               .logflags = 0,
+               .pri = LOG_NOTICE,
+               .tag = NULL,
+       };
+       int ch, prio_prefix;
+       char buf[1024];
        char *usock = NULL;
        char *server = NULL;
        char *port = NULL;
-       int LogSock = -1, socket_type = ALL_TYPES;
+       int socket_type = ALL_TYPES;
 #ifdef HAVE_LIBSYSTEMD
        FILE *jfd = NULL;
 #endif
@@ -362,10 +381,6 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       tag = NULL;
-       pri = LOG_NOTICE;
-       logflags = 0;
-       prio_prefix = 0;
        while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
                                            longopts, NULL)) != -1) {
                switch (ch) {
@@ -375,16 +390,16 @@ int main(int argc, char **argv)
                                    optarg);
                        break;
                case 'i':               /* log process id also */
-                       logflags |= LOG_PID;
+                       ctl.logflags |= LOG_PID;
                        break;
                case 'p':               /* priority */
-                       pri = pencode(optarg);
+                       ctl.pri = pencode(optarg);
                        break;
                case 's':               /* log to standard error */
-                       logflags |= LOG_PERROR;
+                       ctl.logflags |= LOG_PERROR;
                        break;
                case 't':               /* tag */
-                       tag = optarg;
+                       ctl.tag = optarg;
                        break;
                case 'u':               /* unix socket */
                        usock = optarg;
@@ -439,12 +454,16 @@ int main(int argc, char **argv)
                return EXIT_SUCCESS;
        }
 #endif
-       if (server)
-               LogSock = inet_socket(server, port, socket_type);
-       else if (usock)
-               LogSock = unix_socket(usock, socket_type);
-       else
-               openlog(tag ? tag : xgetlogin(), logflags, 0);
+       if (server) {
+               ctl.fd = inet_socket(server, port, socket_type);
+               ctl.syslogfp = syslog_rfc3164;
+       } else if (usock) {
+               ctl.fd = unix_socket(usock, socket_type);
+               ctl.syslogfp = syslog_rfc3164;
+       } else {
+               openlog(ctl.tag ? ctl.tag : xgetlogin(), ctl.logflags, 0);
+               ctl.syslogfp = syslog_local;
+       }
 
        /* log input line if appropriate */
        if (argc > 0) {
@@ -454,35 +473,23 @@ int main(int argc, char **argv)
                for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
                        len = strlen(*argv);
                        if (p + len > endp && p > buf) {
-                               if (!usock && !server)
-                                       syslog(pri, "%s", buf);
-                               else
-                                       mysyslog(LogSock, logflags, pri, tag,
-                                                buf);
+                               ctl.syslogfp(&ctl, buf);
                                p = buf;
                        }
-                       if (len > sizeof(buf) - 1) {
-                               if (!usock && !server)
-                                       syslog(pri, "%s", *argv++);
-                               else
-                                       mysyslog(LogSock, logflags, pri, tag,
-                                                *argv++);
-                       } else {
+                       if (len > sizeof(buf) - 1)
+                               ctl.syslogfp(&ctl, *argv++);
+                       else {
                                if (p != buf)
                                        *p++ = ' ';
                                memmove(p, *argv++, len);
                                *(p += len) = '\0';
                        }
                }
-               if (p != buf) {
-                       if (!usock && !server)
-                               syslog(pri, "%s", buf);
-                       else
-                               mysyslog(LogSock, logflags, pri, tag, buf);
-               }
+               if (p != buf)
+                       ctl.syslogfp(&ctl, buf);
        } else {
                char *msg;
-               int default_priority = pri;
+               int default_priority = ctl.pri;
                while (fgets(buf, sizeof(buf), stdin) != NULL) {
                        /* glibc is buggy and adds an additional newline,
                           so we have to remove it here until glibc is fixed */
@@ -492,20 +499,16 @@ int main(int argc, char **argv)
                                buf[len - 1] = '\0';
 
                        msg = buf;
-                       pri = default_priority;
+                       ctl.pri = default_priority;
                        if (prio_prefix && msg[0] == '<')
-                               msg = get_prio_prefix(msg, &pri);
-
-                       if (!usock && !server)
-                               syslog(pri, "%s", msg);
-                       else
-                               mysyslog(LogSock, logflags, pri, tag, msg);
+                               msg = get_prio_prefix(msg, &ctl.pri);
+                       ctl.syslogfp(&ctl, msg);
                }
        }
        if (!usock && !server)
                closelog();
        else
-               close(LogSock);
+               close(ctl.fd);
 
        return EXIT_SUCCESS;
 }