diff options
| author | Brian Ashworth <bosrsf04@gmail.com> | 2019-01-14 23:30:54 -0500 |
|---|---|---|
| committer | Brian Ashworth <bosrsf04@gmail.com> | 2019-01-14 23:30:54 -0500 |
| commit | e3935ad1431623b5e3966e95cd75b45e7e00e278 (patch) | |
| tree | 01a33b3422b9d17ad3b63fb4decb972876210f88 /log.c | |
| parent | fcf1bd4ebea97ae6ecd30e4f4e0c526b405b871a (diff) | |
Implement logging and remove wlroots dependency
This implements a simpler version of the wlroots logger for swaylock.
With this logger, the dependency on wlroots can be dropped. This also
adds a debug flag and disables debugging output by default
Diffstat (limited to 'log.c')
| -rw-r--r-- | log.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +#define _POSIX_C_SOURCE 199506L +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include "log.h" + +static enum log_importance log_importance = LOG_ERROR; + +static const char *verbosity_colors[] = { + [LOG_SILENT] = "", + [LOG_ERROR ] = "\x1B[1;31m", + [LOG_INFO ] = "\x1B[1;34m", + [LOG_DEBUG ] = "\x1B[1;30m", +}; + +void swaylock_log_init(enum log_importance verbosity) { + if (verbosity < LOG_IMPORTANCE_LAST) { + log_importance = verbosity; + } +} + +void _swaylock_log(enum log_importance verbosity, const char *fmt, ...) { + if (verbosity > log_importance) { + return; + } + + va_list args; + va_start(args, fmt); + + // prefix the time to the log message + struct tm result; + time_t t = time(NULL); + struct tm *tm_info = localtime_r(&t, &result); + char buffer[26]; + + // generate time prefix + strftime(buffer, sizeof(buffer), "%F %T - ", tm_info); + fprintf(stderr, "%s", buffer); + + unsigned c = (verbosity < LOG_IMPORTANCE_LAST) + ? verbosity : LOG_IMPORTANCE_LAST - 1; + + if (isatty(STDERR_FILENO)) { + fprintf(stderr, "%s", verbosity_colors[c]); + } + + vfprintf(stderr, fmt, args); + + if (isatty(STDERR_FILENO)) { + fprintf(stderr, "\x1B[0m"); + } + fprintf(stderr, "\n"); + + va_end(args); +} + +const char *_swaylock_strip_path(const char *filepath) { + if (*filepath == '.') { + while (*filepath == '.' || *filepath == '/') { + ++filepath; + } + } + return filepath; +} |
