aboutsummaryrefslogtreecommitdiffstats
path: root/src/world.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 20:00:25 +0100
committerDavid Robillard <d@drobilla.net>2022-01-14 01:13:45 -0500
commit0825ceb561b2f52cfa253cb8bb0613896f903363 (patch)
tree461d338415debf2fa9b489b53db738fbf7f8ddd8 /src/world.c
parentaf81ace5f5a8f4bb0df93dd937395c65e92a5b6a (diff)
downloadserd-0825ceb561b2f52cfa253cb8bb0613896f903363.tar.gz
serd-0825ceb561b2f52cfa253cb8bb0613896f903363.tar.bz2
serd-0825ceb561b2f52cfa253cb8bb0613896f903363.zip
Add extensible logging API
Diffstat (limited to 'src/world.c')
-rw-r--r--src/world.c76
1 files changed, 37 insertions, 39 deletions
diff --git a/src/world.c b/src/world.c
index bd70d615..d0ad60da 100644
--- a/src/world.c
+++ b/src/world.c
@@ -16,48 +16,53 @@
#include "world.h"
-#include "caret.h"
#include "namespaces.h"
#include "node.h"
+#include "serd_config.h"
-#include <stdarg.h>
+#if USE_FILENO && USE_ISATTY
+# include <unistd.h>
+#endif
+
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLANK_CHARS 12
-SerdStatus
-serd_world_error(const SerdWorld* const world, const SerdError* const e)
+static bool
+terminal_supports_color(FILE* const stream)
{
- if (world->error_func) {
- world->error_func(world->error_handle, e);
- } else {
- fprintf(stderr, "error: ");
- if (e->caret) {
- fprintf(stderr,
- "%s:%u:%u: ",
- serd_node_string(e->caret->file),
- e->caret->line,
- e->caret->col);
- }
- vfprintf(stderr, e->fmt, *e->args);
+ // https://no-color.org/
+ // NOLINTNEXTLINE(concurrency-mt-unsafe)
+ if (getenv("NO_COLOR")) {
+ return false;
}
- return e->status;
-}
-SerdStatus
-serd_world_errorf(const SerdWorld* const world,
- const SerdStatus st,
- const char* const fmt,
- ...)
-{
- va_list args;
- va_start(args, fmt);
- const SerdError e = {st, NULL, fmt, &args};
- serd_world_error(world, &e);
- va_end(args);
- return st;
+ // https://bixense.com/clicolors/
+ // NOLINTNEXTLINE(concurrency-mt-unsafe)
+ const char* const clicolor_force = getenv("CLICOLOR_FORCE");
+ if (clicolor_force && !!strcmp(clicolor_force, "0")) {
+ return true;
+ }
+
+ // https://bixense.com/clicolors/
+ // NOLINTNEXTLINE(concurrency-mt-unsafe)
+ const char* const clicolor = getenv("CLICOLOR");
+ if (clicolor && !strcmp(clicolor, "0")) {
+ return false;
+ }
+
+#if USE_FILENO && USE_ISATTY
+
+ // Assume support if stream is a TTY (blissfully ignoring termcap nightmares)
+ return isatty(fileno(stream));
+
+#else
+ (void)stream;
+ return false;
+#endif
}
SerdWorld*
@@ -85,6 +90,8 @@ serd_world_new(void)
world->blank_node = serd_new_blank(SERD_STRING("b00000000000"));
world->nodes = nodes;
+ world->stderr_color = terminal_supports_color(stderr);
+
return world;
}
@@ -115,12 +122,3 @@ serd_world_get_blank(SerdWorld* const world)
return world->blank_node;
}
-
-void
-serd_world_set_error_func(SerdWorld* world,
- SerdErrorFunc error_func,
- void* handle)
-{
- world->error_func = error_func;
- world->error_handle = handle;
-}