logging changes

This commit is contained in:
Akos Horvath 2023-11-25 13:59:28 +01:00
parent 62bf51d706
commit 2ea1e96401
3 changed files with 46 additions and 13 deletions

View File

@ -148,7 +148,14 @@ char* wm_treenode_to_str(TreeNode *node)
json_object_object_add(pos, "w", json_object_new_int(node->pos.w));
json_object_object_add(pos, "h", json_object_new_int(node->pos.h));
json_object_object_add(jobj, "pos", pos);
json_object_object_add(jobj, "client", json_object_new_uint64((uint64_t)node->client));
if (node->type == NODE_CLIENT && node->client) {
json_object *client = json_object_new_object();
json_object_object_add(client, "address", json_object_new_uint64((unsigned long)node->client));
json_object_object_add(client, "name", json_object_new_string(node->client->name));
json_object_object_add(client, "is_pid_set", json_object_new_boolean(node->client->is_pid_set));
json_object_object_add(client, "pid", json_object_new_int(node->client->pid));
json_object_object_add(jobj, "client", client);
}
json_object_object_add(jobj, "id", json_object_new_uint64((uint64_t)node->id));
char *ret = strdup(json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PRETTY));
@ -164,11 +171,17 @@ PtrArray wm_nonempty_workspaces_to_strptrarray(Wm *wm)
for (size_t i = 0; i < wm->smon->wscount; i++) {
TreeNode *node = wm->smon->workspaces[i].tree;
if (!wm_treenode_is_empty(node) || (node->type == NODE_CLIENT && node->client))
wm_ptrarray_push(&ret, wm_treenode_to_str(wm->smon->workspaces[i].tree));
if (!wm_treenode_is_empty(node) || (node->type == NODE_CLIENT && node->client)) {
WmWorkspaceToStrRet *ws_str_with_index = malloc(sizeof(WmWorkspaceToStrRet));
*ws_str_with_index = (WmWorkspaceToStrRet) {
.ws_index = i,
.str = wm_treenode_to_str(wm->smon->workspaces[i].tree)
};
wm_ptrarray_push(&ret, ws_str_with_index);
}
}
DEBUG_PRINT("%s returning %d\n", __func__, ret.size);
DEBUG_PRINT("%s returning %ld\n", __func__, ret.size);
return ret;
}
@ -178,19 +191,32 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
RETURN_IF_NULL(prefixstr);
RETURN_IF_NULL(logfile);
PtrArray ws_str = wm_nonempty_workspaces_to_strptrarray(wm);
FILE* fptr = fopen(logfile, "a");
FILE *fptr = fopen(logfile, "a");
if (!fptr) {
fprintf(stderr, "wm: could not open log file %s\n", logfile);
return;
}
if (prefixstr) fprintf(fptr, "%lu %s\n", time(NULL), prefixstr);
PtrArray 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();
json_object_object_add(log_entry_obj, prefix, json_object_new_object());
for (size_t i = 0; i < ws_str.size; i++) {
fprintf(fptr, "%s\n", (char*)ws_str.ptrs[i]);
free(ws_str.ptrs[i]);
WmWorkspaceToStrRet *ws_with_index = (WmWorkspaceToStrRet*)ws_str.ptrs[i];
snprintf(str, sizeof(str), "%ld", ws_with_index->ws_index);
json_object_object_add(json_object_object_get(log_entry_obj, prefix), str, json_tokener_parse(ws_with_index->str));
fprintf(fptr, "%s,\n", json_object_to_json_string_ext(log_entry_obj, JSON_C_TO_STRING_PRETTY));
free(ws_with_index->str);
free(ws_with_index);
}
json_object_put(log_entry_obj);
fflush(fptr);
fclose(fptr);
wm_ptrarray_free(&ws_str);
}

View File

@ -61,6 +61,11 @@ struct TreeNode {
unsigned int id;
};
typedef struct {
size_t ws_index;
char *str;
} WmWorkspaceToStrRet;
TreeNode* wm_treenode_new(NodeType type, TreeNode *parent);
void wm_treenode_free(TreeNode *node);
bool wm_treenode_is_empty(TreeNode *node);

View File

@ -65,16 +65,18 @@ if (c == NULL) \
#define CURRENT_WS(wm) (wm)->smon->workspaces[(wm)->smon->selws]
#define WM_PREFIX_LEN 4096
#define WM_LOGFILENAME "./log"
#define WM_LOG_STATE_START(wm) do { \
wm_log_state(wm, __func__, WM_LOGFILENAME); \
} while (0);
#define WM_LOG_STATE_END(wm) do { \
char __prefix[4096] = {0}; \
snprintf(__prefix, 4096, "after %s", __func__); \
wm_log_state(wm, __prefix, WM_LOGFILENAME); \
#define WM_LOG_STATE_END(wm) do { \
char __prefix[WM_PREFIX_LEN] = {0}; \
snprintf(__prefix, WM_PREFIX_LEN, "after %s", __func__); \
wm_log_state(wm, __prefix, WM_LOGFILENAME); \
} while (0);
typedef struct Client Client;