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

@ -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);
}