-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
171 lines (135 loc) · 4.52 KB
/
Copy pathlog.c
File metadata and controls
171 lines (135 loc) · 4.52 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
/* Copyright (c) 2017, David Hauweele <david@hauweele.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __linux__
# define _DEFAULT_SOURCE 1
#endif
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include "log.h"
#include "common.h"
#define MAX_MSG_SIZE 4095
static char msg_buf[MAX_MSG_SIZE];
static int std_loglevel;
void safecall_act_sysstd(const char *msg)
{
sysstd_err(EXIT_FAILURE, LOG_ERR, "%s", msg);
}
void sysstd_openlog(const char *ident, int logopt, int facility, int loglevel)
{
openlog(ident, logopt, facility);
setlogmask(LOG_UPTO(loglevel));
std_loglevel = loglevel;
}
/* Send a message to syslog. Also appends the current error message when
display_error is true. */
static void sysstd_vsyslog(bool display_error, int priority, const char *fmt, va_list args)
{
if(display_error) {
vsnprintf(msg_buf, MAX_MSG_SIZE, fmt, args);
syslog(priority, "%s: %s", msg_buf, strerror(errno));
}
else
vsyslog(priority, fmt, args);
}
/* Redefinition of verr/vwarn functions with a uniform signature
that we use for call indirection in the generic sysstd_output
function below. */
static void sysstd_uniform_verrx(int eval, const char *fmt, va_list args)
{
verrx(eval, fmt, args);
}
static void sysstd_uniform_verr(int eval, const char *fmt, va_list args)
{
verr(eval, fmt, args);
}
static void sysstd_uniform_vwarnx(int eval, const char *fmt, va_list args)
{
UNUSED(eval);
vwarnx(fmt, args);
}
static void sysstd_uniform_vwarn(int eval, const char *fmt, va_list args)
{
UNUSED(eval);
vwarn(fmt, args);
}
static void sysstd_uniform_vlog(int eval, const char *fmt, va_list args)
{
UNUSED(eval);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
}
/* Display message on standard output and also log it into syslog.
This is a generic function that we use in the err/warn wrappers. */
static void sysstd_output(void (*out)(int eval, const char *fmt, va_list args),
bool display_error, int eval, int priority, const char *fmt, va_list args)
{
va_list args_cpy;
va_copy(args_cpy, args);
sysstd_vsyslog(display_error, priority, fmt, args);
if(priority <= std_loglevel)
out(eval, fmt, args_cpy);
va_end(args_cpy);
}
void sysstd_warnx(int priority, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
sysstd_output(sysstd_uniform_vwarnx,
false, 0, priority, fmt, args);
va_end(args);
}
void sysstd_warn(int priority, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
sysstd_output(sysstd_uniform_vwarn,
true, 0, priority, fmt, args);
va_end(args);
}
void sysstd_errx(int eval, int priority, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
sysstd_output(sysstd_uniform_verrx,
false, eval, priority, fmt, args);
va_end(args);
}
void sysstd_err(int eval, int priority, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
sysstd_output(sysstd_uniform_verr,
true, eval, priority, fmt, args);
va_end(args);
}
void sysstd_log(int priority, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
sysstd_output(sysstd_uniform_vlog,
false, 0, priority, fmt, args);
va_end(args);
}