logger.c
5.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012, 2014 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include "argp.h"
#include "mu.h"
static char logger_doc[] = N_("mu logger - log data using Mailutils log facility");
char logger_docstring[] = N_("log data using Mailutils log facility");
static char logger_args_doc[] = N_("[TEXT]");
#define OPT_SYSLOG 256
#define OPT_STDERR 257
static struct argp_option logger_options[] = {
{ "file", 'f', N_("FILE"), 0, N_("read message from FILE") },
{ "priority", 'p', N_("FACILITY[.LEVEL]"), 0,
N_("log at the specified syslog priority (implies --syslog)") },
{ "syslog", OPT_SYSLOG, NULL, 0, N_("log via syslog") },
{ "stderr", OPT_STDERR, NULL, 0, N_("log to the standard error") },
{ "severity", 's', N_("SEV"), 0,
N_("log at Mailutils severity level SEV") },
{ "locus", 'l', N_("FILE:LINE[:COL]"), 0,
N_("set locus for logging") },
{ "tag", 't', N_("syslog tag"), 0,
N_("set syslog tag") },
{ NULL }
};
static char *input_file = NULL;
static int logger_type = MU_STRERR_STDERR;
static int log_severity = MU_LOG_ERROR;
static struct mu_locus locus;
static int syslog_facility = LOG_USER;
static int syslog_priority = LOG_ERR;
static char *syslog_tag = NULL;
static error_t
logger_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'f':
input_file = arg;
break;
case OPT_SYSLOG:
logger_type = MU_STRERR_SYSLOG;
break;
case OPT_STDERR:
logger_type = MU_STRERR_STDERR;
break;
case 's':
{
int i;
for (i = 0; i < _mu_severity_num; i++)
if (mu_c_strcasecmp (_mu_severity_str[i], arg) == 0)
{
log_severity = i;
return 0;
}
argp_error (state, _("unknown severity: %s"), arg);
break;
}
case 't':
syslog_tag = arg;
break;
case 'p':
{
char *s = strchr (arg, '.');
if (s)
*s++ = 0;
if (mu_string_to_syslog_facility (arg, &syslog_facility))
argp_error (state, _("unknown facility: %s"), arg);
if (s &&
mu_string_to_syslog_priority (s, &syslog_priority))
argp_error (state, _("unknown priority: %s"), s);
logger_type = MU_STRERR_SYSLOG;
break;
}
case 'l':
{
char *s;
locus.mu_file = arg;
s = strchr (arg, ':');
if (s)
{
*s++ = 0;
locus.mu_line = strtoul (s, &s, 10);
if (*s == ':')
{
locus.mu_col = strtoul (s + 1, &s, 10);
if (*s)
argp_error (state, _("bad column number: %s"), arg);
}
else if (*s)
argp_error (state, _("bad line number: %s"), arg);
}
break;
}
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp logger_argp = {
logger_options,
logger_parse_opt,
logger_args_doc,
logger_doc,
NULL,
NULL,
NULL
};
int
mutool_logger (int argc, char **argv)
{
int index;
mu_stream_t logger, input;
int rc, mode;
if (argp_parse (&logger_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc && input_file)
{
mu_error (_("both input file and message text given"));
exit (1);
}
if (!syslog_tag)
syslog_tag = "mu-logger";
rc = mu_stdstream_strerr_create (&logger, logger_type,
syslog_facility, syslog_priority,
syslog_tag, NULL);
if (rc)
{
mu_error (_("cannot create log stream: %s"),
mu_strerror (rc));
exit (1);
}
mode = MU_LOGMODE_SEVERITY | MU_LOGMODE_LOCUS;
mu_stream_ioctl (logger, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &mode);
if (locus.mu_file)
mu_stream_ioctl (logger, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &locus);
mu_stream_ioctl (logger, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_SEVERITY, &log_severity);
if (argc)
{
int i;
for (i = 0; i < argc; i++)
{
if (i > 0)
mu_stream_write (logger, " ", 1, NULL);
mu_stream_write (logger, argv[i], strlen (argv[i]), NULL);
}
mu_stream_write (logger, "\n", 1, NULL);
return 0;
}
else if (!input_file || strcmp (input_file, "-") == 0)
{
mu_stream_ref (mu_strin);
input = mu_strin;
}
else
{
rc = mu_file_stream_create (&input, input_file, MU_STREAM_READ);
if (rc)
{
mu_error (_("cannot open input stream %s: %s"),
input_file, mu_strerror (rc));
return 1;
}
}
rc = mu_stream_copy (logger, input, 0, NULL);
mu_stream_unref (input);
mu_stream_unref (logger);
return !!rc;
}
/*
MU Setup: logger
mu-handler: mutool_logger
mu-docstring: logger_docstring
End MU Setup:
*/