diff --git a/include/common/log.h b/include/common/log.h new file mode 100644 index 00000000..b2ae2efd --- /dev/null +++ b/include/common/log.h @@ -0,0 +1,14 @@ +#ifndef __LABWC_LOG_H +#define __LABWC_LOG_H + +/** + * info - print info message + */ +void info(const char *msg, ...); + +/** + * warn - print warning + */ +void warn(const char *err, ...); + +#endif /* __LABWC_LOG_H */ diff --git a/src/action.c b/src/action.c index 4861de9a..ab4a91c9 100644 --- a/src/action.c +++ b/src/action.c @@ -1,5 +1,6 @@ #include "labwc.h" #include "common/spawn.h" +#include "common/log.h" #include @@ -16,7 +17,6 @@ void action(struct server *server, struct keybind *keybind) } else if (!strcasecmp(keybind->action, "debug-views")) { dbg_show_views(server); } else { - fprintf(stderr, "warn: action (%s) not supported\n", - keybind->action); + warn("action (%s) not supported", keybind->action); } } diff --git a/src/common/dir.c b/src/common/dir.c index aa8cadc4..2b8a2dbc 100644 --- a/src/common/dir.c +++ b/src/common/dir.c @@ -11,6 +11,7 @@ #include #include "common/dir.h" +#include "common/log.h" struct dir { const char *prefix; @@ -84,7 +85,7 @@ char *find_dir(struct ctx *ctx) /* handle /etc/xdg... */ ctx->build_path_fn(ctx, NULL, d.path); if (debug) - fprintf(stderr, "DEBUG: %s\n", ctx->buf); + info("%s", ctx->buf); if (isdir(ctx->buf)) return ctx->buf; } else { @@ -96,8 +97,7 @@ char *find_dir(struct ctx *ctx) for (gchar **p = prefixes; *p; p++) { ctx->build_path_fn(ctx, *p, d.path); if (debug) - fprintf(stderr, "DEBUG: %s\n", - ctx->buf); + info("%s", ctx->buf); if (isdir(ctx->buf)) return ctx->buf; } diff --git a/src/common/log.c b/src/common/log.c new file mode 100644 index 00000000..4557d934 --- /dev/null +++ b/src/common/log.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#define LABWC_COLOR_YELLOW "\033[0;33m" +#define LABWC_COLOR_RED "\033[0;31m" +#define LABWC_COLOR_RESET "\033[0m" + +void info(const char *msg, ...) +{ + va_list params; + fprintf(stderr, LABWC_COLOR_YELLOW); + fprintf(stderr, "[labwc] info: "); + va_start(params, msg); + vfprintf(stderr, msg, params); + va_end(params); + fprintf(stderr, LABWC_COLOR_RESET); + fprintf(stderr, "\n"); +} + +void warn(const char *err, ...) +{ + va_list params; + fprintf(stderr, LABWC_COLOR_RED); + fprintf(stderr, "[labwc] warning: "); + va_start(params, err); + vfprintf(stderr, err, params); + va_end(params); + fprintf(stderr, LABWC_COLOR_RESET); + fprintf(stderr, "\n"); +} diff --git a/src/common/meson.build b/src/common/meson.build index 48e1dd56..d2f1af5f 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -3,5 +3,6 @@ labwc_sources += files( 'dir.c', 'font.c', 'grab-file.c', + 'log.c', 'spawn.c', ) diff --git a/src/config/keybind.c b/src/config/keybind.c index bbb47859..cac1047e 100644 --- a/src/config/keybind.c +++ b/src/config/keybind.c @@ -6,6 +6,7 @@ #include "config/keybind.h" #include "config/rcxml.h" +#include "common/log.h" static uint32_t parse_modifier(const char *symname) { @@ -35,8 +36,7 @@ struct keybind *keybind_add(const char *keybind) xkb_keysym_t sym = xkb_keysym_from_name( symname, XKB_KEYSYM_NO_FLAGS); if (sym == XKB_KEY_NoSymbol) { - fprintf(stderr, "unknown keybind (%s)\n", - symname); + warn("unknown keybind (%s)", symname); free(k); k = NULL; break; diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 1e61ec14..d92d6131 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -15,6 +15,7 @@ #include "common/dir.h" #include "common/bug-on.h" #include "common/font.h" +#include "common/log.h" static bool in_keybind = false; static bool is_attribute = false; @@ -205,7 +206,7 @@ void rcxml_parse_xml(struct buf *b) { xmlDoc *d = xmlParseMemory(b->buf, b->len); if (!d) { - fprintf(stderr, "fatal: xmlParseMemory()\n"); + warn("xmlParseMemory()"); exit(EXIT_FAILURE); } xml_tree_walk(xmlDocGetRootElement(d)); @@ -225,7 +226,7 @@ static void bind(const char *binding, const char *action) struct keybind *k = keybind_add(binding); if (k) k->action = strdup(action); - fprintf(stderr, "binding: %s: %s\n", binding, action); + info("binding %s: %s", binding, action); } static void set_title_height(void) @@ -239,7 +240,7 @@ static void set_title_height(void) static void post_processing(void) { if (!wl_list_length(&rc.keybinds)) { - fprintf(stderr, "info: loading default key bindings\n"); + info("loading default key bindings"); bind("A-Escape", "Exit"); bind("A-Tab", "NextWindow"); bind("A-F3", "Execute"); @@ -273,10 +274,10 @@ void rcxml_read(const char *filename) * unit tests. */ rcxml_path(rcxml, sizeof(rcxml), filename); - fprintf(stderr, "info: read config file (%s)\n", rcxml); + info("reading config file (%s)", rcxml); stream = fopen(rcxml, "r"); if (!stream) { - fprintf(stderr, "warn: cannot read '%s'\n", rcxml); + warn("cannot read (%s)", rcxml); goto out; } buf_init(&b); diff --git a/src/keyboard.c b/src/keyboard.c index 94a2d1ea..9510fcc0 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -1,4 +1,5 @@ #include "labwc.h" +#include "common/log.h" static void keyboard_handle_modifiers(struct wl_listener *listener, void *data) { @@ -65,8 +66,7 @@ static void keyboard_handle_key(struct wl_listener *listener, void *data) } else if (event->state == WLR_KEY_PRESSED) { /* cycle to next */ server->cycle_view = next_toplevel(server->cycle_view); - fprintf(stderr, "cycle_view=%p\n", - (void *)server->cycle_view); + info("cycle_view=%p", (void *)server->cycle_view); return; } } diff --git a/src/theme/theme.c b/src/theme/theme.c index c2ce6135..8124cc18 100644 --- a/src/theme/theme.c +++ b/src/theme/theme.c @@ -8,6 +8,7 @@ #include "theme/theme.h" #include "common/dir.h" +#include "common/log.h" static int hex_to_dec(char c) { @@ -96,10 +97,10 @@ void theme_read(const char *theme_name) char themerc[4096]; snprintf(themerc, sizeof(themerc), "%s/themerc", theme_dir(theme_name)); - fprintf(stderr, "info: read themerc (%s)\n", themerc); + info("reading themerc (%s)", themerc); stream = fopen(themerc, "r"); if (!stream) { - fprintf(stderr, "warn: cannot read (%s)\n", themerc); + warn("cannot read (%s)", themerc); return; } while (getline(&line, &len, stream) != -1) { diff --git a/src/xwl.c b/src/xwl.c index 29b938ec..6e81c6cc 100644 --- a/src/xwl.c +++ b/src/xwl.c @@ -1,4 +1,5 @@ #include "labwc.h" +#include "common/log.h" int xwl_nr_parents(struct view *view) { @@ -6,7 +7,7 @@ int xwl_nr_parents(struct view *view) int i = 0; if (!s) { - fprintf(stderr, "warn: (%s) no xwayland surface\n", __func__); + warn("(%s) no xwayland surface\n", __func__); return -1; } while (s->parent) { diff --git a/tests/meson.build b/tests/meson.build index 44756a17..f5584f26 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -6,6 +6,7 @@ rcxml_lib = static_library( '../src/common/dir.c', '../src/common/buf.c', '../src/common/font.c', + '../src/common/log.c', ), dependencies: labwc_deps, include_directories: [labwc_inc],