From 2f6e191b3b990d0155aa5294f273ae28ca35810d Mon Sep 17 00:00:00 2001 From: Akos Horvath Date: Thu, 7 Mar 2024 11:20:38 +0100 Subject: [PATCH] break up logging logic, bugfixes --- src/util.c | 64 ++++++++++++++++++++++++++++++++++++++---------------- src/util.h | 1 + 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/util.c b/src/util.c index 79afad6..c38af53 100644 --- a/src/util.c +++ b/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) diff --git a/src/util.h b/src/util.h index c1170f1..9db6904 100644 --- a/src/util.h +++ b/src/util.h @@ -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);