tools: copy the default log handler to the eis demo server

The default log handler logs to stderr, but we want stdout here to have
it intermixed with our real log messages (easier to debug this way).
Would probably be better to have this somewhere shared but for now this
will do.
This commit is contained in:
Peter Hutterer 2023-03-02 14:10:53 +10:00
parent f300a4919a
commit c5ebd345fd

View file

@ -70,6 +70,50 @@ static void sighandler(int signal) {
static
OBJECT_IMPLEMENT_UNREF_CLEANUP(eis_demo_client);
static void
log_handler(struct eis *eis, enum eis_log_priority priority,
const char *message, struct eis_log_context *ctx)
{
struct lut {
const char *color;
const char *prefix;
} lut[] = {
{ .color = ansi_colorcode[RED], .prefix = "<undefined>", }, /* debug starts at 10 */
{ .color = ansi_colorcode[HIGHLIGHT], .prefix = "DEBUG", },
{ .color = ansi_colorcode[GREEN], .prefix = "INFO", },
{ .color = ansi_colorcode[BLUE], .prefix = "WARN", },
{ .color = ansi_colorcode[RED], .prefix = "ERROR", },
};
static time_t last_time = 0;
const char *reset_code = ansi_colorcode[RESET];
run_only_once {
if (!isatty(STDOUT_FILENO)) {
struct lut *l;
ARRAY_FOR_EACH(lut, l)
l->color = "";
reset_code = "";
}
}
time_t now = time(NULL);
char timestamp[64];
if (last_time != now) {
struct tm *tm = localtime(&now);
strftime(timestamp, sizeof(timestamp), "%T", tm);
} else {
xsnprintf(timestamp, sizeof(timestamp), "...");
}
size_t idx = priority/10;
assert(idx < ARRAY_LENGTH(lut));
fprintf(stdout, " EIS: %8s | %s%4s%s | %s\n", timestamp,
lut[idx].color, lut[idx].prefix, reset_code, message);
last_time = now;
}
static void
eis_demo_client_destroy(struct eis_demo_client *democlient)
{
@ -540,8 +584,10 @@ int main(int argc, char **argv)
_unref_(eis) *eis = eis_new(NULL);
assert(eis);
if (verbose)
if (verbose) {
eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG);
eis_log_set_handler(eis, log_handler);
}
signal(SIGINT, sighandler);