Add --log-timestamps

This commit is contained in:
Ian Gulliver
2016-03-07 20:12:35 -08:00
parent 3bcc2b43ba
commit eb89dafd0d
3 changed files with 26 additions and 1 deletions

View File

@@ -78,6 +78,7 @@ static bool parse_opts(int argc, char *argv[]) {
{"stdin", no_argument, 0, 'i'}, {"stdin", no_argument, 0, 'i'},
{"stdout", required_argument, 0, 'o'}, {"stdout", required_argument, 0, 'o'},
{"log-file", required_argument, 0, '1'}, {"log-file", required_argument, 0, '1'},
{"log-timestamps", no_argument, 0, '2'},
{0, 0, 0, 0 }, {0, 0, 0, 0 },
}; };
@@ -153,6 +154,10 @@ static bool parse_opts(int argc, char *argv[]) {
handler = log_reopen; handler = log_reopen;
break; break;
case '2':
handler = log_enable_timestamps;
break;
case 'h': case 'h':
default: default:
print_usage(argv[0]); print_usage(argv[0]);

View File

@@ -1,11 +1,14 @@
#include <assert.h> #include <assert.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include "log.h" #include "log.h"
@@ -19,6 +22,7 @@ static char *log_path = NULL;
static uint8_t log_id[UUID_LEN]; static uint8_t log_id[UUID_LEN];
static int log_rotate_fd; static int log_rotate_fd;
static struct peer log_rotate_peer; static struct peer log_rotate_peer;
static bool log_timestamps = false;
static char log_module = 'L'; static char log_module = 'L';
@@ -90,10 +94,25 @@ bool log_reopen(const char *path) {
return true; return true;
} }
bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) {
log_timestamps = true;
return true;
}
void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
assert(fprintf(log_stream, "%c [%18s] %s: ", type, loc, id) > 0);
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);
assert(vfprintf(log_stream, fmt, ap) > 0); assert(vfprintf(log_stream, fmt, ap) > 0);
assert(fprintf(log_stream, "\n") == 1); assert(fprintf(log_stream, "\n") == 1);
va_end(ap); va_end(ap);

View File

@@ -11,5 +11,6 @@ void log_init(void);
void log_init2(void); void log_init2(void);
void log_cleanup(void); void log_cleanup(void);
bool log_reopen(const char *); bool log_reopen(const char *);
bool log_enable_timestamps(const char *);
void log_write(char, const char *, const uint8_t *, const char *, ...) void log_write(char, const char *, const uint8_t *, const char *, ...)
__attribute__ ((__format__ (__printf__, 4, 5))); __attribute__ ((__format__ (__printf__, 4, 5)));