Files
adsb-tools/adsbus/log.c

137 lines
3.2 KiB
C
Raw Normal View History

2016-03-05 22:54:26 -08:00
#include <assert.h>
#include <fcntl.h>
2016-03-07 16:14:35 -08:00
#include <signal.h>
2016-03-05 22:54:26 -08:00
#include <stdarg.h>
2016-03-07 20:12:35 -08:00
#include <stdbool.h>
2016-03-05 22:54:26 -08:00
#include <stdio.h>
2016-03-07 11:26:25 -08:00
#include <stdlib.h>
#include <string.h>
2016-03-07 16:14:35 -08:00
#include <sys/socket.h>
2016-03-07 20:12:35 -08:00
#include <sys/stat.h>
2016-03-07 16:14:35 -08:00
#include <sys/types.h>
2016-03-07 20:12:35 -08:00
#include <time.h>
2016-03-05 22:54:26 -08:00
#include <unistd.h>
#include "log.h"
#include "opts.h"
2016-03-07 16:14:35 -08:00
#include "peer.h"
2016-03-07 11:26:25 -08:00
#include "uuid.h"
2016-03-05 22:54:26 -08:00
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
static FILE *log_stream = NULL;
2016-03-07 11:26:25 -08:00
static char *log_path = NULL;
static uint8_t log_id[UUID_LEN];
2016-03-07 16:14:35 -08:00
static int log_rotate_fd;
static struct peer log_rotate_peer;
2016-03-07 20:12:35 -08:00
static bool log_timestamps = false;
static opts_group log_opts;
2016-03-07 11:26:25 -08:00
2016-03-07 17:02:24 -08:00
static char log_module = 'L';
2016-03-07 11:26:25 -08:00
static void log_rotate() {
if (!log_path) {
LOG(log_id, "Received SIGHUP but logging to stderr; ignoring");
return;
}
2016-03-07 11:26:25 -08:00
uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN];
uuid_gen(new_log_id);
2016-03-07 17:02:24 -08:00
LOG(log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path);
2016-03-07 11:26:25 -08:00
memcpy(old_log_id, log_id, UUID_LEN);
memcpy(log_id, new_log_id, UUID_LEN);
assert(!fclose(log_stream));
log_stream = fopen(log_path, "a");
assert(log_stream);
2016-03-07 16:14:35 -08:00
setlinebuf(log_stream);
2016-03-07 17:02:24 -08:00
LOG(log_id, "Log start after switch from log ID %s", old_log_id);
2016-03-07 11:26:25 -08:00
}
2016-03-05 22:54:26 -08:00
2016-03-07 16:14:35 -08:00
static void log_rotate_handler(struct peer *peer) {
char buf[1];
assert(read(peer->fd, buf, 1) == 1);
assert(buf[0] == 'L');
log_rotate();
}
static void log_rotate_signal(int __attribute__ ((unused)) signum) {
assert(write(log_rotate_fd, "L", 1) == 1);
}
static bool log_set_path(const char *path) {
if (log_path) {
return false;
}
log_path = strdup(path);
assert(log_path);
return true;
}
static bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) {
log_timestamps = true;
return true;
}
void log_opts_add() {
opts_add("log-file", "PATH", log_set_path, log_opts);
opts_add("log-timestamps", NULL, log_enable_timestamps, log_opts);
}
2016-03-05 22:54:26 -08:00
void log_init() {
opts_call(log_opts);
if (log_path) {
log_stream = fopen(log_path, "a");
} else {
int fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0);
assert(fd >= 0);
log_stream = fdopen(fd, "a");
}
2016-03-05 22:54:26 -08:00
assert(log_stream);
setlinebuf(log_stream);
2016-03-07 11:26:25 -08:00
uuid_gen(log_id);
2016-03-07 17:02:24 -08:00
LOG(log_id, "Log start");
2016-03-05 22:54:26 -08:00
}
2016-03-07 16:14:35 -08:00
void log_init2() {
int rotate_fds[2];
assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, rotate_fds));
log_rotate_peer.fd = rotate_fds[0];
assert(!shutdown(log_rotate_peer.fd, SHUT_WR));
log_rotate_peer.event_handler = log_rotate_handler;
peer_epoll_add(&log_rotate_peer, EPOLLIN);
log_rotate_fd = rotate_fds[1];
signal(SIGHUP, log_rotate_signal);
}
2016-03-05 22:54:26 -08:00
void log_cleanup() {
2016-03-07 17:02:24 -08:00
LOG(log_id, "Log end");
2016-03-05 22:54:26 -08:00
assert(!fclose(log_stream));
2016-03-07 16:14:35 -08:00
assert(!close(log_rotate_fd));
assert(!close(log_rotate_peer.fd));
2016-03-07 11:26:25 -08:00
if (log_path) {
free(log_path);
log_path = NULL;
}
}
2016-03-07 17:08:36 -08:00
void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) {
2016-03-05 22:54:26 -08:00
va_list ap;
va_start(ap, fmt);
2016-03-07 20:12:35 -08:00
char datetime[64] = "";
if (log_timestamps) {
time_t now;
assert(time(&now) >= 0);
struct tm tmnow;
assert(gmtime_r(&now, &tmnow));
assert(strftime(datetime, sizeof(datetime), "%FT%TZ ", &tmnow) > 0);
}
assert(fprintf(log_stream, "%s[%18s] %c %s: ", datetime, loc, type, id) > 0);
2016-03-05 22:54:26 -08:00
assert(vfprintf(log_stream, fmt, ap) > 0);
assert(fprintf(log_stream, "\n") == 1);
va_end(ap);
}