]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add log.c with info() and warn()
authorJohan Malm <jgm323@gmail.com>
Wed, 12 Aug 2020 18:37:44 +0000 (19:37 +0100)
committerJohan Malm <jgm323@gmail.com>
Wed, 12 Aug 2020 18:37:44 +0000 (19:37 +0100)
include/common/log.h [new file with mode: 0644]
src/action.c
src/common/dir.c
src/common/log.c [new file with mode: 0644]
src/common/meson.build
src/config/keybind.c
src/config/rcxml.c
src/keyboard.c
src/theme/theme.c
src/xwl.c
tests/meson.build

diff --git a/include/common/log.h b/include/common/log.h
new file mode 100644 (file)
index 0000000..b2ae2ef
--- /dev/null
@@ -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 */
index 4861de9aa6d881b67e919e92443018248093a2bd..ab4a91c96a977b4aa93b1539f417b5fc291d0855 100644 (file)
@@ -1,5 +1,6 @@
 #include "labwc.h"
 #include "common/spawn.h"
+#include "common/log.h"
 
 #include <strings.h>
 
@@ -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);
        }
 }
index aa8cadc4de8979f6c2e663725cf90fe210d65b24..2b8a2dbcdde2f4659504b41ff8be06e8f277f162 100644 (file)
@@ -11,6 +11,7 @@
 #include <glib.h>
 
 #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 (file)
index 0000000..4557d93
--- /dev/null
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#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");
+}
index 48e1dd56a7320c1f2feb871d6e4b9faf10fbb979..d2f1af5f1eec556eaef8398ef5385323cae714ea 100644 (file)
@@ -3,5 +3,6 @@ labwc_sources += files(
   'dir.c',
   'font.c',
   'grab-file.c',
+  'log.c',
   'spawn.c',
 )
index bbb47859c7b88cc9e0e5e6cd2af2c267cade8a68..cac1047edab0e12c26b23568673f52a9df54d71d 100644 (file)
@@ -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;
index 1e61ec14c68fc927b470d0b7ee084aa82feb172e..d92d61311decfbe05ec249fd979c920f56d8a877 100644 (file)
@@ -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);
index 94a2d1ea7782794d1c4832eef942e4316b134101..9510fcc0ef23beb48cc4b4dca4d48681e49f4b70 100644 (file)
@@ -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;
                }
        }
index c2ce613539cbbd2ef411b917b6501eb6347a8fc6..8124cc1842bfea380fd87a12766243a9ad9f9ec9 100644 (file)
@@ -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) {
index 29b938ecde7eb1ed3e8617d972d8cfa667ff5a2b..6e81c6cc6da254542785764bbcc6325e4b3201e3 100644 (file)
--- 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) {
index 44756a17c7655c5f039ce5da640684588ff751f7..f5584f267075187e0527b267bb183d4f5c480e98 100644 (file)
@@ -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],