libyang REFACTOR simplify logging and extend location information

Simplify logger interface by using location information maintained in
the background. logger now prints all the available information: schema
path, data path and line numbers. However, the line number are quite
inaccurate (e.g. points to XML closing parent element) and some future
tuning would be great.
diff --git a/src/tree_data.c b/src/tree_data.c
index e466505..ba12500 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -56,7 +56,7 @@
 LY_ERR
 lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
         size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
-        const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
+        const struct lysc_node *ctx_node, ly_bool *incomplete)
 {
     LY_ERR ret;
     struct ly_err_item *err = NULL;
@@ -73,10 +73,10 @@
         }
     } else if (ret) {
         if (err) {
-            LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg);
+            LOGVAL(ctx, err->vecode, err->msg);
             ly_err_free(err);
         } else {
-            LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
+            LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
         }
         return ret;
     }
@@ -89,7 +89,7 @@
 
 LY_ERR
 lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
-        const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
+        const struct lyd_node *ctx_node, const struct lyd_node *tree)
 {
     LY_ERR ret;
     struct ly_err_item *err = NULL;
@@ -99,10 +99,10 @@
     ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
     if (ret) {
         if (err) {
-            LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg);
+            LOGVAL(ctx, err->vecode, err->msg);
             ly_err_free(err);
         } else {
-            LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
+            LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
         }
         return ret;
     }
@@ -136,7 +136,9 @@
     } else if (rc && err) {
         if (ctx) {
             /* log only in case the ctx was provided as input parameter */
-            LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
+            LOG_LOCSET(ctx, NULL, NULL, err->path, NULL);
+            LOGVAL(ctx, err->vecode, err->msg);
+            LOG_LOCBACK(ctx, 0, 0, 1, 0);
         }
         ly_err_free(err);
     }
@@ -179,7 +181,9 @@
     if (rc) {
         if (err) {
             if (ctx) {
-                LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
+                LOG_LOCSET(ctx, NULL, (const struct lyd_node *)node, NULL, NULL);
+                LOGVAL(ctx, err->vecode, err->msg);
+                LOG_LOCBACK(ctx, 0, 1, 0, 0);
             }
             ly_err_free(err);
         }
@@ -215,8 +219,9 @@
     type = ((struct lysc_node_leaf *)node->schema)->type;
 
     /* store the value */
-    ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema,
-            NULL, LY_VLOG_LYSC, node->schema);
+    LOG_LOCSET(ctx, node->schema, (const struct lyd_node *)node, NULL, NULL);
+    ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
+    LOG_LOCBACK(ctx, 1, 1, 0, 0);
     LY_CHECK_RET(ret);
 
     /* compare values */
@@ -533,8 +538,10 @@
     term->prev = (struct lyd_node *)term;
     term->flags = LYD_NEW;
 
+    LOG_LOCSET(schema->module->ctx, schema, NULL, NULL, NULL);
     ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
-            value_len, dynamic, format, prefix_data, hints, schema, incomplete, LY_VLOG_LYSC, schema);
+            value_len, dynamic, format, prefix_data, hints, schema, incomplete);
+    LOG_LOCBACK(schema->module->ctx, 1, 0, 0, 0);
     LY_CHECK_ERR_RET(ret, free(term), ret);
     lyd_hash((struct lyd_node *)term);
 
@@ -610,6 +617,8 @@
     /* create list */
     LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
 
+    LOG_LOCSET(schema->module->ctx, NULL, list, NULL, NULL);
+
     /* create and insert all the keys */
     LY_ARRAY_FOR(predicates, u) {
         LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
@@ -624,6 +633,7 @@
     list = NULL;
 
 cleanup:
+    LOG_LOCBACK(schema->module->ctx, 0, 1, 0, 0);
     lyd_free_tree(list);
     return ret;
 }
@@ -637,6 +647,8 @@
     enum ly_path_pred_type pred_type = 0;
     struct ly_path_predicate *predicates = NULL;
 
+    LOG_LOCSET(schema->module->ctx, schema, NULL, NULL, NULL);
+
     /* parse keys */
     LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
             LY_PATH_PRED_KEYS, &expr), cleanup);
@@ -649,6 +661,7 @@
     LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
 
 cleanup:
+    LOG_LOCBACK(schema->module->ctx, 1, 0, 0, 0);
     lyxp_expr_free(schema->module->ctx, expr);
     ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
     return ret;
@@ -1150,8 +1163,10 @@
     type = ((struct lysc_node_leaf *)term->schema)->type;
 
     /* parse the new value */
+    LOG_LOCSET(LYD_CTX(term), term->schema, term, NULL, NULL);
     ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
-            term->schema, NULL, LY_VLOG_LYD, term);
+            term->schema, NULL);
+    LOG_LOCBACK(LYD_CTX(term), term->schema ? 1 : 0, 1, 0, 0);
     LY_CHECK_GOTO(ret, cleanup);
 
     /* compare original and new value */
@@ -1291,8 +1306,10 @@
     schema = p[LY_ARRAY_COUNT(p) - 1].node;
     if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE) &&
             !(options & LYD_NEW_PATH_OPAQ)) {
-        LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
-                lys_nodetype2str(schema->nodetype), schema->name);
+        LOG_LOCSET(ctx, schema, NULL, NULL, NULL);
+        LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
+                lys_nodetype2str(schema->nodetype), schema->name, path);
+        LOG_LOCBACK(ctx, 1, 0, 0, 0);
         ret = LY_EINVAL;
         goto cleanup;
     } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
@@ -1311,7 +1328,7 @@
             LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
 
             ret = lyd_value_store(ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value, strlen(value),
-                    NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL, LY_VLOG_LYSC, schema);
+                    NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
             LY_CHECK_GOTO(ret, cleanup);
             ++((struct lysc_type *)pred->value.realtype)->refcount;
         } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
@@ -1323,7 +1340,9 @@
         if (ret == LY_SUCCESS) {
             /* the node exists, are we supposed to update it or is it just a default? */
             if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
-                LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
+                LOG_LOCSET(ctx, NULL, node, NULL, NULL);
+                LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
+                LOG_LOCBACK(ctx, 0, 1, 0, 0);
                 ret = LY_EEXIST;
                 goto cleanup;
             }
@@ -2234,13 +2253,15 @@
         size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
         void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
 {
-    LY_ERR rc;
+    LY_ERR ret = LY_SUCCESS;
     struct lysc_ext_instance *ant = NULL;
     struct lyd_meta *mt, *last;
     LY_ARRAY_COUNT_TYPE u;
 
     assert((parent || meta) && mod);
 
+    LOG_LOCSET(mod->ctx, parent ? parent->schema : NULL, parent, NULL, NULL);
+
     LY_ARRAY_FOR(mod->compiled->exts, u) {
         if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
                 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
@@ -2251,20 +2272,21 @@
     }
     if (!ant) {
         /* attribute is not defined as a metadata annotation (RFC 7952) */
-        LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
+        LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
                 mod->name, name_len, name);
-        return LY_EINVAL;
+        ret = LY_EINVAL;
+        goto cleanup;
     }
 
     mt = calloc(1, sizeof *mt);
-    LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
+    LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
     mt->parent = parent;
     mt->annotation = ant;
-    rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic,
-            format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL);
-    LY_CHECK_ERR_RET(rc, free(mt), rc);
-    rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
-    LY_CHECK_ERR_RET(rc, free(mt), rc);
+    ret = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic,
+            format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
+    LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
+    ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
+    LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
 
     /* insert as the last attribute */
     if (parent) {
@@ -2277,7 +2299,10 @@
     if (meta) {
         *meta = mt;
     }
-    return LY_SUCCESS;
+
+cleanup:
+    LOG_LOCBACK(mod->ctx, (parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
+    return ret;
 }
 
 void