aboutsummaryrefslogtreecommitdiffstats
path: root/common/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/log.c')
-rw-r--r--common/log.c151
1 files changed, 5 insertions, 146 deletions
diff --git a/common/log.c b/common/log.c
index 6dc9d743..2cc7289c 100644
--- a/common/log.c
+++ b/common/log.c
@@ -1,167 +1,26 @@
1#define _POSIX_C_SOURCE 199506L
2#include <errno.h>
3#include <libgen.h>
4#include <signal.h> 1#include <signal.h>
5#include <stdarg.h> 2#include <stdarg.h>
6#include <stdio.h>
7#include <stdlib.h> 3#include <stdlib.h>
8#include <unistd.h>
9#include <string.h>
10#include <time.h>
11#include "log.h" 4#include "log.h"
12#include "sway.h"
13#include "readline.h"
14 5
15static int colored = 1; 6void sway_terminate(int code);
16static log_importance_t loglevel_default = L_ERROR;
17static log_importance_t v = L_SILENT;
18 7
19static const char *verbosity_colors[] = { 8void _sway_abort(const char *format, ...) {
20 [L_SILENT] = "",
21 [L_ERROR ] = "\x1B[1;31m",
22 [L_INFO ] = "\x1B[1;34m",
23 [L_DEBUG ] = "\x1B[1;30m",
24};
25static const char verbosity_chars[] = {
26 [L_SILENT] = '\0',
27 [L_ERROR ] = 'E',
28 [L_INFO ] = 'I',
29 [L_DEBUG ] = 'D',
30};
31
32void init_log(log_importance_t verbosity) {
33 if (verbosity != L_DEBUG) {
34 // command "debuglog" needs to know the user specified log level when
35 // turning off debug logging.
36 loglevel_default = verbosity;
37 }
38 v = verbosity;
39}
40
41void set_log_level(log_importance_t verbosity) {
42 v = verbosity;
43}
44
45log_importance_t get_log_level(void) {
46 return v;
47}
48
49void reset_log_level(void) {
50 v = loglevel_default;
51}
52
53bool toggle_debug_logging(void) {
54 v = (v == L_DEBUG) ? loglevel_default : L_DEBUG;
55 return (v == L_DEBUG);
56}
57
58void sway_log_colors(int mode) {
59 colored = (mode == 1) ? 1 : 0;
60}
61
62void _sway_vlog(const char *filename, int line, log_importance_t verbosity,
63 const char *format, va_list args) {
64 if (verbosity <= v) {
65 // prefix the time to the log message
66 static struct tm result;
67 static time_t t;
68 static struct tm *tm_info;
69 char buffer[26];
70
71 unsigned int c = verbosity;
72 if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) {
73 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
74 }
75
76 // First, if not printing color, show the log level
77 if (!(colored && isatty(STDERR_FILENO)) && c != L_SILENT) {
78 fprintf(stderr, "%c: ", verbosity_chars[c]);
79 }
80
81 // get current time
82 t = time(NULL);
83 // convert time to local time (determined by the locale)
84 tm_info = localtime_r(&t, &result);
85 // generate time prefix
86 strftime(buffer, sizeof(buffer), "%x %X - ", tm_info);
87 fprintf(stderr, "%s", buffer);
88
89 if (colored && isatty(STDERR_FILENO)) {
90 fprintf(stderr, "%s", verbosity_colors[c]);
91 }
92
93 if (filename && line) {
94 const char *file = filename + strlen(filename);
95 while (file != filename && *file != '/') {
96 --file;
97 }
98 if (*file == '/') {
99 ++file;
100 }
101 fprintf(stderr, "[%s:%d] ", file, line);
102 }
103
104 vfprintf(stderr, format, args);
105
106 if (colored && isatty(STDERR_FILENO)) {
107 fprintf(stderr, "\x1B[0m");
108 }
109 fprintf(stderr, "\n");
110 }
111}
112
113void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) {
114 va_list args;
115 va_start(args, format);
116 _sway_vlog(filename, line, verbosity, format, args);
117 va_end(args);
118}
119
120
121void _sway_abort(const char *filename, int line, const char* format, ...) {
122 va_list args; 9 va_list args;
123 va_start(args, format); 10 va_start(args, format);
124 _sway_vlog(filename, line, L_ERROR, format, args); 11 _wlr_vlog(L_ERROR, format, args);
125 va_end(args); 12 va_end(args);
126 sway_terminate(EXIT_FAILURE); 13 sway_terminate(EXIT_FAILURE);
127} 14}
128 15
129void sway_log_errno(log_importance_t verbosity, char* format, ...) { 16bool _sway_assert(bool condition, const char *format, ...) {
130 if (verbosity <= v) {
131 unsigned int c = verbosity;
132 if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) {
133 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
134 }
135
136 if (colored && isatty(STDERR_FILENO)) {
137 fprintf(stderr, "%s", verbosity_colors[c]);
138 } else if (c != L_SILENT) {
139 fprintf(stderr, "%c: ", verbosity_chars[c]);
140 }
141
142 va_list args;
143 va_start(args, format);
144 vfprintf(stderr, format, args);
145 va_end(args);
146
147 fprintf(stderr, ": ");
148 fprintf(stderr, "%s", strerror(errno));
149
150 if (colored && isatty(STDERR_FILENO)) {
151 fprintf(stderr, "\x1B[0m");
152 }
153 fprintf(stderr, "\n");
154 }
155}
156
157bool _sway_assert(bool condition, const char *filename, int line, const char* format, ...) {
158 if (condition) { 17 if (condition) {
159 return true; 18 return true;
160 } 19 }
161 20
162 va_list args; 21 va_list args;
163 va_start(args, format); 22 va_start(args, format);
164 _sway_vlog(filename, line, L_ERROR, format, args); 23 _wlr_vlog(L_ERROR, format, args);
165 va_end(args); 24 va_end(args);
166 25
167#ifndef NDEBUG 26#ifndef NDEBUG