Rewrite Logger
This reverts all of Roop's changes to the C code, and then rewrites the logger logic to be cleaner. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -16,6 +16,23 @@
|
||||
#include <sys/mman.h>
|
||||
#include "ringlogger.h"
|
||||
|
||||
enum {
|
||||
MAX_LOG_LINE_LENGTH = 512,
|
||||
MAX_LINES = 1024,
|
||||
MAGIC = 0xdeadbeefU
|
||||
};
|
||||
|
||||
struct log_line {
|
||||
struct timeval tv;
|
||||
char line[MAX_LOG_LINE_LENGTH];
|
||||
};
|
||||
|
||||
struct log {
|
||||
struct { uint32_t first, len; } header;
|
||||
struct log_line lines[MAX_LINES];
|
||||
uint32_t magic;
|
||||
};
|
||||
|
||||
void write_msg_to_log(struct log *log, const char *msg)
|
||||
{
|
||||
struct log_line *line = &log->lines[(log->header.first + log->header.len) % MAX_LINES];
|
||||
@@ -29,13 +46,6 @@ void write_msg_to_log(struct log *log, const char *msg)
|
||||
strncpy(line->line, msg, MAX_LOG_LINE_LENGTH - 1);
|
||||
line->line[MAX_LOG_LINE_LENGTH - 1] = '\0';
|
||||
|
||||
// Trim trailing newlines
|
||||
unsigned long length = strlen(msg);
|
||||
while ((length > 0) && (msg[length - 1] == '\n' || msg[length - 1] == '\r')) {
|
||||
line->line[length - 1] = '\0';
|
||||
length--;
|
||||
}
|
||||
|
||||
msync(&log->header, sizeof(log->header), MS_ASYNC);
|
||||
msync(line, sizeof(*line), MS_ASYNC);
|
||||
}
|
||||
@@ -44,12 +54,12 @@ static bool first_before_second(const struct log_line *line1, const struct log_l
|
||||
{
|
||||
if (line1->tv.tv_sec <= line2->tv.tv_sec)
|
||||
return true;
|
||||
else if (line1->tv.tv_sec == line2->tv.tv_sec)
|
||||
if (line1->tv.tv_sec == line2->tv.tv_sec)
|
||||
return line1->tv.tv_usec <= line2->tv.tv_usec;
|
||||
return false;
|
||||
}
|
||||
|
||||
int write_logs_to_file(const char *file_name, const char *tag1, const struct log *log1, const char *tag2, const struct log *log2)
|
||||
int write_logs_to_file(const char *file_name, const struct log *log1, const struct log *log2)
|
||||
{
|
||||
uint32_t i1, i2, len1 = log1->header.len, len2 = log2->header.len;
|
||||
char buf[MAX_LOG_LINE_LENGTH];
|
||||
@@ -68,22 +78,19 @@ int write_logs_to_file(const char *file_name, const char *tag1, const struct log
|
||||
const struct log_line *line1 = &log1->lines[(log1->header.first + i1) % MAX_LINES];
|
||||
const struct log_line *line2 = &log2->lines[(log2->header.first + i2) % MAX_LINES];
|
||||
const struct log_line *line;
|
||||
const char *tag;
|
||||
|
||||
if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) {
|
||||
line = line1;
|
||||
tag = (const char *) tag1;
|
||||
++i1;
|
||||
} else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) {
|
||||
line = line2;
|
||||
tag = (const char *) tag2;
|
||||
++i2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
memcpy(buf, line->line, MAX_LOG_LINE_LENGTH);
|
||||
buf[MAX_LOG_LINE_LENGTH - 1] = '\0';
|
||||
if (fprintf(file, "%lu.%06d: [%s] %s\n", line->tv.tv_sec, line->tv.tv_usec, tag, buf) < 0) {
|
||||
if (fprintf(file, "%lu.%06d: %s\n", line->tv.tv_sec, line->tv.tv_usec, buf) < 0) {
|
||||
int ret = -errno;
|
||||
fclose(file);
|
||||
return ret;
|
||||
@@ -102,10 +109,10 @@ struct log *open_log(const char *file_name)
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
if (ftruncate(fd, sizeof(*log)))
|
||||
return NULL;
|
||||
goto err;
|
||||
log = mmap(NULL, sizeof(*log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (log == MAP_FAILED)
|
||||
return NULL;
|
||||
goto err;
|
||||
close(fd);
|
||||
|
||||
if (log->magic != MAGIC) {
|
||||
@@ -115,4 +122,13 @@ struct log *open_log(const char *file_name)
|
||||
}
|
||||
|
||||
return log;
|
||||
|
||||
err:
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void close_log(struct log *log)
|
||||
{
|
||||
munmap(log, sizeof(*log));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user