Skip to content

Commit b29a3c4

Browse files
committed
Add vlogger(8)
vlogger(8) is a alternative to logger(1), by default it sends messages from stdin to syslog. The main reason to replace logger(1) is, that logger only connects once to the syslog socket in default mode and puts all messages into the void if syslog is not running at the time. logger(1) has a new `--socket-errors=on` mode which would work, but some void uses don't use syslog at all and in this case the log service would constantly restart. As a bonus vlogger(8) looks for /etc/vlogger and if its executable it just executes it and is replaced by it. This can be used to avoid syslog and just write all logs to files with svlogd(8) as example, without having to edit all log services.
1 parent 8fe7df9 commit b29a3c4

4 files changed

Lines changed: 252 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
halt
22
pause
3+
vlogger

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SCRIPTS= 1 2 3 ctrlaltdel
44
all:
55
$(CC) $(CFLAGS) halt.c -o halt $(LDFLAGS)
66
$(CC) $(CFLAGS) pause.c -o pause $(LDFLAGS)
7+
$(CC) $(CFLAGS) vlogger.c -o vlogger $(LDFLAGS)
78

89
install:
910
install -d ${DESTDIR}/${PREFIX}/sbin
@@ -22,6 +23,7 @@ install:
2223
install -m644 shutdown.8 ${DESTDIR}/${PREFIX}/share/man/man8
2324
install -m644 halt.8 ${DESTDIR}/${PREFIX}/share/man/man8
2425
install -m644 modules-load.8 ${DESTDIR}/${PREFIX}/share/man/man8
26+
install -m644 vlogger.8 ${DESTDIR}/${PREFIX}/share/man/man8
2527
ln -sf halt.8 ${DESTDIR}/${PREFIX}/share/man/man8/poweroff.8
2628
ln -sf halt.8 ${DESTDIR}/${PREFIX}/share/man/man8/reboot.8
2729
install -d ${DESTDIR}/etc/sv

vlogger.8

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
.Dd March 1, 2017
2+
.Dt VLOGGER 8
3+
.Os Linux
4+
.Sh NAME
5+
.Nm vlogger
6+
.Nd log messages to syslog or an arbitrary executable
7+
.Sh SYNOPSIS
8+
.Nm vlogger
9+
.Op Fl p Ar priority
10+
.Op Fl t Ar tag
11+
.Sh DESCRIPTION
12+
By default,
13+
.Nm
14+
reads lines from
15+
.Dv stdin
16+
and sends them to syslog.
17+
If the file
18+
.Pa /etc/vlogger
19+
exists and is executable it is executed with
20+
.Ar tag
21+
as only argument and replaces
22+
.Nm .
23+
.Pp
24+
If
25+
.Nm
26+
is executed as a log service for runit or another daemontools like
27+
supervision suite it uses the service name as default
28+
.Ar tag .
29+
As example if
30+
.Nm
31+
is linked to
32+
.Dv /var/service/foo/log/run
33+
it uses
34+
.Dv foo
35+
as default
36+
.Ar tag .
37+
.Bl -tag -width indent
38+
.It Fl p Ar priority
39+
The.
40+
.Ar priority
41+
can be
42+
.Pa facility.level
43+
or just
44+
.Pa facility .
45+
See
46+
.Sx FACILITIES ,
47+
.Sx LEVELS
48+
or
49+
.Xr syslog 3 .
50+
The default is
51+
.Pa daemon.info .
52+
.It Fl t Ar tag
53+
Defines the
54+
.Xr openlog 3
55+
.Pa ident
56+
which is used as prefix for each log message or passed as first argument to
57+
.Pa /etc/vlogger .
58+
.El
59+
.Sh FACILITIES
60+
.Bl -tag -width 11n -compact
61+
.It auth
62+
.It authpriv
63+
.It cron
64+
.It daemon
65+
.It ftp
66+
.It kern
67+
can not be used from userspace replaced with
68+
.Pa daemon .
69+
.It lpr
70+
.It mail
71+
.It news
72+
.It syslog
73+
.It user
74+
.It uucp
75+
.It local[0-7]
76+
.It security
77+
deprecated synonym for
78+
.Pa auth.
79+
.El
80+
.Sh LEVELS
81+
.Bl -tag -width 11n -compact
82+
.It emerg
83+
.It alert
84+
.It crit
85+
.It err
86+
.It warning
87+
.It notice
88+
.It info
89+
.It debug
90+
.It panic
91+
deprecated synonym for
92+
.Pa emerg .
93+
.It error
94+
deprecated synonym for
95+
.Ar err .
96+
.It warn
97+
deprecated synonym for
98+
.Pa warning .
99+
.El
100+
.Sh FILES
101+
.Bl -tag -width indent
102+
.It /etc/vlogger
103+
An optional executable file that is used to to handle the messages.
104+
It is executed with
105+
.Ar tag
106+
as first argument and replaces the
107+
.Nm
108+
process.
109+
.El
110+
.Sh EXIT STATUS
111+
.Ex -std
112+
.Sh EXAMPLES
113+
.Pa /etc/vlogger :
114+
.Bd -literal -offset indent
115+
#!/bin/sh
116+
exec svlogd /var/log/$1
117+
.Ed
118+
.Sh SEE ALSO
119+
.Xr logger 1 ,
120+
.Xr syslog 3 ,
121+
.Xr svlogd 8
122+
.Sh HISTORY
123+
This program is a replacement for the
124+
.Nm logger
125+
utility provided by
126+
.Nm util-linux .
127+
.Sh AUTHOR
128+
.An Duncan Overbruck Aq Mt mail@duncano.de
129+
.Sh LICENSE
130+
.Nm
131+
is in the public domain.
132+
.Pp
133+
To the extent possible under law,
134+
the creator of this work
135+
has waived all copyright and related or
136+
neighboring rights to this work.
137+
.Pp
138+
.Lk http://creativecommons.org/publicdomain/zero/1.0/

vlogger.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <syslog.h>
5+
#include <unistd.h>
6+
7+
typedef struct _code {
8+
const char *const c_name;
9+
int c_val;
10+
} CODE;
11+
12+
CODE prioritynames[] = {
13+
{ "alert", LOG_ALERT },
14+
{ "crit", LOG_CRIT },
15+
{ "debug", LOG_DEBUG },
16+
{ "emerg", LOG_EMERG },
17+
{ "err", LOG_ERR },
18+
{ "error", LOG_ERR },
19+
{ "info", LOG_INFO },
20+
{ "notice", LOG_NOTICE },
21+
{ "panic", LOG_EMERG },
22+
{ "warn", LOG_WARNING },
23+
{ "warning", LOG_WARNING },
24+
{ 0, -1 }
25+
};
26+
27+
CODE facilitynames[] = {
28+
{ "auth", LOG_AUTH },
29+
{ "authpriv", LOG_AUTHPRIV },
30+
{ "cron", LOG_CRON },
31+
{ "daemon", LOG_DAEMON },
32+
{ "ftp", LOG_FTP },
33+
{ "kern", LOG_KERN },
34+
{ "lpr", LOG_LPR },
35+
{ "mail", LOG_MAIL },
36+
{ "news", LOG_NEWS },
37+
{ "security", LOG_AUTH },
38+
{ "syslog", LOG_SYSLOG },
39+
{ "user", LOG_USER },
40+
{ "uucp", LOG_UUCP },
41+
{ "local0", LOG_LOCAL0 },
42+
{ "local1", LOG_LOCAL1 },
43+
{ "local2", LOG_LOCAL2 },
44+
{ "local3", LOG_LOCAL3 },
45+
{ "local4", LOG_LOCAL4 },
46+
{ "local5", LOG_LOCAL5 },
47+
{ "local6", LOG_LOCAL6 },
48+
{ "local7", LOG_LOCAL7 },
49+
{ 0, -1 }
50+
};
51+
52+
static char *tag;
53+
54+
static void
55+
strpriority(char *s, int *facility, int *level)
56+
{
57+
char *p;
58+
CODE *cp;
59+
60+
if ((p = strchr(s, '.'))) {
61+
*p++ = 0;
62+
for (cp = prioritynames; cp->c_name; cp++) {
63+
if (strcmp(cp->c_name, p) == 0)
64+
*level = cp->c_val;
65+
}
66+
}
67+
if (*s)
68+
for (cp = facilitynames; cp->c_name; cp++) {
69+
if (strcmp(cp->c_name, s) == 0)
70+
*facility = cp->c_val;
71+
}
72+
}
73+
74+
int
75+
main(int argc, char *argv[])
76+
{
77+
char c, *p;
78+
int facility = LOG_DAEMON;
79+
int level = LOG_INFO;
80+
81+
if (((p = strrchr(*argv, '/')) && !strncmp(p+1, "run", 3)) &&
82+
(*p = 0, (p = strrchr(*argv, '/')) && !strncmp(p+1, "log", 3)) &&
83+
(*p = 0, (p = strrchr(*argv, '/'))) != 0) {
84+
tag = strdup(p+1);
85+
}
86+
87+
while ((c = getopt(argc, argv, "p:t:")) != -1)
88+
switch (c) {
89+
case 'p': strpriority(optarg, &facility, &level); break;
90+
case 't': tag = optarg; break;
91+
default:
92+
fprintf(stderr, "usage: vlogger [-p priority] [-t tag]\n");
93+
exit(1);
94+
}
95+
96+
if (access("/etc/vlogger", X_OK) != -1)
97+
execl("/etc/vlogger", "/etc/vlogger", tag, (char *)0);
98+
99+
openlog(tag, 0, facility);
100+
101+
char *line;
102+
size_t linelen = 0;
103+
ssize_t rd;
104+
while ((rd = getline(&line, &linelen, stdin)) != -1) {
105+
if (line[rd-1] == '\n')
106+
line[rd-1] = 0;
107+
syslog(level|facility, "%s", line);
108+
}
109+
110+
return 1;
111+
}

0 commit comments

Comments
 (0)