aboutsummaryrefslogtreecommitdiffstats
path: root/common
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
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')
-rw-r--r--common/background-image.c10
-rw-r--r--common/ipc-client.c2
-rw-r--r--common/log.c89
-rw-r--r--common/loop.c6
-rw-r--r--common/pango.c6
-rw-r--r--common/util.c4
6 files changed, 99 insertions, 18 deletions
diff --git a/common/background-image.c b/common/background-image.c
index 72f39a79..d15c2989 100644
--- a/common/background-image.c
+++ b/common/background-image.c
@@ -1,8 +1,8 @@
1#include <assert.h> 1#include <assert.h>
2#include <stdbool.h> 2#include <stdbool.h>
3#include <wlr/util/log.h>
4#include "background-image.h" 3#include "background-image.h"
5#include "cairo.h" 4#include "cairo.h"
5#include "log.h"
6 6
7enum background_mode parse_background_mode(const char *mode) { 7enum background_mode parse_background_mode(const char *mode) {
8 if (strcmp(mode, "stretch") == 0) { 8 if (strcmp(mode, "stretch") == 0) {
@@ -18,7 +18,7 @@ enum background_mode parse_background_mode(const char *mode) {
18 } else if (strcmp(mode, "solid_color") == 0) { 18 } else if (strcmp(mode, "solid_color") == 0) {
19 return BACKGROUND_MODE_SOLID_COLOR; 19 return BACKGROUND_MODE_SOLID_COLOR;
20 } 20 }
21 wlr_log(WLR_ERROR, "Unsupported background mode: %s", mode); 21 sway_log(SWAY_ERROR, "Unsupported background mode: %s", mode);
22 return BACKGROUND_MODE_INVALID; 22 return BACKGROUND_MODE_INVALID;
23} 23}
24 24
@@ -28,7 +28,7 @@ cairo_surface_t *load_background_image(const char *path) {
28 GError *err = NULL; 28 GError *err = NULL;
29 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &err); 29 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &err);
30 if (!pixbuf) { 30 if (!pixbuf) {
31 wlr_log(WLR_ERROR, "Failed to load background image (%s).", 31 sway_log(SWAY_ERROR, "Failed to load background image (%s).",
32 err->message); 32 err->message);
33 return false; 33 return false;
34 } 34 }
@@ -38,11 +38,11 @@ cairo_surface_t *load_background_image(const char *path) {
38 image = cairo_image_surface_create_from_png(path); 38 image = cairo_image_surface_create_from_png(path);
39#endif // HAVE_GDK_PIXBUF 39#endif // HAVE_GDK_PIXBUF
40 if (!image) { 40 if (!image) {
41 wlr_log(WLR_ERROR, "Failed to read background image."); 41 sway_log(SWAY_ERROR, "Failed to read background image.");
42 return NULL; 42 return NULL;
43 } 43 }
44 if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) { 44 if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) {
45 wlr_log(WLR_ERROR, "Failed to read background image: %s." 45 sway_log(SWAY_ERROR, "Failed to read background image: %s."
46#if !HAVE_GDK_PIXBUF 46#if !HAVE_GDK_PIXBUF
47 "\nSway was compiled without gdk_pixbuf support, so only" 47 "\nSway was compiled without gdk_pixbuf support, so only"
48 "\nPNG images can be loaded. This is the likely cause." 48 "\nPNG images can be loaded. This is the likely cause."
diff --git a/common/ipc-client.c b/common/ipc-client.c
index 79ed3555..13e2dfa3 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -111,7 +111,7 @@ error_2:
111 free(response); 111 free(response);
112 free(payload); 112 free(payload);
113error_1: 113error_1:
114 wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response"); 114 sway_log(SWAY_ERROR, "Unable to allocate memory for IPC response");
115 return NULL; 115 return NULL;
116} 116}
117 117
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}
diff --git a/common/loop.c b/common/loop.c
index 295f3a30..aecad2f3 100644
--- a/common/loop.c
+++ b/common/loop.c
@@ -34,7 +34,7 @@ struct loop {
34struct loop *loop_create(void) { 34struct loop *loop_create(void) {
35 struct loop *loop = calloc(1, sizeof(struct loop)); 35 struct loop *loop = calloc(1, sizeof(struct loop));
36 if (!loop) { 36 if (!loop) {
37 wlr_log(WLR_ERROR, "Unable to allocate memory for loop"); 37 sway_log(SWAY_ERROR, "Unable to allocate memory for loop");
38 return NULL; 38 return NULL;
39 } 39 }
40 loop->fd_capacity = 10; 40 loop->fd_capacity = 10;
@@ -107,7 +107,7 @@ void loop_add_fd(struct loop *loop, int fd, short mask,
107 void (*callback)(int fd, short mask, void *data), void *data) { 107 void (*callback)(int fd, short mask, void *data), void *data) {
108 struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event)); 108 struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
109 if (!event) { 109 if (!event) {
110 wlr_log(WLR_ERROR, "Unable to allocate memory for event"); 110 sway_log(SWAY_ERROR, "Unable to allocate memory for event");
111 return; 111 return;
112 } 112 }
113 event->callback = callback; 113 event->callback = callback;
@@ -129,7 +129,7 @@ struct loop_timer *loop_add_timer(struct loop *loop, int ms,
129 void (*callback)(void *data), void *data) { 129 void (*callback)(void *data), void *data) {
130 struct loop_timer *timer = calloc(1, sizeof(struct loop_timer)); 130 struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
131 if (!timer) { 131 if (!timer) {
132 wlr_log(WLR_ERROR, "Unable to allocate memory for timer"); 132 sway_log(SWAY_ERROR, "Unable to allocate memory for timer");
133 return NULL; 133 return NULL;
134 } 134 }
135 timer->callback = callback; 135 timer->callback = callback;
diff --git a/common/pango.c b/common/pango.c
index 7e164043..fc3d0688 100644
--- a/common/pango.c
+++ b/common/pango.c
@@ -61,7 +61,7 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
61 pango_layout_set_text(layout, buf, -1); 61 pango_layout_set_text(layout, buf, -1);
62 free(buf); 62 free(buf);
63 } else { 63 } else {
64 wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, 64 sway_log(SWAY_ERROR, "pango_parse_markup '%s' -> error %s", text,
65 error->message); 65 error->message);
66 g_error_free(error); 66 g_error_free(error);
67 markup = false; // fallback to plain text 67 markup = false; // fallback to plain text
@@ -92,7 +92,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
92 92
93 char *buf = malloc(length); 93 char *buf = malloc(length);
94 if (buf == NULL) { 94 if (buf == NULL) {
95 wlr_log(WLR_ERROR, "Failed to allocate memory"); 95 sway_log(SWAY_ERROR, "Failed to allocate memory");
96 return; 96 return;
97 } 97 }
98 va_start(args, fmt); 98 va_start(args, fmt);
@@ -119,7 +119,7 @@ void pango_printf(cairo_t *cairo, const char *font,
119 119
120 char *buf = malloc(length); 120 char *buf = malloc(length);
121 if (buf == NULL) { 121 if (buf == NULL) {
122 wlr_log(WLR_ERROR, "Failed to allocate memory"); 122 sway_log(SWAY_ERROR, "Failed to allocate memory");
123 return; 123 return;
124 } 124 }
125 va_start(args, fmt); 125 va_start(args, fmt);
diff --git a/common/util.c b/common/util.c
index d66058a6..27039dcb 100644
--- a/common/util.c
+++ b/common/util.c
@@ -116,7 +116,7 @@ uint32_t parse_color(const char *color) {
116 116
117 int len = strlen(color); 117 int len = strlen(color);
118 if (len != 6 && len != 8) { 118 if (len != 6 && len != 8) {
119 wlr_log(WLR_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); 119 sway_log(SWAY_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
120 return 0xFFFFFFFF; 120 return 0xFFFFFFFF;
121 } 121 }
122 uint32_t res = (uint32_t)strtoul(color, NULL, 16); 122 uint32_t res = (uint32_t)strtoul(color, NULL, 16);
@@ -147,7 +147,7 @@ float parse_float(const char *value) {
147 char *end; 147 char *end;
148 float flt = strtof(value, &end); 148 float flt = strtof(value, &end);
149 if (*end || errno) { 149 if (*end || errno) {
150 wlr_log(WLR_DEBUG, "Invalid float value '%s', defaulting to NAN", value); 150 sway_log(SWAY_DEBUG, "Invalid float value '%s', defaulting to NAN", value);
151 return NAN; 151 return NAN;
152 } 152 }
153 return flt; 153 return flt;