logger: add support for logging to file

This commit is contained in:
Wim Taymans 2018-09-26 10:48:01 +02:00
parent 9b2a051daf
commit 7a0a150337

View file

@ -41,6 +41,7 @@ struct impl {
struct spa_log log; struct spa_log log;
bool colors; bool colors;
FILE *file;
struct spa_ringbuffer trace_rb; struct spa_ringbuffer trace_rb;
uint8_t trace_data[TRACE_BUFFER]; uint8_t trace_data[TRACE_BUFFER];
@ -93,9 +94,10 @@ impl_log_logv(struct spa_log *log,
spa_ringbuffer_write_update(&impl->trace_rb, index + size); spa_ringbuffer_write_update(&impl->trace_rb, index + size);
if (write(impl->source.fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) if (write(impl->source.fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
fprintf(stderr, "error signaling eventfd: %s\n", strerror(errno)); fprintf(impl->file, "error signaling eventfd: %s\n", strerror(errno));
} else } else
fputs(location, stderr); fputs(location, impl->file);
fflush(impl->file);
} }
@ -121,7 +123,7 @@ static void on_trace_event(struct spa_source *source)
uint64_t count; uint64_t count;
if (read(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t)) if (read(source->fd, &count, sizeof(uint64_t)) != sizeof(uint64_t))
fprintf(stderr, "failed to read event fd: %s", strerror(errno)); fprintf(impl->file, "failed to read event fd: %s", strerror(errno));
while ((avail = spa_ringbuffer_get_read_index(&impl->trace_rb, &index)) > 0) { while ((avail = spa_ringbuffer_get_read_index(&impl->trace_rb, &index)) > 0) {
uint32_t offset, first; uint32_t offset, first;
@ -133,11 +135,12 @@ static void on_trace_event(struct spa_source *source)
offset = index & (TRACE_BUFFER - 1); offset = index & (TRACE_BUFFER - 1);
first = SPA_MIN(avail, TRACE_BUFFER - offset); first = SPA_MIN(avail, TRACE_BUFFER - offset);
fwrite(impl->trace_data + offset, first, 1, stderr); fwrite(impl->trace_data + offset, first, 1, impl->file);
if (SPA_UNLIKELY(avail > first)) { if (SPA_UNLIKELY(avail > first)) {
fwrite(impl->trace_data, avail - first, 1, stderr); fwrite(impl->trace_data, avail - first, 1, impl->file);
} }
spa_ringbuffer_read_update(&impl->trace_rb, index + avail); spa_ringbuffer_read_update(&impl->trace_rb, index + avail);
fflush(impl->file);
} }
} }
@ -215,9 +218,17 @@ impl_init(const struct spa_handle_factory *factory,
if (support[i].type == SPA_TYPE_INTERFACE_MainLoop) if (support[i].type == SPA_TYPE_INTERFACE_MainLoop)
loop = support[i].data; loop = support[i].data;
} }
if (info) {
if (info && (str = spa_dict_lookup(info, "log.colors")) != NULL) if ((str = spa_dict_lookup(info, "log.colors")) != NULL)
this->colors = (strcmp(str, "true") == 0 || atoi(str) == 1); this->colors = (strcmp(str, "true") == 0 || atoi(str) == 1);
if ((str = spa_dict_lookup(info, "log.file")) != NULL) {
this->file = fopen(str, "w");
if (this->file == NULL)
fprintf(stderr, "failed to open file %s: (%s)", str, strerror(errno));
}
}
if (this->file == NULL)
this->file = stderr;
if (loop) { if (loop) {
this->source.func = on_trace_event; this->source.func = on_trace_event;