forked from flux-framework/flux-core
/
flog.c
291 lines (257 loc) · 7.59 KB
/
flog.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
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
/*****************************************************************************\
* Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS).
* LLNL-CODE-658032 All rights reserved.
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* This program 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 2 of the license, or (at your option)
* any later version.
*
* Flux 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 terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses/
\*****************************************************************************/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <stdbool.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <inttypes.h>
#include <zmq.h>
#include "flog.h"
#include "info.h"
#include "message.h"
#include "rpc.h"
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/wallclock.h"
#include "src/common/libutil/stdlog.h"
typedef struct {
char appname[STDLOG_MAX_APPNAME + 1];
char procid[STDLOG_MAX_PROCID + 1];
char buf[FLUX_MAX_LOGBUF + 1];
flux_log_f cb;
void *cb_arg;
} logctx_t;
static void freectx (void *arg)
{
logctx_t *ctx = arg;
free (ctx);
}
static logctx_t *getctx (flux_t *h)
{
logctx_t *ctx = (logctx_t *)flux_aux_get (h, "flux::log");
extern char *__progname;
// or glib-ism: program_invocation_short_name
if (!ctx) {
ctx = xzmalloc (sizeof (*ctx));
snprintf (ctx->procid, sizeof (ctx->procid), "%d", getpid ());
snprintf (ctx->appname, sizeof (ctx->appname), "%s", __progname);
flux_aux_set (h, "flux::log", ctx, freectx);
}
return ctx;
}
void flux_log_set_appname (flux_t *h, const char *s)
{
logctx_t *ctx = getctx (h);
snprintf (ctx->appname, sizeof (ctx->appname), "%s", s);
}
void flux_log_set_procid (flux_t *h, const char *s)
{
logctx_t *ctx = getctx (h);
snprintf (ctx->procid, sizeof (ctx->procid), "%s", s);
}
void flux_log_set_redirect (flux_t *h, flux_log_f fun, void *arg)
{
logctx_t *ctx = getctx (h);
ctx->cb = fun;
ctx->cb_arg = arg;
}
const char *flux_strerror (int errnum)
{
/* zeromq-4.2.1 reports EHOSTUNREACH as "Host unreachable",
* but "No route to host" is canonical on Linux and we have some
* tests that depend on it, so remap here.
*/
if (errnum == EHOSTUNREACH)
return "No route to host";
return zmq_strerror (errnum);
}
static int log_rpc (flux_t *h, const char *buf, int len, int flags)
{
flux_rpc_t *r = NULL;
int rc = (flags & FLUX_RPC_NORESPONSE) ? 0 : -1;
if (!(r = flux_rpc_raw (h, "log.append", buf, len, FLUX_NODEID_ANY, flags)))
goto done;
if ((flags & FLUX_RPC_NORESPONSE))
goto done;
if (flux_rpc_get (r, NULL) < 0)
goto done;
rc = 0;
done:
flux_rpc_destroy (r);
return rc;
}
int flux_vlog (flux_t *h, int level, const char *fmt, va_list ap)
{
logctx_t *ctx = getctx (h);
int saved_errno = errno;
uint32_t rank;
int len;
char timestamp[WALLCLOCK_MAXLEN];
char hostname[STDLOG_MAX_HOSTNAME + 1];
struct stdlog_header hdr;
int rpc_flags = FLUX_RPC_NORESPONSE;
if ((level & FLUX_LOG_CHECK)) {
rpc_flags &= ~FLUX_RPC_NORESPONSE;
level &= ~FLUX_LOG_CHECK;
}
stdlog_init (&hdr);
hdr.pri = STDLOG_PRI (level, LOG_USER);
if (wallclock_get_zulu (timestamp, sizeof (timestamp)) >= 0)
hdr.timestamp = timestamp;
if (flux_get_rank (h, &rank) == 0) {
snprintf (hostname, sizeof (hostname), "%" PRIu32, rank);
hdr.hostname = hostname;
}
hdr.appname = ctx->appname;
hdr.procid = ctx->procid;
len = stdlog_vencodef (ctx->buf, sizeof (ctx->buf), &hdr,
STDLOG_NILVALUE, fmt, ap);
if (len >= sizeof (ctx->buf))
len = sizeof (ctx->buf);
if (ctx->cb) {
ctx->cb (ctx->buf, len, ctx->cb_arg);
} else {
if (log_rpc (h, ctx->buf, len, rpc_flags) < 0)
return -1;
}
errno = saved_errno;
return 0;
}
int flux_log (flux_t *h, int lev, const char *fmt, ...)
{
va_list ap;
int rc;
va_start (ap, fmt);
rc = flux_vlog (h, lev, fmt, ap);
va_end (ap);
return rc;
}
void flux_log_verror (flux_t *h, const char *fmt, va_list ap)
{
int saved_errno = errno;
char *s = xvasprintf (fmt, ap);
flux_log (h, LOG_ERR, "%s: %s", s, flux_strerror (errno));
free (s);
errno = saved_errno;
}
void flux_log_error (flux_t *h, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
flux_log_verror (h, fmt, ap);
va_end (ap);
}
static int dmesg_clear (flux_t *h, int seq)
{
flux_rpc_t *rpc;
int rc = -1;
if (!(rpc = flux_rpcf (h, "log.clear", FLUX_NODEID_ANY, 0,
"{s:i}", "seq", seq)))
goto done;
if (flux_rpc_get (rpc, NULL) < 0)
goto done;
rc = 0;
done:
flux_rpc_destroy (rpc);
return rc;
}
static flux_rpc_t *dmesg_rpc (flux_t *h, int seq, bool follow)
{
return flux_rpcf (h, "log.dmesg", FLUX_NODEID_ANY, 0,
"{s:i s:b}", "seq", seq, "follow", follow);
}
static int dmesg_rpc_get (flux_rpc_t *rpc, int *seq, flux_log_f fun, void *arg)
{
const char *buf;
int rc = -1;
if (flux_rpc_getf (rpc, "{s:i s:s}", "seq", seq, "buf", &buf) < 0)
goto done;
fun (buf, strlen (buf), arg);
rc = 0;
done:
return rc;
}
int flux_dmesg (flux_t *h, int flags, flux_log_f fun, void *arg)
{
int rc = -1;
int seq = -1;
bool eof = false;
bool follow = false;
if (flags & FLUX_DMESG_FOLLOW)
follow = true;
if (fun) {
while (!eof) {
flux_rpc_t *rpc;
if (!(rpc = dmesg_rpc (h, seq, follow)))
goto done;
if (dmesg_rpc_get (rpc, &seq, fun, arg) < 0) {
if (errno != ENOENT) {
flux_rpc_destroy (rpc);
goto done;
}
eof = true;
}
flux_rpc_destroy (rpc);
}
}
if ((flags & FLUX_DMESG_CLEAR)) {
if (dmesg_clear (h, seq) < 0)
goto done;
}
rc = 0;
done:
return rc;
}
void flux_log_fprint (const char *buf, int len, void *arg)
{
FILE *f = arg;
struct stdlog_header hdr;
const char *msg;
int msglen, severity;
uint32_t nodeid;
if (f) {
if (stdlog_decode (buf, len, &hdr, NULL, NULL, &msg, &msglen) < 0)
fprintf (f, "%.*s\n", len, buf);
else {
nodeid = strtoul (hdr.hostname, NULL, 10);
severity = STDLOG_SEVERITY (hdr.pri);
fprintf (f, "%s %s.%s[%" PRIu32 "]: %.*s\n",
hdr.timestamp,
hdr.appname,
stdlog_severity_to_string (severity),
nodeid,
msglen, msg);
}
fflush (f);
}
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/