aboutsummaryrefslogtreecommitdiffstats
path: root/common/log.c
diff options
context:
space:
mode:
authorLibravatar M Stoeckl <code@mstoeckl.com>2019-01-20 13:51:12 -0500
committerLibravatar emersion <contact@emersion.fr>2019-01-21 12:59:42 +0100
commit1211a81aad18bbc4d9e8fb9973238ad8e7e1f688 (patch)
tree5c3f60e0219cb8b4a1b7cafb760a871661866e32 /common/log.c
parentLog libinput_config_status errors (diff)
downloadsway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.tar.gz
sway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.tar.zst
sway-1211a81aad18bbc4d9e8fb9973238ad8e7e1f688.zip
Replace wlr_log with sway_log
This commit mostly duplicates the wlr_log functions, although with a sway_* prefix. (This is very similar to PR #2009.) However, the logging function no longer needs to be replaceable, so sway_log_init's second argument is used to set the exit callback for sway_abort. wlr_log_init is still invoked in sway/main.c This commit makes it easier to remove the wlroots dependency for the helper programs swaymsg, swaybg, swaybar, and swaynag.
Diffstat (limited to 'common/log.c')
-rw-r--r--common/log.c89
1 files changed, 85 insertions, 4 deletions
diff --git a/common/log.c b/common/log.c
index 847f3952..669fd360 100644
--- a/common/log.c
+++ b/common/log.c
@@ -1,16 +1,20 @@
1#define _POSIX_C_SOURCE 199506L
1#include <signal.h> 2#include <signal.h>
2#include <stdarg.h> 3#include <stdarg.h>
4#include <stdio.h>
3#include <stdlib.h> 5#include <stdlib.h>
6#include <time.h>
7#include <unistd.h>
4#include "log.h" 8#include "log.h"
5 9
6void sway_terminate(int code); 10static terminate_callback_t log_terminate = exit;
7 11
8void _sway_abort(const char *format, ...) { 12void _sway_abort(const char *format, ...) {
9 va_list args; 13 va_list args;
10 va_start(args, format); 14 va_start(args, format);
11 _wlr_vlog(WLR_ERROR, format, args); 15 _sway_vlog(SWAY_ERROR, format, args);
12 va_end(args); 16 va_end(args);
13 sway_terminate(EXIT_FAILURE); 17 log_terminate(EXIT_FAILURE);
14} 18}
15 19
16bool _sway_assert(bool condition, const char *format, ...) { 20bool _sway_assert(bool condition, const char *format, ...) {
@@ -20,7 +24,7 @@ bool _sway_assert(bool condition, const char *format, ...) {
20 24
21 va_list args; 25 va_list args;
22 va_start(args, format); 26 va_start(args, format);
23 _wlr_vlog(WLR_ERROR, format, args); 27 _sway_vlog(SWAY_ERROR, format, args);
24 va_end(args); 28 va_end(args);
25 29
26#ifndef NDEBUG 30#ifndef NDEBUG
@@ -29,3 +33,80 @@ bool _sway_assert(bool condition, const char *format, ...) {
29 33
30 return false; 34 return false;
31} 35}
36
37static bool colored = true;
38static sway_log_importance_t log_importance = SWAY_ERROR;
39
40static const char *verbosity_colors[] = {
41 [SWAY_SILENT] = "",
42 [SWAY_ERROR ] = "\x1B[1;31m",
43 [SWAY_INFO ] = "\x1B[1;34m",
44 [SWAY_DEBUG ] = "\x1B[1;30m",
45};
46
47static void sway_log_stderr(sway_log_importance_t verbosity, const char *fmt,
48 va_list args) {
49 if (verbosity > log_importance) {
50 return;
51 }
52 // prefix the time to the log message
53 struct tm result;
54 time_t t = time(NULL);
55 struct tm *tm_info = localtime_r(&t, &result);
56 char buffer[26];
57
58 // generate time prefix
59 strftime(buffer, sizeof(buffer), "%F %T - ", tm_info);
60 fprintf(stderr, "%s", buffer);
61
62 unsigned c = (verbosity < SWAY_LOG_IMPORTANCE_LAST) ? verbosity :
63 SWAY_LOG_IMPORTANCE_LAST - 1;
64
65 if (colored && isatty(STDERR_FILENO)) {
66 fprintf(stderr, "%s", verbosity_colors[c]);
67 }
68
69 vfprintf(stderr, fmt, args);
70
71 if (colored && isatty(STDERR_FILENO)) {
72 fprintf(stderr, "\x1B[0m");
73 }
74 fprintf(stderr, "\n");
75}
76
77void sway_log_init(sway_log_importance_t verbosity, terminate_callback_t callback) {
78 if (verbosity < SWAY_LOG_IMPORTANCE_LAST) {
79 log_importance = verbosity;
80 }
81 if (callback) {
82 log_terminate = callback;
83 }
84}
85
86void _sway_vlog(sway_log_importance_t verbosity, const char *fmt, va_list args) {
87 sway_log_stderr(verbosity, fmt, args);
88}
89
90void _sway_log(sway_log_importance_t verbosity, const char *fmt, ...) {
91 va_list args;
92 va_start(args, fmt);
93 sway_log_stderr(verbosity, fmt, args);
94 va_end(args);
95}
96
97// strips the path prefix from filepath
98// will try to strip SWAY_SRC_DIR as well as a relative src dir
99// e.g. '/src/build/sway/util/log.c' and
100// '../util/log.c' will both be stripped to
101// 'util/log.c'
102const char *_sway_strip_path(const char *filepath) {
103 static int srclen = sizeof(SWAY_SRC_DIR);
104 if (strstr(filepath, SWAY_SRC_DIR) == filepath) {
105 filepath += srclen;
106 } else if (*filepath == '.') {
107 while (*filepath == '.' || *filepath == '/') {
108 ++filepath;
109 }
110 }
111 return filepath;
112}