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

@@ -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, ...) {