-
Notifications
You must be signed in to change notification settings - Fork 0
/
houselog_live.c
451 lines (373 loc) · 14.1 KB
/
houselog_live.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* houseportal - A simple web portal for home servers
*
* Copyright 2019, Pascal Martin
*
* 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* houselog_live.c - An application log recording modules (live portion).
*
* SYNOPSYS:
*
* void houselog_initialize (const char *application,
* int argc, const char **argv);
*
* Initialize the environment required to record logs. This must be
* the first function that the application calls.
*
* void houselog_trace (const char *file, int line, const char *level,
* const char *object,
* const char *format, ...);
*
* Record a new trace. The first 3 parameters are handled by macros:
* HOUSE_INFO, HOUSE_WARNING, HOUSE_FAILURE.
*
* void houselog_event (const char *category,
* const char *object,
* const char *action,
* const char *format, ...);
*
* Record a new event.
*
* void houselog_event_local (const char *category,
* const char *object,
* const char *action,
* const char *format, ...);
*
* Record a new local event. Local events are not propagated to the
* history storage services: they will only appear on this service's
* event page.
* This is typically used with events that are only useful when
* troubleshooting that specific service.
*
* void houselog_background (time_t now);
*
* This function must be called a regular intervals for background
* processing, e.g. cleanup of expired resources, file backup, etc.
*
* const char *houselog_host (void);
*
* Return the name of the local machine, as used in the logs.
*
* Event are stored through the houselog_storage.c module: this portion
* only handles the storage to RAM of the latest log entries.
*/
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include "echttp.h"
#include "echttp_static.h"
#include "houselog.h"
#include "houselog_storage.h"
#include "housediscover.h"
static const char *LogName = "portal";
static const char *PortalHost = 0;
static char LocalHost[256] = {0};
// Keep the most recent events. This is a relatively long
// history because it is used to feed the web event pages.
//
struct EventRecord {
struct timeval timestamp;
int unsaved;
char category[32];
char object[32];
char action[16];
char description[128];
};
#define EVENT_DEPTH 256
static struct EventRecord EventHistory[EVENT_DEPTH];
static int EventCursor = 0;
static long EventLatestId = 0;
static long EventLastFlushed = 0;
// Keep the most recent traces. This is a short history
// because it is only used to buffer before sending to storage.
//
struct TraceRecord {
struct timeval timestamp;
int unsaved;
char file[32];
int line;
char level[12];
char object[16];
char description[128];
};
#define TRACE_DEPTH 16
static struct TraceRecord TraceHistory[TRACE_DEPTH];
static int TraceCursor = 0;
static long TraceLatestId = 0;
static long TraceLastFlushed = 0;
static void safecpy (char *t, const char *s, int size) {
if (s) {
strncpy (t, s, size);
t[size-1] = 0;
} else {
t[0] = 0;
}
}
static int houselog_getheader (time_t now, char *buffer, int size) {
if (PortalHost) {
return snprintf (buffer, size,
"{\"host\":\"%s\",\"proxy\":\"%s\",\"apps\":[\"%s\"],"
"\"timestamp\":%lld,\"%s\":{\"latest\":%ld",
LocalHost, PortalHost, LogName,
(long long)now, LogName, EventLatestId);
}
return snprintf (buffer, size,
"{\"host\":\"%s\",\"apps\":[\"%s\"],"
"\"timestamp\":%lld,\"%s\":{\"latest\":%ld",
LocalHost, LogName,
(long long)now, LogName, EventLatestId);
}
static const char *houselog_trace_json (time_t now) {
static char buffer[128+TRACE_DEPTH*(sizeof(struct TraceRecord)+24)] = {0};
const char * prefix = "";
int length;
int i;
length = houselog_getheader (now, buffer, sizeof(buffer));
length += snprintf (buffer+length, sizeof(buffer)-length, ",\"traces\":[");
for (i = TraceCursor + 1; i != TraceCursor; ++i) {
if (i >= TRACE_DEPTH) {
i = 0;
if (!TraceCursor) break;
}
struct TraceRecord *cursor = TraceHistory + i;
if (!(cursor->timestamp.tv_sec)) continue;
if (!(cursor->unsaved)) continue;
int wrote = snprintf (buffer+length, sizeof(buffer)-length,
"%s[%lld%03d,\"%s\",%d,\"%s\",\"%s\",\"%s\"]",
prefix,
(long long)(cursor->timestamp.tv_sec),
(int)(cursor->timestamp.tv_usec/1000),
cursor->file,
cursor->line,
cursor->level,
cursor->object,
cursor->description);
if (wrote >= sizeof(buffer)-length) {
buffer[length] = 0;
break;
}
length += wrote;
prefix = ",";
cursor->unsaved = 2;
}
snprintf (buffer+length, sizeof(buffer)-length, "]}}");
return buffer;
}
static const char *houselog_event_json (time_t now, int filtered) {
static char buffer[128+EVENT_DEPTH*(sizeof(struct EventRecord)+24)] = {0};
const char *prefix = "";
int length;
int i;
length = houselog_getheader (now, buffer, sizeof(buffer));
length += snprintf (buffer+length, sizeof(buffer)-length, ",\"events\":[");
for (i = EventCursor + 1; i != EventCursor; ++i) {
if (i >= EVENT_DEPTH) {
i = 0;
if (!EventCursor) break;
}
struct EventRecord *cursor = EventHistory + i;
if (!(cursor->timestamp.tv_sec)) continue;
if (filtered && !(cursor->unsaved)) continue;
int wrote = snprintf (buffer+length, sizeof(buffer)-length,
"%s[%lld%03d,\"%s\",\"%s\",\"%s\",\"%s\"]",
prefix,
(long long)(cursor->timestamp.tv_sec),
(int)(cursor->timestamp.tv_usec/1000),
cursor->category,
cursor->object,
cursor->action,
cursor->description);
if (wrote >= sizeof(buffer)-length) {
buffer[length] = 0;
break;
}
length += wrote;
prefix = ",";
if (filtered) cursor->unsaved = 2;
}
if (prefix[0] == 0) return 0; // We did not include any event.
snprintf (buffer+length, sizeof(buffer)-length, "]}}");
return buffer;
}
static void houselog_event_flush (void) {
// We may not have anything to propagate if the new events were all local.
//
const char *data = houselog_event_json (time(0), 1);
if (!data) return; // Nothing to propagate.
int newunsaved = 1;
if (houselog_storage_flush ("events", data)) {
EventLastFlushed = EventLatestId;
newunsaved = 0;
}
int i;
for (i = EVENT_DEPTH-1; i >= 0; --i) {
if (EventHistory[i].unsaved == 2) EventHistory[i].unsaved = newunsaved;
}
}
static void houselog_trace_flush (void) {
int newunsaved = 1;
if (houselog_storage_flush ("traces", houselog_trace_json (time(0)))) {
TraceLastFlushed = TraceLatestId;
newunsaved = 0;
}
int i;
for (i = TRACE_DEPTH-1; i >= 0; --i) {
if (TraceHistory[i].unsaved == 2) TraceHistory[i].unsaved = newunsaved;
}
}
static const char *houselog_weblatest (const char *method, const char *uri,
const char *data, int length) {
static char buffer[256];
int written = houselog_getheader (time(0), buffer, sizeof(buffer));
snprintf (buffer+written, sizeof(buffer)-written, "}}");
return buffer;
}
static const char *houselog_webget (const char *method, const char *uri,
const char *data, int length) {
// Show the most recent data.
time_t now = time(0);
time_t start = now - 600; // Default: show the last 10 minutes.
struct tm local = *localtime (&start);
echttp_content_type_json ();
return houselog_event_json (now, 0);
}
void houselog_trace (const char *file, int line, const char *level,
const char *object,
const char *format, ...) {
va_list ap;
struct TraceRecord *cursor = TraceHistory + TraceCursor;
gettimeofday (&(cursor->timestamp), 0);
va_start (ap, format);
vsnprintf (cursor->description, sizeof(cursor->description), format, ap);
va_end (ap);
safecpy (cursor->file, file, sizeof(cursor->file));
cursor->line = line;
safecpy (cursor->level, level, sizeof(cursor->level));
safecpy (cursor->object, object, sizeof(cursor->object));
cursor->unsaved = 1;
TraceCursor += 1;
if (TraceCursor >= TRACE_DEPTH) TraceCursor = 0;
cursor = TraceHistory + TraceCursor;
if ((cursor->timestamp.tv_sec) && (cursor->unsaved)) {
houselog_trace_flush (); // Send for storage before deleting.
}
cursor->timestamp.tv_sec = 0;
cursor->unsaved = 0;
if (TraceLatestId == 0) {
// Seed the latest event ID based on the first event's time.
// This makes it random enough to make its value change after
// a restart.
TraceLatestId = (long) (time(0) & 0xffff);
}
TraceLatestId += 1;
if (echttp_isdebug())
printf ("%s %s, %d: %s %s\n", level, file, line, object, cursor->description);
}
static void houselog_event_new (const char *category,
const char *object,
const char *action,
const char *text, int propagate) {
struct EventRecord *cursor = EventHistory + EventCursor;
gettimeofday (&(cursor->timestamp), 0);
safecpy (cursor->category, category, sizeof(cursor->category));
safecpy (cursor->object, object, sizeof(cursor->object));
safecpy (cursor->action, action, sizeof(cursor->action));
safecpy (cursor->description, text, sizeof(cursor->description));
cursor->unsaved = propagate;
EventCursor += 1;
if (EventCursor >= EVENT_DEPTH) EventCursor = 0;
cursor = EventHistory + EventCursor;
if ((cursor->timestamp.tv_sec) && (cursor->unsaved)) {
houselog_event_flush (); // Send for storage before deleting.
}
cursor->timestamp.tv_sec = 0;
cursor->unsaved = 0;
if (EventLatestId == 0) {
// Seed the latest event ID based on the first event's time.
// This makes it random enough to make its value change after
// a restart.
EventLatestId = (long) (time(0) & 0xffff);
}
EventLatestId += 1;
}
void houselog_event (const char *category,
const char *object,
const char *action,
const char *format, ...) {
char text[sizeof(EventHistory[0].description)];
va_list ap;
va_start (ap, format);
vsnprintf (text, sizeof(text), format, ap);
va_end (ap);
houselog_event_new (category, object, action, text, 1); // Propagated
}
void houselog_event_local (const char *category,
const char *object,
const char *action,
const char *format, ...) {
char text[sizeof(EventHistory[0].description)];
va_list ap;
va_start (ap, format);
vsnprintf (text, sizeof(text), format, ap);
va_end (ap);
houselog_event_new (category, object, action, text, 0); // Not propagated
}
void houselog_initialize (const char *name, int argc, const char **argv) {
int i;
char uri[256];
const char *folder;
const char *portal = 0;
for (i = 1; i < argc; ++i) {
if (echttp_option_match("-portal-server=", argv[i], &portal)) continue;
}
if (name) LogName = strdup(name);
gethostname (LocalHost, sizeof(LocalHost));
PortalHost = portal ? portal : LocalHost;
snprintf (uri, sizeof(uri), "/%s/log/events", LogName);
echttp_route_uri (strdup(uri), houselog_webget);
snprintf (uri, sizeof(uri), "/%s/log/latest", LogName);
echttp_route_uri (strdup(uri), houselog_weblatest);
// Alternate paths for application-independent web pages.
// (The log files are stored at the same place for all applications.)
//
echttp_route_uri ("/log/events", houselog_webget);
echttp_route_uri ("/log/latest", houselog_weblatest);
houselog_background (time(0)); // Initial state (with nothing to flush).
// Mark the point of (re)start in both logs.
houselog_trace (HOUSE_INFO, LogName, "STARTING", "");
houselog_event ("SERVICE", LogName, "STARTING", "");
}
void houselog_background (time_t now) {
static time_t EventLastFlushTime = 0;
houselog_storage_background (now);
if (EventLastFlushed != EventLatestId) {
if (now < EventLastFlushTime + 2) return;
houselog_event_flush ();
EventLastFlushTime = now;
}
// We buffer the traces less because these are for debug.
if (TraceLastFlushed != TraceLatestId) {
houselog_trace_flush ();
}
}
const char *houselog_host (void) {
return LocalHost;
}