[ctxt] add logging

This commit is contained in:
Milo Turner 2020-02-13 13:10:08 -05:00
parent f2b0c145a0
commit d964f03b0b
4 changed files with 34 additions and 2 deletions

View File

@ -18,7 +18,7 @@ void ax_free(struct ax_ctxt* ax);
const char* ax_get_error(struct ax_ctxt* ax);
void ax_set_logger_fd(struct ax_ctxt* ax, int fd);
void ax_set_logger(struct ax_ctxt* ax, int fd, bool auto_close);
void ax_log(struct ax_ctxt* ax, const char* string);
/* -----------------------------------------------------------------------------

View File

@ -1,5 +1,7 @@
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include "util/region.h"
struct ax_theme;
@ -8,6 +10,10 @@ struct ax_theme_builder;
struct ax_ctxt {
struct rgn* init_rgn;
// logging
FILE* log_out;
bool log_auto_close;
// errors
struct rgn err_rgn;
const char* err;

View File

@ -38,6 +38,9 @@ void ax__ctxt_init(struct ax_ctxt* ax, struct rgn* init_rgn)
{
ax->init_rgn = init_rgn;
ax->log_out = NULL;
ax->log_auto_close = false;
rgn_init(&ax->err_rgn, SMALL);
ax->err = "";
@ -52,6 +55,26 @@ void ax__ctxt_cleanup(struct ax_ctxt* ax)
rgn_cleanup(&ax->err_rgn);
}
/* -----------------------------------------------------------------------------
* API functions :: logging
* -------------------------------------------------------------------------- */
void ax_set_logger(struct ax_ctxt* ax, int fd, bool auto_close)
{
if (ax->log_auto_close) {
fclose(ax->log_out);
}
ax->log_out = fdopen(fd, "w");
ax->log_auto_close = auto_close;
}
void ax_log(struct ax_ctxt* ax, const char* string)
{
if (ax->log_out != NULL) {
fprintf(ax->log_out, "[LOG] %s\n", string);
}
}
/* -----------------------------------------------------------------------------
* API functions :: themes
* -------------------------------------------------------------------------- */
@ -116,5 +139,6 @@ int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm)
}
// TODO: see if font is loaded
ax->sel_theme = thm;
ax_log(ax, "theme selected");
return 0;
}

View File

@ -6,6 +6,8 @@
int main(void)
{
struct ax_ctxt* ax = ax_new();
ax_set_logger(ax, 0, false);
int rv;
#define GUARD(f, ...) if ((rv = f(__VA_ARGS__)) != 0) goto cleanup
@ -17,7 +19,7 @@ int main(void)
struct ax_theme* thm;
ax_end_theme(ax, &thm);
printf("----\ngot here\n----\n");
ax_log(ax, "got here");
if ((rv = ax_select_theme(ax, thm)) != 0) {
goto cleanup;