Compare commits
2 Commits
836241c014
...
2f6e191b3b
Author | SHA1 | Date | |
---|---|---|---|
2f6e191b3b | |||
cc0de7a72e |
70
src/util.c
70
src/util.c
@ -225,33 +225,47 @@ UIntArray* wm_nonempty_workspaces_to_strptrarray(Wm *wm)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
|
||||
json_object* wm_state_to_json_object(Wm *wm, const char *prefixstr)
|
||||
{
|
||||
RETURN_IF_NULL(prefixstr);
|
||||
RETURN_IF_NULL(logfile);
|
||||
assert(prefixstr);
|
||||
|
||||
json_object *ret = json_object_new_object();
|
||||
|
||||
UIntArray *ws_str = wm_nonempty_workspaces_to_strptrarray(wm);
|
||||
|
||||
char str[128] = {0};
|
||||
char prefix[WM_PREFIX_LEN + 32] = {0};
|
||||
snprintf(prefix, sizeof(prefix), "%lu:%s", time(NULL), prefixstr);
|
||||
|
||||
json_object *log_entry_obj = json_object_new_object();
|
||||
|
||||
for (size_t i = 0; i < ws_str->size; i++) {
|
||||
WmWorkspaceToStrRet *ws_with_index = (WmWorkspaceToStrRet*)
|
||||
wm_uintarray_at(ws_str, i);
|
||||
|
||||
snprintf(str, sizeof(str), "%ld", ws_with_index->ws_index);
|
||||
json_object_object_add(log_entry_obj, str, json_tokener_parse(ws_with_index->str));
|
||||
json_object_object_add(ret, str, json_tokener_parse(ws_with_index->str));
|
||||
|
||||
free(ws_with_index->str);
|
||||
free(ws_with_index);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
|
||||
{
|
||||
RETURN_IF_NULL(prefixstr);
|
||||
RETURN_IF_NULL(logfile);
|
||||
|
||||
DEBUG_PRINT("saving state to file: %s\n", logfile);
|
||||
|
||||
char prefix[WM_PREFIX_LEN + 32] = {0};
|
||||
snprintf(prefix, sizeof(prefix), "%lu:%s", time(NULL), prefixstr);
|
||||
|
||||
json_object *log_entry_obj = wm_state_to_json_object(wm, prefixstr);
|
||||
|
||||
int fd = -1;
|
||||
struct json_object *jobj = NULL;
|
||||
if (access(logfile, F_OK) != 0) {
|
||||
|
||||
int access_ret = access(logfile, F_OK);
|
||||
if (access_ret != 0) {
|
||||
DEBUG_PRINT("logfile does not exist\n");
|
||||
fd = creat(logfile, 0644);
|
||||
|
||||
if (fd < 0) {
|
||||
@ -261,19 +275,32 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
|
||||
|
||||
jobj = json_object_new_object();
|
||||
} else {
|
||||
fd = open(logfile, O_RDWR);
|
||||
DEBUG_PRINT("logfile exists\n");
|
||||
|
||||
struct stat st;
|
||||
if (stat(logfile, &st) < 0) {
|
||||
perror("stat");
|
||||
goto ret;
|
||||
}
|
||||
|
||||
fd = open(logfile, O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
goto ret;
|
||||
}
|
||||
|
||||
jobj = json_object_from_fd(fd);
|
||||
if (!jobj) {
|
||||
const char *err = json_util_get_last_err();
|
||||
fprintf(stderr, "wm: could not read json: %s\n", err);
|
||||
// TODO maybe free
|
||||
goto ret;
|
||||
if (st.st_size == 0) {
|
||||
DEBUG_PRINT("logfile is empty");
|
||||
jobj = json_object_new_object();
|
||||
} else {
|
||||
jobj = json_object_from_fd(fd);
|
||||
if (!jobj) {
|
||||
const char *err = json_util_get_last_err();
|
||||
fprintf(stderr, "wm: could not read json: %s\n", err);
|
||||
DEBUG_PRINT("wm: could not read json: %s\n", err);
|
||||
// TODO maybe free
|
||||
goto ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -285,7 +312,7 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
|
||||
goto ret;
|
||||
}
|
||||
|
||||
ret = open(logfile, O_TRUNC | O_WRONLY);
|
||||
fd = open(logfile, O_TRUNC | O_WRONLY);
|
||||
|
||||
if (ret < 0) {
|
||||
perror("open");
|
||||
@ -306,7 +333,6 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
|
||||
|
||||
ret:
|
||||
json_object_put(jobj);
|
||||
wm_uintarray_free(ws_str);
|
||||
}
|
||||
|
||||
TreeNode* wm_treenode_lmd(TreeNode *node)
|
||||
@ -772,7 +798,8 @@ static void recursive_mkdir(char *path, mode_t mode)
|
||||
|
||||
void wm_logfile_init(const char *path)
|
||||
{
|
||||
char *log_file_dir = dirname((char*)path);
|
||||
char *dup = strdup(path);
|
||||
char *log_file_dir = dirname(dup);
|
||||
|
||||
if (access(path, F_OK) == 0) return;
|
||||
|
||||
@ -785,6 +812,7 @@ void wm_logfile_init(const char *path)
|
||||
ret = close(ret);
|
||||
if (ret < 0) CREATE_LOGFILE_ERROR(strerror(errno));
|
||||
|
||||
free(dup);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -799,6 +827,8 @@ void wm_logfile_init(const char *path)
|
||||
} else {
|
||||
CREATE_LOGFILE_ERROR(strerror(errno));
|
||||
}
|
||||
|
||||
free(dup);
|
||||
}
|
||||
|
||||
/* UIntArray<LogEntry*> */
|
||||
|
@ -121,6 +121,7 @@ TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client);
|
||||
NodeArray* wm_treenode_find_client_nodes(TreeNode *root);
|
||||
TreeNode* wm_treenode_split_get_sibling(TreeNode *node);
|
||||
NodeArray* wm_all_nodes_to_array(TreeNode *root);
|
||||
json_object* wm_state_to_json_object(Wm *wm, const char *prefixstr);
|
||||
void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile);
|
||||
|
||||
TreeNode* wm_treenode_lmd(TreeNode *node);
|
||||
|
Loading…
x
Reference in New Issue
Block a user