forked from michaelforney/sbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
114 lines (100 loc) · 2.74 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#define SYSLOG_NAMES
typedef struct {
char *c_name;
int c_val;
} CODE;
#define prioritynames ((CODE *)(const CODE []){ \
{ "alert", LOG_ALERT }, { "crit", LOG_CRIT }, { "debug", LOG_DEBUG }, \
{ "emerg", LOG_EMERG }, { "err", LOG_ERR }, { "error", LOG_ERR }, \
{ "info", LOG_INFO }, { "none", INTERNAL_NOPRI }, \
{ "notice", LOG_NOTICE }, { "panic", LOG_EMERG }, \
{ "warn", LOG_WARNING }, { "warning", LOG_WARNING }, { 0, -1 } })
#define facilitynames ((CODE *)(const CODE []){ \
{ "auth", LOG_AUTH }, { "authpriv", LOG_AUTHPRIV }, \
{ "cron", LOG_CRON }, { "daemon", LOG_DAEMON }, { "ftp", LOG_FTP }, \
{ "kern", LOG_KERN }, { "lpr", LOG_LPR }, { "mail", LOG_MAIL }, \
{ "mark", INTERNAL_MARK }, { "news", LOG_NEWS }, \
{ "security", LOG_AUTH }, { "syslog", LOG_SYSLOG }, \
{ "user", LOG_USER }, { "uucp", LOG_UUCP }, \
{ "local0", LOG_LOCAL0 }, { "local1", LOG_LOCAL1 }, \
{ "local2", LOG_LOCAL2 }, { "local3", LOG_LOCAL3 }, \
{ "local4", LOG_LOCAL4 }, { "local5", LOG_LOCAL5 }, \
{ "local6", LOG_LOCAL6 }, { "local7", LOG_LOCAL7 }, { 0, -1 } })
#include <syslog.h>
#include <unistd.h>
#include "util.h"
#define INTERNAL_NOPRI 0x10
#define INTERNAL_MARK (LOG_NFACILITIES<<3)
static int
decodetable(CODE *table, char *name)
{
CODE *c;
for (c = table; c->c_name; c++)
if (!strcasecmp(name, c->c_name))
return c->c_val;
eprintf("invalid priority name: %s\n", name);
return -1; /* not reached */
}
static int
decodepri(char *pri)
{
char *lev, *fac = pri;
if (!(lev = strchr(pri, '.')))
eprintf("invalid priority name: %s\n", pri);
*lev++ = '\0';
if (!*lev)
eprintf("invalid priority name: %s\n", pri);
return (decodetable(facilitynames, fac) & LOG_FACMASK) |
(decodetable(prioritynames, lev) & LOG_PRIMASK);
}
static void
usage(void)
{
eprintf("usage: %s [-is] [-p priority] [-t tag] [message ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
size_t sz;
int logflags = 0, priority = LOG_NOTICE, i;
char *buf = NULL, *tag = NULL;
ARGBEGIN {
case 'i':
logflags |= LOG_PID;
break;
case 'p':
priority = decodepri(EARGF(usage()));
break;
case 's':
logflags |= LOG_PERROR;
break;
case 't':
tag = EARGF(usage());
break;
default:
usage();
} ARGEND
openlog(tag ? tag : getlogin(), logflags, 0);
if (!argc) {
while (getline(&buf, &sz, stdin) > 0)
syslog(priority, "%s", buf);
} else {
for (i = 0, sz = 0; i < argc; i++)
sz += strlen(argv[i]);
sz += argc;
buf = ecalloc(1, sz);
for (i = 0; i < argc; i++) {
estrlcat(buf, argv[i], sz);
if (i + 1 < argc)
estrlcat(buf, " ", sz);
}
syslog(priority, "%s", buf);
}
closelog();
return fshut(stdin, "<stdin>");
}