From 5039ce457e09883e972b67d2262c8bbf40ba0298 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Mon, 8 Dec 2025 06:55:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20logger=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=20printf=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/event.c | 4 ++-- src/text.c | 5 +++-- src/utils.h | 17 +++++++++++++++++ src/wm.c | 12 ++++++------ src/xkb.c | 6 +++--- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/event.c b/src/event.c index 3801103..9c1094d 100644 --- a/src/event.c +++ b/src/event.c @@ -72,7 +72,7 @@ static void key_press(xcb_key_press_event_t *ev) { xcb_keysym_t keysym = xcb_key_press_lookup_keysym(wm.key_symbols, ev, 0); char key_name[16]; if (xkb_keysym_get_name(keysym, key_name, sizeof(key_name)) != -1) { - printf("key %s: %s\n", is_press ? "press" : "release", key_name); + logger("key %s: %s\n", is_press ? "press" : "release", key_name); } if (!is_press) return; @@ -119,7 +119,7 @@ static void map_request(xcb_map_request_event_t *ev) { static void handle_xcb_event(xcb_generic_event_t *event) { uint8_t event_type = XCB_EVENT_RESPONSE_TYPE(event); const char *label = xcb_event_get_label(event_type); - printf("event type: %u[%s], xkb_event: %u\n", event_type, label, + logger("event type: %u[%s], xkb_event: %u\n", event_type, label, wm.event_base_xkb); switch (event_type) { diff --git a/src/text.c b/src/text.c index 19480b7..685f87b 100644 --- a/src/text.c +++ b/src/text.c @@ -16,6 +16,7 @@ #include "base.h" #include "color.h" +#include "utils.h" static PangoContext *context = nullptr; static PangoLayout *layout = nullptr; @@ -29,7 +30,7 @@ static PangoAttrList *attr_list = nullptr; * @return layout 高度,可用于确定 bar 高度 */ int text_init_pango_layout(const char *family, uint32_t size, uint32_t dpi) { - printf("family: %s, size: %u, dpi: %u\n", family, size, dpi); + logger("family: %s, size: %u, dpi: %u\n", family, size, dpi); static const char *layout_family = nullptr; static uint32_t layout_size = 0; @@ -38,7 +39,7 @@ int text_init_pango_layout(const char *family, uint32_t size, uint32_t dpi) { if (layout_family != nullptr && strcmp(layout_family, family) == 0 && layout_size == size && layout_dpi == dpi) { - printf("text pango layout config cached\n"); + logger("text pango layout config cached\n"); return text_height; } diff --git a/src/utils.h b/src/utils.h index 8adc2a6..4f8277a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -70,3 +72,18 @@ void _warn(int line, const char *function, const char *file, const char *format, ...) __attribute__((format(printf, 4, 5))); const char *current_time_str(void); + +#if defined(RELEASE) +#define logger(format, ...) +#elif defined(LOG_FILE) +static inline void logger(const char *format, ...) { + va_list ap; + va_start(ap, format); + FILE *log_file = fopen(LOG_FILE, "a+t"); + vfprintf(log_file, format, ap); + va_end(ap); + fclose(log_file); +} +#else +#define logger(format, ...) printf(format, ##__VA_ARGS__) +#endif diff --git a/src/wm.c b/src/wm.c index fc99184..dfb174e 100644 --- a/src/wm.c +++ b/src/wm.c @@ -281,7 +281,7 @@ static void wm_setup(void) { int font_height = text_init_pango_layout(font_family, font_size, default_dpi); wm.bar_height = (uint16_t)font_height + 2 * bar_y_padding; - printf("font height: %d, bar height: %u\n", font_height, wm.bar_height); + logger("font height: %d, bar height: %u\n", font_height, wm.bar_height); uint32_t tag_count = 0; for (monitor_t *m = wm.monitor_list; m; m = m->next) { @@ -337,17 +337,17 @@ void wm_quit(void) { } static void debug_show_monitor_list(void) { - printf("\n========================== monitors ==========================\n"); + logger("\n========================== monitors ==========================\n"); for (monitor_t *m = wm.monitor_list; m; m = m->next) { - printf("x: %4d, y: %4d, width: %4u, height: %4u -> monitor[%s]\n", + logger("x: %4d, y: %4d, width: %4u, height: %4u -> monitor[%s]\n", m->geometry.x, m->geometry.y, m->geometry.width, m->geometry.height, m->name); - printf("x: %4d, y: %4d, width: %4u, height: %4u -> workarea[%s]: %u\n", + logger("x: %4d, y: %4d, width: %4u, height: %4u -> workarea[%s]: %u\n", m->workarea.x, m->workarea.y, m->workarea.width, m->workarea.height, m->name, wm.bar_height); - printf("tag extend: [%d, %d]\n", m->tag_extent.start, m->tag_extent.end); + logger("tag extend: [%d, %d]\n", m->tag_extent.start, m->tag_extent.end); for (const tag_t *tag = m->tag_list; tag; tag = tag->next) { - printf("tag[%u]: %4u, \"%s\", [%d, %d]\n", tag->index, tag->mask, + logger("tag[%u]: %4u, \"%s\", [%d, %d]\n", tag->index, tag->mask, tag->name, tag->bar_extent.start, tag->bar_extent.end); } } diff --git a/src/xkb.c b/src/xkb.c index beec7da..0f680e6 100644 --- a/src/xkb.c +++ b/src/xkb.c @@ -116,7 +116,7 @@ void xkb_init(void) { wm.xcb_conn, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, nullptr, nullptr, &wm.event_base_xkb, nullptr); - printf("xkb base event: %u\n", wm.event_base_xkb); + logger("xkb base event: %u\n", wm.event_base_xkb); if (!wm.have_xkb) { warn("XKB not found or not supported"); xkb_init_keymap(); @@ -182,7 +182,7 @@ static void xkb_schedule_refresh(void) { void xkb_handle_event(xcb_generic_event_t *event) { assert(wm.have_xkb); - printf("xkb event type: %u\n", event->pad0); + logger("xkb event type: %u\n", event->pad0); /* XKB 中没有对应所有事件的泛型类型,xcb_generic_event_t 类型中的 pad0 * 字段对应 XKB 事件中的 xkbType 字段,故用该字段判断 XKB 事件类型 */ @@ -205,7 +205,7 @@ void xkb_handle_event(xcb_generic_event_t *event) { state_notify_event->baseGroup, state_notify_event->latchedGroup, state_notify_event->lockedGroup); - printf("changed: %u\n", + logger("changed: %u\n", state_notify_event->changed & XCB_XKB_STATE_PART_GROUP_STATE); if (state_notify_event->changed & XCB_XKB_STATE_PART_GROUP_STATE) { xkb_schedule_refresh();