aboutsummaryrefslogtreecommitdiffstats
path: root/common/log.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-11-12 19:04:01 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-11-12 19:04:01 -0500
commitbfcabe48ef3fc7a0388de007504fc232f826fb84 (patch)
tree8bef61a10259765dbafed49c9a2a76b4bf9ced2d /common/log.c
parentMerge branch 'master' of github.com:SirCmpwn/sway (diff)
downloadsway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.gz
sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.zst
sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.zip
Start fleshing out wayland client implementation
This introduces a basic shared framework for making wayland clients within sway itself.
Diffstat (limited to 'common/log.c')
-rw-r--r--common/log.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/common/log.c b/common/log.c
new file mode 100644
index 00000000..02aac4c1
--- /dev/null
+++ b/common/log.c
@@ -0,0 +1,159 @@
1#include "log.h"
2#include "sway.h"
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <libgen.h>
7#include <fcntl.h>
8#include <unistd.h>
9#include <signal.h>
10#include <errno.h>
11#include <string.h>
12#include <stringop.h>
13#include <execinfo.h>
14
15int colored = 1;
16log_importance_t loglevel_default = L_ERROR;
17log_importance_t v = L_SILENT;
18
19static const char *verbosity_colors[] = {
20 [L_SILENT] = "",
21 [L_ERROR ] = "\x1B[1;31m",
22 [L_INFO ] = "\x1B[1;34m",
23 [L_DEBUG ] = "\x1B[1;30m",
24};
25
26void init_log(log_importance_t verbosity) {
27 if (verbosity != L_DEBUG) {
28 // command "debuglog" needs to know the user specified log level when
29 // turning off debug logging.
30 loglevel_default = verbosity;
31 }
32 v = verbosity;
33 signal(SIGSEGV, error_handler);
34 signal(SIGABRT, error_handler);
35}
36
37void set_log_level(log_importance_t verbosity) {
38 v = verbosity;
39}
40
41void reset_log_level(void) {
42 v = loglevel_default;
43}
44
45bool toggle_debug_logging(void) {
46 v = (v == L_DEBUG) ? loglevel_default : L_DEBUG;
47 return (v == L_DEBUG);
48}
49
50void sway_log_colors(int mode) {
51 colored = (mode == 1) ? 1 : 0;
52}
53
54void sway_abort(const char *format, ...) {
55 fprintf(stderr, "ERROR: ");
56 va_list args;
57 va_start(args, format);
58 vfprintf(stderr, format, args);
59 va_end(args);
60 fprintf(stderr, "\n");
61 sway_terminate();
62}
63
64#ifndef NDEBUG
65void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) {
66#else
67void _sway_log(log_importance_t verbosity, const char* format, ...) {
68#endif
69 if (verbosity <= v) {
70 unsigned int c = verbosity;
71 if (c > sizeof(verbosity_colors) / sizeof(char *)) {
72 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
73 }
74
75 if (colored && isatty(STDERR_FILENO)) {
76 fprintf(stderr, "%s", verbosity_colors[c]);
77 }
78
79 va_list args;
80 va_start(args, format);
81#ifndef NDEBUG
82 char *file = strdup(filename);
83 fprintf(stderr, "[%s:%d] ", basename(file), line);
84 free(file);
85#endif
86 vfprintf(stderr, format, args);
87 va_end(args);
88
89 if (colored && isatty(STDERR_FILENO)) {
90 fprintf(stderr, "\x1B[0m");
91 }
92 fprintf(stderr, "\n");
93 }
94}
95
96void sway_log_errno(log_importance_t verbosity, char* format, ...) {
97 if (verbosity <= v) {
98 unsigned int c = verbosity;
99 if (c > sizeof(verbosity_colors) / sizeof(char *)) {
100 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
101 }
102
103 if (colored && isatty(STDERR_FILENO)) {
104 fprintf(stderr, "%s", verbosity_colors[c]);
105 }
106
107 va_list args;
108 va_start(args, format);
109 vfprintf(stderr, format, args);
110 va_end(args);
111
112 fprintf(stderr, ": ");
113 fprintf(stderr, "%s", strerror(errno));
114
115 if (colored && isatty(STDERR_FILENO)) {
116 fprintf(stderr, "\x1B[0m");
117 }
118 fprintf(stderr, "\n");
119 }
120}
121
122bool _sway_assert(bool condition, const char* format, ...) {
123 if (condition) {
124 return true;
125 }
126
127 va_list args;
128 va_start(args, format);
129 sway_log(L_ERROR, format, args);
130 va_end(args);
131
132#ifndef NDEBUG
133 raise(SIGABRT);
134#endif
135
136 return false;
137}
138
139void error_handler(int sig) {
140 int i;
141 int max_lines = 20;
142 void *array[max_lines];
143 char **bt;
144 size_t bt_len;
145
146 sway_log(L_ERROR, "Error: Signal %d. Printing backtrace", sig);
147 bt_len = backtrace(array, max_lines);
148 bt = backtrace_symbols(array, bt_len);
149 if (!bt) {
150 sway_log(L_ERROR, "Could not allocate sufficient memory for backtrace_symbols(), falling back to stderr");
151 backtrace_symbols_fd(array, bt_len, STDERR_FILENO);
152 exit(1);
153 }
154
155 for (i = 0; (size_t)i < bt_len; i++) {
156 sway_log(L_ERROR, "Backtrace: %s", bt[i]);
157 }
158 exit(1);
159}