Add --log-file

This commit is contained in:
Ian Gulliver
2016-03-07 11:26:25 -08:00
parent 51eaabe27a
commit 46696933aa
15 changed files with 110 additions and 61 deletions

View File

@@ -35,6 +35,7 @@ static void print_usage(const char *name) {
"\n"
"Options:\n"
"\t--help\n"
"\n"
"\t--connect-receive=HOST/PORT\n"
"\t--connect-send=FORMAT=HOST/PORT\n"
"\t--connect-send-receive=FORMAT=HOST/PORT\n"
@@ -51,6 +52,8 @@ static void print_usage(const char *name) {
"\t--exec-send-receive=FORMAT=COMMAND\n"
"\t--stdin\n"
"\t--stdout=FORMAT\n"
"\n"
"\t--log-file=PATH\n"
, name);
receive_print_usage();
send_print_usage();
@@ -58,6 +61,7 @@ static void print_usage(const char *name) {
static bool parse_opts(int argc, char *argv[]) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"connect-receive", required_argument, 0, 'c'},
{"connect-send", required_argument, 0, 's'},
{"connect-send-receive", required_argument, 0, 't'},
@@ -74,13 +78,13 @@ static bool parse_opts(int argc, char *argv[]) {
{"exec-send-receive", required_argument, 0, 'g'},
{"stdin", no_argument, 0, 'i'},
{"stdout", required_argument, 0, 'o'},
{"help", no_argument, 0, 'h'},
{"log-file", required_argument, 0, '1'},
{0, 0, 0, 0 },
};
int opt;
while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) {
bool (*handler)(char *) = NULL;
bool (*handler)(const char *) = NULL;
switch (opt) {
case 'c':
handler = opts_add_connect_receive;
@@ -146,6 +150,10 @@ static bool parse_opts(int argc, char *argv[]) {
handler = opts_add_stdout;
break;
case '1':
handler = log_reopen;
break;
case 'h':
default:
print_usage(argv[0]);
@@ -177,10 +185,11 @@ static void reopen(int fd, char *path, int flags) {
}
int main(int argc, char *argv[]) {
log_init();
hex_init();
rand_init();
log_init();
resolve_init();
server_init();
wakeup_init();
@@ -200,6 +209,7 @@ int main(int argc, char *argv[]) {
reopen(STDIN_FILENO, "/dev/null", O_RDONLY);
reopen(STDOUT_FILENO, "/dev/full", O_WRONLY);
reopen(STDERR_FILENO, "/dev/full", O_WRONLY);
peer_loop();
@@ -225,7 +235,7 @@ int main(int argc, char *argv[]) {
assert(!close(STDIN_FILENO));
assert(!close(STDOUT_FILENO));
close(STDERR_FILENO); // 2>&1 breaks this
assert(!close(STDERR_FILENO));
return EXIT_SUCCESS;
}

View File

@@ -169,7 +169,7 @@ void exec_cleanup() {
}
}
void exec_new(char *command, struct flow *flow, void *passthrough) {
void exec_new(const char *command, struct flow *flow, void *passthrough) {
flow_ref_inc(flow);
struct exec *exec = malloc(sizeof(*exec));

View File

@@ -3,4 +3,4 @@
struct flow;
void exec_cleanup(void);
void exec_new(char *, struct flow *, void *);
void exec_new(const char *, struct flow *, void *);

View File

@@ -107,7 +107,7 @@ static void file_open_wrapper(struct peer *peer) {
file_open(file);
}
static void file_new(char *path, int flags, struct flow *flow, void *passthrough) {
static void file_new(const char *path, int flags, struct flow *flow, void *passthrough) {
flow_ref_inc(flow);
struct file *file = malloc(sizeof(*file));
@@ -133,14 +133,14 @@ void file_cleanup() {
}
}
void file_read_new(char *path, struct flow *flow, void *passthrough) {
void file_read_new(const char *path, struct flow *flow, void *passthrough) {
file_new(path, O_RDONLY, flow, passthrough);
}
void file_write_new(char *path, struct flow *flow, void *passthrough) {
void file_write_new(const char *path, struct flow *flow, void *passthrough) {
file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC, flow, passthrough);
}
void file_append_new(char *path, struct flow *flow, void *passthrough) {
void file_append_new(const char *path, struct flow *flow, void *passthrough) {
file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW, flow, passthrough);
}

View File

@@ -3,6 +3,6 @@
struct flow;
void file_cleanup(void);
void file_read_new(char *, struct flow *, void *);
void file_write_new(char *, struct flow *, void *);
void file_append_new(char *, struct flow *, void *);
void file_read_new(const char *, struct flow *, void *);
void file_write_new(const char *, struct flow *, void *);
void file_append_new(const char *, struct flow *, void *);

View File

@@ -148,7 +148,7 @@ void incoming_cleanup() {
}
}
void incoming_new(char *node, char *service, struct flow *flow, void *passthrough) {
void incoming_new(const char *node, const char *service, struct flow *flow, void *passthrough) {
flow_ref_inc(flow);
struct incoming *incoming = malloc(sizeof(*incoming));

View File

@@ -3,4 +3,4 @@
struct flow;
void incoming_cleanup(void);
void incoming_new(char *, char *, struct flow *, void *);
void incoming_new(const char *, const char *, struct flow *, void *);

View File

@@ -1,22 +1,59 @@
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "uuid.h"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
static FILE *log_stream = NULL;
static char *log_path = NULL;
static uint8_t log_id[UUID_LEN];
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);
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);
log_write('L', log_id, "Log start after switch from log ID %s", old_log_id);
}
void log_init() {
log_stream = fdopen(STDERR_FILENO, "a");
int fd = dup(STDERR_FILENO);
assert(fd >= 0);
log_stream = fdopen(fd, "a");
assert(log_stream);
setlinebuf(log_stream);
uuid_gen(log_id);
log_write('L', log_id, "Log start");
}
void log_cleanup() {
log_write('L', log_id, "Log end");
assert(!fclose(log_stream));
if (log_path) {
free(log_path);
log_path = NULL;
}
}
bool log_reopen(const char *path) {
if (log_path) {
free(log_path);
}
log_path = strdup(path);
assert(log_path);
log_rotate();
return true;
}
void log_write(char type, const uint8_t *id, const char *fmt, ...) {

View File

@@ -1,8 +1,10 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
void log_init(void);
void log_cleanup(void);
bool log_reopen(const char *);
void log_write(char, const uint8_t *, const char *, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));

View File

@@ -17,7 +17,7 @@
#include "opts.h"
static char *opts_split(char **arg, char delim) {
static char *opts_split(const char **arg, char delim) {
char *split = strchr(*arg, delim);
if (!split) {
return NULL;
@@ -27,7 +27,7 @@ static char *opts_split(char **arg, char delim) {
return ret;
}
static bool opts_add_listen(char *host_port, struct flow *flow, void *passthrough) {
static bool opts_add_listen(const char *host_port, struct flow *flow, void *passthrough) {
char *host = opts_split(&host_port, '/');
if (host) {
incoming_new(host, host_port, flow, passthrough);
@@ -38,7 +38,7 @@ static bool opts_add_listen(char *host_port, struct flow *flow, void *passthroug
return true;
}
static bool opts_add_connect(char *host_port, struct flow *flow, void *passthrough) {
static bool opts_add_connect(const char *host_port, struct flow *flow, void *passthrough) {
char *host = opts_split(&host_port, '/');
if (!host) {
return false;
@@ -49,22 +49,22 @@ static bool opts_add_connect(char *host_port, struct flow *flow, void *passthrou
return true;
}
static bool opts_add_file_write_int(char *path, struct flow *flow, void *passthrough) {
static bool opts_add_file_write_int(const char *path, struct flow *flow, void *passthrough) {
file_write_new(path, flow, passthrough);
return true;
}
static bool opts_add_file_append_int(char *path, struct flow *flow, void *passthrough) {
static bool opts_add_file_append_int(const char *path, struct flow *flow, void *passthrough) {
file_append_new(path, flow, passthrough);
return true;
}
static bool opts_add_exec(char *cmd, struct flow *flow, void *passthrough) {
static bool opts_add_exec(const char *cmd, struct flow *flow, void *passthrough) {
exec_new(cmd, flow, passthrough);
return true;
}
static struct serializer *opts_get_serializer(char **arg) {
static struct serializer *opts_get_serializer(const char **arg) {
char *format = opts_split(arg, '=');
if (!format) {
return NULL;
@@ -79,7 +79,7 @@ static struct serializer *opts_get_serializer(char **arg) {
return serializer;
}
static bool opts_add_send(bool (*next)(char *, struct flow *, void *), struct flow *flow, char *arg) {
static bool opts_add_send(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) {
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
return false;
@@ -87,71 +87,71 @@ static bool opts_add_send(bool (*next)(char *, struct flow *, void *), struct fl
return next(arg, flow, serializer);
}
bool opts_add_connect_receive(char *arg) {
bool opts_add_connect_receive(const char *arg) {
return opts_add_connect(arg, receive_flow, NULL);
}
bool opts_add_connect_send(char *arg) {
bool opts_add_connect_send(const char *arg) {
return opts_add_send(opts_add_connect, send_flow, arg);
}
bool opts_add_connect_send_receive(char *arg) {
bool opts_add_connect_send_receive(const char *arg) {
return opts_add_send(opts_add_connect, send_receive_flow, arg);
}
bool opts_add_listen_receive(char *arg) {
bool opts_add_listen_receive(const char *arg) {
return opts_add_listen(arg, receive_flow, NULL);
}
bool opts_add_listen_send(char *arg) {
bool opts_add_listen_send(const char *arg) {
return opts_add_send(opts_add_listen, send_flow, arg);
}
bool opts_add_listen_send_receive(char *arg) {
bool opts_add_listen_send_receive(const char *arg) {
return opts_add_send(opts_add_listen, send_receive_flow, arg);
}
bool opts_add_file_read(char *arg) {
bool opts_add_file_read(const char *arg) {
file_read_new(arg, receive_flow, NULL);
return true;
}
bool opts_add_file_write(char *arg) {
bool opts_add_file_write(const char *arg) {
return opts_add_send(opts_add_file_write_int, send_flow, arg);
}
bool opts_add_file_write_read(char *arg) {
bool opts_add_file_write_read(const char *arg) {
return opts_add_send(opts_add_file_write_int, send_receive_flow, arg);
}
bool opts_add_file_append(char *arg) {
bool opts_add_file_append(const char *arg) {
return opts_add_send(opts_add_file_append_int, send_flow, arg);
}
bool opts_add_file_append_read(char *arg) {
bool opts_add_file_append_read(const char *arg) {
return opts_add_send(opts_add_file_append_int, send_receive_flow, arg);
}
bool opts_add_exec_receive(char *arg) {
bool opts_add_exec_receive(const char *arg) {
exec_new(arg, receive_flow, NULL);
return true;
}
bool opts_add_exec_send(char *arg) {
bool opts_add_exec_send(const char *arg) {
return opts_add_send(opts_add_exec, send_flow, arg);
}
bool opts_add_exec_send_receive(char *arg) {
bool opts_add_exec_send_receive(const char *arg) {
return opts_add_send(opts_add_exec, send_receive_flow, arg);
}
bool opts_add_stdin(char __attribute__((unused)) *arg) {
bool opts_add_stdin(const char __attribute__((unused)) *arg) {
int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
assert(fd >= 0);
return flow_new_send_hello(fd, receive_flow, NULL, NULL);
}
bool opts_add_stdout(char *arg) {
bool opts_add_stdout(const char *arg) {
struct serializer *serializer = send_get_serializer(arg);
if (!serializer) {
return false;

View File

@@ -2,19 +2,19 @@
#include <stdbool.h>
bool opts_add_connect_receive(char *);
bool opts_add_connect_send(char *);
bool opts_add_connect_send_receive(char *);
bool opts_add_listen_receive(char *);
bool opts_add_listen_send(char *);
bool opts_add_listen_send_receive(char *);
bool opts_add_file_read(char *);
bool opts_add_file_write(char *);
bool opts_add_file_write_read(char *);
bool opts_add_file_append(char *);
bool opts_add_file_append_read(char *);
bool opts_add_exec_receive(char *);
bool opts_add_exec_send(char *);
bool opts_add_exec_send_receive(char *);
bool opts_add_stdout(char *);
bool opts_add_stdin(char *);
bool opts_add_connect_receive(const char *);
bool opts_add_connect_send(const char *);
bool opts_add_connect_send_receive(const char *);
bool opts_add_listen_receive(const char *);
bool opts_add_listen_send(const char *);
bool opts_add_listen_send_receive(const char *);
bool opts_add_file_read(const char *);
bool opts_add_file_write(const char *);
bool opts_add_file_write_read(const char *);
bool opts_add_file_append(const char *);
bool opts_add_file_append_read(const char *);
bool opts_add_exec_receive(const char *);
bool opts_add_exec_send(const char *);
bool opts_add_exec_send_receive(const char *);
bool opts_add_stdout(const char *);
bool opts_add_stdin(const char *);

View File

@@ -158,7 +158,7 @@ void outgoing_cleanup() {
}
}
void outgoing_new(char *node, char *service, struct flow *flow, void *passthrough) {
void outgoing_new(const char *node, const char *service, struct flow *flow, void *passthrough) {
flow_ref_inc(flow);
struct outgoing *outgoing = malloc(sizeof(*outgoing));

View File

@@ -3,4 +3,4 @@
struct flow;
void outgoing_cleanup(void);
void outgoing_new(char *, char *, struct flow *, void *);
void outgoing_new(const char *, const char *, struct flow *, void *);

View File

@@ -141,7 +141,7 @@ void send_cleanup() {
}
}
void *send_get_serializer(char *name) {
void *send_get_serializer(const char *name) {
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
if (strcasecmp(serializers[i].name, name) == 0) {
return &serializers[i];

View File

@@ -6,7 +6,7 @@ struct packet;
void send_init(void);
void send_cleanup(void);
void *send_get_serializer(char *);
void *send_get_serializer(const char *);
void send_get_hello(struct buf **, void *);
void send_write(struct packet *);
void send_print_usage(void);