aboutsummaryrefslogtreecommitdiffstats
path: root/common/log.c
diff options
context:
space:
mode:
authorLibravatar Dominique Martinet <asmadeus@codewreck.org>2018-01-05 23:36:32 +0100
committerLibravatar Dominique Martinet <asmadeus@codewreck.org>2018-01-05 23:40:09 +0100
commitd4ddfec32e15fd1f47568f1b82eacc27cda5266a (patch)
tree79386d3d2aa90718f8c071a3337c695cb8a71f78 /common/log.c
parentswaymsg: convert to wlr_log (diff)
downloadsway-d4ddfec32e15fd1f47568f1b82eacc27cda5266a.tar.gz
sway-d4ddfec32e15fd1f47568f1b82eacc27cda5266a.tar.zst
sway-d4ddfec32e15fd1f47568f1b82eacc27cda5266a.zip
common/log: finish removing most log functions
Keep sway_abort and sway_assert and convert them to use wlr_log functions
Diffstat (limited to 'common/log.c')
-rw-r--r--common/log.c149
1 files changed, 4 insertions, 145 deletions
diff --git a/common/log.c b/common/log.c
index c47b4eea..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 "readline.h"
13
14static int colored = 1;
15static log_importance_t loglevel_default = L_ERROR;
16static log_importance_t v = L_SILENT;
17
18static const char *verbosity_colors[] = {
19 [L_SILENT] = "",
20 [L_ERROR ] = "\x1B[1;31m",
21 [L_INFO ] = "\x1B[1;34m",
22 [L_DEBUG ] = "\x1B[1;30m",
23};
24static const char verbosity_chars[] = {
25 [L_SILENT] = '\0',
26 [L_ERROR ] = 'E',
27 [L_INFO ] = 'I',
28 [L_DEBUG ] = 'D',
29};
30
31void init_log(log_importance_t verbosity) {
32 if (verbosity != L_DEBUG) {
33 // command "debuglog" needs to know the user specified log level when
34 // turning off debug logging.
35 loglevel_default = verbosity;
36 }
37 v = verbosity;
38}
39
40void set_log_level(log_importance_t verbosity) {
41 v = verbosity;
42}
43
44log_importance_t get_log_level(void) {
45 return v;
46}
47
48void reset_log_level(void) {
49 v = loglevel_default;
50}
51
52bool toggle_debug_logging(void) {
53 v = (v == L_DEBUG) ? loglevel_default : L_DEBUG;
54 return (v == L_DEBUG);
55}
56
57void sway_log_colors(int mode) {
58 colored = (mode == 1) ? 1 : 0;
59}
60
61void _sway_vlog(const char *filename, int line, log_importance_t verbosity,
62 const char *format, va_list args) {
63 if (verbosity <= v) {
64 // prefix the time to the log message
65 static struct tm result;
66 static time_t t;
67 static struct tm *tm_info;
68 char buffer[26];
69
70 unsigned int c = verbosity;
71 if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) {
72 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
73 }
74
75 // First, if not printing color, show the log level
76 if (!(colored && isatty(STDERR_FILENO)) && c != L_SILENT) {
77 fprintf(stderr, "%c: ", verbosity_chars[c]);
78 }
79
80 // get current time
81 t = time(NULL);
82 // convert time to local time (determined by the locale)
83 tm_info = localtime_r(&t, &result);
84 // generate time prefix
85 strftime(buffer, sizeof(buffer), "%x %X - ", tm_info);
86 fprintf(stderr, "%s", buffer);
87
88 if (colored && isatty(STDERR_FILENO)) {
89 fprintf(stderr, "%s", verbosity_colors[c]);
90 }
91
92 if (filename && line) {
93 const char *file = filename + strlen(filename);
94 while (file != filename && *file != '/') {
95 --file;
96 }
97 if (*file == '/') {
98 ++file;
99 }
100 fprintf(stderr, "[%s:%d] ", file, line);
101 }
102
103 vfprintf(stderr, format, args);
104
105 if (colored && isatty(STDERR_FILENO)) {
106 fprintf(stderr, "\x1B[0m");
107 }
108 fprintf(stderr, "\n");
109 }
110}
111
112void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) {
113 va_list args;
114 va_start(args, format);
115 _sway_vlog(filename, line, verbosity, format, args);
116 va_end(args);
117}
118 5
119void sway_terminate(int code); 6void sway_terminate(int code);
120 7
121void _sway_abort(const char *filename, int line, const char* format, ...) { 8void _sway_abort(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