1 #define _XOPEN_SOURCE 700 // for snprintf
2 #include <errno.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <wayland-server-core.h>
10 #include <wlr/util/log.h>
11 #include "util/time.h"
12 
13 static bool colored = true;
14 static enum wlr_log_importance log_importance = WLR_ERROR;
15 static struct timespec start_time = {-1};
16 
17 static const char *verbosity_colors[] = {
18 	[WLR_SILENT] = "",
19 	[WLR_ERROR] = "\x1B[1;31m",
20 	[WLR_INFO] = "\x1B[1;34m",
21 	[WLR_DEBUG] = "\x1B[1;90m",
22 };
23 
24 static const char *verbosity_headers[] = {
25 	[WLR_SILENT] = "",
26 	[WLR_ERROR] = "[ERROR]",
27 	[WLR_INFO] = "[INFO]",
28 	[WLR_DEBUG] = "[DEBUG]",
29 };
30 
init_start_time(void)31 static void init_start_time(void) {
32 	if (start_time.tv_sec >= 0) {
33 		return;
34 	}
35 	clock_gettime(CLOCK_MONOTONIC, &start_time);
36 }
37 
log_stderr(enum wlr_log_importance verbosity,const char * fmt,va_list args)38 static void log_stderr(enum wlr_log_importance verbosity, const char *fmt,
39 		va_list args) {
40 	init_start_time();
41 
42 	if (verbosity > log_importance) {
43 		return;
44 	}
45 
46 	struct timespec ts = {0};
47 	clock_gettime(CLOCK_MONOTONIC, &ts);
48 	timespec_sub(&ts, &ts, &start_time);
49 
50 	fprintf(stderr, "%02d:%02d:%02d.%03ld ", (int)(ts.tv_sec / 60 / 60),
51 		(int)(ts.tv_sec / 60 % 60), (int)(ts.tv_sec % 60),
52 		ts.tv_nsec / 1000000);
53 
54 	unsigned c = (verbosity < WLR_LOG_IMPORTANCE_LAST) ? verbosity : WLR_LOG_IMPORTANCE_LAST - 1;
55 
56 	if (colored && isatty(STDERR_FILENO)) {
57 		fprintf(stderr, "%s", verbosity_colors[c]);
58 	} else {
59 		fprintf(stderr, "%s ", verbosity_headers[c]);
60 	}
61 
62 	vfprintf(stderr, fmt, args);
63 
64 	if (colored && isatty(STDERR_FILENO)) {
65 		fprintf(stderr, "\x1B[0m");
66 	}
67 	fprintf(stderr, "\n");
68 }
69 
70 static wlr_log_func_t log_callback = log_stderr;
71 
log_wl(const char * fmt,va_list args)72 static void log_wl(const char *fmt, va_list args) {
73 	static char wlr_fmt[1024];
74 	int n = snprintf(wlr_fmt, sizeof(wlr_fmt), "[wayland] %s", fmt);
75 	if (n > 0 && wlr_fmt[n - 1] == '\n') {
76 		wlr_fmt[n - 1] = '\0';
77 	}
78 	_wlr_vlog(WLR_INFO, wlr_fmt, args);
79 }
80 
wlr_log_init(enum wlr_log_importance verbosity,wlr_log_func_t callback)81 void wlr_log_init(enum wlr_log_importance verbosity, wlr_log_func_t callback) {
82 	init_start_time();
83 
84 	if (verbosity < WLR_LOG_IMPORTANCE_LAST) {
85 		log_importance = verbosity;
86 	}
87 	if (callback) {
88 		log_callback = callback;
89 	}
90 
91 	wl_log_set_handler_server(log_wl);
92 }
93 
_wlr_vlog(enum wlr_log_importance verbosity,const char * fmt,va_list args)94 void _wlr_vlog(enum wlr_log_importance verbosity, const char *fmt, va_list args) {
95 	log_callback(verbosity, fmt, args);
96 }
97 
_wlr_log(enum wlr_log_importance verbosity,const char * fmt,...)98 void _wlr_log(enum wlr_log_importance verbosity, const char *fmt, ...) {
99 	va_list args;
100 	va_start(args, fmt);
101 	log_callback(verbosity, fmt, args);
102 	va_end(args);
103 }
104 
wlr_log_get_verbosity(void)105 enum wlr_log_importance wlr_log_get_verbosity(void) {
106 	return log_importance;
107 }
108