diff --git a/src/config.c b/src/config.c index 1b619ec..66512e5 100644 --- a/src/config.c +++ b/src/config.c @@ -20,7 +20,7 @@ #include "keyd.h" #include "ini.h" #include "keys.h" -#include "error.h" +#include "log.h" #include "string.h" #include "unicode.h" @@ -28,7 +28,7 @@ #define MAX_LINE_LEN 256 #undef warn -#define warn(fmt, ...) fprintf(stderr, "\t\033[31;1mERROR:\033[0m "fmt"\n", ##__VA_ARGS__) +#define warn(fmt, ...) keyd_log("\tr{ERROR:} "fmt"\n", ##__VA_ARGS__) static struct { const char *name; diff --git a/src/error.c b/src/error.c deleted file mode 100644 index 9b14415..0000000 --- a/src/error.c +++ /dev/null @@ -1,9 +0,0 @@ -/* - * keyd - A key remapping daemon. - * - * © 2019 Raheman Vaiya (see also: LICENSE). - */ -#include "error.h" - -char errstr[2048]; -int debug_level; diff --git a/src/error.h b/src/error.h deleted file mode 100644 index b0dca61..0000000 --- a/src/error.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * keyd - A key remapping daemon. - * - * © 2019 Raheman Vaiya (see also: LICENSE). - */ -#ifndef ERROR_H -#define ERROR_H - -#include - -#define dbg(fmt, ...) { \ - if (debug_level >= 1) \ - fprintf(stderr, "DEBUG: %s:%d: "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__); \ -} - -#define dbg2(fmt, ...) { \ - if (debug_level >= 2) \ - fprintf(stderr, "DEBUG: %s:%d: "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__); \ -} - -#define err(fmt, ...) snprintf(errstr, sizeof(errstr), fmt, ##__VA_ARGS__); - -extern int debug_level; -extern char errstr[2048]; - -#endif diff --git a/src/keyd.c b/src/keyd.c index a99bd37..d08ac48 100644 --- a/src/keyd.c +++ b/src/keyd.c @@ -219,9 +219,14 @@ int main(int argc, char *argv[]) { size_t i; - debug_level = + log_level = atoi(getenv("KEYD_DEBUG") ? getenv("KEYD_DEBUG") : ""); + if (isatty(1)) + suppress_colours = getenv("NO_COLOR") ? 1 : 0; + else + suppress_colours = 1; + dbg("Debug mode activated"); signal(SIGTERM, exit); diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..e0864a5 --- /dev/null +++ b/src/log.c @@ -0,0 +1,80 @@ +/* + * keyd - A key remapping daemon. + * + * © 2019 Raheman Vaiya (see also: LICENSE). + */ +#include "log.h" +#include + +char errstr[2048]; + +int log_level = 0; +int suppress_colours = 0; + +static const char *colorize(const char *s) +{ + int i; + + static char buf[1024]; + size_t n = 0; + int inside_escape = 0; + + for (i = 0; s[i] != 0 && n < sizeof(buf); i++) { + if (s[i+1] == '{') { + int escape_num = 0; + + switch (s[i]) { + case 'r': escape_num = 1; break; + case 'g': escape_num = 2; break; + case 'y': escape_num = 3; break; + case 'b': escape_num = 4; break; + case 'm': escape_num = 5; break; + case 'c': escape_num = 6; break; + case 'w': escape_num = 7; break; + default: break; + } + + if (escape_num) { + if (!suppress_colours && (sizeof(buf)-n > 5)) { + buf[n++] = '\033'; + buf[n++] = '['; + buf[n++] = '3'; + buf[n++] = '0' + escape_num; + buf[n++] = 'm'; + } + + inside_escape = 1; + + i++; + continue; + } + } + + if (s[i] == '}' && inside_escape) { + if (!suppress_colours && (sizeof(buf)-n > 4)) { + memcpy(buf+n, "\033[0m", 4); + n += 4; + } + + inside_escape = 0; + continue; + } + + buf[n++] = s[i]; + } + + buf[n] = 0; + + return buf; +} + +void _keyd_log(int level, const char *fmt, ...) +{ + if (level > log_level) + return; + + va_list ap; + va_start(ap, fmt); + vprintf(colorize(fmt), ap); + va_end(ap); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..4f41e91 --- /dev/null +++ b/src/log.h @@ -0,0 +1,26 @@ +/* + * keyd - A key remapping daemon. + * + * © 2019 Raheman Vaiya (see also: LICENSE). + */ +#ifndef KEYD_LOG_H +#define KEYD_LOG_H + +#include +#include +#include + +#define keyd_log(fmt, ...) _keyd_log(0, fmt, ##__VA_ARGS__); + +#define dbg(fmt, ...) _keyd_log(1, "r{DEBUG:} b{%s:%d:} "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__) +#define dbg2(fmt, ...) _keyd_log(2, "r{DEBUG:} b{%s:%d:} "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__) + +#define err(fmt, ...) snprintf(errstr, sizeof(errstr), fmt, ##__VA_ARGS__); + +void _keyd_log(int level, const char *fmt, ...); + +extern int log_level; +extern int suppress_colours; +extern char errstr[2048]; + +#endif diff --git a/src/monitor.c b/src/monitor.c index d6d9a92..e0e23c3 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -38,14 +38,14 @@ int event_handler(struct event *ev) const char *name; case EV_DEV_ADD: - printf("device added: %04x:%04x %s (%s)\n", - ev->dev->vendor_id, ev->dev->product_id, - ev->dev->name, ev->dev->path); + keyd_log("device added: %04x:%04x %s (%s)\n", + ev->dev->vendor_id, ev->dev->product_id, + ev->dev->name, ev->dev->path); break; case EV_DEV_REMOVE: - printf("device removed: %04x:%04x %s (%s)\n", - ev->dev->vendor_id, ev->dev->product_id, - ev->dev->name, ev->dev->path); + keyd_log("device removed: %04x:%04x %s (%s)\n", + ev->dev->vendor_id, ev->dev->product_id, + ev->dev->name, ev->dev->path); break; case EV_DEV_EVENT: switch (ev->devev->type) { @@ -53,13 +53,13 @@ int event_handler(struct event *ev) name = keycode_table[ev->devev->code].name; if (time_flag) - printf("+%ld ms\t", ev->timestamp - last_time); + keyd_log("r{+%ld} ms\t", ev->timestamp - last_time); - printf("%s\t%04x:%04x\t%s %s\n", - ev->dev->name, - ev->dev->vendor_id, - ev->dev->product_id, name, - ev->devev->pressed ? "down" : "up"); + keyd_log("%s\t%04x:%04x\t%s %s\n", + ev->dev->name, + ev->dev->vendor_id, + ev->dev->product_id, name, + ev->devev->pressed ? "down" : "up"); break; default: