diff options
| author | Tomas Bzatek <tbzatek@redhat.com> | 2024-01-19 18:26:34 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@redhat.com> | 2024-01-19 18:26:34 +0100 |
| commit | a55d09bb2d74944b7ea5a7e81b7d3e86bc04cd42 (patch) | |
| tree | 7b2229b1fce0b342d1968069eae98a3be4afc3a8 /common/filelist.c | |
| parent | e9036281ebb3a5be97e42f56667bb8dfebc5b4fe (diff) | |
| download | tuxcmd-modules-a55d09bb2d74944b7ea5a7e81b7d3e86bc04cd42.tar.xz | |
common: Logging rework
Use common logging macros for a consistent output. A logging domain
needs to be set in a Makefile first (the _LOG_DOMAIN define).
Heavy debugging output is guarded by the __DEBUG_INTERNAL define
and omitted by default.
Logging severity is controlled by the TUXCMD_DEBUG env. var,
typically set by the tuxcmd process itself.
Diffstat (limited to 'common/filelist.c')
| -rw-r--r-- | common/filelist.c | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/common/filelist.c b/common/filelist.c index 2926376..023668d 100644 --- a/common/filelist.c +++ b/common/filelist.c @@ -23,6 +23,7 @@ #include "filelist.h" #include "strutils.h" +#include "logutils.h" #include "vfsutils.h" @@ -32,8 +33,9 @@ filelist_tree_new () { struct PathTree *tree; + log_debug ("filelist_tree_new()"); + tree = g_malloc0 (sizeof (struct PathTree)); - log ("filelist_tree_new()\n"); tree->items = g_ptr_array_new (); tree->data = NULL; tree->original_index = 0; @@ -51,10 +53,8 @@ filelist_tree_free (struct PathTree *tree) { int i; - if (! tree) { - fprintf (stderr, "filelist_tree_free: tree == NULL !\n"); + if (tree == NULL) return; - } if (tree->items && tree->items->len > 0) { for (i = 0; i < tree->items->len; i++) @@ -80,7 +80,7 @@ filelist_tree_print_recurr (struct PathTree *tree, unsigned int level) if (tree) { fill = g_strnfill (level * 2, 32); - g_print (" %s#%lu. \"%s\"\n", fill, tree->original_index, tree->node); + g_print (" %s[%lu] '%s'\n", fill, tree->original_index, tree->node); if (tree->items && tree->items->len > 0) for (i = 0; i < tree->items->len; i++) { @@ -94,7 +94,10 @@ filelist_tree_print_recurr (struct PathTree *tree, unsigned int level) void filelist_tree_print (struct PathTree *tree) { + if (_log_level < LOG_INFO) + return; filelist_tree_print_recurr (tree, 0); + g_print ("\n"); } @@ -115,17 +118,17 @@ filelist_tree_find_node_by_path (struct PathTree *tree, const char *path) path += 2; /* remove leading and trailing '/' if present */ p = exclude_trailing_path_sep (path + G_IS_DIR_SEPARATOR (*path)); - log (" filelist_tree_find_node_by_path: path = '%s', p = '%s'\n", path, p); + log_debug (" filelist_tree_find_node_by_path: path = '%s', p = '%s'", path, p); node = NULL; if (tree && tree->node && g_strcmp0 (tree->node, "/") == 0 && g_strcmp0 (path, "/") == 0) { - log (" filelist_tree_find_node_by_path: matched root '/' element, returning.\n"); + log_debug (" filelist_tree_find_node_by_path: matched root '/' element, returning."); node = tree; } else if (tree && tree->items && tree->items->len > 0) { split_path (p, &first_part, &last_part); - log (" filelist_tree_find_node_by_path: first_part = '%s'\n", first_part); - log (" filelist_tree_find_node_by_path: last_part = '%s'\n", last_part); + log_debug (" filelist_tree_find_node_by_path: first_part = '%s'", first_part); + log_debug (" filelist_tree_find_node_by_path: last_part = '%s'", last_part); /* find existing node */ for (i = 0; i < tree->items->len; i++) { @@ -134,16 +137,16 @@ filelist_tree_find_node_by_path (struct PathTree *tree, const char *path) /* this is the final station */ if (! last_part) { node = t; - log (" filelist_tree_find_node_by_path: found final node '%s', returning.\n", t->node); + log_debug (" filelist_tree_find_node_by_path: found final node '%s', returning.", t->node); } else /* recurse to child items */ if (t->items && last_part) { - log (" filelist_tree_find_node_by_path: found node '%s', recursing in deep.\n", t->node); + log_debug (" filelist_tree_find_node_by_path: found node '%s', recursing in deep.", t->node); node = filelist_tree_find_node_by_path (t, last_part); } else { /* item found but no subitems */ - log (" filelist_tree_find_node_by_path: found node '%s', but no subitems found.\n", t->node); + log_debug (" filelist_tree_find_node_by_path: found node '%s', but no subitems found.", t->node); } break; @@ -178,12 +181,12 @@ filelist_tree_add_item_recurr (struct PathTree *tree, const char *path, struct T unsigned int i; split_path (path, &first_part, &last_part); - log (" filelist_tree_add_item_recur: first_part = '%s'\n", first_part); - log (" filelist_tree_add_item_recur: last_part = '%s'\n", last_part); + log_debug (" filelist_tree_add_item_recur: first_part = '%s'", first_part); + log_debug (" filelist_tree_add_item_recur: last_part = '%s'", last_part); if (last_part == NULL) { /* final destination, create new item here */ - log (" filelist_tree_add_item_recur: creating new item here.\n"); + log_debug (" filelist_tree_add_item_recur: creating new item here."); t = g_malloc0 (sizeof (struct PathTree)); t->items = NULL; t->data = item; @@ -202,7 +205,7 @@ filelist_tree_add_item_recurr (struct PathTree *tree, const char *path, struct T g_ptr_array_add (tree->items, t); } else { /* not a final place, find parent node or create new one if doesn't exist */ - log (" filelist_tree_add_item_recur: node '%s', path '%s'\n", tree->node, path); + log_debug (" filelist_tree_add_item_recur: node '%s', path '%s'", tree->node, path); if (! tree->items) tree->items = g_ptr_array_new (); @@ -211,7 +214,7 @@ filelist_tree_add_item_recurr (struct PathTree *tree, const char *path, struct T for (i = 0; i < tree->items->len; i++) { t = g_ptr_array_index (tree->items, i); if (g_strcmp0 (t->node, first_part) == 0) { - log (" filelist_tree_add_item_recur: found node '%s'\n", t->node); + log_debug (" filelist_tree_add_item_recur: found node '%s'", t->node); node = t; break; } @@ -219,7 +222,7 @@ filelist_tree_add_item_recurr (struct PathTree *tree, const char *path, struct T if (node == NULL) { /* create new path holder node */ - log (" filelist_tree_add_item_recur: parent node not found, creating new path holder\n"); + log_debug (" filelist_tree_add_item_recur: parent node not found, creating new path holder"); node = g_malloc0 (sizeof (struct PathTree)); node->items = g_ptr_array_new (); node->node = g_strdup (first_part); @@ -247,16 +250,16 @@ filelist_tree_add_item (struct PathTree *tree, const char *path, struct TVFSItem struct PathTree *found; if (! tree) { - fprintf (stderr, "filelist_tree_add_item: tree == NULL !\n"); + log_error ("filelist_tree_add_item: tree == NULL!"); return FALSE; } if (! path) { - fprintf (stderr, "filelist_tree_add_item: path == NULL !\n"); + log_error ("filelist_tree_add_item: path == NULL!"); return FALSE; } if (g_strcmp0 (path, "/") == 0 || g_strcmp0 (path, ".") == 0 || g_strcmp0 (path, "..") == 0 || g_strcmp0 (path, "./") == 0) { - fprintf (stderr, "filelist_tree_add_item: path '%s' is not a valid path\n", path); + log_error ("filelist_tree_add_item: path '%s' is not a valid path", path); return FALSE; } @@ -266,7 +269,7 @@ filelist_tree_add_item (struct PathTree *tree, const char *path, struct TVFSItem /* remove leading and trailing '/' if present */ p = exclude_trailing_path_sep (path + G_IS_DIR_SEPARATOR (*path)); - log (" filelist_tree_add_item: p = '%s'\n", p); + log_debug (" filelist_tree_add_item: p = '%s'", p); pp = canonicalize_filename (p); if (! pp) @@ -275,7 +278,7 @@ filelist_tree_add_item (struct PathTree *tree, const char *path, struct TVFSItem found = filelist_tree_find_node_by_path (tree, pp); if (found) { /* replace old data with current ones (record might have been created automatically during path building) */ - log (" filelist_tree_add_item: found old item, replacing data\n"); + log_debug (" filelist_tree_add_item: found old item, replacing data"); found->original_index = original_index; /* free old data */ free_vfs_item (found->data); @@ -320,7 +323,7 @@ resolve_symlink_recurr (struct PathTree *node, struct PathTree *root_tree, const struct PathTree *link_t; rel = resolve_relative (path, link_dest); - g_print (" resolve_symlink_recurr: relative = '%s'\n", rel); + log_debug (" resolve_symlink_recurr: relative = '%s'", rel); if (rel) { link_t = filelist_tree_find_node_by_path (root_tree, rel); if (link_t && link_t->data) { @@ -329,21 +332,21 @@ resolve_symlink_recurr (struct PathTree *node, struct PathTree *root_tree, const new_path = g_path_get_dirname (rel); if (level > FILELIST_MAX_SYMLINK_DEPTH || ! new_path) { node->symlink_target_data = link_t->data; - g_print (" resolve_symlink_recurr: max depth reached, link is broken. new_path = '%s'\n", new_path); + log_debug (" resolve_symlink_recurr: max depth reached, link is broken. new_path = '%s'", new_path); } else { - g_print (" resolve_symlink_recurr: going deep, rel = '%s', new_path = '%s'\n", rel, new_path); + log_debug (" resolve_symlink_recurr: going deep, rel = '%s', new_path = '%s'", rel, new_path); resolve_symlink_recurr (node, root_tree, new_path, link_t->data->sLinkTo, level + 1); } g_free (new_path); } else { /* final destination */ node->symlink_target_data = link_t->data; - g_print (" resolve_symlink_recurr: reached target '%s'\n", rel); + log_debug (" resolve_symlink_recurr: reached target '%s'", rel); } } else { /* target not found, link is broken */ node->symlink_target_data = NULL; - g_print (" resolve_symlink_recurr: target '%s' not found, link is broken\n", rel); + log_debug (" resolve_symlink_recurr: target '%s' not found, link is broken", rel); } } g_free (rel); @@ -361,7 +364,7 @@ filelist_tree_resolve_symlinks_recurr (struct PathTree *tree, struct PathTree *r t = g_ptr_array_index (tree->items, i); if (t && t->data && t->data->IsLink && t->data->sLinkTo) { - g_print ("filelist_tree_resolve_symlinks: found '%s/%s' --> '%s', resolving...\n", path, t->node, t->data->sLinkTo); + log_debug ("filelist_tree_resolve_symlinks: found '%s/%s' --> '%s', resolving...", path, t->node, t->data->sLinkTo); resolve_symlink_recurr (t, root_tree, path, t->data->sLinkTo, 1); } new_path = g_build_filename (path, t->node, NULL); @@ -374,7 +377,7 @@ filelist_tree_resolve_symlinks_recurr (struct PathTree *tree, struct PathTree *r void filelist_tree_resolve_symlinks (struct PathTree *tree) { - log ("filelist_tree_resolve_symlinks: begin\n"); + log_debug ("filelist_tree_resolve_symlinks: begin"); filelist_tree_resolve_symlinks_recurr (tree, tree, "/"); - log ("filelist_tree_resolve_symlinks: end\n"); + log_debug ("filelist_tree_resolve_symlinks: end"); } |
