Use DLOG/ELOG-macros, provide --verbose-option

next
Axel Wagner 2010-09-17 06:49:28 +02:00
parent ca1a295714
commit 920721bb93
8 changed files with 83 additions and 66 deletions

View File

@ -3,6 +3,7 @@
typedef struct config_t {
int hide_on_modifier;
int verbose;
} config_t;
config_t config;

View File

@ -44,3 +44,14 @@
walk = TAILQ_FIRST(l); \
} \
} while (0)
/* Use cool logging-macros */
#define DLOG(fmt, ...) do { \
if (config.verbose) { \
printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
} \
} while(0)
#define ELOG(fmt, ...) do { \
fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
} while(0)

View File

@ -59,7 +59,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
buffer[rec-1] = '\0';
break;
}
printf("ERROR: read() failed!");
ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
@ -80,7 +80,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
}
FREE(statusline);
statusline = buffer;
printf("%s\n", buffer);
DLOG("%s\n", buffer);
draw_bars();
}
@ -91,7 +91,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
*
*/
void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
printf("Child (pid: %d) unexpectedly exited with status %d\n",
DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
child_pid,
watcher->rstatus);
cleanup();
@ -111,7 +111,7 @@ void start_child(char *command) {
child_pid = fork();
switch (child_pid) {
case -1:
printf("ERROR: Couldn't fork()");
ELOG("Couldn't fork()\n");
exit(EXIT_FAILURE);
case 0:
/* Child-process. Reroute stdout and start shell */

View File

@ -30,7 +30,7 @@ typedef void(*handler_t)(char*);
int get_ipc_fd(const char *socket_path) {
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("ERROR: Could not create Socket!\n");
ELOG("Could not create Socket!\n");
exit(EXIT_FAILURE);
}
@ -39,7 +39,7 @@ int get_ipc_fd(const char *socket_path) {
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path, socket_path);
if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
printf("ERROR: Could not connct to i3\n");
ELOG("Could not connct to i3!\n");
exit(EXIT_FAILURE);
}
return sockfd;
@ -59,7 +59,7 @@ void got_command_reply(char *reply) {
*
*/
void got_workspace_reply(char *reply) {
printf("Got Workspace-Data!\n");
DLOG("Got Workspace-Data!\n");
parse_workspaces_json(reply);
draw_bars();
}
@ -70,7 +70,7 @@ void got_workspace_reply(char *reply) {
*
*/
void got_subscribe_reply(char *reply) {
printf("Got Subscribe Reply: %s\n", reply);
DLOG("Got Subscribe Reply: %s\n", reply);
/* TODO: Error handling for subscribe-commands */
}
@ -79,9 +79,9 @@ void got_subscribe_reply(char *reply) {
*
*/
void got_output_reply(char *reply) {
printf("Parsing Outputs-JSON...\n");
DLOG("Parsing Outputs-JSON...\n");
parse_outputs_json(reply);
printf("Reconfiguring Windows...\n");
DLOG("Reconfiguring Windows...\n");
reconfig_windows();
}
@ -98,7 +98,7 @@ handler_t reply_handlers[] = {
*
*/
void got_workspace_event(char *event) {
printf("Got Workspace Event!\n");
DLOG("Got Workspace Event!\n");
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
}
@ -107,7 +107,7 @@ void got_workspace_event(char *event) {
*
*/
void got_output_event(char *event) {
printf("Got Output Event!\n");
DLOG("Got Output Event!\n");
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
}
@ -123,14 +123,14 @@ handler_t event_handlers[] = {
*
*/
void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
printf("Got data!\n");
DLOG("Got data!\n");
int fd = watcher->fd;
/* First we only read the header, because we know it's length */
uint32_t header_len = strlen(I3_IPC_MAGIC) + sizeof(uint32_t)*2;
char *header = malloc(header_len);
if (header == NULL) {
printf("ERROR: Could not allocate memory!\n");
ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
@ -140,21 +140,21 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
while (rec < header_len) {
int n = read(fd, header + rec, header_len - rec);
if (n == -1) {
printf("ERROR: read() failed!\n");
ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
printf("ERROR: Nothing to read!\n");
ELOG("Nothing to read!\n");
exit(EXIT_FAILURE);
}
rec += n;
}
if (strncmp(header, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC))) {
printf("ERROR: Wrong magic code: %.*s\n Expected: %s\n",
(int) strlen(I3_IPC_MAGIC),
header,
I3_IPC_MAGIC);
ELOG("Wrong magic code: %.*s\n Expected: %s\n",
(int) strlen(I3_IPC_MAGIC),
header,
I3_IPC_MAGIC);
exit(EXIT_FAILURE);
}
@ -167,7 +167,7 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
* of the message */
char *buffer = malloc(size + 1);
if (buffer == NULL) {
printf("ERROR: Could not allocate memory!\n");
ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
rec = 0;
@ -175,11 +175,11 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
while (rec < size) {
int n = read(fd, buffer + rec, size - rec);
if (n == -1) {
printf("ERROR: read() failed!\n");
ELOG("read() failed!\n");
exit(EXIT_FAILURE);
}
if (n == 0) {
printf("ERROR: Nothing to read!\n");
ELOG("Nothing to read!\n");
exit(EXIT_FAILURE);
}
rec += n;
@ -216,7 +216,7 @@ int i3_send_msg(uint32_t type, const char *payload) {
* but we leave it for now */
char *buffer = malloc(to_write);
if (buffer == NULL) {
printf("ERROR: Could not allocate memory\n");
ELOG("Could not allocate memory\n");
exit(EXIT_FAILURE);
}
@ -236,7 +236,7 @@ int i3_send_msg(uint32_t type, const char *payload) {
while (to_write > 0) {
int n = write(i3_connection->fd, buffer + written, to_write);
if (n == -1) {
printf("ERROR: write() failed!\n");
ELOG("write() failed!\n");
exit(EXIT_FAILURE);
}

View File

@ -25,12 +25,12 @@
char *expand_path(char *path) {
static glob_t globbuf;
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
printf("glob() failed");
ELOG("glob() failed\n");
exit(EXIT_FAILURE);
}
char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
if (result == NULL) {
printf("malloc() failed");
ELOG("malloc() failed\n");
exit(EXIT_FAILURE);
}
globfree(&globbuf);
@ -38,13 +38,14 @@ char *expand_path(char *path) {
}
void print_usage(char *elf_name) {
printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-h]\n", elf_name);
printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-V] [-h]\n", elf_name);
printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
printf("-c <command>\tExecute <command> to get stdin\n");
printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
printf("\t\tand a SIGCONT on unhiding of the bars\n");
printf("-f <font>\tUse X-Core-Font <font> for display\n");
printf("-V\t\tBe (very) verbose with the debug-output\n");
printf("-h\t\tDisplay this help-message and exit\n");
}
@ -66,10 +67,11 @@ int main(int argc, char **argv) {
{ "font", required_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "verbose", no_argument, 0, 'V' },
{ NULL, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "s:c:mf:hv", long_opt, &option_index)) != -1) {
while ((opt = getopt_long(argc, argv, "s:c:mf:hvV", long_opt, &option_index)) != -1) {
switch (opt) {
case 's':
socket_path = expand_path(optarg);
@ -87,6 +89,9 @@ int main(int argc, char **argv) {
printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
exit(EXIT_SUCCESS);
break;
case 'V':
config.verbose = 1;
break;
default:
print_usage(argv[0]);
exit(EXIT_SUCCESS);
@ -102,7 +107,7 @@ int main(int argc, char **argv) {
}
if (socket_path == NULL) {
printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
socket_path = expand_path(i3_default_sock_path);
}

View File

@ -230,7 +230,7 @@ void parse_outputs_json(char *json) {
case yajl_status_client_canceled:
case yajl_status_insufficient_data:
case yajl_status_error:
printf("ERROR: Could not parse outputs-reply!\n");
ELOG("Could not parse outputs-reply!\n");
exit(EXIT_FAILURE);
break;
}

View File

@ -119,10 +119,10 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne
predict_text_extents(params->workspaces_walk->ucs2_name,
params->workspaces_walk->name_glyphs);
printf("Got Workspace %s, name_width: %d, glyphs: %d\n",
params->workspaces_walk->name,
params->workspaces_walk->name_width,
params->workspaces_walk->name_glyphs);
DLOG("Got Workspace %s, name_width: %d, glyphs: %d\n",
params->workspaces_walk->name,
params->workspaces_walk->name_width,
params->workspaces_walk->name_glyphs);
FREE(params->cur_key);
return 1;
@ -184,7 +184,7 @@ static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, uns
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
if (params->cur_key == NULL) {
printf("ERROR: Could not allocate memory!\n");
ELOG("Could not allocate memory!\n");
exit(EXIT_FAILURE);
}
strncpy(params->cur_key, (const char*) keyVal, keyLen);
@ -238,7 +238,7 @@ void parse_workspaces_json(char *json) {
case yajl_status_client_canceled:
case yajl_status_insufficient_data:
case yajl_status_error:
printf("ERROR: Could not parse workspaces-reply!\n");
ELOG("Could not parse workspaces-reply!\n");
exit(EXIT_FAILURE);
break;
}

View File

@ -69,7 +69,7 @@ ev_io *xkb_io;
int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
xcb_generic_error_t *err;
if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
printf("%s:%d - %s. X Error Code: %d", __FILE__, line, err_msg, err->error_code);
ELOG("%s. X Error Code: %d\n", err_msg, err->error_code);
return err->error_code;
}
return 0;
@ -235,7 +235,7 @@ void unhide_bars() {
values[2] = walk->rect.w;
values[3] = font_height + 6;
values[4] = XCB_STACK_MODE_ABOVE;
printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
cookie = xcb_configure_window_checked(xcb_connection,
walk->bar,
mask,
@ -267,11 +267,11 @@ void handle_button(xcb_button_press_event_t *event) {
}
if (walk == NULL) {
printf("Unknown Bar klicked!\n");
DLOG("Unknown Bar klicked!\n");
return;
}
/* TODO: Move this to exern get_ws_for_output() */
/* TODO: Move this to extern get_ws_for_output() */
TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
if (cur_ws->visible) {
break;
@ -279,20 +279,20 @@ void handle_button(xcb_button_press_event_t *event) {
}
if (cur_ws == NULL) {
printf("No Workspace active?\n");
DLOG("No Workspace active?\n");
return;
}
int32_t x = event->event_x;
printf("Got Button %d\n", event->detail);
DLOG("Got Button %d\n", event->detail);
switch (event->detail) {
case 1:
/* Left Mousbutton. We determine, which button was clicked
* and set cur_ws accordingly */
TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
printf("x = %d\n", x);
DLOG("x = %d\n", x);
if (x < cur_ws->name_width + 10) {
break;
}
@ -375,18 +375,18 @@ void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
XkbEvent ev;
int modstate;
printf("Got XKB-Event!\n");
DLOG("Got XKB-Event!\n");
while (XPending(xkb_dpy)) {
XNextEvent(xkb_dpy, (XEvent*)&ev);
if (ev.type != xkb_event_base) {
printf("ERROR: No Xkb-Event!\n");
ELOG("No Xkb-Event!\n");
continue;
}
if (ev.any.xkb_type != XkbStateNotify) {
printf("ERROR: No State Notify!\n");
ELOG("No State Notify!\n");
continue;
}
@ -396,10 +396,10 @@ void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
if (modstate != mod_pressed) {
if (modstate == 0) {
printf("Mod4 got released!\n");
DLOG("Mod4 got released!\n");
hide_bars();
} else {
printf("Mod4 got pressed!\n");
DLOG("Mod4 got pressed!\n");
unhide_bars();
}
mod_pressed = modstate;
@ -414,10 +414,10 @@ void init_xcb(char *fontname) {
/* FIXME: xcb_connect leaks Memory */
xcb_connection = xcb_connect(NULL, NULL);
if (xcb_connection_has_error(xcb_connection)) {
printf("Cannot open display\n");
ELOG("Cannot open display\n");
exit(EXIT_FAILURE);
}
printf("Connected to xcb\n");
DLOG("Connected to xcb\n");
/* We have to request the atoms we need */
#define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
@ -456,23 +456,23 @@ void init_xcb(char *fontname) {
&xkb_err);
if (xkb_dpy == NULL) {
printf("ERROR: No XKB!\n");
ELOG("No XKB!\n");
exit(EXIT_FAILURE);
}
if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
exit(EXIT_FAILURE);
}
int i1;
if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
printf("ERROR: XKB not supported by X-server!\n");
ELOG("XKB not supported by X-server!\n");
exit(EXIT_FAILURE);
}
if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
printf("Could not grab Key!\n");
ELOG("Could not grab Key!\n");
exit(EXIT_FAILURE);
}
@ -532,7 +532,7 @@ void init_xcb(char *fontname) {
font_table = xcb_query_font_char_infos(font_info);
}
printf("Calculated Font-height: %d\n", font_height);
DLOG("Calculated Font-height: %d\n", font_height);
if (xcb_request_failed(sl_ctx_cookie, "Could not create context for statusline")) {
exit(EXIT_FAILURE);
@ -571,14 +571,14 @@ void get_atoms() {
xcb_intern_atom_reply_t *reply;
#define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
if (reply == NULL) { \
printf("ERROR: Could not get atom %s\n", #name); \
ELOG("Could not get atom %s\n", #name); \
exit(EXIT_FAILURE); \
} \
atoms[name] = reply->atom; \
free(reply);
#include "xcb_atoms.def"
printf("Got Atoms\n");
DLOG("Got Atoms\n");
}
/*
@ -609,12 +609,12 @@ void reconfig_windows() {
if (!walk->active) {
/* If an output is not active, we destroy it's bar */
/* FIXME: Maybe we rather want to unmap? */
printf("Destroying window for output %s\n", walk->name);
DLOG("Destroying window for output %s\n", walk->name);
destroy_window(walk);
continue;
}
if (walk->bar == XCB_NONE) {
printf("Creating Window for output %s\n", walk->name);
DLOG("Creating Window for output %s\n", walk->name);
walk->bar = xcb_generate_id(xcb_connection);
walk->buffer = xcb_generate_id(xcb_connection);
@ -690,7 +690,7 @@ void reconfig_windows() {
values[2] = walk->rect.w;
values[3] = font_height + 6;
values[4] = XCB_STACK_MODE_ABOVE;
printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
walk->bar,
mask,
@ -707,7 +707,7 @@ void reconfig_windows() {
*
*/
void draw_bars() {
printf("Drawing Bars...\n");
DLOG("Drawing Bars...\n");
int i = 0;
refresh_statusline();
@ -715,7 +715,7 @@ void draw_bars() {
i3_output *outputs_walk;
SLIST_FOREACH(outputs_walk, outputs, slist) {
if (!outputs_walk->active) {
printf("Output %s inactive, skipping...\n", outputs_walk->name);
DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
continue;
}
if (outputs_walk->bar == XCB_NONE) {
@ -736,7 +736,7 @@ void draw_bars() {
&rect);
if (statusline != NULL) {
printf("Printing statusline!\n");
DLOG("Printing statusline!\n");
/* Luckily we already prepared a seperate pixmap containing the rendered
* statusline, we just have to copy the relevant parts to the relevant
@ -752,13 +752,13 @@ void draw_bars() {
i3_ws *ws_walk;
TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
uint32_t color = get_colorpixel("240000");
if (ws_walk->visible) {
color = get_colorpixel("480000");
}
if (ws_walk->urgent) {
printf("WS %s is urgent!\n", ws_walk->name);
DLOG("WS %s is urgent!\n", ws_walk->name);
color = get_colorpixel("002400");
/* The urgent-hint should get noticed, so we unhide the bars shortly */
unhide_bars();