Add log.c with info() and warn()

This commit is contained in:
Johan Malm 2020-08-12 19:37:44 +01:00
parent 5ea1527558
commit 2a17df0f8b
11 changed files with 68 additions and 17 deletions

14
include/common/log.h Normal file
View file

@ -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 */

View 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);
}
}

View 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;
}

32
src/common/log.c Normal file
View file

@ -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");
}

View file

@ -3,5 +3,6 @@ labwc_sources += files(
'dir.c',
'font.c',
'grab-file.c',
'log.c',
'spawn.c',
)

View 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;

View 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);

View 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;
}
}

View 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) {

View file

@ -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) {

View 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],