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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* Copyright (c) 2004, 2005, 2006, 2007 David Young. All rights reserved.
*
* See COPYING at the top of the HDF5 distribution for license terms.
*/
/*
* Copyright (c) 2004 Urbana-Champaign Independent Media Center.
* All rights reserved.
*
* See COPYING at the top of the HDF5 distribution for license terms.
*/
#include <err.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h> /* for uintmax_t */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/param.h>
#include <sys/cdefs.h>
#include "hlog.h"
#include "H5time_private.h"
TAILQ_HEAD(, hlog_outlet) hlog_outlets = TAILQ_HEAD_INITIALIZER(hlog_outlets);
HLOG_OUTLET_TOP_DEFN(all);
static struct timespec timestamp_zero;
void hlog_init(void) _constructor;
static void hlog_init_timestamps(void);
void
hlog_init(void)
{
const char *settings0;
char *item, *settings;
if ((settings0 = getenv("HLOG")) == NULL)
return;
if ((settings = strdup(settings0)) == NULL) {
warn("%s: cannot duplicate settings string", __func__);
return;
}
while ((item = strsep(&settings, " ,")) != NULL) {
hlog_outlet_state_t state;
char key[64 + 1], val[4 + 1]; // + 1 for the terminating NUL
int nconverted;
nconverted = sscanf(item, " %64[0-9a-z_] = %4s ", key, val);
if (nconverted != 2) {
warnx("%s: malformed HLOG item \"%s\"", __func__, item);
continue;
}
if (strcmp(val, "on") == 0 || strcmp(val, "yes") == 0)
state = HLOG_OUTLET_S_ON;
else if (strcmp(val, "off") == 0 || strcmp(val, "no") == 0)
state = HLOG_OUTLET_S_OFF;
else if (strcmp(val, "pass") == 0)
state = HLOG_OUTLET_S_PASS;
else {
warnx("%s: bad HLOG value \"%s\" in item \"%s\"", __func__,
val, item);
continue;
}
if (hlog_set_state(key, state, true) == -1) {
warn("%s: could not set state for HLOG item \"%s\"", __func__,
item);
}
}
free(settings);
}
static void
hlog_init_timestamps(void)
{
static bool initialized = false;
if (initialized)
return;
if (clock_gettime(CLOCK_MONOTONIC, ×tamp_zero) == -1)
err(EXIT_FAILURE, "%s: clock_gettime", __func__);
initialized = true;
}
static void
hlog_print_time(void)
{
struct timespec elapsed, now;
hlog_init_timestamps();
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
err(EXIT_FAILURE, "%s: clock_gettime", __func__);
timespecsub(&now, ×tamp_zero, &elapsed);
fprintf(stderr, "%ju.%.9ld ", (uintmax_t)elapsed.tv_sec, elapsed.tv_nsec);
}
void
vhlog(const char *fmt, va_list ap)
{
hlog_print_time();
(void)vfprintf(stderr, fmt, ap);
(void)fputc('\n', stderr);
}
static char *
message_extend_stderr(const char *fmt0)
{
static const char sep[] = ": ";
const char *m;
char *fmt;
size_t fmtlen;
m = strerror(errno);
fmtlen = strlen(fmt0) + strlen(m) + sizeof(sep);
if ((fmt = malloc(fmtlen)) == NULL) {
err(EXIT_FAILURE, "%s: malloc failed", __func__);
return NULL;
}
/* Note well the safe strcpy, strcat usage. Thank you. */
strcpy(fmt, fmt0);
strcat(fmt, sep);
strcat(fmt, m);
return fmt;
}
static char *
message_extend(const char *fmt0)
{
return message_extend_stderr(fmt0);
}
void
vhlog_err(int status, const char *fmt0, va_list ap)
{
char *fmt;
if ((fmt = message_extend(fmt0)) == NULL)
exit(status);
vhlog(fmt, ap);
free(fmt);
exit(status);
}
void
vhlog_errx(int status, const char *fmt, va_list ap)
{
vhlog(fmt, ap);
exit(status);
}
void
vhlog_warn(const char *fmt0, va_list ap)
{
char *fmt;
if ((fmt = message_extend(fmt0)) == NULL)
return;
vhlog(fmt, ap);
free(fmt);
}
void
vhlog_warnx(const char *fmt, va_list ap)
{
vhlog(fmt, ap);
}
void
hlog_err(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vhlog_err(status, fmt, ap);
va_end(ap);
}
void
hlog_errx(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vhlog_errx(status, fmt, ap);
va_end(ap);
}
void
hlog_warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vhlog_warn(fmt, ap);
va_end(ap);
}
void
hlog_warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vhlog_warnx(fmt, ap);
va_end(ap);
}
struct hlog_outlet *
hlog_outlet_find_active(struct hlog_outlet *ls0)
{
struct hlog_outlet *ls;
HLOG_OUTLET_FOREACH(ls, ls0) {
switch (ls->ls_state) {
case HLOG_OUTLET_S_PASS:
continue;
case HLOG_OUTLET_S_OFF:
return NULL;
case HLOG_OUTLET_S_ON:
default:
return ls;
}
}
return NULL;
}
void
hlog_always(struct hlog_outlet *ls _unused, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vhlog(fmt, ap);
va_end(ap);
}
void
hlog_impl(struct hlog_outlet *ls0, const char *fmt, ...)
{
struct hlog_outlet *ls;
va_list ap;
if ((ls = hlog_outlet_find_active(ls0)) == NULL) {
ls0->ls_resolved = HLOG_OUTLET_S_OFF;
return;
}
ls0->ls_resolved = HLOG_OUTLET_S_ON;
va_start(ap, fmt);
vhlog(fmt, ap);
va_end(ap);
}
static void
hlog_outlet_reset_all(void)
{
struct hlog_outlet *ls;
TAILQ_FOREACH(ls, &hlog_outlets, ls_next)
ls->ls_resolved = HLOG_OUTLET_S_PASS;
}
struct hlog_outlet *
hlog_outlet_lookup(const char *name)
{
struct hlog_outlet *ls;
TAILQ_FOREACH(ls, &hlog_outlets, ls_next) {
if (strcmp(ls->ls_name, name) == 0)
return ls;
}
return NULL;
}
static struct hlog_outlet *
hlog_outlet_create(const char *name)
{
struct hlog_outlet *ls;
if ((ls = calloc(1, sizeof(*ls))) == NULL)
return NULL;
else if ((ls->ls_name0 = strdup(name)) == NULL) {
free(ls);
return NULL;
}
ls->ls_name = ls->ls_name0;
ls->ls_rendezvous = true;
return ls;
}
static void
hlog_outlet_destroy(struct hlog_outlet *ls)
{
/*LINTED*/
if (ls->ls_name0 != NULL)
free(ls->ls_name0);
free(ls);
}
int
hlog_set_state(const char *name, hlog_outlet_state_t state, bool rendezvous)
{
struct hlog_outlet *ls;
errno = 0;
switch (state) {
case HLOG_OUTLET_S_PASS:
case HLOG_OUTLET_S_OFF:
case HLOG_OUTLET_S_ON:
break;
default:
errno = EINVAL;
return -1;
}
if ((ls = hlog_outlet_lookup(name)) == NULL && !rendezvous) {
errno = ESRCH;
return -1;
} else if (ls == NULL) {
if ((ls = hlog_outlet_create(name)) == NULL)
return -1;
TAILQ_INSERT_TAIL(&hlog_outlets, ls, ls_next);
}
ls->ls_state = state;
hlog_outlet_reset_all();
return 0;
}
void
hlog_outlet_register(struct hlog_outlet *ls_arg)
{
struct hlog_outlet *ls;
if ((ls = hlog_outlet_lookup(ls_arg->ls_name)) == NULL ||
ls->ls_rendezvous) {
TAILQ_INSERT_TAIL(&hlog_outlets, ls_arg, ls_next);
if (ls == NULL)
return;
warnx("%s: rendezvous with log-outlet '%s'", __func__,
ls->ls_name);
ls_arg->ls_state = ls->ls_state;
TAILQ_REMOVE(&hlog_outlets, ls, ls_next);
hlog_outlet_destroy(ls);
} else
warnx("%s: duplicate log-outlet, '%s'", __func__, ls->ls_name);
}
|