debug: Colorize output for easier reading

Signed-off-by: Caitlyn <caitlynrosestewart@gmail.com>
This commit is contained in:
Caitlyn 2025-05-27 22:40:14 +01:00 committed by Simon Ser
parent 827d0c30ad
commit ecff0ee10c
4 changed files with 69 additions and 18 deletions

View file

@ -1494,7 +1494,7 @@ wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection)
void
wl_closure_print(struct wl_closure *closure, struct wl_object *target,
int send, int discarded, uint32_t (*n_parse)(union wl_argument *arg),
const char *queue_name)
const char *queue_name, int color)
{
int i;
struct argument_details arg;
@ -1512,17 +1512,28 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
clock_gettime(CLOCK_REALTIME, &tp);
time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
fprintf(f, "%s[%7u.%03u] ",
color ? WL_DEBUG_COLOR_GREEN : "",
time / 1000, time % 1000);
fprintf(f, "[%7u.%03u] ", time / 1000, time % 1000);
if (queue_name) {
fprintf(f, "%s{%s} ",
color ? WL_DEBUG_COLOR_YELLOW : "",
queue_name);
}
if (queue_name)
fprintf(f, "{%s} ", queue_name);
fprintf(f, "%s%s%s#%u.%s(",
fprintf(f, "%s%s%s%s%s%s%s#%u%s.%s%s(",
color ? WL_DEBUG_COLOR_RED : "",
discarded ? "discarded " : "",
color ? WL_DEBUG_COLOR_RESET : "",
send ? " -> " : "",
target->interface->name, target->id,
closure->message->name);
color ? WL_DEBUG_COLOR_BLUE : "",
target->interface->name,
color ? WL_DEBUG_COLOR_MAGENTA : "",
target->id,
color ? WL_DEBUG_COLOR_CYAN : "",
closure->message->name,
color ? WL_DEBUG_COLOR_RESET : "");
for (i = 0; i < closure->count; i++) {
signature = get_next_argument(signature, &arg);
@ -1587,7 +1598,7 @@ wl_closure_print(struct wl_closure *closure, struct wl_object *target,
}
}
fprintf(f, ")\n");
fprintf(f, ")%s\n", color ? WL_DEBUG_COLOR_RESET : "");
if (fclose(f) == 0) {
fprintf(stderr, "%s", buffer);

View file

@ -115,6 +115,7 @@ struct wl_display {
/** \endcond */
static int debug_client = 0;
static int debug_color = 0;
/**
* This helper function wakes up all threads that are
@ -936,7 +937,7 @@ wl_proxy_marshal_array_flags(struct wl_proxy *proxy, uint32_t opcode,
queue_name = wl_event_queue_get_name(queue);
wl_closure_print(closure, &proxy->object, true, false, NULL,
queue_name);
queue_name, debug_color);
}
if (wl_closure_send(closure, proxy->display->connection)) {
@ -1229,10 +1230,23 @@ wl_display_connect_to_fd(int fd)
{
struct wl_display *display;
const char *debug;
const char *no_color;
const char *force_color;
no_color = getenv("NO_COLOR");
force_color = getenv("FORCE_COLOR");
debug = getenv("WAYLAND_DEBUG");
if (debug && (strstr(debug, "client") || strstr(debug, "1")))
if (debug && (strstr(debug, "client") || strstr(debug, "1"))) {
debug_client = 1;
if (isatty(fileno(stderr)))
debug_color = 1;
}
if (force_color && force_color[0] != '\0')
debug_color = 1;
if (no_color && no_color[0] != '\0')
debug_color = 0;
display = zalloc(sizeof *display);
if (display == NULL) {
@ -1578,12 +1592,16 @@ queue_event(struct wl_display *display, int len)
if (debug_client) {
clock_gettime(CLOCK_REALTIME, &tp);
time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
fprintf(stderr, "[%7u.%03u] discarded [%s]#%d.[event %d]"
fprintf(stderr, "%s[%7u.%03u] %sdiscarded %s[%s]%s#%d%s.[event %d]%s"
"(%d fd, %d byte)\n",
debug_color ? WL_DEBUG_COLOR_GREEN : "",
time / 1000, time % 1000,
debug_color ? WL_DEBUG_COLOR_RED : "",
debug_color ? WL_DEBUG_COLOR_BLUE : "",
zombie ? "zombie" : "unknown",
id, opcode,
debug_color ? WL_DEBUG_COLOR_MAGENTA : "", id,
debug_color ? WL_DEBUG_COLOR_BLUE : "", opcode,
debug_color ? WL_DEBUG_COLOR_RESET : "",
num_zombie_fds, size);
}
if (num_zombie_fds > 0)
@ -1668,7 +1686,7 @@ dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
!(proxy->dispatcher || proxy->object.implementation);
wl_closure_print(closure, &proxy->object, false, discarded,
id_from_object, queue->name);
id_from_object, queue->name, debug_color);
}
if (proxy_destroyed) {

View file

@ -50,6 +50,14 @@
#define WL_BUFFER_DEFAULT_SIZE_POT 12
#define WL_BUFFER_DEFAULT_MAX_SIZE (1 << WL_BUFFER_DEFAULT_SIZE_POT)
#define WL_DEBUG_COLOR_RESET "\e[0m"
#define WL_DEBUG_COLOR_RED "\e[31m"
#define WL_DEBUG_COLOR_GREEN "\e[32m"
#define WL_DEBUG_COLOR_YELLOW "\e[33m"
#define WL_DEBUG_COLOR_BLUE "\e[34m"
#define WL_DEBUG_COLOR_MAGENTA "\e[35m"
#define WL_DEBUG_COLOR_CYAN "\e[36m"
/**
* Argument types used in signatures.
*/
@ -230,7 +238,7 @@ void
wl_closure_print(struct wl_closure *closure,
struct wl_object *target, int send, int discarded,
uint32_t (*n_parse)(union wl_argument *arg),
const char *queue_name);
const char *queue_name, int color);
void
wl_closure_destroy(struct wl_closure *closure);

View file

@ -149,6 +149,7 @@ struct wl_protocol_logger {
};
static int debug_server = 0;
static int debug_color = 0;
static void
log_closure(struct wl_resource *resource,
@ -160,7 +161,7 @@ log_closure(struct wl_resource *resource,
struct wl_protocol_logger_message message;
if (debug_server)
wl_closure_print(closure, object, send, false, NULL, NULL);
wl_closure_print(closure, object, send, false, NULL, NULL, debug_color);
if (!wl_list_empty(&display->protocol_loggers)) {
message.resource = resource;
@ -1168,10 +1169,23 @@ wl_display_create(void)
{
struct wl_display *display;
const char *debug;
const char *no_color;
const char *force_color;
no_color = getenv("NO_COLOR");
force_color = getenv("FORCE_COLOR");
debug = getenv("WAYLAND_DEBUG");
if (debug && (strstr(debug, "server") || strstr(debug, "1")))
if (debug && (strstr(debug, "server") || strstr(debug, "1"))) {
debug_server = 1;
if (isatty(fileno(stderr)))
debug_color = 1;
}
if (force_color && force_color[0] != '\0')
debug_color = 1;
if (no_color && no_color[0] != '\0')
debug_color = 0;
display = zalloc(sizeof *display);
if (display == NULL)