shmlog: store meta information in the buffer itself, store path as X11 atom

This makes i3-dump-log completely independent of a running i3 instance.
This commit is contained in:
Michael Stapelberg
2012-01-06 23:40:07 +00:00
parent ed37a63942
commit dee7c07ad2
13 changed files with 59 additions and 157 deletions

View File

@ -643,48 +643,6 @@ IPC_HANDLER(get_bar_config) {
y(free);
}
/*
* Formats the reply message for a GET_LOG_MARKERS request and sends it to the
* client.
*
*/
IPC_HANDLER(get_log_markers) {
#if YAJL_MAJOR >= 2
yajl_gen gen = yajl_gen_alloc(NULL);
#else
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
#endif
int offset_next_write, offset_last_wrap, logsize;
get_log_markers(&offset_next_write, &offset_last_wrap, &logsize);
y(map_open);
ystr("offset_next_write");
y(integer, offset_next_write);
ystr("offset_last_wrap");
y(integer, offset_last_wrap);
ystr("shmname");
ystr(shmlogname);
ystr("size");
y(integer, logsize);
y(map_close);
const unsigned char *payload;
#if YAJL_MAJOR >= 2
size_t length;
#else
unsigned int length;
#endif
y(get_buf, &payload, &length);
ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_LOG_MARKERS, payload);
y(free);
}
/*
* Callback for the YAJL parser (will be called when a string is parsed).
*
@ -770,7 +728,7 @@ IPC_HANDLER(subscribe) {
/* The index of each callback function corresponds to the numeric
* value of the message type (see include/i3/ipc.h) */
handler_t handlers[8] = {
handler_t handlers[7] = {
handle_command,
handle_get_workspaces,
handle_subscribe,
@ -778,7 +736,6 @@ handler_t handlers[8] = {
handle_tree,
handle_get_marks,
handle_get_bar_config,
handle_get_log_markers
};
/*

View File

@ -23,6 +23,7 @@
#include "log.h"
#include "i3.h"
#include "libi3.h"
#include "shmlog.h"
/* loglevels.h is autogenerated at make time */
#include "loglevels.h"
@ -52,6 +53,20 @@ static int logbuffer_size;
/* File descriptor for shm_open. */
static int logbuffer_shm;
/*
* Writes the offsets for the next write and for the last wrap to the
* shmlog_header.
* Necessary to print the i3 SHM log in the correct order.
*
*/
static void store_log_markers() {
i3_shmlog_header *header = (i3_shmlog_header*)logbuffer;
header->offset_next_write = (logwalk - logbuffer);
header->offset_last_wrap = (loglastwrap - logbuffer);
header->size = logbuffer_size;
}
/*
* Initializes logging by creating an error logfile in /tmp (or
* XDG_RUNTIME_DIR, see get_process_filename()).
@ -103,8 +118,9 @@ void init_logging() {
logbuffer = NULL;
return;
}
logwalk = logbuffer;
logwalk = logbuffer + sizeof(i3_shmlog_header);
loglastwrap = logbuffer + logbuffer_size;
store_log_markers();
}
}
@ -141,17 +157,6 @@ void add_loglevel(const char *level) {
}
}
/*
* Returns the offsets for the next write and for the last wrap.
* Necessary to print the i3 SHM log in the correct order.
*
*/
void get_log_markers(int *offset_next_write, int *offset_last_wrap, int *size) {
*offset_next_write = (logwalk - logbuffer);
*offset_last_wrap = (loglastwrap - logbuffer);
*size = logbuffer_size;
}
/*
* Logs the given message to stdout (if print is true) while prefixing the
* current time to it. Additionally, the message will be saved in the i3 SHM
@ -208,7 +213,7 @@ static void vlog(const bool print, const char *fmt, va_list args) {
* beginning again. */
if ((len+1) >= (logbuffer_size - (logwalk - logbuffer))) {
loglastwrap = logwalk;
logwalk = logbuffer;
logwalk = logbuffer + sizeof(i3_shmlog_header);
}
/* Copy the buffer, terminate it, move the write pointer to the byte after
@ -217,6 +222,8 @@ static void vlog(const bool print, const char *fmt, va_list args) {
logwalk[len] = '\0';
logwalk += len + 1;
store_log_markers();
if (print)
fwrite(message, len, 1, stdout);
}

View File

@ -335,7 +335,7 @@ int main(int argc, char *argv[]) {
break;
} else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
strcmp(long_options[option_index].name, "get_socketpath") == 0) {
char *socket_path = socket_path_from_x11();
char *socket_path = root_atom_contents("I3_SOCKET_PATH");
if (socket_path) {
printf("%s\n", socket_path);
return 0;
@ -416,7 +416,7 @@ int main(int argc, char *argv[]) {
optind++;
}
LOG("Command is: %s (%d bytes)\n", payload, strlen(payload));
char *socket_path = socket_path_from_x11();
char *socket_path = root_atom_contents("I3_SOCKET_PATH");
if (!socket_path) {
ELOG("Could not get i3 IPC socket path\n");
return 1;

View File

@ -928,6 +928,8 @@ void x_set_i3_atoms() {
current_socketpath);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
strlen(current_configpath), current_configpath);
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
strlen(shmlogname), shmlogname);
}
/*