2018-12-19 18:10:58 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2019-01-20 16:15:20 -05:00
|
|
|
#include <stdlib.h>
|
2018-12-19 18:10:58 -05:00
|
|
|
#include <string.h>
|
2018-12-19 23:42:18 -05:00
|
|
|
#include <sys/uio.h>
|
2018-12-19 18:10:58 -05:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_OPENGL
|
2019-02-08 19:36:14 -05:00
|
|
|
#include <GL/gl.h>
|
|
|
|
#include "backend/gl/glx.h"
|
2019-01-27 13:16:28 -05:00
|
|
|
#include "backend/gl/gl_common.h"
|
2018-12-19 18:10:58 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
thread_local struct log *tls_logger;
|
|
|
|
|
|
|
|
struct log_target;
|
|
|
|
|
|
|
|
struct log {
|
|
|
|
struct log_target *head;
|
|
|
|
|
|
|
|
int log_level;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct log_target {
|
|
|
|
const struct log_ops *ops;
|
|
|
|
struct log_target *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct log_ops {
|
|
|
|
void (*write)(struct log_target *, const char *, size_t);
|
2018-12-19 23:42:18 -05:00
|
|
|
void (*writev)(struct log_target *, const struct iovec *, int vcnt);
|
2018-12-19 18:10:58 -05:00
|
|
|
void (*destroy)(struct log_target *);
|
|
|
|
|
|
|
|
/// Additional strings to print around the log_level string
|
|
|
|
const char *(*colorize_begin)(enum log_level);
|
|
|
|
const char *(*colorize_end)(enum log_level);
|
|
|
|
};
|
|
|
|
|
2018-12-19 23:42:18 -05:00
|
|
|
/// Fallback writev for targets don't implement it
|
|
|
|
static attr_unused void
|
|
|
|
log_default_writev(struct log_target *tgt, const struct iovec *vec, int vcnt) {
|
|
|
|
size_t total = 0;
|
|
|
|
for (int i = 0; i < vcnt; i++) {
|
|
|
|
total += vec[i].iov_len;
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:47:29 -05:00
|
|
|
if (!total) {
|
|
|
|
// Nothing to write
|
|
|
|
return;
|
|
|
|
}
|
2018-12-19 23:42:18 -05:00
|
|
|
char *buf = ccalloc(total, char);
|
|
|
|
total = 0;
|
|
|
|
for (int i = 0; i < vcnt; i++) {
|
|
|
|
memcpy(buf + total, vec[i].iov_base, vec[i].iov_len);
|
|
|
|
total += vec[i].iov_len;
|
|
|
|
}
|
|
|
|
tgt->ops->write(tgt, buf, total);
|
|
|
|
free(buf);
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static attr_const const char *log_level_to_string(enum log_level level) {
|
|
|
|
switch (level) {
|
|
|
|
case LOG_LEVEL_TRACE: return "TRACE";
|
|
|
|
case LOG_LEVEL_DEBUG: return "DEBUG";
|
|
|
|
case LOG_LEVEL_INFO: return "INFO";
|
|
|
|
case LOG_LEVEL_WARN: return "WARN";
|
|
|
|
case LOG_LEVEL_ERROR: return "ERROR";
|
2018-12-19 20:58:23 -05:00
|
|
|
case LOG_LEVEL_FATAL: return "FATAL ERROR";
|
2019-02-01 20:58:41 -05:00
|
|
|
default: return "????";
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:21:51 -05:00
|
|
|
enum log_level string_to_log_level(const char *str) {
|
2018-12-19 18:10:58 -05:00
|
|
|
if (strcasecmp(str, "TRACE") == 0)
|
|
|
|
return LOG_LEVEL_TRACE;
|
|
|
|
else if (strcasecmp(str, "DEBUG") == 0)
|
|
|
|
return LOG_LEVEL_DEBUG;
|
|
|
|
else if (strcasecmp(str, "INFO") == 0)
|
|
|
|
return LOG_LEVEL_INFO;
|
|
|
|
else if (strcasecmp(str, "WARN") == 0)
|
|
|
|
return LOG_LEVEL_WARN;
|
|
|
|
else if (strcasecmp(str, "ERROR") == 0)
|
|
|
|
return LOG_LEVEL_ERROR;
|
|
|
|
return LOG_LEVEL_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct log *log_new(void) {
|
|
|
|
auto ret = cmalloc(struct log);
|
|
|
|
ret->log_level = LOG_LEVEL_WARN;
|
|
|
|
ret->head = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_add_target(struct log *l, struct log_target *tgt) {
|
2018-12-19 23:42:18 -05:00
|
|
|
assert(tgt->ops->writev);
|
2018-12-19 18:10:58 -05:00
|
|
|
tgt->next = l->head;
|
|
|
|
l->head = tgt;
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:48:39 -05:00
|
|
|
/// Remove a previously added log target for a log struct, and destroy it. If the log
|
|
|
|
/// target was never added, nothing happens.
|
2019-02-07 16:48:39 -05:00
|
|
|
void log_remove_target(struct log *l, struct log_target *tgt) {
|
|
|
|
struct log_target *now = l->head, **prev = &l->head;
|
|
|
|
while (now) {
|
|
|
|
if (now == tgt) {
|
|
|
|
*prev = now->next;
|
|
|
|
tgt->ops->destroy(tgt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = &now->next;
|
|
|
|
now = now->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy a log struct and every log target added to it
|
2018-12-19 18:10:58 -05:00
|
|
|
void log_destroy(struct log *l) {
|
|
|
|
// free all tgt
|
|
|
|
struct log_target *head = l->head;
|
|
|
|
while (head) {
|
|
|
|
auto next = head->next;
|
|
|
|
head->ops->destroy(head);
|
|
|
|
head = next;
|
|
|
|
}
|
|
|
|
free(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_set_level(struct log *l, int level) {
|
2018-12-19 20:58:23 -05:00
|
|
|
assert(level <= LOG_LEVEL_FATAL && level >= 0);
|
2018-12-19 18:10:58 -05:00
|
|
|
l->log_level = level;
|
|
|
|
}
|
|
|
|
|
2018-12-19 21:34:45 -05:00
|
|
|
enum log_level log_get_level(const struct log *l) {
|
|
|
|
return l->log_level;
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:10:58 -05:00
|
|
|
attr_printf(4, 5) void log_printf(struct log *l, int level, const char *func,
|
|
|
|
const char *fmt, ...) {
|
2018-12-19 20:58:23 -05:00
|
|
|
assert(level <= LOG_LEVEL_FATAL && level >= 0);
|
2018-12-19 18:10:58 -05:00
|
|
|
if (level < l->log_level)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *buf = NULL;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2019-03-30 05:07:21 -04:00
|
|
|
int blen = vasprintf(&buf, fmt, args);
|
2018-12-19 18:10:58 -05:00
|
|
|
va_end(args);
|
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
if (blen < 0 || !buf) {
|
|
|
|
free(buf);
|
2018-12-19 23:42:18 -05:00
|
|
|
return;
|
2019-03-30 05:07:21 -04:00
|
|
|
}
|
2018-12-19 23:42:18 -05:00
|
|
|
|
2018-12-19 18:10:58 -05:00
|
|
|
struct timespec ts;
|
|
|
|
timespec_get(&ts, TIME_UTC);
|
|
|
|
auto tm = localtime(&ts.tv_sec);
|
|
|
|
char time_buf[100];
|
|
|
|
strftime(time_buf, sizeof time_buf, "%x %T", tm);
|
|
|
|
|
2018-12-19 23:42:18 -05:00
|
|
|
char *time = NULL;
|
2019-03-30 05:07:21 -04:00
|
|
|
int tlen = asprintf(&time, "%s.%03ld", time_buf, ts.tv_nsec / 1000000);
|
|
|
|
if (tlen < 0 || !time) {
|
2018-12-19 23:42:18 -05:00
|
|
|
free(buf);
|
2019-03-30 05:07:21 -04:00
|
|
|
free(time);
|
2018-12-19 18:10:58 -05:00
|
|
|
return;
|
2018-12-19 23:42:18 -05:00
|
|
|
}
|
2018-12-19 18:10:58 -05:00
|
|
|
|
|
|
|
const char *log_level_str = log_level_to_string(level);
|
2018-12-19 23:42:18 -05:00
|
|
|
size_t llen = strlen(log_level_str);
|
|
|
|
size_t flen = strlen(func);
|
2018-12-19 18:10:58 -05:00
|
|
|
|
|
|
|
struct log_target *head = l->head;
|
|
|
|
while (head) {
|
2018-12-19 23:42:18 -05:00
|
|
|
const char *p = "", *s = "";
|
|
|
|
size_t plen = 0, slen = 0;
|
|
|
|
|
2018-12-19 18:10:58 -05:00
|
|
|
if (head->ops->colorize_begin) {
|
|
|
|
// construct target specific prefix
|
2018-12-19 23:42:18 -05:00
|
|
|
p = head->ops->colorize_begin(level);
|
|
|
|
plen = strlen(p);
|
|
|
|
if (head->ops->colorize_end) {
|
2018-12-19 18:10:58 -05:00
|
|
|
s = head->ops->colorize_end(level);
|
2018-12-19 23:42:18 -05:00
|
|
|
slen = strlen(s);
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 23:42:18 -05:00
|
|
|
head->ops->writev(
|
|
|
|
head,
|
|
|
|
(struct iovec[]){{.iov_base = "[ ", .iov_len = 2},
|
2019-03-30 05:07:21 -04:00
|
|
|
{.iov_base = time, .iov_len = (size_t)tlen},
|
2018-12-19 23:42:18 -05:00
|
|
|
{.iov_base = " ", .iov_len = 1},
|
|
|
|
{.iov_base = (void *)func, .iov_len = flen},
|
|
|
|
{.iov_base = " ", .iov_len = 1},
|
|
|
|
{.iov_base = (void *)p, .iov_len = plen},
|
|
|
|
{.iov_base = (void *)log_level_str, .iov_len = llen},
|
|
|
|
{.iov_base = (void *)s, .iov_len = slen},
|
|
|
|
{.iov_base = " ] ", .iov_len = 3},
|
2019-03-30 05:07:21 -04:00
|
|
|
{.iov_base = buf, .iov_len = (size_t)blen},
|
2018-12-19 23:42:18 -05:00
|
|
|
{.iov_base = "\n", .iov_len = 1}},
|
|
|
|
11);
|
2018-12-19 18:10:58 -05:00
|
|
|
head = head->next;
|
|
|
|
}
|
2018-12-19 23:42:18 -05:00
|
|
|
free(time);
|
|
|
|
free(buf);
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A trivial deinitializer that simply frees the memory
|
|
|
|
static attr_unused void logger_trivial_destroy(struct log_target *tgt) {
|
|
|
|
free(tgt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A null log target that does nothing
|
|
|
|
static const struct log_ops null_logger_ops;
|
|
|
|
static struct log_target null_logger_target = {
|
|
|
|
.ops = &null_logger_ops,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct log_target *null_logger_new(void) {
|
|
|
|
return &null_logger_target;
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:42:18 -05:00
|
|
|
static void null_logger_write(struct log_target *attr_unused tgt,
|
|
|
|
const char *attr_unused str, size_t attr_unused len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_logger_writev(struct log_target *attr_unused tgt,
|
|
|
|
const struct iovec *attr_unused vec, int attr_unused vcnt) {
|
2018-12-19 18:10:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct log_ops null_logger_ops = {
|
|
|
|
.write = null_logger_write,
|
2018-12-19 23:42:18 -05:00
|
|
|
.writev = null_logger_writev,
|
2018-12-19 18:10:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A file based logger that writes to file (or stdout/stderr)
|
|
|
|
struct file_logger {
|
|
|
|
struct log_target tgt;
|
|
|
|
FILE *f;
|
|
|
|
struct log_ops ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
void file_logger_write(struct log_target *tgt, const char *str, size_t len) {
|
|
|
|
auto f = (struct file_logger *)tgt;
|
|
|
|
fwrite(str, 1, len, f->f);
|
|
|
|
}
|
|
|
|
|
2018-12-19 23:42:18 -05:00
|
|
|
void file_logger_writev(struct log_target *tgt, const struct iovec *vec, int vcnt) {
|
|
|
|
auto f = (struct file_logger *)tgt;
|
|
|
|
fflush(f->f);
|
|
|
|
writev(fileno(f->f), vec, vcnt);
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:10:58 -05:00
|
|
|
void file_logger_destroy(struct log_target *tgt) {
|
|
|
|
auto f = (struct file_logger *)tgt;
|
|
|
|
fclose(f->f);
|
|
|
|
free(tgt);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ANSI(x) "\033[" x "m"
|
|
|
|
const char *terminal_colorize_begin(enum log_level level) {
|
|
|
|
switch (level) {
|
|
|
|
case LOG_LEVEL_TRACE: return ANSI("30;2");
|
|
|
|
case LOG_LEVEL_DEBUG: return ANSI("37;2");
|
|
|
|
case LOG_LEVEL_INFO: return ANSI("92");
|
|
|
|
case LOG_LEVEL_WARN: return ANSI("33");
|
|
|
|
case LOG_LEVEL_ERROR: return ANSI("31;1");
|
2018-12-19 20:58:23 -05:00
|
|
|
case LOG_LEVEL_FATAL: return ANSI("30;103;1");
|
2019-02-01 20:58:41 -05:00
|
|
|
default: return "";
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *terminal_colorize_end(enum log_level level) {
|
|
|
|
return ANSI("0");
|
|
|
|
}
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
static const struct log_ops file_logger_ops = {
|
|
|
|
.write = file_logger_write,
|
2018-12-19 23:42:18 -05:00
|
|
|
.writev = file_logger_writev,
|
2018-12-19 18:10:58 -05:00
|
|
|
.destroy = file_logger_destroy,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct log_target *file_logger_new(const char *filename) {
|
2018-12-21 18:02:19 -05:00
|
|
|
FILE *f = fopen(filename, "a");
|
2018-12-19 18:10:58 -05:00
|
|
|
if (!f) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = cmalloc(struct file_logger);
|
|
|
|
ret->tgt.ops = &ret->ops;
|
|
|
|
ret->f = f;
|
|
|
|
|
|
|
|
// Always assume a file is not a terminal
|
|
|
|
ret->ops = file_logger_ops;
|
|
|
|
|
|
|
|
return &ret->tgt;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct log_target *stderr_logger_new(void) {
|
|
|
|
int fd = dup(STDERR_FILENO);
|
|
|
|
if (fd < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fdopen(fd, "w");
|
|
|
|
if (!f) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = cmalloc(struct file_logger);
|
|
|
|
ret->tgt.ops = &ret->ops;
|
|
|
|
ret->f = f;
|
|
|
|
ret->ops = file_logger_ops;
|
|
|
|
|
|
|
|
if (isatty(fd)) {
|
|
|
|
ret->ops.colorize_begin = terminal_colorize_begin;
|
|
|
|
ret->ops.colorize_end = terminal_colorize_end;
|
|
|
|
}
|
|
|
|
return &ret->tgt;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_OPENGL
|
|
|
|
/// An opengl logger that can be used for logging into opengl debugging tools,
|
|
|
|
/// such as apitrace
|
2019-03-27 10:51:20 -04:00
|
|
|
struct gl_string_marker_logger {
|
2018-12-19 18:10:58 -05:00
|
|
|
struct log_target tgt;
|
2019-03-30 05:07:21 -04:00
|
|
|
PFNGLSTRINGMARKERGREMEDYPROC gl_string_marker;
|
2018-12-19 18:10:58 -05:00
|
|
|
};
|
|
|
|
|
2019-03-27 10:51:20 -04:00
|
|
|
void gl_string_marker_logger_write(struct log_target *tgt, const char *str, size_t len) {
|
|
|
|
auto g = (struct gl_string_marker_logger *)tgt;
|
2019-03-30 05:07:21 -04:00
|
|
|
g->gl_string_marker((GLsizei)len, str);
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 10:51:20 -04:00
|
|
|
static const struct log_ops gl_string_marker_logger_ops = {
|
|
|
|
.write = gl_string_marker_logger_write,
|
2018-12-19 23:42:18 -05:00
|
|
|
.writev = log_default_writev,
|
2018-12-19 18:10:58 -05:00
|
|
|
.destroy = logger_trivial_destroy,
|
|
|
|
};
|
|
|
|
|
2019-03-27 10:51:20 -04:00
|
|
|
struct log_target *gl_string_marker_logger_new(void) {
|
2019-01-27 13:16:28 -05:00
|
|
|
if (!gl_has_extension("GL_GREMEDY_string_marker")) {
|
2019-01-09 16:58:16 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:10:58 -05:00
|
|
|
void *fnptr = glXGetProcAddress((GLubyte *)"glStringMarkerGREMEDY");
|
|
|
|
if (!fnptr)
|
|
|
|
return NULL;
|
|
|
|
|
2019-03-27 10:51:20 -04:00
|
|
|
auto ret = cmalloc(struct gl_string_marker_logger);
|
|
|
|
ret->tgt.ops = &gl_string_marker_logger_ops;
|
|
|
|
ret->gl_string_marker = fnptr;
|
2018-12-19 18:10:58 -05:00
|
|
|
return &ret->tgt;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2019-03-27 10:51:20 -04:00
|
|
|
struct log_target *gl_string_marker_logger_new(void) {
|
2019-01-27 12:45:38 -05:00
|
|
|
return NULL;
|
2018-12-19 18:10:58 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// vim: set noet sw=8 ts=8:
|