Compare commits

...

2 Commits

Author SHA1 Message Date
2f6e191b3b break up logging logic, bugfixes 2024-03-07 11:20:38 +01:00
cc0de7a72e fix logfile_init 2024-03-07 11:17:59 +01:00
2 changed files with 51 additions and 20 deletions

View File

@ -225,33 +225,47 @@ UIntArray* wm_nonempty_workspaces_to_strptrarray(Wm *wm)
return ret; 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); assert(prefixstr);
RETURN_IF_NULL(logfile);
json_object *ret = json_object_new_object();
UIntArray *ws_str = wm_nonempty_workspaces_to_strptrarray(wm); UIntArray *ws_str = wm_nonempty_workspaces_to_strptrarray(wm);
char str[128] = {0}; 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++) { for (size_t i = 0; i < ws_str->size; i++) {
WmWorkspaceToStrRet *ws_with_index = (WmWorkspaceToStrRet*) WmWorkspaceToStrRet *ws_with_index = (WmWorkspaceToStrRet*)
wm_uintarray_at(ws_str, i); wm_uintarray_at(ws_str, i);
snprintf(str, sizeof(str), "%ld", ws_with_index->ws_index); 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->str);
free(ws_with_index); 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; int fd = -1;
struct json_object *jobj = NULL; 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); fd = creat(logfile, 0644);
if (fd < 0) { if (fd < 0) {
@ -261,19 +275,32 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
jobj = json_object_new_object(); jobj = json_object_new_object();
} else { } 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) { if (fd < 0) {
perror("open"); perror("open");
goto ret; goto ret;
} }
jobj = json_object_from_fd(fd); if (st.st_size == 0) {
if (!jobj) { DEBUG_PRINT("logfile is empty");
const char *err = json_util_get_last_err(); jobj = json_object_new_object();
fprintf(stderr, "wm: could not read json: %s\n", err); } else {
// TODO maybe free jobj = json_object_from_fd(fd);
goto ret; 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; goto ret;
} }
ret = open(logfile, O_TRUNC | O_WRONLY); fd = open(logfile, O_TRUNC | O_WRONLY);
if (ret < 0) { if (ret < 0) {
perror("open"); perror("open");
@ -306,7 +333,6 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
ret: ret:
json_object_put(jobj); json_object_put(jobj);
wm_uintarray_free(ws_str);
} }
TreeNode* wm_treenode_lmd(TreeNode *node) 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) 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; if (access(path, F_OK) == 0) return;
@ -785,6 +812,7 @@ void wm_logfile_init(const char *path)
ret = close(ret); ret = close(ret);
if (ret < 0) CREATE_LOGFILE_ERROR(strerror(errno)); if (ret < 0) CREATE_LOGFILE_ERROR(strerror(errno));
free(dup);
return; return;
} }
@ -799,6 +827,8 @@ void wm_logfile_init(const char *path)
} else { } else {
CREATE_LOGFILE_ERROR(strerror(errno)); CREATE_LOGFILE_ERROR(strerror(errno));
} }
free(dup);
} }
/* UIntArray<LogEntry*> */ /* UIntArray<LogEntry*> */

View File

@ -121,6 +121,7 @@ TreeNode* wm_treenode_ptr_find_client_node(TreeNode *root, Client *client);
NodeArray* wm_treenode_find_client_nodes(TreeNode *root); NodeArray* wm_treenode_find_client_nodes(TreeNode *root);
TreeNode* wm_treenode_split_get_sibling(TreeNode *node); TreeNode* wm_treenode_split_get_sibling(TreeNode *node);
NodeArray* wm_all_nodes_to_array(TreeNode *root); 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); void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile);
TreeNode* wm_treenode_lmd(TreeNode *node); TreeNode* wm_treenode_lmd(TreeNode *node);