diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/ipc-client.c | 2 | ||||
-rw-r--r-- | common/log.c | 149 | ||||
-rw-r--r-- | common/readline.c | 4 | ||||
-rw-r--r-- | common/util.c | 2 |
4 files changed, 8 insertions, 149 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 1ab6627b..582c5e86 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c | |||
@@ -79,7 +79,7 @@ struct ipc_response *ipc_recv_response(int socketfd) { | |||
79 | error_2: | 79 | error_2: |
80 | free(response); | 80 | free(response); |
81 | error_1: | 81 | error_1: |
82 | sway_log(L_ERROR, "Unable to allocate memory for IPC response"); | 82 | wlr_log(L_ERROR, "Unable to allocate memory for IPC response"); |
83 | return NULL; | 83 | return NULL; |
84 | } | 84 | } |
85 | 85 | ||
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 | |||
14 | static int colored = 1; | ||
15 | static log_importance_t loglevel_default = L_ERROR; | ||
16 | static log_importance_t v = L_SILENT; | ||
17 | |||
18 | static 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 | }; | ||
24 | static const char verbosity_chars[] = { | ||
25 | [L_SILENT] = '\0', | ||
26 | [L_ERROR ] = 'E', | ||
27 | [L_INFO ] = 'I', | ||
28 | [L_DEBUG ] = 'D', | ||
29 | }; | ||
30 | |||
31 | void 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 | |||
40 | void set_log_level(log_importance_t verbosity) { | ||
41 | v = verbosity; | ||
42 | } | ||
43 | |||
44 | log_importance_t get_log_level(void) { | ||
45 | return v; | ||
46 | } | ||
47 | |||
48 | void reset_log_level(void) { | ||
49 | v = loglevel_default; | ||
50 | } | ||
51 | |||
52 | bool toggle_debug_logging(void) { | ||
53 | v = (v == L_DEBUG) ? loglevel_default : L_DEBUG; | ||
54 | return (v == L_DEBUG); | ||
55 | } | ||
56 | |||
57 | void sway_log_colors(int mode) { | ||
58 | colored = (mode == 1) ? 1 : 0; | ||
59 | } | ||
60 | |||
61 | void _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 | |||
112 | void _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 | ||
119 | void sway_terminate(int code); | 6 | void sway_terminate(int code); |
120 | 7 | ||
121 | void _sway_abort(const char *filename, int line, const char* format, ...) { | 8 | void _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 | ||
129 | void sway_log_errno(log_importance_t verbosity, char* format, ...) { | 16 | bool _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 | |||
157 | bool _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 |
diff --git a/common/readline.c b/common/readline.c index cc40a2cc..ed5801de 100644 --- a/common/readline.c +++ b/common/readline.c | |||
@@ -8,7 +8,7 @@ char *read_line(FILE *file) { | |||
8 | char *string = malloc(size); | 8 | char *string = malloc(size); |
9 | char lastChar = '\0'; | 9 | char lastChar = '\0'; |
10 | if (!string) { | 10 | if (!string) { |
11 | sway_log(L_ERROR, "Unable to allocate memory for read_line"); | 11 | wlr_log(L_ERROR, "Unable to allocate memory for read_line"); |
12 | return NULL; | 12 | return NULL; |
13 | } | 13 | } |
14 | while (1) { | 14 | while (1) { |
@@ -29,7 +29,7 @@ char *read_line(FILE *file) { | |||
29 | char *new_string = realloc(string, size *= 2); | 29 | char *new_string = realloc(string, size *= 2); |
30 | if (!new_string) { | 30 | if (!new_string) { |
31 | free(string); | 31 | free(string); |
32 | sway_log(L_ERROR, "Unable to allocate memory for read_line"); | 32 | wlr_log(L_ERROR, "Unable to allocate memory for read_line"); |
33 | return NULL; | 33 | return NULL; |
34 | } | 34 | } |
35 | string = new_string; | 35 | string = new_string; |
diff --git a/common/util.c b/common/util.c index 83981160..fb7f9454 100644 --- a/common/util.c +++ b/common/util.c | |||
@@ -113,7 +113,7 @@ uint32_t parse_color(const char *color) { | |||
113 | 113 | ||
114 | int len = strlen(color); | 114 | int len = strlen(color); |
115 | if (len != 6 && len != 8) { | 115 | if (len != 6 && len != 8) { |
116 | sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); | 116 | wlr_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); |
117 | return 0xFFFFFFFF; | 117 | return 0xFFFFFFFF; |
118 | } | 118 | } |
119 | uint32_t res = (uint32_t)strtoul(color, NULL, 16); | 119 | uint32_t res = (uint32_t)strtoul(color, NULL, 16); |