Switch to LOG macro

This commit is contained in:
Ian Gulliver
2016-03-07 17:02:24 -08:00
parent 602c6f729b
commit b434b67dd8
12 changed files with 75 additions and 51 deletions

View File

@@ -29,6 +29,8 @@ struct exec {
static struct list_head exec_head = LIST_HEAD_INIT(exec_head);
static char log_module = 'E';
static void exec_spawn_wrapper(struct peer *);
static void exec_harvest(struct exec *exec) {
@@ -37,10 +39,10 @@ static void exec_harvest(struct exec *exec) {
assert(waitpid(exec->child, &status, 0) == exec->child);
exec->child = -1;
if (WIFEXITED(status)) {
log_write('E', exec->id, "Client exited with status %d", WEXITSTATUS(status));
LOG(exec->id, "Client exited with status %d", WEXITSTATUS(status));
} else {
assert(WIFSIGNALED(status));
log_write('E', exec->id, "Client exited with signal %d", WTERMSIG(status));
LOG(exec->id, "Client exited with signal %d", WTERMSIG(status));
}
}
if (exec->log_peer.fd >= 0) {
@@ -53,7 +55,7 @@ static void exec_del(struct exec *exec) {
flow_ref_dec(exec->flow);
if (exec->child > 0) {
log_write('E', exec->id, "Sending SIGTERM to child process %d", exec->child);
LOG(exec->id, "Sending SIGTERM to child process %d", exec->child);
// Racy with the process terminating, so don't assert on it
kill(exec->child, SIGTERM);
}
@@ -67,7 +69,7 @@ static void exec_close_handler(struct peer *peer) {
struct exec *exec = (struct exec *) peer;
exec_harvest(exec);
uint32_t delay = wakeup_get_retry_delay_ms(1);
log_write('E', exec->id, "Will retry in %ds", delay / 1000);
LOG(exec->id, "Will retry in %ds", delay / 1000);
exec->peer.event_handler = exec_spawn_wrapper;
wakeup_add((struct peer *) exec, delay);
}
@@ -79,7 +81,7 @@ static void exec_log_handler(struct peer *peer) {
char linebuf[4096];
ssize_t ret = read(exec->log_peer.fd, linebuf, 4096);
if (ret <= 0) {
log_write('E', exec->id, "Log input stream closed");
LOG(exec->id, "Log input stream closed");
assert(!close(exec->log_peer.fd));
exec->log_peer.fd = -1;
return;
@@ -89,18 +91,18 @@ static void exec_log_handler(struct peer *peer) {
while ((eol = memchr(iter, '\n', len))) {
assert(eol >= iter);
size_t linelen = (size_t) (eol - iter);
log_write('E', exec->id, "(child output) %.*s", (int) linelen, iter);
LOG(exec->id, "(child output) %.*s", (int) linelen, iter);
iter += (linelen + 1);
len -= (linelen + 1);
}
if (len) {
log_write('E', exec->id, "(child output) %.*s", (int) len, iter);
LOG(exec->id, "(child output) %.*s", (int) len, iter);
}
}
static void exec_parent(struct exec *exec, pid_t child, int data_fd, int log_fd) {
exec->child = child;
log_write('E', exec->id, "Child started as process %d", exec->child);
LOG(exec->id, "Child started as process %d", exec->child);
exec->log_peer.fd = log_fd;
exec->log_peer.event_handler = exec_log_handler;
@@ -135,7 +137,7 @@ static void __attribute__ ((noreturn)) exec_child(const struct exec *exec, int d
}
static void exec_spawn(struct exec *exec) {
log_write('E', exec->id, "Executing: %s", exec->command);
LOG(exec->id, "Executing: %s", exec->command);
int data_fds[2], log_fds[2];
// Leave these sockets blocking; we move in lock step with subprograms
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, data_fds));

View File

@@ -29,6 +29,8 @@ struct file {
static struct list_head file_head = LIST_HEAD_INIT(file_head);
static char log_module = 'F';
static void file_open_wrapper(struct peer *);
static bool file_should_retry(int fd, struct file *file) {
@@ -63,14 +65,14 @@ static void file_del(struct file *file) {
static void file_retry(struct file *file) {
uint32_t delay = wakeup_get_retry_delay_ms(file->attempt++);
log_write('F', file->id, "Will retry in %ds", delay / 1000);
LOG(file->id, "Will retry in %ds", delay / 1000);
file->peer.event_handler = file_open_wrapper;
wakeup_add((struct peer *) file, delay);
}
static void file_handle_close(struct peer *peer) {
struct file *file = (struct file *) peer;
log_write('F', file->id, "File closed: %s", file->path);
LOG(file->id, "File closed: %s", file->path);
if (file->retry) {
file_retry(file);
@@ -80,10 +82,10 @@ static void file_handle_close(struct peer *peer) {
}
static void file_open(struct file *file) {
log_write('F', file->id, "Opening file: %s", file->path);
LOG(file->id, "Opening file: %s", file->path);
int fd = open(file->path, file->flags | O_CLOEXEC, S_IRUSR | S_IWUSR);
if (fd == -1) {
log_write('F', file->id, "Error opening file: %s", strerror(errno));
LOG(file->id, "Error opening file: %s", strerror(errno));
file_retry(file);
return;
}
@@ -92,7 +94,7 @@ static void file_open(struct file *file) {
file->peer.event_handler = file_handle_close;
file->attempt = 0;
if (!flow_new_send_hello(fd, file->flow, file->passthrough, (struct peer *) file)) {
log_write('F', file->id, "Error writing greeting");
LOG(file->id, "Error writing greeting");
file_retry(file);
return;
}

View File

@@ -31,11 +31,13 @@ struct incoming {
static struct list_head incoming_head = LIST_HEAD_INIT(incoming_head);
static char log_module = 'I';
static void incoming_resolve_wrapper(struct peer *);
static void incoming_retry(struct incoming *incoming) {
uint32_t delay = wakeup_get_retry_delay_ms(incoming->attempt++);
log_write('I', incoming->id, "Will retry in %ds", delay / 1000);
LOG(incoming->id, "Will retry in %ds", delay / 1000);
incoming->peer.event_handler = incoming_resolve_wrapper;
wakeup_add((struct peer *) incoming, delay);
}
@@ -48,7 +50,7 @@ static void incoming_handler(struct peer *peer) {
int fd = accept4(incoming->peer.fd, &peer_addr, &peer_addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (fd == -1) {
log_write('I', incoming->id, "Failed to accept new connection on %s/%s: %s", incoming->node, incoming->service, strerror(errno));
LOG(incoming->id, "Failed to accept new connection on %s/%s: %s", incoming->node, incoming->service, strerror(errno));
return;
}
@@ -57,7 +59,7 @@ static void incoming_handler(struct peer *peer) {
assert(getnameinfo(&peer_addr, peer_addrlen, peer_hbuf, sizeof(peer_hbuf), peer_sbuf, sizeof(peer_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0);
assert(getnameinfo(&local_addr, local_addrlen, local_hbuf, sizeof(local_hbuf), local_sbuf, sizeof(local_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0);
log_write('I', incoming->id, "New incoming connection on %s/%s (%s/%s) from %s/%s",
LOG(incoming->id, "New incoming connection on %s/%s (%s/%s) from %s/%s",
incoming->node, incoming->service,
local_hbuf, local_sbuf,
peer_hbuf, peer_sbuf);
@@ -65,7 +67,7 @@ static void incoming_handler(struct peer *peer) {
flow_socket_connected(fd, incoming->flow);
if (!flow_new_send_hello(fd, incoming->flow, incoming->passthrough, NULL)) {
log_write('I', incoming->id, "Error writing greeting");
LOG(incoming->id, "Error writing greeting");
return;
}
}
@@ -87,7 +89,7 @@ static void incoming_listen(struct peer *peer) {
struct addrinfo *addrs;
int err = resolve_result(peer, &addrs);
if (err) {
log_write('I', incoming->id, "Failed to resolve %s/%s: %s", incoming->node, incoming->service, gai_strerror(err));
LOG(incoming->id, "Failed to resolve %s/%s: %s", incoming->node, incoming->service, gai_strerror(err));
incoming_retry(incoming);
return;
}
@@ -96,7 +98,7 @@ static void incoming_listen(struct peer *peer) {
for (addr = addrs; addr; addr = addr->ai_next) {
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
assert(getnameinfo(addr->ai_addr, addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0);
log_write('I', incoming->id, "Listening on %s/%s...", hbuf, sbuf);
LOG(incoming->id, "Listening on %s/%s...", hbuf, sbuf);
incoming->peer.fd = socket(addr->ai_family, addr->ai_socktype | SOCK_CLOEXEC, addr->ai_protocol);
assert(incoming->peer.fd >= 0);
@@ -104,7 +106,7 @@ static void incoming_listen(struct peer *peer) {
socket_pre_bind(incoming->peer.fd);
if (bind(incoming->peer.fd, addr->ai_addr, addr->ai_addrlen) != 0) {
log_write('I', incoming->id, "Failed to bind to %s/%s: %s", hbuf, sbuf, strerror(errno));
LOG(incoming->id, "Failed to bind to %s/%s: %s", hbuf, sbuf, strerror(errno));
assert(!close(incoming->peer.fd));
continue;
}
@@ -120,7 +122,7 @@ static void incoming_listen(struct peer *peer) {
freeaddrinfo(addrs);
if (addr == NULL) {
log_write('I', incoming->id, "Failed to bind any addresses for %s/%s...", incoming->node, incoming->service);
LOG(incoming->id, "Failed to bind any addresses for %s/%s...", incoming->node, incoming->service);
incoming_retry(incoming);
return;
}
@@ -131,7 +133,7 @@ static void incoming_listen(struct peer *peer) {
}
static void incoming_resolve(struct incoming *incoming) {
log_write('I', incoming->id, "Resolving %s/%s...", incoming->node, incoming->service);
LOG(incoming->id, "Resolving %s/%s...", incoming->node, incoming->service);
incoming->peer.event_handler = incoming_listen;
resolve((struct peer *) incoming, incoming->node, incoming->service, AI_PASSIVE);
}

View File

@@ -26,6 +26,8 @@ struct json_parser_state {
static json_t *json_prev = NULL;
static struct buf json_hello_buf = BUF_INIT;
static char log_module = 'R'; // borrowing
static void json_serialize_to_buf(json_t *obj, struct buf *buf) {
assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0);
json_decref(obj);
@@ -79,11 +81,11 @@ static bool json_parse_header(json_t *in, struct packet *packet, struct json_par
}
if (!strcmp(json_server_id, (const char *) server_id)) {
log_write('R', packet->source_id, "Attempt to receive json data from our own server ID (%s); loop!", server_id);
LOG(packet->source_id, "Attempt to receive json data from our own server ID (%s); loop!", server_id);
return false;
}
log_write('R', packet->source_id, "Connected to server ID: %s", json_server_id);
LOG(packet->source_id, "Connected to server ID: %s", json_server_id);
state->mlat_timestamp_mhz = (uint16_t) mlat_timestamp_mhz;
state->mlat_timestamp_max = (uint64_t) mlat_timestamp_max;

View File

@@ -20,17 +20,19 @@ static uint8_t log_id[UUID_LEN];
static int log_rotate_fd;
static struct peer log_rotate_peer;
static char log_module = 'L';
static void log_rotate() {
uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN];
uuid_gen(new_log_id);
log_write('L', log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path);
LOG(log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path);
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);
setlinebuf(log_stream);
log_write('L', log_id, "Log start after switch from log ID %s", old_log_id);
LOG(log_id, "Log start after switch from log ID %s", old_log_id);
}
static void log_rotate_handler(struct peer *peer) {
@@ -52,7 +54,7 @@ void log_init() {
setlinebuf(log_stream);
uuid_gen(log_id);
log_write('L', log_id, "Log start");
LOG(log_id, "Log start");
}
void log_init2() {
@@ -68,7 +70,7 @@ void log_init2() {
}
void log_cleanup() {
log_write('L', log_id, "Log end");
LOG(log_id, "Log end");
assert(!fclose(log_stream));
assert(!close(log_rotate_fd));
assert(!close(log_rotate_peer.fd));

View File

@@ -3,6 +3,8 @@
#include <stdbool.h>
#include <stdint.h>
#define LOG(id, ...) log_write((log_module), (id), __VA_ARGS__)
void log_init(void);
void log_init2(void);
void log_cleanup(void);

View File

@@ -33,13 +33,15 @@ struct outgoing {
static struct list_head outgoing_head = LIST_HEAD_INIT(outgoing_head);
static char log_module = 'O';
static void outgoing_connect_result(struct outgoing *, int);
static void outgoing_resolve(struct outgoing *);
static void outgoing_resolve_wrapper(struct peer *);
static void outgoing_retry(struct outgoing *outgoing) {
uint32_t delay = wakeup_get_retry_delay_ms(++outgoing->attempt);
log_write('O', outgoing->id, "Will retry in %ds", delay / 1000);
LOG(outgoing->id, "Will retry in %ds", delay / 1000);
outgoing->peer.event_handler = outgoing_resolve_wrapper;
wakeup_add((struct peer *) outgoing, delay);
}
@@ -47,14 +49,14 @@ static void outgoing_retry(struct outgoing *outgoing) {
static void outgoing_connect_next(struct outgoing *outgoing) {
if (outgoing->addr == NULL) {
freeaddrinfo(outgoing->addrs);
log_write('O', outgoing->id, "Can't connect to any addresses of %s/%s", outgoing->node, outgoing->service);
LOG(outgoing->id, "Can't connect to any addresses of %s/%s", outgoing->node, outgoing->service);
outgoing_retry(outgoing);
return;
}
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0);
log_write('O', outgoing->id, "Connecting to %s/%s...", hbuf, sbuf);
LOG(outgoing->id, "Connecting to %s/%s...", hbuf, sbuf);
outgoing->peer.fd = socket(outgoing->addr->ai_family, outgoing->addr->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC, outgoing->addr->ai_protocol);
assert(outgoing->peer.fd >= 0);
@@ -81,7 +83,7 @@ static void outgoing_disconnect_handler(struct peer *peer) {
if (outgoing->peer.fd != -1) {
assert(!close(outgoing->peer.fd));
}
log_write('O', outgoing->id, "Peer disconnected; reconnecting...");
LOG(outgoing->id, "Peer disconnected; reconnecting...");
outgoing_retry(outgoing);
}
@@ -90,7 +92,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) {
assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0);
switch (result) {
case 0:
log_write('O', outgoing->id, "Connected to %s/%s", hbuf, sbuf);
LOG(outgoing->id, "Connected to %s/%s", hbuf, sbuf);
freeaddrinfo(outgoing->addrs);
outgoing->attempt = 0;
int fd = outgoing->peer.fd;
@@ -107,7 +109,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) {
break;
default:
log_write('O', outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result));
LOG(outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result));
assert(!close(outgoing->peer.fd));
outgoing->peer.fd = -1;
outgoing->addr = outgoing->addr->ai_next;
@@ -121,7 +123,7 @@ static void outgoing_resolve_handler(struct peer *peer) {
struct outgoing *outgoing = (struct outgoing *) peer;
int err = resolve_result(peer, &outgoing->addrs);
if (err) {
log_write('O', outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err));
LOG(outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err));
outgoing_retry(outgoing);
} else {
outgoing->addr = outgoing->addrs;
@@ -130,7 +132,7 @@ static void outgoing_resolve_handler(struct peer *peer) {
}
static void outgoing_resolve(struct outgoing *outgoing) {
log_write('O', outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service);
LOG(outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service);
outgoing->peer.event_handler = outgoing_resolve_handler;
resolve((struct peer *) outgoing, outgoing->node, outgoing->service, 0);
}

View File

@@ -14,6 +14,8 @@
#include "peer.h"
static char log_module = 'X';
uint32_t peer_count_in = 0, peer_count_out = 0, peer_count_out_in = 0;
static int peer_epoll_fd;
@@ -22,7 +24,7 @@ static bool peer_shutdown_flag = false;
static struct list_head peer_always_trigger_head = LIST_HEAD_INIT(peer_always_trigger_head);
static void peer_shutdown_handler(struct peer *peer) {
log_write('X', server_id, "Shutting down");
LOG(server_id, "Shutting down");
assert(!close(peer->fd));
free(peer);
peer_shutdown_flag = true;
@@ -96,13 +98,13 @@ void peer_call(struct peer *peer) {
}
void peer_loop() {
log_write('X', server_id, "Starting event loop");
LOG(server_id, "Starting event loop");
while (!peer_shutdown_flag) {
if (!(peer_count_in + peer_count_out_in)) {
log_write('X', server_id, "No remaining inputs");
LOG(server_id, "No remaining inputs");
peer_shutdown(0);
} else if (!(peer_count_out + peer_count_out_in)) {
log_write('X', server_id, "No remaining outputs");
LOG(server_id, "No remaining outputs");
peer_shutdown(0);
}
#define MAX_EVENTS 10

View File

@@ -22,6 +22,8 @@ struct proto_parser_state {
bool have_header;
};
static char log_module = 'R'; // borrowing
static Adsb *proto_prev = NULL;
static struct buf proto_hello_buf = BUF_INIT;
@@ -143,12 +145,12 @@ static bool proto_parse_header(AdsbHeader *header, struct packet *packet, struct
state->rssi_max = header->rssi_max;
if (!strcmp(header->server_id, (const char *) server_id)) {
log_write('R', packet->source_id, "Attempt to receive proto data from our own server ID (%s); loop!", server_id);
LOG(packet->source_id, "Attempt to receive proto data from our own server ID (%s); loop!", server_id);
return false;
}
state->have_header = true;
log_write('R', packet->source_id, "Connected to server ID: %s", header->server_id);
LOG(packet->source_id, "Connected to server ID: %s", header->server_id);
return true;
}

View File

@@ -38,6 +38,8 @@ struct receive {
};
static struct list_head receive_head = LIST_HEAD_INIT(receive_head);
static char log_module = 'R';
static void receive_new(int, void *, struct peer *);
static struct flow _receive_flow = {
@@ -91,7 +93,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac
for (size_t i = 0; i < NUM_PARSERS; i++) {
if (parsers[i].parse(buf, packet, state)) {
log_write('R', receive->id, "Detected input format: %s", parsers[i].name);
LOG(receive->id, "Detected input format: %s", parsers[i].name);
receive->parser_wrapper = receive_parse_wrapper;
receive->parser = parsers[i].parse;
return true;
@@ -104,7 +106,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac
}
static void receive_del(struct receive *receive) {
log_write('R', receive->id, "Connection closed");
LOG(receive->id, "Connection closed");
peer_count_in--;
peer_epoll_del((struct peer *) receive);
assert(!close(receive->peer.fd));
@@ -133,14 +135,14 @@ static void receive_read(struct peer *peer) {
continue;
}
if (++packet.hops > receive_max_hops) {
log_write('R', receive->id, "Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.", packet.hops, receive_max_hops);
LOG(receive->id, "Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.", packet.hops, receive_max_hops);
continue;
}
send_write(&packet);
}
if (receive->buf.length == BUF_LEN_MAX) {
log_write('R', receive->id, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking.");
LOG(receive->id, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking.");
receive_del(receive);
return;
}
@@ -165,7 +167,7 @@ static void receive_new(int fd, void __attribute__((unused)) *passthrough, struc
peer_epoll_add((struct peer *) receive, EPOLLIN);
log_write('R', receive->id, "New receive connection");
LOG(receive->id, "New receive connection");
}
void receive_init() {

View File

@@ -45,6 +45,8 @@ static struct flow _send_flow = {
};
struct flow *send_flow = &_send_flow;
static char log_module = 'S';
typedef void (*serialize)(struct packet *, struct buf *);
typedef void (*hello)(struct buf **);
static struct serializer {
@@ -87,7 +89,7 @@ static struct serializer {
#define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers))
static void send_del(struct send *send) {
log_write('S', send->id, "Connection closed");
LOG(send->id, "Connection closed");
peer_count_out--;
peer_epoll_del((struct peer *) send);
assert(!close(send->peer.fd));
@@ -119,7 +121,7 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) {
peer_epoll_add((struct peer *) send, 0);
log_write('S', send->id, "New send connection: %s", serializer->name);
LOG(send->id, "New send connection: %s", serializer->name);
}
void send_init() {

View File

@@ -6,7 +6,9 @@
uint8_t server_id[UUID_LEN];
char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1";
static char log_module = 'X';
void server_init() {
uuid_gen(server_id);
log_write('X', server_id, "Server start");
LOG(server_id, "Server start");
}