使用 logger 替代 printf 函数
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
17
src/utils.h
17
src/utils.h
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -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
|
||||
|
||||
12
src/wm.c
12
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user