libyang CHANGE bind errors and store them in a context
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 2712fe1..b20877e 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -3,7 +3,7 @@
  * @author Radek Krejci <rkrejci@cesnet.cz>
  * @brief YIN parser for libyang
  *
- * Copyright (c) 2015 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -32,25 +32,25 @@
 #include "tree_internal.h"
 #include "xml_internal.h"
 
-#define GETVAL(value, node, arg)                                                 \
+#define GETVAL(ctx, value, node, arg)                                                 \
     value = lyxml_get_attr(node, arg, NULL);                                     \
     if (!value) {                                                                \
-        LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, arg, node->name);                \
+        LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, arg, node->name);                \
         goto error;                                                              \
     }
 
-#define YIN_CHECK_ARRAY_OVERFLOW_CODE(counter, storage, name, parent, code)      \
+#define YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, counter, storage, name, parent, code)      \
     if ((counter) == LY_ARRAY_MAX(storage)) {                                    \
-        LOGERR(LY_EINT, "Reached limit (%"PRIu64") for storing %s in %s statement.", \
+        LOGERR(ctx, LY_EINT, "Reached limit (%"PRIu64") for storing %s in %s statement.", \
                LY_ARRAY_MAX(storage), name, parent);                             \
         code;                                                                    \
     }
 
-#define YIN_CHECK_ARRAY_OVERFLOW_RETURN(counter, storage, name, parent, retval)  \
-    YIN_CHECK_ARRAY_OVERFLOW_CODE(counter, storage, name, parent, return retval)
+#define YIN_CHECK_ARRAY_OVERFLOW_RETURN(ctx, counter, storage, name, parent, retval)  \
+    YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, counter, storage, name, parent, return retval)
 
-#define YIN_CHECK_ARRAY_OVERFLOW_GOTO(counter, storage, name, parent, target)    \
-    YIN_CHECK_ARRAY_OVERFLOW_CODE(counter, storage, name, parent, goto target)
+#define YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, counter, storage, name, parent, target)    \
+    YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, counter, storage, name, parent, goto target)
 
 #define OPT_IDENT       0x01
 #define OPT_CFG_PARSE   0x02
@@ -96,7 +96,7 @@
     struct unres_ext *info;
 
     info = malloc(sizeof *info);
-    LY_CHECK_ERR_RETURN(!info, LOGMEM, EXIT_FAILURE);
+    LY_CHECK_ERR_RETURN(!info, LOGMEM(module->ctx), EXIT_FAILURE);
     lyxml_unlink(module->ctx, yin);
     info->data.yin = yin;
     info->datatype = LYS_IN_YIN;
@@ -122,8 +122,8 @@
 
     /* there should be <text> child */
     if (!node->child || !node->child->name || strcmp(node->child->name, name)) {
-        LOGERR(LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, node->name);
+        LOGERR(ctx, LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
+        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, name, node->name);
         return NULL;
     } else if (node->child->content) {
         len = strlen(node->child->content);
@@ -236,7 +236,7 @@
         statement = "deviation";
         break;
     default:
-        LOGERR(LY_EINT, "parent type %d", elem_type);
+        LOGERR(mod->ctx, LY_EINT, "parent type %d", elem_type);
         return EXIT_FAILURE;
     }
 
@@ -256,10 +256,10 @@
         /* parse it as extension */
 parseext:
 
-        YIN_CHECK_ARRAY_OVERFLOW_RETURN(*ext_size, *ext_size, "extension", statement, EXIT_FAILURE);
+        YIN_CHECK_ARRAY_OVERFLOW_RETURN(mod->ctx, *ext_size, *ext_size, "extension", statement, EXIT_FAILURE);
         /* first, allocate a space for the extension instance in the parent elem */
         reallocated = realloc(*ext, (1 + (*ext_size)) * sizeof **ext);
-        LY_CHECK_ERR_RETURN(!reallocated, LOGMEM, EXIT_FAILURE);
+        LY_CHECK_ERR_RETURN(!reallocated, LOGMEM(mod->ctx), EXIT_FAILURE);
         (*ext) = reallocated;
 
         /* init memory */
@@ -286,11 +286,12 @@
     int r, c_ext = 0;
     const char *value;
     struct lyxml_elem *node, *next;
+    struct ly_ctx *ctx = parent->module->ctx;
 
-    GETVAL(value, yin, "name");
+    GETVAL(ctx, value, yin, "name");
 
     if ((lys_node_module(parent)->version != 2) && ((value[0] == '(') || strchr(value, ' '))) {
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
+        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
 error:
         return EXIT_FAILURE;
     }
@@ -300,7 +301,7 @@
     }
 
     r = resolve_iffeature_compile(iffeat, value, parent, parent_is_feature, unres);
-    lydict_remove(parent->module->ctx, value);
+    lydict_remove(ctx, value);
     if (r) {
         return EXIT_FAILURE;
     }
@@ -308,19 +309,19 @@
     LY_TREE_FOR_SAFE(yin->child, next, node) {
         if (!node->ns) {
             /* garbage */
-            lyxml_free(parent->module->ctx, node);
+            lyxml_free(ctx, node);
         } else if (strcmp(node->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_RETURN(c_ext, iffeat->ext_size, "extensions", "if-feature", EXIT_FAILURE);
+            YIN_CHECK_ARRAY_OVERFLOW_RETURN(ctx, c_ext, iffeat->ext_size, "extensions", "if-feature", EXIT_FAILURE);
             c_ext++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "if-feature");
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "if-feature");
             return EXIT_FAILURE;
         }
     }
     if (c_ext) {
         iffeat->ext = calloc(c_ext, sizeof *iffeat->ext);
-        LY_CHECK_ERR_RETURN(!iffeat->ext, LOGMEM, EXIT_FAILURE);
+        LY_CHECK_ERR_RETURN(!iffeat->ext, LOGMEM(ctx), EXIT_FAILURE);
         LY_TREE_FOR_SAFE(yin->child, next, node) {
             /* extensions */
             r = lyp_yin_fill_ext(iffeat, LYEXT_PAR_IDENT, 0, 0, parent->module, node,
@@ -340,12 +341,13 @@
 fill_yin_identity(struct lys_module *module, struct lyxml_elem *yin, struct lys_ident *ident, struct unres_schema *unres)
 {
     struct lyxml_elem *node, *next;
+    struct ly_ctx *ctx = module->ctx;
     const char *value;
     int rc;
     int c_ftrs = 0, c_base = 0, c_ext = 0;
     void *reallocated;
 
-    GETVAL(value, yin, "name");
+    GETVAL(ctx, value, yin, "name");
     ident->name = value;
 
     if (read_yin_common(module, NULL, ident, LYEXT_PAR_IDENT, yin, OPT_IDENT | OPT_MODULE, unres)) {
@@ -359,25 +361,25 @@
     LY_TREE_FOR(yin->child, node) {
         if (strcmp(node->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, ident->ext_size, "extensions", "identity", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, ident->ext_size, "extensions", "identity", error);
             c_ext++;
         } else if (!strcmp(node->name, "base")) {
             if (c_base && (module->version < 2)) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "base", "identity");
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, "base", "identity");
                 goto error;
             }
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_base, ident->base_size, "bases", "identity", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_base, ident->base_size, "bases", "identity", error);
             if (lyp_yin_parse_subnode_ext(module, ident, LYEXT_PAR_IDENT, node, LYEXT_SUBSTMT_BASE, c_base, unres)) {
                 goto error;
             }
             c_base++;
 
         } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, ident->iffeature_size, "if-features", "identity", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, ident->iffeature_size, "if-features", "identity", error);
             c_ftrs++;
 
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "identity");
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "identity");
             goto error;
         }
     }
@@ -385,16 +387,16 @@
     if (c_base) {
         ident->base_size = 0;
         ident->base = calloc(c_base, sizeof *ident->base);
-        LY_CHECK_ERR_GOTO(!ident->base, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!ident->base, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         ident->iffeature = calloc(c_ftrs, sizeof *ident->iffeature);
-        LY_CHECK_ERR_GOTO(!ident->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!ident->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(ident->ext, (c_ext + ident->ext_size) * sizeof *ident->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         ident->ext = reallocated;
 
         /* init memory */
@@ -410,17 +412,17 @@
                 goto error;
             }
         } else if (!strcmp(node->name, "base")) {
-            GETVAL(value, node, "name");
+            GETVAL(ctx, value, node, "name");
             value = transform_schema2json(module, value);
             if (!value) {
                 goto error;
             }
 
             if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, value) == -1) {
-                lydict_remove(module->ctx, value);
+                lydict_remove(ctx, value);
                 goto error;
             }
-            lydict_remove(module->ctx, value);
+            lydict_remove(ctx, value);
         } else if (!strcmp(node->name, "if-feature")) {
             rc = fill_yin_iffeature((struct lys_node *)ident, 0, node, &ident->iffeature[ident->iffeature_size], unres);
             ident->iffeature_size++;
@@ -443,6 +445,7 @@
 {
     struct lyxml_elem *child, *next;
     const char *value;
+    struct ly_ctx *ctx = module->ctx;
 
     LY_TREE_FOR_SAFE(yin->child, next, child) {
         if (!child->ns) {
@@ -455,52 +458,52 @@
             }
         } else if (!strcmp(child->name, "description")) {
             if (restr->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 return EXIT_FAILURE;
             }
             if (lyp_yin_parse_subnode_ext(module, restr, LYEXT_PAR_RESTR, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
                 return EXIT_FAILURE;
             }
-            restr->dsc = read_yin_subnode(module->ctx, child, "text");
+            restr->dsc = read_yin_subnode(ctx, child, "text");
             if (!restr->dsc) {
                 return EXIT_FAILURE;
             }
         } else if (!strcmp(child->name, "reference")) {
             if (restr->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 return EXIT_FAILURE;
             }
             if (lyp_yin_parse_subnode_ext(module, restr, LYEXT_PAR_RESTR, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
                 return EXIT_FAILURE;
             }
-            restr->ref = read_yin_subnode(module->ctx, child, "text");
+            restr->ref = read_yin_subnode(ctx, child, "text");
             if (!restr->ref) {
                 return EXIT_FAILURE;
             }
         } else if (!strcmp(child->name, "error-app-tag")) {
             if (restr->eapptag) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 return EXIT_FAILURE;
             }
             if (lyp_yin_parse_subnode_ext(module, restr, LYEXT_PAR_RESTR, child, LYEXT_SUBSTMT_ERRTAG, 0, unres)) {
                 return EXIT_FAILURE;
             }
-            GETVAL(value, child, "value");
-            restr->eapptag = lydict_insert(module->ctx, value, 0);
+            GETVAL(ctx, value, child, "value");
+            restr->eapptag = lydict_insert(ctx, value, 0);
         } else if (!strcmp(child->name, "error-message")) {
             if (restr->emsg) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 return EXIT_FAILURE;
             }
             if (lyp_yin_parse_subnode_ext(module, restr, LYEXT_PAR_RESTR, child, LYEXT_SUBSTMT_ERRMSG, 0, unres)) {
                 return EXIT_FAILURE;
             }
-            restr->emsg = read_yin_subnode(module->ctx, child, "value");
+            restr->emsg = read_yin_subnode(ctx, child, "value");
             if (!restr->emsg) {
                 return EXIT_FAILURE;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             return EXIT_FAILURE;
         }
     }
@@ -523,6 +526,7 @@
     struct lys_type_bit bit, *bits_sc = NULL;
     struct lys_type_enum *enms_sc = NULL; /* shortcut */
     struct lys_type *dertype;
+    struct ly_ctx *ctx = module->ctx;
     int rc, val_set, c_ftrs, c_ext = 0;
     unsigned int i, j;
     int ret = -1;
@@ -535,7 +539,7 @@
     /* init */
     memset(&exts, 0, sizeof exts);
 
-    GETVAL(value, yin, "name");
+    GETVAL(ctx, value, yin, "name");
     value = transform_schema2json(module, value);
     if (!value) {
         goto error;
@@ -543,19 +547,19 @@
 
     i = parse_identifier(value);
     if (i < 1) {
-        LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
-        lydict_remove(module->ctx, value);
+        LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
+        lydict_remove(ctx, value);
         goto error;
     }
     /* module name */
     name = value;
     if (value[i]) {
-        module_name = lydict_insert(module->ctx, value, i);
+        module_name = lydict_insert(ctx, value, i);
         name += i;
         if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
-            LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
-            lydict_remove(module->ctx, module_name);
-            lydict_remove(module->ctx, value);
+            LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
+            lydict_remove(ctx, module_name);
+            lydict_remove(ctx, value);
             goto error;
         }
         /* name is in dictionary, but moved */
@@ -564,21 +568,21 @@
 
     rc = resolve_superior_type(name, module_name, module, parent, &type->der);
     if (rc == -1) {
-        LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, module_name);
-        lydict_remove(module->ctx, module_name);
-        lydict_remove(module->ctx, value);
+        LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, module_name);
+        lydict_remove(ctx, module_name);
+        lydict_remove(ctx, value);
         goto error;
 
     /* the type could not be resolved or it was resolved to an unresolved typedef */
     } else if (rc == EXIT_FAILURE) {
-        LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
-        lydict_remove(module->ctx, module_name);
-        lydict_remove(module->ctx, value);
+        LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
+        lydict_remove(ctx, module_name);
+        lydict_remove(ctx, value);
         ret = EXIT_FAILURE;
         goto error;
     }
-    lydict_remove(module->ctx, module_name);
-    lydict_remove(module->ctx, value);
+    lydict_remove(ctx, module_name);
+    lydict_remove(ctx, value);
 
     if (type->base == LY_TYPE_ERR) {
         /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less
@@ -588,7 +592,7 @@
             assert(((struct lys_node_grp *)siter)->unres_count);
             ((struct lys_node_grp *)siter)->unres_count--;
         } else {
-            LOGINT;
+            LOGINT(ctx);
             goto error;
         }
     }
@@ -596,7 +600,7 @@
 
     /* check status */
     if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
-                     type->der->flags, type->der->module, type->der->name,  parent)) {
+                         type->der->flags, type->der->module, type->der->name,  parent)) {
         return -1;
     }
 
@@ -604,22 +608,22 @@
     LY_TREE_FOR_SAFE(yin->child, next, node) {
         if (!node->ns) {
             /* garbage */
-            lyxml_free(module->ctx, node);
+            lyxml_free(ctx, node);
             continue;
         } else if (!strcmp(node->ns->value, LY_NSYIN)) {
             /* YANG (YIN) statements - process later */
             continue;
         }
 
-        YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, type->ext_size, "extensions", "type", error);
+        YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, type->ext_size, "extensions", "type", error);
 
-        lyxml_unlink_elem(module->ctx, node, 2);
-        lyxml_add_child(module->ctx, &exts, node);
+        lyxml_unlink_elem(ctx, node, 2);
+        lyxml_add_child(ctx, &exts, node);
         c_ext++;
     }
     if (c_ext) {
         type->ext = calloc(c_ext, sizeof *type->ext);
-        LY_CHECK_ERR_GOTO(!type->ext, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!type->ext, LOGMEM(ctx), error);
 
         LY_TREE_FOR_SAFE(exts.child, next, node) {
             rc = lyp_yin_fill_ext(type, LYEXT_PAR_TYPE, 0, 0, module, node, &type->ext, type->ext_size, unres);
@@ -637,11 +641,11 @@
         /* get bit specifications, at least one must be present */
         LY_TREE_FOR_SAFE(yin->child, next, node) {
             if (!strcmp(node->name, "bit")) {
-                YIN_CHECK_ARRAY_OVERFLOW_CODE(type->info.bits.count, type->info.bits.count, "bits", "type",
+                YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, type->info.bits.count, type->info.bits.count, "bits", "type",
                                               type->info.bits.count = 0; goto error);
                 type->info.bits.count++;
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 type->info.bits.count = 0;
                 goto error;
             }
@@ -650,7 +654,7 @@
         if (!dertype->der) {
             if (!type->info.bits.count) {
                 /* type is derived directly from buit-in bits type and bit statement is required */
-                LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
+                LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
                 goto error;
             }
         } else {
@@ -658,14 +662,14 @@
             if (module->version < 2 && type->info.bits.count) {
                 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
                  * since YANG 1.1 the bit statements can be used to restrict the base bits type */
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
                 type->info.bits.count = 0;
                 goto error;
             }
         }
 
         type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit);
-        LY_CHECK_ERR_GOTO(!type->info.bits.bit, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!type->info.bits.bit, LOGMEM(ctx), error);
 
         p = 0;
         i = -1;
@@ -673,12 +677,12 @@
             i++;
             c_ftrs = 0;
 
-            GETVAL(value, next, "name");
-            if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
+            GETVAL(ctx, value, next, "name");
+            if (lyp_check_identifier(ctx, value, LY_IDENT_SIMPLE, NULL, NULL)) {
                 goto error;
             }
 
-            type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value));
+            type->info.bits.bit[i].name = lydict_insert(ctx, value, strlen(value));
             if (read_yin_common(module, NULL, &type->info.bits.bit[i], LYEXT_PAR_TYPE_BIT, next, 0, unres)) {
                 type->info.bits.count = i + 1;
                 goto error;
@@ -688,7 +692,7 @@
                 /* check the name uniqueness */
                 for (j = 0; j < i; j++) {
                     if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) {
-                        LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
+                        LOGVAL(ctx, LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
                         type->info.bits.count = i + 1;
                         goto error;
                     }
@@ -702,7 +706,7 @@
                     }
                 }
                 if (j == dertype->info.bits.count) {
-                    LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, value);
+                    LOGVAL(ctx, LYE_BITS_INNAME, LY_VLOG_NONE, NULL, value);
                     type->info.bits.count = i + 1;
                     goto error;
                 }
@@ -717,22 +721,22 @@
                 } else if (strcmp(node->ns->value, LY_NSYIN)) {
                     /* extension */
                     if (lyp_yin_parse_subnode_ext(module, &type->info.bits.bit[i], LYEXT_PAR_TYPE_BIT, node,
-                                             LYEXT_SUBSTMT_SELF, 0, unres)) {
+                                                  LYEXT_SUBSTMT_SELF, 0, unres)) {
                         goto error;
                     }
                 } else if (!strcmp(node->name, "position")) {
                     if (p_ != -1) {
-                        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
+                        LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
                         type->info.bits.count = i + 1;
                         goto error;
                     }
 
-                    GETVAL(value, node, "value");
+                    GETVAL(ctx, value, node, "value");
                     p_ = strtoll(value, NULL, 10);
 
                     /* range check */
                     if (p_ < 0 || p_ > UINT32_MAX) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "bit/position");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "bit/position");
                         type->info.bits.count = i + 1;
                         goto error;
                     }
@@ -747,7 +751,7 @@
                             /* check that the value is unique */
                             for (j = 0; j < i; j++) {
                                 if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) {
-                                    LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL,
+                                    LOGVAL(ctx, LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL,
                                            type->info.bits.bit[i].pos, type->info.bits.bit[i].name,
                                            type->info.bits.bit[j].name);
                                     type->info.bits.count = i + 1;
@@ -762,10 +766,10 @@
                         goto error;
                     }
                 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
-                    YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, type->info.bits.bit[i].iffeature_size, "if-features", "bit", error);
+                    YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, type->info.bits.bit[i].iffeature_size, "if-features", "bit", error);
                     c_ftrs++;
                 } else {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                     goto error;
                 }
             }
@@ -774,7 +778,7 @@
                 if (p_ == -1) {
                     /* assign value automatically */
                     if (p > UINT32_MAX) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
                         type->info.bits.count = i + 1;
                         goto error;
                     }
@@ -793,7 +797,7 @@
                     if (p_ != bits_sc[j].pos) {
                         /* p_ - assigned position in restricted bits
                          * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
-                        LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos,
+                        LOGVAL(ctx, LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos,
                                type->info.bits.bit[i].name, bits_sc[j].pos);
                         type->info.bits.count = i + 1;
                         goto error;
@@ -806,7 +810,7 @@
                 bits_sc = &type->info.bits.bit[i];
                 bits_sc->iffeature = calloc(c_ftrs, sizeof *bits_sc->iffeature);
                 if (!bits_sc->iffeature) {
-                    LOGMEM;
+                    LOGMEM(ctx);
                     type->info.bits.count = i + 1;
                     goto error;
                 }
@@ -842,14 +846,14 @@
 
             if (!strcmp(node->name, "range")) {
                 if (type->info.dec64.range) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
 
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
                 type->info.dec64.range = calloc(1, sizeof *type->info.dec64.range);
-                LY_CHECK_ERR_GOTO(!type->info.dec64.range, LOGMEM, error);
-                type->info.dec64.range->expr = lydict_insert(module->ctx, value, 0);
+                LY_CHECK_ERR_GOTO(!type->info.dec64.range, LOGMEM(ctx), error);
+                type->info.dec64.range->expr = lydict_insert(ctx, value, 0);
 
                 /* get possible substatements */
                 if (read_restr_substmt(module, type->info.dec64.range, node, unres)) {
@@ -857,15 +861,15 @@
                 }
             } else if (!strcmp(node->name, "fraction-digits")) {
                 if (type->info.dec64.dig) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
                 v = strtol(value, NULL, 10);
 
                 /* range check */
                 if (v < 1 || v > 18) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                     goto error;
                 }
                 type->info.dec64.dig = (uint8_t)v;
@@ -879,7 +883,7 @@
                     goto error;
                 }
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
@@ -887,12 +891,12 @@
         /* mandatory sub-statement(s) check */
         if (!type->info.dec64.dig && !type->der->type.der) {
             /* decimal64 type directly derived from built-in type requires fraction-digits */
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
             goto error;
         }
         if (type->info.dec64.dig && type->der->type.der) {
             /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
             goto error;
         }
 
@@ -902,8 +906,8 @@
             type->info.dec64.div = type->der->type.info.dec64.div;
         }
 
-        if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) {
-            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "range");
+        if (type->info.dec64.range && lyp_check_length_range(ctx, type->info.dec64.range->expr, type)) {
+            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "range");
             goto error;
         }
         break;
@@ -915,11 +919,11 @@
         LY_TREE_FOR_SAFE(yin->child, next, node) {
 
             if (!strcmp(node->name, "enum")) {
-                YIN_CHECK_ARRAY_OVERFLOW_CODE(type->info.enums.count, type->info.enums.count, "enums", "type",
+                YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, type->info.enums.count, type->info.enums.count, "enums", "type",
                                               type->info.enums.count = 0; goto error);
                 type->info.enums.count++;
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 type->info.enums.count = 0;
                 goto error;
             }
@@ -928,7 +932,7 @@
         if (!dertype->der) {
             if (!type->info.enums.count) {
                 /* type is derived directly from buit-in enumeartion type and enum statement is required */
-                LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
+                LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
                 goto error;
             }
         } else {
@@ -936,14 +940,14 @@
             if (module->version < 2 && type->info.enums.count) {
                 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
                  * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
                 type->info.enums.count = 0;
                 goto error;
             }
         }
 
         type->info.enums.enm = calloc(type->info.enums.count, sizeof *type->info.enums.enm);
-        LY_CHECK_ERR_GOTO(!type->info.enums.enm, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!type->info.enums.enm, LOGMEM(ctx), error);
 
         v = 0;
         i = -1;
@@ -951,13 +955,13 @@
             i++;
             c_ftrs = 0;
 
-            GETVAL(value, next, "name");
+            GETVAL(ctx, value, next, "name");
             if (!value[0]) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
                 goto error;
             }
-            type->info.enums.enm[i].name = lydict_insert(module->ctx, value, strlen(value));
+            type->info.enums.enm[i].name = lydict_insert(ctx, value, strlen(value));
             if (read_yin_common(module, NULL, &type->info.enums.enm[i], LYEXT_PAR_TYPE_ENUM, next, 0, unres)) {
                 type->info.enums.count = i + 1;
                 goto error;
@@ -966,7 +970,7 @@
             /* the assigned name MUST NOT have any leading or trailing whitespace characters */
             value = type->info.enums.enm[i].name;
             if (isspace(value[0]) || isspace(value[strlen(value) - 1])) {
-                LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, value);
+                LOGVAL(ctx, LYE_ENUM_WS, LY_VLOG_NONE, NULL, value);
                 type->info.enums.count = i + 1;
                 goto error;
             }
@@ -975,7 +979,7 @@
                 /* check the name uniqueness */
                 for (j = 0; j < i; j++) {
                     if (ly_strequal(type->info.enums.enm[j].name, value, 1)) {
-                        LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, value);
+                        LOGVAL(ctx, LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, value);
                         type->info.enums.count = i + 1;
                         goto error;
                     }
@@ -989,7 +993,7 @@
                     }
                 }
                 if (j == dertype->info.enums.count) {
-                    LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, value);
+                    LOGVAL(ctx, LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, value);
                     type->info.enums.count = i + 1;
                     goto error;
                 }
@@ -1008,17 +1012,17 @@
                     }
                 } else if (!strcmp(node->name, "value")) {
                     if (val_set) {
-                        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
+                        LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
                         type->info.enums.count = i + 1;
                         goto error;
                     }
 
-                    GETVAL(value, node, "value");
+                    GETVAL(ctx, value, node, "value");
                     v_ = strtoll(value, NULL, 10);
 
                     /* range check */
                     if (v_ < INT32_MIN || v_ > INT32_MAX) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum/value");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "enum/value");
                         type->info.enums.count = i + 1;
                         goto error;
                     }
@@ -1038,7 +1042,7 @@
                                 /* check that the value is unique */
                                 for (j = 0; j < i; j++) {
                                     if (type->info.enums.enm[j].value == type->info.enums.enm[i].value) {
-                                        LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
+                                        LOGVAL(ctx, LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
                                                type->info.enums.enm[i].value, type->info.enums.enm[i].name,
                                                type->info.enums.enm[j].name);
                                         type->info.enums.count = i + 1;
@@ -1055,11 +1059,11 @@
                         goto error;
                     }
                 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
-                    YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, type->info.enums.enm[i].iffeature_size, "if-features", "enum", error);
+                    YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, type->info.enums.enm[i].iffeature_size, "if-features", "enum", error);
                     c_ftrs++;
 
                 } else {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                     goto error;
                 }
             }
@@ -1068,7 +1072,7 @@
                 if (!val_set) {
                     /* assign value automatically */
                     if (v > INT32_MAX) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
                         type->info.enums.count = i + 1;
                         goto error;
                     }
@@ -1087,7 +1091,7 @@
                     if (v_ != enms_sc[j].value) {
                         /* v_ - assigned value in restricted enum
                          * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
-                        LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL,
+                        LOGVAL(ctx, LYE_ENUM_INVAL, LY_VLOG_NONE, NULL,
                                type->info.enums.enm[i].value, type->info.enums.enm[i].name, enms_sc[j].value);
                         type->info.enums.count = i + 1;
                         goto error;
@@ -1100,7 +1104,7 @@
                 enms_sc = &type->info.enums.enm[i];
                 enms_sc->iffeature = calloc(c_ftrs, sizeof *enms_sc->iffeature);
                 if (!enms_sc->iffeature) {
-                    LOGMEM;
+                    LOGMEM(ctx);
                     type->info.enums.count = i + 1;
                     goto error;
                 }
@@ -1128,18 +1132,18 @@
         LY_TREE_FOR_SAFE(yin->child, next, node) {
 
             if (strcmp(node->name, "base")) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
 
-            GETVAL(value, yin->child, "name");
+            GETVAL(ctx, value, yin->child, "name");
             /* store in the JSON format */
             value = transform_schema2json(module, value);
             if (!value) {
                 goto error;
             }
             rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
-            lydict_remove(module->ctx, value);
+            lydict_remove(ctx, value);
             if (rc == -1) {
                 goto error;
             }
@@ -1154,16 +1158,16 @@
                 /* this is just a derived type with no base required */
                 break;
             }
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
             goto error;
         } else {
             if (type->der->type.der) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
                 goto error;
             }
         }
         if (yin->child->next) {
-            LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, yin->child->next->name, yin->name);
+            LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, yin->child->next->name, yin->name);
             goto error;
         }
         break;
@@ -1174,16 +1178,16 @@
 
             if (!strcmp(node->name, "require-instance")) {
                 if (type->info.inst.req) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
                 if (!strcmp(value, "true")) {
                     type->info.inst.req = 1;
                 } else if (!strcmp(value, "false")) {
                     type->info.inst.req = -1;
                 } else {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                     goto error;
                 }
 
@@ -1192,7 +1196,7 @@
                     goto error;
                 }
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
@@ -1227,25 +1231,25 @@
 
             if (!strcmp(node->name, name)) {
                 if (*restrs) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
 
-                GETVAL(value, node, "value");
-                if (lyp_check_length_range(value, type)) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, name);
+                GETVAL(ctx, value, node, "value");
+                if (lyp_check_length_range(ctx, value, type)) {
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, name);
                     goto error;
                 }
                 *restrs = calloc(1, sizeof **restrs);
-                LY_CHECK_ERR_GOTO(!(*restrs), LOGMEM, error);
-                (*restrs)->expr = lydict_insert(module->ctx, value, 0);
+                LY_CHECK_ERR_GOTO(!(*restrs), LOGMEM(ctx), error);
+                (*restrs)->expr = lydict_insert(ctx, value, 0);
 
                 /* get possible substatements */
                 if (read_restr_substmt(module, *restrs, node, unres)) {
                     goto error;
                 }
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
@@ -1266,11 +1270,11 @@
 
             if (!strcmp(node->name, "path") && !type->der->type.der) {
                 if (type->info.lref.path) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
 
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
                 /* store in the JSON format */
                 type->info.lref.path = transform_schema2json(module, value);
                 if (!type->info.lref.path) {
@@ -1294,16 +1298,16 @@
                 }
             } else if (module->version >= 2 && !strcmp(node->name, "require-instance")) {
                 if (type->info.lref.req) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
                 if (!strcmp(value, "true")) {
                     type->info.lref.req = 1;
                 } else if (!strcmp(value, "false")) {
                     type->info.lref.req = -1;
                 } else {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                     goto error;
                 }
 
@@ -1312,18 +1316,18 @@
                     goto error;
                 }
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
 
         if (!type->info.lref.path) {
             if (!type->der->type.der) {
-                LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
+                LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
                 goto error;
             } else {
                 /* copy leafref definition into the derived type */
-                type->info.lref.path = lydict_insert(module->ctx, type->der->type.info.lref.path, 0);
+                type->info.lref.path = lydict_insert(ctx, type->der->type.info.lref.path, 0);
                 /* and resolve the path at the place we are (if not in grouping/typedef) */
                 if (!parenttype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
                     goto error;
@@ -1341,29 +1345,29 @@
 
             if (!strcmp(node->name, "length")) {
                 if (type->info.str.length) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                     goto error;
                 }
 
-                GETVAL(value, node, "value");
-                if (lyp_check_length_range(value, type)) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "length");
+                GETVAL(ctx, value, node, "value");
+                if (lyp_check_length_range(ctx, value, type)) {
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "length");
                     goto error;
                 }
                 type->info.str.length = calloc(1, sizeof *type->info.str.length);
-                LY_CHECK_ERR_GOTO(!type->info.str.length, LOGMEM, error);
-                type->info.str.length->expr = lydict_insert(module->ctx, value, 0);
+                LY_CHECK_ERR_GOTO(!type->info.str.length, LOGMEM(ctx), error);
+                type->info.str.length->expr = lydict_insert(ctx, value, 0);
 
                 /* get possible sub-statements */
                 if (read_restr_substmt(module, type->info.str.length, node, unres)) {
                     goto error;
                 }
-                lyxml_free(module->ctx, node);
+                lyxml_free(ctx, node);
             } else if (!strcmp(node->name, "pattern")) {
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(i, type->info.str.pat_count, "patterns", "type", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, i, type->info.str.pat_count, "patterns", "type", error);
                 i++;
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
@@ -1373,21 +1377,21 @@
                 in_grp = 1;
             }
             type->info.str.patterns = calloc(i, sizeof *type->info.str.patterns);
-            LY_CHECK_ERR_GOTO(!type->info.str.patterns, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!type->info.str.patterns, LOGMEM(ctx), error);
 #ifdef LY_ENABLED_CACHE
             if (!in_grp) {
                 /* do not compile patterns in groupings */
                 type->info.str.patterns_pcre = calloc(2 * i, sizeof *type->info.str.patterns_pcre);
-                LY_CHECK_ERR_GOTO(!type->info.str.patterns_pcre, LOGMEM, error);
+                LY_CHECK_ERR_GOTO(!type->info.str.patterns_pcre, LOGMEM(ctx), error);
             }
 #endif
             LY_TREE_FOR(yin->child, node) {
-                GETVAL(value, node, "value");
+                GETVAL(ctx, value, node, "value");
 
-                if (!(module->ctx->models.flags & LY_CTX_TRUSTED)) {
+                if (!(ctx->models.flags & LY_CTX_TRUSTED)) {
                     if (in_grp) {
                         /* in grouping, just check the pattern syntax */
-                        if (lyp_check_pattern(value, NULL)) {
+                        if (lyp_check_pattern(ctx, value, NULL)) {
                             goto error;
                         }
                     }
@@ -1411,15 +1415,15 @@
                     LY_TREE_FOR_SAFE(node->child, next2, child) {
                         if (child->ns && !strcmp(child->ns->value, LY_NSYIN) && !strcmp(child->name, "modifier")) {
                             if (name) {
-                                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
+                                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
                                 goto error;
                             }
 
-                            GETVAL(name, child, "value");
+                            GETVAL(ctx, name, child, "value");
                             if (!strcmp(name, "invert-match")) {
                                 modifier = 0x15; /* NACK */
                             } else {
-                                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
+                                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
                                 goto error;
                             }
                             /* get extensions of the modifier */
@@ -1428,18 +1432,18 @@
                                 goto error;
                             }
 
-                            lyxml_free(module->ctx, child);
+                            lyxml_free(ctx, child);
                         }
                     }
                 }
 
                 len = strlen(value);
                 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
-                LY_CHECK_ERR_GOTO(!buf, LOGMEM, error);
+                LY_CHECK_ERR_GOTO(!buf, LOGMEM(ctx), error);
                 buf[0] = modifier;
                 strcpy(&buf[1], value);
 
-                restr->expr = lydict_insert_zc(module->ctx, buf);
+                restr->expr = lydict_insert_zc(ctx, buf);
 
                 /* get possible sub-statements */
                 if (read_restr_substmt(module, restr, node, unres)) {
@@ -1458,19 +1462,19 @@
             if (!strcmp(node->name, "type")) {
                 if (type->der->type.der) {
                     /* type can be a substatement only in "union" type, not in derived types */
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "derived type");
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "derived type");
                     goto error;
                 }
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(i, type->info.uni.count, "types", "type", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, i, type->info.uni.count, "types", "type", error);
                 i++;
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
                 goto error;
             }
         }
 
         if (!i && !type->der->type.der) {
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
             goto error;
         }
 
@@ -1482,7 +1486,7 @@
         /* allocate array for union's types ... */
         if (i) {
             type->info.uni.types = calloc(i, sizeof *type->info.uni.types);
-            LY_CHECK_ERR_GOTO(!type->info.uni.types, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!type->info.uni.types, LOGMEM(ctx), error);
         }
 
         /* ... and fill the structures */
@@ -1495,10 +1499,10 @@
                 if (module->version < 2) {
                     /* union's type cannot be empty or leafref */
                     if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_EMPTY) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", node->name);
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, "empty", node->name);
                         rc = -1;
                     } else if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", node->name);
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, "leafref", node->name);
                         rc = -1;
                     }
                 }
@@ -1511,7 +1515,7 @@
             if (rc) {
                 /* even if we got EXIT_FAILURE, throw it all away, too much trouble doing something else */
                 for (i = 0; i < type->info.uni.count; ++i) {
-                    lys_type_free(module->ctx, &type->info.uni.types[i], NULL);
+                    lys_type_free(ctx, &type->info.uni.types[i], NULL);
                 }
                 free(type->info.uni.types);
                 type->info.uni.types = NULL;
@@ -1532,21 +1536,20 @@
     case LY_TYPE_EMPTY:
         /* no sub-statement allowed */
         if (yin->child) {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, yin->child->name);
             goto error;
         }
         break;
 
     default:
-        LOGINT;
+        LOGINT(ctx);
         goto error;
     }
 
     return EXIT_SUCCESS;
 
 error:
-    lyxml_free_withsiblings(module->ctx, exts.child);
-
+    lyxml_free_withsiblings(ctx, exts.child);
     return ret;
 }
 
@@ -1557,14 +1560,15 @@
 {
     const char *value;
     struct lyxml_elem *node, *next;
+    struct ly_ctx *ctx = module->ctx;
     int rc, has_type = 0, c_ext = 0;
     void *reallocated;
 
-    GETVAL(value, yin, "name");
-    if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent)) {
+    GETVAL(ctx, value, yin, "name");
+    if (lyp_check_identifier(ctx, value, LY_IDENT_TYPE, module, parent)) {
         goto error;
     }
-    tpdf->name = lydict_insert(module->ctx, value, strlen(value));
+    tpdf->name = lydict_insert(ctx, value, strlen(value));
 
     /* generic part - status, description, reference */
     if (read_yin_common(module, NULL, tpdf, LYEXT_PAR_TPDF, yin, OPT_MODULE, unres)) {
@@ -1574,12 +1578,12 @@
     LY_TREE_FOR_SAFE(yin->child, next, node) {
         if (strcmp(node->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, tpdf->ext_size, "extensions", "typedef", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, tpdf->ext_size, "extensions", "typedef", error);
             c_ext++;
             continue;
         } else if (!strcmp(node->name, "type")) {
             if (has_type) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                 goto error;
             }
             /* HACK for unres */
@@ -1594,43 +1598,43 @@
             continue;
         } else if (!strcmp(node->name, "default")) {
             if (tpdf->dflt) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                 goto error;
             }
-            GETVAL(value, node, "value");
-            tpdf->dflt = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, node, "value");
+            tpdf->dflt = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, tpdf, LYEXT_PAR_TPDF, node, LYEXT_SUBSTMT_DEFAULT, 0, unres)) {
                 goto error;
             }
         } else if (!strcmp(node->name, "units")) {
             if (tpdf->units) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                 goto error;
             }
-            GETVAL(value, node, "name");
-            tpdf->units = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, node, "name");
+            tpdf->units = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, tpdf, LYEXT_PAR_TPDF, node, LYEXT_SUBSTMT_UNITS, 0, unres)) {
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, value);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, value);
             goto error;
         }
 
-        lyxml_free(module->ctx, node);
+        lyxml_free(ctx, node);
     }
 
     /* check mandatory value */
     if (!has_type) {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
         goto error;
     }
 
     /* check default value (if not defined, there still could be some restrictions
      * that need to be checked against a default value from a derived type) */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) &&
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) &&
             unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPEDEF_DFLT, (struct lys_node *)(&tpdf->dflt)) == -1) {
         goto error;
     }
@@ -1639,7 +1643,7 @@
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(tpdf->ext, (c_ext + tpdf->ext_size) * sizeof *tpdf->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         tpdf->ext = reallocated;
 
         /* init memory */
@@ -1657,24 +1661,24 @@
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
 static int
 fill_yin_extension(struct lys_module *module, struct lyxml_elem *yin, struct lys_ext *ext, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     const char *value;
     struct lyxml_elem *child, *node, *next, *next2;
     int c_ext = 0, rc;
     void *reallocated;
 
-    GETVAL(value, yin, "name");
+    GETVAL(ctx, value, yin, "name");
 
-    if (lyp_check_identifier(value, LY_IDENT_EXTENSION, module, NULL)) {
+    if (lyp_check_identifier(ctx, value, LY_IDENT_EXTENSION, module, NULL)) {
         goto error;
     }
-    ext->name = lydict_insert(module->ctx, value, strlen(value));
+    ext->name = lydict_insert(ctx, value, strlen(value));
 
     if (read_yin_common(module, NULL, ext, LYEXT_PAR_EXT, yin, OPT_MODULE, unres)) {
         goto error;
@@ -1683,12 +1687,12 @@
     LY_TREE_FOR_SAFE(yin->child, next, node) {
         if (strcmp(node->ns->value, LY_NSYIN)) {
             /* possible extension instance */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, ext->ext_size, "extensions", "extension", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, ext->ext_size, "extensions", "extension", error);
             c_ext++;
         } else if (!strcmp(node->name, "argument")) {
             /* argument */
-            GETVAL(value, node, "name");
-            ext->argument = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, node, "name");
+            ext->argument = lydict_insert(ctx, value, strlen(value));
             if (lyp_yin_parse_subnode_ext(module, ext, LYEXT_PAR_EXT, node, LYEXT_SUBSTMT_ARGUMENT, 0, unres)) {
                 goto error;
             }
@@ -1696,7 +1700,7 @@
             /* yin-element */
             LY_TREE_FOR_SAFE(node->child, next2, child) {
                 if (child->ns == node->ns && !strcmp(child->name, "yin-element")) {
-                    GETVAL(value, child, "value");
+                    GETVAL(ctx, value, child, "value");
                     if (ly_strequal(value, "true", 0)) {
                         ext->flags |= LYS_YINELEM;
                     }
@@ -1706,15 +1710,15 @@
                     }
                 } else if (child->ns) {
                     /* unexpected YANG statement */
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->name);
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->name);
                     goto error;
                 } /* else garbage, but save resource needed for unlinking */
             }
 
-            lyxml_free(module->ctx, node);
+            lyxml_free(ctx, node);
         } else {
             /* unexpected YANG statement */
-            LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->name);
+            LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->name);
             goto error;
         }
     }
@@ -1722,7 +1726,7 @@
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(ext->ext, (c_ext + ext->ext_size) * sizeof *ext->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         ext->ext = reallocated;
 
         /* init memory */
@@ -1751,16 +1755,17 @@
 static int
 fill_yin_feature(struct lys_module *module, struct lyxml_elem *yin, struct lys_feature *f, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     const char *value;
     struct lyxml_elem *child, *next;
     int c_ftrs = 0, c_ext = 0, ret;
     void *reallocated;
 
-    GETVAL(value, yin, "name");
-    if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) {
+    GETVAL(ctx, value, yin, "name");
+    if (lyp_check_identifier(ctx, value, LY_IDENT_FEATURE, module, NULL)) {
         goto error;
     }
-    f->name = lydict_insert(module->ctx, value, strlen(value));
+    f->name = lydict_insert(ctx, value, strlen(value));
     f->module = module;
 
     if (read_yin_common(module, NULL, f, LYEXT_PAR_FEATURE, yin, 0, unres)) {
@@ -1770,25 +1775,25 @@
     LY_TREE_FOR(yin->child, child) {
         if (strcmp(child->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, f->ext_size, "extensions", "feature", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, f->ext_size, "extensions", "feature", error);
             c_ext++;
         } else if (!strcmp(child->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, f->iffeature_size, "if-feature", "feature", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, f->iffeature_size, "if-feature", "feature", error);
             c_ftrs++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
 
     if (c_ftrs) {
         f->iffeature = calloc(c_ftrs, sizeof *f->iffeature);
-        LY_CHECK_ERR_GOTO(!f->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!f->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(f->ext, (c_ext + f->ext_size) * sizeof *f->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         f->ext = reallocated;
 
         /* init memory */
@@ -1822,7 +1827,6 @@
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
@@ -1832,7 +1836,7 @@
 {
     const char *value;
 
-    GETVAL(value, yin, "condition");
+    GETVAL(module->ctx, value, yin, "condition");
     must->expr = transform_schema2json(module, value);
     if (!must->expr) {
         goto error;
@@ -1848,11 +1852,12 @@
 fill_yin_revision(struct lys_module *module, struct lyxml_elem *yin, struct lys_revision *rev,
                   struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *next, *child;
     const char *value;
 
-    GETVAL(value, yin, "date");
-    if (lyp_check_date(value)) {
+    GETVAL(ctx, value, yin, "date");
+    if (lyp_check_date(ctx, value)) {
         goto error;
     }
     memcpy(rev->date, value, LY_REV_SIZE - 1);
@@ -1869,32 +1874,32 @@
             }
         } else if (!strcmp(child->name, "description")) {
             if (rev->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, rev, LYEXT_PAR_REVISION,
                                           child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
                 goto error;
             }
-            rev->dsc = read_yin_subnode(module->ctx, child, "text");
+            rev->dsc = read_yin_subnode(ctx, child, "text");
             if (!rev->dsc) {
                 goto error;
             }
         } else if (!strcmp(child->name, "reference")) {
             if (rev->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, rev, LYEXT_PAR_REVISION,
                                           child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
                 goto error;
             }
-            rev->ref = read_yin_subnode(module->ctx, child, "text");
+            rev->ref = read_yin_subnode(ctx, child, "text");
             if (!rev->ref) {
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
@@ -1909,19 +1914,20 @@
 fill_yin_unique(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_unique *unique,
                 struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     int i, j, ret = EXIT_FAILURE;
     const char *orig;
     char *value, *vaux, *start = NULL, c;
     struct unres_list_uniq *unique_info;
 
     /* get unique value (list of leafs supposed to be unique */
-    GETVAL(orig, yin, "tag");
+    GETVAL(ctx, orig, yin, "tag");
 
     /* count the number of unique leafs in the value */
     start = value = vaux = strdup(orig);
-    LY_CHECK_ERR_GOTO(!vaux, LOGMEM, error);
+    LY_CHECK_ERR_GOTO(!vaux, LOGMEM(ctx), error);
     while ((vaux = strpbrk(vaux, " \t\n"))) {
-        YIN_CHECK_ARRAY_OVERFLOW_CODE(unique->expr_size, unique->expr_size, "referenced items", "unique",
+        YIN_CHECK_ARRAY_OVERFLOW_CODE(ctx, unique->expr_size, unique->expr_size, "referenced items", "unique",
                                       unique->expr_size = 0; goto error);
         unique->expr_size++;
         while (isspace(*vaux)) {
@@ -1930,7 +1936,7 @@
     }
     unique->expr_size++;
     unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
-    LY_CHECK_ERR_GOTO(!unique->expr, LOGMEM, error);
+    LY_CHECK_ERR_GOTO(!unique->expr, LOGMEM(ctx), error);
 
     for (i = 0; i < unique->expr_size; i++) {
         vaux = strpbrk(value, " \t\n");
@@ -1948,8 +1954,8 @@
         /* check that the expression does not repeat */
         for (j = 0; j < i; j++) {
             if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, unique->expr[i], "unique");
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, unique->expr[i], "unique");
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
                 goto error;
             }
         }
@@ -1957,7 +1963,7 @@
         /* try to resolve leaf */
         if (unres) {
             unique_info = malloc(sizeof *unique_info);
-            LY_CHECK_ERR_GOTO(!unique_info, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error);
             unique_info->list = parent;
             unique_info->expr = unique->expr[i];
             unique_info->trg_type = &unique->trg_type;
@@ -1972,7 +1978,7 @@
 
         /* move to next token */
         value = vaux;
-        while(value && isspace(*value)) {
+        while (value && isspace(*value)) {
             value++;
         }
     }
@@ -1995,10 +2001,11 @@
     char *endptr;
     unsigned long val;
     uint32_t *ui32val, *min, *max;
+    struct ly_ctx *ctx = target->module->ctx;
 
     /* del min/max is forbidden */
     if (d->mod == LY_DEVIATE_DEL) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, (type ? "max-elements" : "min-elements"), "deviate delete");
+        LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, (type ? "max-elements" : "min-elements"), "deviate delete");
         goto error;
     }
 
@@ -2010,12 +2017,12 @@
         max = &((struct lys_node_list *)target)->max;
         min = &((struct lys_node_list *)target)->min;
     } else {
-        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
-        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", node->name);
+        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", node->name);
         goto error;
     }
 
-    GETVAL(value, node, "value");
+    GETVAL(ctx, value, node, "value");
     while (isspace(value[0])) {
         value++;
     }
@@ -2030,7 +2037,7 @@
         endptr = NULL;
         val = strtoul(value, &endptr, 10);
         if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
-            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
             goto error;
         }
         if (type) {
@@ -2047,8 +2054,8 @@
     if (d->mod == LY_DEVIATE_ADD) {
         /* check that there is no current value */
         if (*ui32val) {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
-            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
+            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
             goto error;
         }
     } else if (d->mod == LY_DEVIATE_RPL) {
@@ -2063,11 +2070,11 @@
     /* check min-elements is smaller than max-elements */
     if (*max && *min > *max) {
         if (type) {
-            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "max-elements");
-            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
+            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "max-elements");
+            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
         } else {
-            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "min-elements");
-            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
+            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "min-elements");
+            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
         }
         goto error;
     }
@@ -2075,7 +2082,6 @@
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
@@ -2090,7 +2096,7 @@
     int f_min = 0, f_max = 0; /* flags */
     int i, j, k, rc;
     unsigned int u;
-    struct ly_ctx *ctx;
+    struct ly_ctx *ctx = module->ctx;
     struct lys_deviate *d = NULL;
     struct lys_node *node = NULL, *parent, *dev_target = NULL;
     struct lys_node_choice *choice = NULL;
@@ -2105,9 +2111,7 @@
     struct lys_module *mod;
     void *reallocated;
 
-    ctx = module->ctx;
-
-    GETVAL(value, yin, "target-node");
+    GETVAL(ctx, value, yin, "target-node");
     dev->target_name = transform_schema2json(module, value);
     if (!dev->target_name) {
         goto error;
@@ -2116,7 +2120,7 @@
     /* resolve target node */
     rc = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
     if (rc == -1) {
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
+        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
         ly_set_free(set);
         goto error;
     }
@@ -2124,8 +2128,8 @@
     ly_set_free(set);
 
     if (dev_target->module == lys_main_module(module)) {
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
-        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
+        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
+        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
         goto error;
     }
 
@@ -2136,12 +2140,12 @@
             continue;
         } else if (strcmp(child->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, dev->ext_size, "extensions", "deviation", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, dev->ext_size, "extensions", "deviation", error);
             c_ext++;
             continue;
         } else if (!strcmp(child->name, "description")) {
             if (dev->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, dev, LYEXT_PAR_DEVIATION, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
@@ -2153,7 +2157,7 @@
             }
         } else if (!strcmp(child->name, "reference")) {
             if (dev->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, dev, LYEXT_PAR_DEVIATION, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
@@ -2164,7 +2168,7 @@
                 goto error;
             }
         } else if (!strcmp(child->name, "deviate")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_dev, dev->deviate_size, "deviates", "deviation", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_dev, dev->deviate_size, "deviates", "deviation", error);
             c_dev++;
 
             /* skip lyxml_free() at the end of the loop, node will be
@@ -2173,7 +2177,7 @@
             continue;
 
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
 
@@ -2182,15 +2186,15 @@
 
     if (c_dev) {
         dev->deviate = calloc(c_dev, sizeof *dev->deviate);
-        LY_CHECK_ERR_GOTO(!dev->deviate, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!dev->deviate, LOGMEM(ctx), error);
     } else {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "deviate", "deviation");
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "deviate", "deviation");
         goto error;
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(dev->ext, (c_ext + dev->ext_size) * sizeof *dev->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         dev->ext = reallocated;
 
         /* init memory */
@@ -2218,15 +2222,15 @@
         c_ext = 0;
 
         /* get deviation type */
-        GETVAL(value, develem, "value");
+        GETVAL(ctx, value, develem, "value");
         if (!strcmp(value, "not-supported")) {
             dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO;
             /* no other deviate statement is expected,
              * not-supported deviation must be the only deviation of the target
              */
             if (dev->deviate_size || develem->next) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
                 goto error;
             }
 
@@ -2234,8 +2238,8 @@
             if ((dev_target->nodetype == LYS_LEAF) && lys_parent(dev_target) && (lys_parent(dev_target)->nodetype == LYS_LIST)) {
                 for (i = 0; i < ((struct lys_node_list *)lys_parent(dev_target))->keys_size; ++i) {
                     if (((struct lys_node_list *)lys_parent(dev_target))->keys[i] == (struct lys_node_leaf *)dev_target) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
                         goto error;
                     }
                 }
@@ -2260,7 +2264,7 @@
         } else if (!strcmp(value, "delete")) {
             dev->deviate[dev->deviate_size].mod = LY_DEVIATE_DEL;
         } else {
-            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
+            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
             goto error;
         }
         d = &dev->deviate[dev->deviate_size];
@@ -2272,7 +2276,7 @@
             dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, &tmp_unres, 1);
             /* just to be safe */
             if (tmp_unres.count) {
-                LOGINT;
+                LOGINT(ctx);
                 goto error;
             }
         }
@@ -2285,15 +2289,15 @@
                 continue;
             } else if  (strcmp(child->ns->value, LY_NSYIN)) {
                 /* extensions */
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, d->ext_size, "extensions", "deviate", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, d->ext_size, "extensions", "deviate", error);
                 c_ext++;
             } else if (d->mod == LY_DEVIATE_NO) {
                 /* no YIN substatement expected in this case */
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
                 goto error;
             } else if (!strcmp(child->name, "config")) {
                 if (d->flags & LYS_CONFIG_MASK) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
 
@@ -2302,19 +2306,19 @@
                  * that config is specified in every node. But for delete, we check that the value
                  * is the same as here in deviation
                  */
-                GETVAL(value, child, "value");
+                GETVAL(ctx, value, child, "value");
                 if (!strcmp(value, "false")) {
                     d->flags |= LYS_CONFIG_R;
                 } else if (!strcmp(value, "true")) {
                     d->flags |= LYS_CONFIG_W;
                 } else {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
                     goto error;
                 }
 
                 if (d->mod == LY_DEVIATE_DEL) {
                     /* del config is forbidden */
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "config", "deviate delete");
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "config", "deviate delete");
                     goto error;
                 } else { /* add and replace are the same in this case */
                     /* remove current config value of the target ... */
@@ -2331,21 +2335,21 @@
                 if (lyp_yin_parse_subnode_ext(module, d, LYEXT_PAR_DEVIATE, child, LYEXT_SUBSTMT_DEFAULT, c_dflt, unres)) {
                     goto error;
                 }
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_dflt, d->dflt_size, "defaults", "deviate", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_dflt, d->dflt_size, "defaults", "deviate", error);
                 c_dflt++;
 
                 /* check target node type */
                 if (module->version < 2 && dev_target->nodetype == LYS_LEAFLIST) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
                     goto error;
                 } else if (c_dflt > 1 && dev_target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
                     goto error;
                 } else if (c_dflt == 1 && (!(dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE)))) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
                     goto error;
                 }
 
@@ -2354,32 +2358,32 @@
 
             } else if (!strcmp(child->name, "mandatory")) {
                 if (d->flags & LYS_MAND_MASK) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
 
                 /* check target node type */
                 if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
                     goto error;
                 }
 
-                GETVAL(value, child, "value");
+                GETVAL(ctx, value, child, "value");
                 if (!strcmp(value, "false")) {
                     d->flags |= LYS_MAND_FALSE;
                 } else if (!strcmp(value, "true")) {
                     d->flags |= LYS_MAND_TRUE;
                 } else {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
                     goto error;
                 }
 
                 if (d->mod == LY_DEVIATE_ADD) {
                     /* check that there is no current value */
                     if (dev_target->flags & LYS_MAND_MASK) {
-                        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
+                        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
                         goto error;
                     }
 
@@ -2387,15 +2391,15 @@
                     if (d->flags & LYS_MAND_TRUE) {
                         if (dev_target->nodetype == LYS_CHOICE) {
                             if (((struct lys_node_choice *)(dev_target))->dflt) {
-                                LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
-                                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
+                                LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
+                                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
                                        "Adding the \"mandatory\" statement is forbidden on choice with the \"default\" statement.");
                                 goto error;
                             }
                         } else if (dev_target->nodetype == LYS_LEAF) {
                             if (((struct lys_node_leaf *)(dev_target))->dflt) {
-                                LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
-                                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
+                                LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
+                                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
                                        "Adding the \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
                                 goto error;
                             }
@@ -2406,8 +2410,8 @@
                 } else if (d->mod == LY_DEVIATE_RPL) {
                     /* check that there was a value before */
                     if (!(dev_target->flags & LYS_MAND_MASK)) {
-                        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
+                        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
                         goto error;
                     }
 
@@ -2415,7 +2419,7 @@
                     dev_target->flags |= d->flags & LYS_MAND_MASK;
                 } else if (d->mod == LY_DEVIATE_DEL) {
                     /* del mandatory is forbidden */
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "deviate delete");
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "deviate delete");
                     goto error;
                 }
 
@@ -2440,7 +2444,7 @@
                 }
             } else if (!strcmp(child->name, "min-elements")) {
                 if (f_min) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
                 f_min = 1;
@@ -2453,7 +2457,7 @@
                 }
             } else if (!strcmp(child->name, "max-elements")) {
                 if (f_max) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
                 f_max = 1;
@@ -2465,22 +2469,22 @@
                     goto error;
                 }
             } else if (!strcmp(child->name, "must")) {
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, d->must_size, "musts", "deviate", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, d->must_size, "musts", "deviate", error);
                 c_must++;
                 /* skip lyxml_free() at the end of the loop, this node will be processed later */
                 continue;
             } else if (!strcmp(child->name, "type")) {
                 if (d->type) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
 
                 /* add, del type is forbidden */
                 if (d->mod == LY_DEVIATE_ADD) {
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate add");
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate add");
                     goto error;
                 } else if (d->mod == LY_DEVIATE_DEL) {
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate delete");
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate delete");
                     goto error;
                 }
 
@@ -2496,8 +2500,8 @@
                         ly_set_add(dflt_check, dev_target, 0);
                     }
                 } else {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
                     goto error;
                 }
 
@@ -2515,13 +2519,13 @@
                 if (lyp_yin_parse_subnode_ext(module, d, LYEXT_PAR_DEVIATE, child, LYEXT_SUBSTMT_UNIQUE, c_uniq, unres)) {
                     goto error;
                 }
-                YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_uniq, d->unique_size, "uniques", "deviate", error);
+                YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_uniq, d->unique_size, "uniques", "deviate", error);
                 c_uniq++;
                 /* skip lyxml_free() at the end of the loop, this node will be processed later */
                 continue;
             } else if (!strcmp(child->name, "units")) {
                 if (d->units) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                     goto error;
                 }
 
@@ -2531,21 +2535,21 @@
                 } else if (dev_target->nodetype == LYS_LEAF) {
                     stritem = &((struct lys_node_leaf *)dev_target)->units;
                 } else {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
                     goto error;
                 }
 
                 /* get units value */
-                GETVAL(value, child, "name");
+                GETVAL(ctx, value, child, "name");
                 d->units = lydict_insert(ctx, value, 0);
 
                 /* apply to target */
                 if (d->mod == LY_DEVIATE_ADD) {
                     /* check that there is no current value */
                     if (*stritem) {
-                        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
+                        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
                         goto error;
                     }
 
@@ -2553,8 +2557,8 @@
                 } else if (d->mod == LY_DEVIATE_RPL) {
                     /* check that there was a value before */
                     if (!*stritem) {
-                        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
+                        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
                         goto error;
                     }
 
@@ -2563,8 +2567,8 @@
                 } else if (d->mod == LY_DEVIATE_DEL) {
                     /* check values */
                     if (!ly_strequal(*stritem, d->units, 1)) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
                         goto error;
                     }
                     /* remove current units value of the target */
@@ -2583,7 +2587,7 @@
                     goto error;
                 }
             } else {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
                 goto error;
             }
 
@@ -2615,8 +2619,8 @@
                 trg_must_size = &((struct lys_node_anydata *)dev_target)->must_size;
                 break;
             default:
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
                 goto error;
             }
 
@@ -2624,31 +2628,31 @@
 
             if (d->mod == LY_DEVIATE_RPL) {
                 /* replace must is forbidden */
-                LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "must", "deviate replace");
+                LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "must", "deviate replace");
                 goto error;
             } else if (d->mod == LY_DEVIATE_ADD) {
                 /* reallocate the must array of the target */
                 d->must = ly_realloc(*trg_must, (c_must + *trg_must_size) * sizeof *d->must);
-                LY_CHECK_ERR_GOTO(!d->must, LOGMEM, error);
+                LY_CHECK_ERR_GOTO(!d->must, LOGMEM(ctx), error);
                 *trg_must = d->must;
                 d->must = &((*trg_must)[*trg_must_size]);
                 d->must_size = c_must;
             } else { /* LY_DEVIATE_DEL */
                 d->must = calloc(c_must, sizeof *d->must);
             }
-            LY_CHECK_ERR_GOTO(!d->must, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!d->must, LOGMEM(ctx), error);
         }
         if (c_uniq) {
             /* replace unique is forbidden */
             if (d->mod == LY_DEVIATE_RPL) {
-                LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "unique", "deviate replace");
+                LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "unique", "deviate replace");
                 goto error;
             }
 
             /* check target node type */
             if (dev_target->nodetype != LYS_LIST) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
                 goto error;
             }
 
@@ -2656,13 +2660,13 @@
             if (d->mod == LY_DEVIATE_ADD) {
                 /* reallocate the unique array of the target */
                 d->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *d->unique);
-                LY_CHECK_ERR_GOTO(!d->unique, LOGMEM, error);
+                LY_CHECK_ERR_GOTO(!d->unique, LOGMEM(ctx), error);
                 list->unique = d->unique;
                 d->unique = &list->unique[list->unique_size];
                 d->unique_size = c_uniq;
             } else { /* LY_DEVIATE_DEL */
                 d->unique = calloc(c_uniq, sizeof *d->unique);
-                LY_CHECK_ERR_GOTO(!d->unique, LOGMEM, error);
+                LY_CHECK_ERR_GOTO(!d->unique, LOGMEM(ctx), error);
             }
         }
         if (c_dflt) {
@@ -2670,16 +2674,16 @@
                 /* check that there is no current value */
                 if ((dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) ||
                         (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt)) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
                     goto error;
                 }
 
                 /* check collision with mandatory/min-elements */
                 if ((dev_target->flags & LYS_MAND_TRUE) ||
                         (dev_target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev_target)->min)) {
-                    LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
+                    LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
                            "Adding the \"default\" statement is forbidden on %s statement.",
                            (dev_target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\"");
                     goto error;
@@ -2688,8 +2692,8 @@
                 /* check that there was a value before */
                 if (((dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev_target)->dflt) ||
                         (dev_target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev_target)->dflt)) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
+                    LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
                     goto error;
                 }
             }
@@ -2700,24 +2704,24 @@
                 if (d->mod == LY_DEVIATE_ADD) {
                     /* reallocate (enlarge) the unique array of the target */
                     llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *d->dflt);
-                    LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, error);
+                    LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
                 } else if (d->mod == LY_DEVIATE_RPL) {
                     /* reallocate (replace) the unique array of the target */
                     for (i = 0; i < llist->dflt_size; i++) {
-                        lydict_remove(llist->module->ctx, llist->dflt[i]);
+                        lydict_remove(ctx, llist->dflt[i]);
                     }
                     llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *d->dflt);
                     llist->dflt_size = 0;
-                    LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, error);
+                    LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
                 }
             }
             d->dflt = calloc(c_dflt, sizeof *d->dflt);
-            LY_CHECK_ERR_GOTO(!d->dflt, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!d->dflt, LOGMEM(ctx), error);
         }
         if (c_ext) {
             /* some extensions may be already present from the substatements */
             reallocated = realloc(d->ext, (c_ext + d->ext_size) * sizeof *d->ext);
-            LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
             d->ext = reallocated;
 
             /* init memory */
@@ -2770,9 +2774,9 @@
                     d->must_size++;
                     if (i != -1) {
                         /* no match found */
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL,
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL,
                                d->must[d->must_size - 1].expr, child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
                         goto error;
                     }
                 } else { /* replace or add */
@@ -2838,8 +2842,8 @@
                     d->unique_size++;
                     if (i != -1) {
                         /* no match found */
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, lyxml_get_attr(child, "tag", NULL), child->name);
-                        LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, lyxml_get_attr(child, "tag", NULL), child->name);
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
                         goto error;
                     }
 
@@ -2863,21 +2867,21 @@
                     }
                 }
             } else if (!strcmp(child->name, "default")) {
-                GETVAL(value, child, "value");
+                GETVAL(ctx, value, child, "value");
                 u = strlen(value);
-                d->dflt[d->dflt_size++] = lydict_insert(module->ctx, value, u);
+                d->dflt[d->dflt_size++] = lydict_insert(ctx, value, u);
 
                 if (dev_target->nodetype == LYS_CHOICE) {
                     choice = (struct lys_node_choice *)dev_target;
                     rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node);
                     if (rc || !node) {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
                         goto error;
                     }
                     if (d->mod == LY_DEVIATE_DEL) {
                         if (!choice->dflt || (choice->dflt != node)) {
-                            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
+                            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
                             goto error;
                         }
                         choice->dflt = NULL;
@@ -2891,7 +2895,7 @@
                         choice->dflt = node;
                         if (!choice->dflt) {
                             /* default branch not found */
-                            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
                             goto error;
                         }
                     }
@@ -2899,8 +2903,8 @@
                     leaf = (struct lys_node_leaf *)dev_target;
                     if (d->mod == LY_DEVIATE_DEL) {
                         if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) {
-                            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
+                            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
                             goto error;
                         }
                         /* remove value */
@@ -2932,7 +2936,7 @@
                         for (i = 0; i < llist->dflt_size; i++) {
                             if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) {
                                 /* match, remove the value */
-                                lydict_remove(llist->module->ctx, llist->dflt[i]);
+                                lydict_remove(ctx, llist->dflt[i]);
                                 llist->dflt[i] = NULL;
 
                                 /* remove extensions of this default instance from the target node */
@@ -2950,8 +2954,8 @@
                             }
                         }
                         if (i == llist->dflt_size) {
-                            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                            LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
+                            LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                            LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
                             goto error;
                         }
                     } else {
@@ -2960,13 +2964,13 @@
                         /* we just want to check that the value isn't already in the list */
                         for (i = 0; i < llist->dflt_size; i++) {
                             if (ly_strequal(llist->dflt[i], value, 1)) {
-                                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
+                                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
                                 goto error;
                             }
                         }
                         /* store it in target node */
-                        llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, u);
+                        llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u);
 
                         /* remember to check it later (it may not fit now, but the type can be deviated too) */
                         ly_set_add(dflt_check, dev_target, 0);
@@ -2990,7 +2994,7 @@
     }
 
     /* now check whether default value, if any, matches the type */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED)) {
         for (u = 0; u < dflt_check->number; ++u) {
             value = NULL;
             rc = EXIT_SUCCESS;
@@ -3011,8 +3015,8 @@
 
             }
             if (rc == -1) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
                     "The default value \"%s\" of the deviated node \"%s\" no longer matches its type.",
                     dev->target_name);
                 goto error;
@@ -3027,7 +3031,7 @@
             mod->deviated = 1;            /* main module */
             parent->module->deviated = 1; /* possible submodule */
             if (lys_set_implemented(mod)) {
-                LOGERR(ly_errno, "Setting the deviated module \"%s\" implemented failed.", mod->name);
+                LOGERR(ctx, ly_errno, "Setting the deviated module \"%s\" implemented failed.", mod->name);
                 goto error;
             }
         }
@@ -3046,6 +3050,7 @@
 fill_yin_augment(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_node_augment *aug,
                  int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     const char *value;
     struct lyxml_elem *sub, *next;
     struct lys_node *node;
@@ -3053,7 +3058,7 @@
     void *reallocated;
 
     aug->nodetype = LYS_AUGMENT;
-    GETVAL(value, yin, "target-node");
+    GETVAL(ctx, value, yin, "target-node");
     aug->target_name = transform_schema2json(module, value);
     if (!aug->target_name) {
         goto error;
@@ -3067,25 +3072,25 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, aug->ext_size, "extensions", "augment", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, aug->ext_size, "extensions", "augment", error);
             c_ext++;
             continue;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, aug->iffeature_size, "if-features", "augment", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, aug->iffeature_size, "if-features", "augment", error);
             c_ftrs++;
             continue;
         } else if (!strcmp(sub->name, "when")) {
             if (aug->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
 
             aug->when = read_yin_when(module, sub, unres);
             if (!aug->when) {
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
             continue;
 
         /* check allowed data sub-statements */
@@ -3112,7 +3117,7 @@
         } else if (!strcmp(sub->name, "notification")) {
             node = read_yin_notif(module, (struct lys_node *)aug, sub, options, unres);
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
             goto error;
         }
 
@@ -3121,17 +3126,17 @@
         }
 
         node = NULL;
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     if (c_ftrs) {
         aug->iffeature = calloc(c_ftrs, sizeof *aug->iffeature);
-        LY_CHECK_ERR_GOTO(!aug->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!aug->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(aug->ext, (c_ext + aug->ext_size) * sizeof *aug->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         aug->ext = reallocated;
 
         /* init memory */
@@ -3152,7 +3157,7 @@
             if (ret) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         }
     }
 
@@ -3169,7 +3174,7 @@
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && aug->when) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && aug->when) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax((struct lys_node *)aug)) {
                 goto error;
@@ -3184,7 +3189,6 @@
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
@@ -3192,6 +3196,7 @@
 static int
 fill_yin_refine(struct lys_node *uses, struct lyxml_elem *yin, struct lys_refine *rfn, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = uses->module->ctx;
     struct lys_module *module;
     struct lyxml_elem *sub, *next;
     const char *value;
@@ -3205,7 +3210,7 @@
     assert(uses);
     module = uses->module; /* shorthand */
 
-    GETVAL(value, yin, "target-node");
+    GETVAL(ctx, value, yin, "target-node");
     rfn->target_name = transform_schema2json(module, value);
     if (!rfn->target_name) {
         goto error;
@@ -3216,13 +3221,13 @@
             /* garbage */
         } else if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, rfn->ext_size, "extensions", "refine", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, rfn->ext_size, "extensions", "refine", error);
             c_ext++;
             continue;
 
         } else if (!strcmp(sub->name, "description")) {
             if (rfn->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
 
@@ -3230,13 +3235,13 @@
                 goto error;
             }
 
-            rfn->dsc = read_yin_subnode(module->ctx, sub, "text");
+            rfn->dsc = read_yin_subnode(ctx, sub, "text");
             if (!rfn->dsc) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "reference")) {
             if (rfn->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
 
@@ -3244,22 +3249,22 @@
                 goto error;
             }
 
-            rfn->ref = read_yin_subnode(module->ctx, sub, "text");
+            rfn->ref = read_yin_subnode(ctx, sub, "text");
             if (!rfn->ref) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "config")) {
             if (rfn->flags & LYS_CONFIG_MASK) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "false")) {
                 rfn->flags |= LYS_CONFIG_R;
             } else if (!strcmp(value, "true")) {
                 rfn->flags |= LYS_CONFIG_W;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                 goto error;
             }
             rfn->flags |= LYS_CONFIG_SET;
@@ -3275,7 +3280,7 @@
                 if (c_dflt) {
                     /* multiple defaults are allowed only in leaf-list */
                     if (module->version < 2) {
-                        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                        LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                         goto error;
                     }
                     rfn->target_type &= LYS_LEAFLIST;
@@ -3288,8 +3293,8 @@
                     }
                 }
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
@@ -3301,7 +3306,7 @@
                 }
             }
 
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_dflt, rfn->dflt_size, "defaults", "refine", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_dflt, rfn->dflt_size, "defaults", "refine", error);
             if (lyp_yin_parse_subnode_ext(module, rfn, LYEXT_PAR_REFINE, sub, LYEXT_SUBSTMT_DEFAULT, c_dflt, unres)) {
                 goto error;
             }
@@ -3310,7 +3315,7 @@
         } else if (!strcmp(sub->name, "mandatory")) {
             /* leaf, choice or anyxml */
             if (f_mand) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in leaf is not sufficient, we would allow
@@ -3322,21 +3327,21 @@
             if (rfn->target_type) {
                 rfn->target_type &= (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA);
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_LEAF | LYS_CHOICE | LYS_ANYDATA;
             }
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "true")) {
                 rfn->flags |= LYS_MAND_TRUE;
             } else if (!strcmp(value, "false")) {
                 rfn->flags |= LYS_MAND_FALSE;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, rfn, LYEXT_PAR_REFINE, sub, LYEXT_SUBSTMT_MANDATORY, 0, unres)) {
@@ -3345,7 +3350,7 @@
         } else if (!strcmp(sub->name, "min-elements")) {
             /* list or leaf-list */
             if (f_min) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
             f_min = 1;
@@ -3354,15 +3359,15 @@
             if (rfn->target_type) {
                 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
             }
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -3372,7 +3377,7 @@
             endptr = NULL;
             val = strtoul(value, &endptr, 10);
             if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                 goto error;
             }
             rfn->mod.list.min = (uint32_t) val;
@@ -3384,7 +3389,7 @@
         } else if (!strcmp(sub->name, "max-elements")) {
             /* list or leaf-list */
             if (f_max) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
             f_max = 1;
@@ -3393,15 +3398,15 @@
             if (rfn->target_type) {
                 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
             }
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -3414,7 +3419,7 @@
                 endptr = NULL;
                 val = strtoul(value, &endptr, 10);
                 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                     goto error;
                 }
                 rfn->mod.list.max = (uint32_t) val;
@@ -3427,7 +3432,7 @@
         } else if (!strcmp(sub->name, "presence")) {
             /* container */
             if (rfn->mod.presence) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
                 goto error;
             }
 
@@ -3435,16 +3440,16 @@
             if (rfn->target_type) {
                 rfn->target_type &= LYS_CONTAINER;
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_CONTAINER;
             }
 
-            GETVAL(value, sub, "value");
-            rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, sub, "value");
+            rfn->mod.presence = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, rfn, LYEXT_PAR_REFINE, sub, LYEXT_SUBSTMT_PRESENCE, 0, unres)) {
                 goto error;
@@ -3455,15 +3460,15 @@
             if (rfn->target_type) {
                 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
             }
 
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, rfn->must_size, "musts", "refine", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, rfn->must_size, "musts", "refine", error);
             c_must++;
             continue;
 
@@ -3473,42 +3478,42 @@
             if (rfn->target_type) {
                 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
                 if (!rfn->target_type) {
-                    LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
+                    LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
                     goto error;
                 }
             } else {
                 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
             }
 
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, rfn->iffeature_size, "if-feature", "refine", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, rfn->iffeature_size, "if-feature", "refine", error);
             c_ftrs++;
             continue;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     /* process nodes with cardinality of 0..n */
     if (c_must) {
         rfn->must = calloc(c_must, sizeof *rfn->must);
-        LY_CHECK_ERR_GOTO(!rfn->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!rfn->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         rfn->iffeature = calloc(c_ftrs, sizeof *rfn->iffeature);
-        LY_CHECK_ERR_GOTO(!rfn->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!rfn->iffeature, LOGMEM(ctx), error);
     }
     if (c_dflt) {
         rfn->dflt = calloc(c_dflt, sizeof *rfn->dflt);
-        LY_CHECK_ERR_GOTO(!rfn->dflt, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!rfn->dflt, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(rfn->ext, (c_ext + rfn->ext_size) * sizeof *rfn->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         rfn->ext = reallocated;
 
         /* init memory */
@@ -3536,24 +3541,23 @@
                 goto error;
             }
         } else { /* default */
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
 
             /* check for duplicity */
             for (r = 0; r < rfn->dflt_size; r++) {
                 if (ly_strequal(rfn->dflt[r], value, 1)) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
-                    LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
                     goto error;
                 }
             }
-            rfn->dflt[rfn->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
+            rfn->dflt[rfn->dflt_size++] = lydict_insert(ctx, value, strlen(value));
         }
     }
 
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
@@ -3561,6 +3565,7 @@
 static int
 fill_yin_import(struct lys_module *module, struct lyxml_elem *yin, struct lys_import *imp, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *child, *next, exts;
     const char *value;
     int r, c_ext = 0;
@@ -3575,27 +3580,27 @@
             continue;
         } else if (strcmp(child->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, imp->ext_size, "extensions", "import", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, imp->ext_size, "extensions", "import", error);
             c_ext++;
-            lyxml_unlink_elem(module->ctx, child, 2);
-            lyxml_add_child(module->ctx, &exts, child);
+            lyxml_unlink_elem(ctx, child, 2);
+            lyxml_add_child(ctx, &exts, child);
         } else if (!strcmp(child->name, "prefix")) {
-            GETVAL(value, child, "value");
-            if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
+            GETVAL(ctx, value, child, "value");
+            if (lyp_check_identifier(ctx, value, LY_IDENT_PREFIX, module, NULL)) {
                 goto error;
             }
-            imp->prefix = lydict_insert(module->ctx, value, strlen(value));
+            imp->prefix = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, imp, LYEXT_PAR_IMPORT, child, LYEXT_SUBSTMT_PREFIX, 0, unres)) {
                 goto error;
             }
         } else if (!strcmp(child->name, "revision-date")) {
             if (imp->rev[0]) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
-            GETVAL(value, child, "date");
-            if (lyp_check_date(value)) {
+            GETVAL(ctx, value, child, "date");
+            if (lyp_check_date(ctx, value)) {
                 goto error;
             }
             memcpy(imp->rev, value, LY_REV_SIZE - 1);
@@ -3605,37 +3610,37 @@
             }
         } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
             if (imp->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, imp, LYEXT_PAR_IMPORT, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
                 goto error;
             }
-            imp->dsc = read_yin_subnode(module->ctx, child, "text");
+            imp->dsc = read_yin_subnode(ctx, child, "text");
             if (!imp->dsc) {
                 goto error;
             }
         } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
             if (imp->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, imp, LYEXT_PAR_IMPORT, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
                 goto error;
             }
-            imp->ref = read_yin_subnode(module->ctx, child, "text");
+            imp->ref = read_yin_subnode(ctx, child, "text");
             if (!imp->ref) {
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
 
     /* check mandatory information */
     if (!imp->prefix) {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", yin->name);
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", yin->name);
         goto error;
     }
 
@@ -3643,7 +3648,7 @@
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(imp->ext, (c_ext + imp->ext_size) * sizeof *imp->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         imp->ext = reallocated;
 
         /* init memory */
@@ -3659,13 +3664,12 @@
         }
     }
 
-    GETVAL(value, yin, "module");
+    GETVAL(ctx, value, yin, "module");
     return lyp_check_import(module, value, imp);
 
 error:
-
     while (exts.child) {
-        lyxml_free(module->ctx, exts.child);
+        lyxml_free(ctx, exts.child);
     }
     return EXIT_FAILURE;
 }
@@ -3679,6 +3683,7 @@
 fill_yin_include(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
                  struct lys_include *inc, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *child, *next, exts;
     const char *value;
     int r, c_ext = 0;
@@ -3693,17 +3698,17 @@
             continue;
         } else if (strcmp(child->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, inc->ext_size, "extensions", "include", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, inc->ext_size, "extensions", "include", error);
             c_ext++;
-            lyxml_unlink_elem(module->ctx, child, 2);
-            lyxml_add_child(module->ctx, &exts, child);
+            lyxml_unlink_elem(ctx, child, 2);
+            lyxml_add_child(ctx, &exts, child);
         } else if (!strcmp(child->name, "revision-date")) {
             if (inc->rev[0]) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "revision-date", yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, "revision-date", yin->name);
                 goto error;
             }
-            GETVAL(value, child, "date");
-            if (lyp_check_date(value)) {
+            GETVAL(ctx, value, child, "date");
+            if (lyp_check_date(ctx, value)) {
                 goto error;
             }
             memcpy(inc->rev, value, LY_REV_SIZE - 1);
@@ -3713,30 +3718,30 @@
             }
         } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
             if (inc->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, inc, LYEXT_PAR_INCLUDE, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
                 goto error;
             }
-            inc->dsc = read_yin_subnode(module->ctx, child, "text");
+            inc->dsc = read_yin_subnode(ctx, child, "text");
             if (!inc->dsc) {
                 goto error;
             }
         } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
             if (inc->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, inc, LYEXT_PAR_INCLUDE, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
                 goto error;
             }
-            inc->ref = read_yin_subnode(module->ctx, child, "text");
+            inc->ref = read_yin_subnode(ctx, child, "text");
             if (!inc->ref) {
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
@@ -3745,7 +3750,7 @@
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(inc->ext, (c_ext + inc->ext_size) * sizeof *inc->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         inc->ext = reallocated;
 
         /* init memory */
@@ -3761,11 +3766,10 @@
         }
     }
 
-    GETVAL(value, yin, "module");
+    GETVAL(ctx, value, yin, "module");
     return lyp_check_include(submodule ? (struct lys_module *)submodule : module, value, inc, unres);
 
 error:
-
     return -1;
 }
 
@@ -3790,8 +3794,8 @@
     }
 
     if (opt & OPT_IDENT) {
-        GETVAL(value, xmlnode, "name");
-        if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
+        GETVAL(ctx, value, xmlnode, "name");
+        if (lyp_check_identifier(ctx, value, LY_IDENT_NAME, NULL, NULL)) {
             goto error;
         }
         node->name = lydict_insert(ctx, value, strlen(value));
@@ -3811,7 +3815,7 @@
 
         if (!strcmp(sub->name, "description")) {
             if (node->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
                 goto error;
             }
 
@@ -3825,7 +3829,7 @@
             }
         } else if (!strcmp(sub->name, "reference")) {
             if (node->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
                 goto error;
             }
 
@@ -3839,10 +3843,10 @@
             }
         } else if (!strcmp(sub->name, "status")) {
             if (node->flags & LYS_STATUS_MASK) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
                 goto error;
             }
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "current")) {
                 node->flags |= LYS_STATUS_CURR;
             } else if (!strcmp(value, "deprecated")) {
@@ -3850,7 +3854,7 @@
             } else if (!strcmp(value, "obsolete")) {
                 node->flags |= LYS_STATUS_OBSLT;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                 goto error;
             }
 
@@ -3860,16 +3864,16 @@
         } else if ((opt & (OPT_CFG_PARSE | OPT_CFG_IGNORE)) && !strcmp(sub->name, "config")) {
             if (opt & OPT_CFG_PARSE) {
                 if (node->flags & LYS_CONFIG_MASK) {
-                    LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
+                    LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
                     goto error;
                 }
-                GETVAL(value, sub, "value");
+                GETVAL(ctx, value, sub, "value");
                 if (!strcmp(value, "false")) {
                     node->flags |= LYS_CONFIG_R;
                 } else if (!strcmp(value, "true")) {
                     node->flags |= LYS_CONFIG_W;
                 } else {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
                     goto error;
                 }
                 node->flags |= LYS_CONFIG_SET;
@@ -3909,7 +3913,8 @@
             } else {
                 str = lys_path(parent, LYS_PATH_FIRST_PREFIX);
             }
-            LOGWRN("Missing status in %s subtree (%s), inheriting.", parent->flags & LYS_STATUS_DEPRC ? "deprecated" : "obsolete", str);
+            LOGWRN(ctx, "Missing status in %s subtree (%s), inheriting.",
+                   parent->flags & LYS_STATUS_DEPRC ? "deprecated" : "obsolete", str);
             free(str);
             node->flags |= parent->flags & LYS_STATUS_MASK;
         } else if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
@@ -3917,11 +3922,11 @@
             switch (node->flags & LYS_STATUS_MASK) {
             case 0:
             case LYS_STATUS_CURR:
-                LOGVAL(LYE_INSTATUS, LY_VLOG_LYS, parent, "current", xmlnode->name, "is child of",
+                LOGVAL(ctx, LYE_INSTATUS, LY_VLOG_LYS, parent, "current", xmlnode->name, "is child of",
                        parent->flags & LYS_STATUS_DEPRC ? "deprecated" : "obsolete", parent->name);
                 break;
             case LYS_STATUS_DEPRC:
-                LOGVAL(LYE_INSTATUS, LY_VLOG_LYS, parent, "deprecated", xmlnode->name, "is child of",
+                LOGVAL(ctx, LYE_INSTATUS, LY_VLOG_LYS, parent, "deprecated", xmlnode->name, "is child of",
                        "obsolete", parent->name);
                 break;
             }
@@ -3932,7 +3937,6 @@
     return EXIT_SUCCESS;
 
 error:
-
     return EXIT_FAILURE;
 }
 
@@ -3940,14 +3944,15 @@
 static struct lys_when *
 read_yin_when(struct lys_module *module, struct lyxml_elem *yin, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lys_when *retval = NULL;
     struct lyxml_elem *child, *next;
     const char *value;
 
     retval = calloc(1, sizeof *retval);
-    LY_CHECK_ERR_RETURN(!retval, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL);
 
-    GETVAL(value, yin, "condition");
+    GETVAL(ctx, value, yin, "condition");
     retval->cond = transform_schema2json(module, value);
     if (!retval->cond) {
         goto error;
@@ -3964,7 +3969,7 @@
             }
         } else if (!strcmp(child->name, "description")) {
             if (retval->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
 
@@ -3972,13 +3977,13 @@
                 goto error;
             }
 
-            retval->dsc = read_yin_subnode(module->ctx, child, "text");
+            retval->dsc = read_yin_subnode(ctx, child, "text");
             if (!retval->dsc) {
                 goto error;
             }
         } else if (!strcmp(child->name, "reference")) {
             if (retval->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
 
@@ -3986,12 +3991,12 @@
                 goto error;
             }
 
-            retval->ref = read_yin_subnode(module->ctx, child, "text");
+            retval->ref = read_yin_subnode(ctx, child, "text");
             if (!retval->ref) {
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
@@ -3999,8 +4004,7 @@
     return retval;
 
 error:
-
-    lys_when_free(module->ctx, retval, NULL);
+    lys_when_free(ctx, retval, NULL);
     return NULL;
 }
 
@@ -4009,6 +4013,7 @@
 read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node_case *cs;
     struct lys_node *retval, *node = NULL;
@@ -4019,7 +4024,7 @@
     memset(&root, 0, sizeof root);
 
     cs = calloc(1, sizeof *cs);
-    LY_CHECK_ERR_RETURN(!cs, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!cs, LOGMEM(ctx), NULL);
     cs->nodetype = LYS_CASE;
     cs->prev = (struct lys_node *)cs;
     retval = (struct lys_node *)cs;
@@ -4040,7 +4045,7 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "case", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "case", error);
             c_ext++;
         } else if (!strcmp(sub->name, "container") ||
                 !strcmp(sub->name, "leaf-list") ||
@@ -4051,14 +4056,14 @@
                 !strcmp(sub->name, "anyxml") ||
                 !strcmp(sub->name, "anydata")) {
 
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "case", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "case", error);
             c_ftrs++;
         } else if (!strcmp(sub->name, "when")) {
             if (cs->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
@@ -4067,21 +4072,21 @@
                 goto error;
             }
 
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
 
     if (c_ftrs) {
         cs->iffeature = calloc(c_ftrs, sizeof *cs->iffeature);
-        LY_CHECK_ERR_GOTO(!cs->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!cs->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -4129,11 +4134,11 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && cs->when) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && cs->when) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -4148,9 +4153,8 @@
     return retval;
 
 error:
-
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
     lys_node_free(retval, NULL, 0);
 
@@ -4171,7 +4175,7 @@
     void *reallocated;
 
     choice = calloc(1, sizeof *choice);
-    LY_CHECK_ERR_RETURN(!choice, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!choice, LOGMEM(ctx), NULL);
 
     choice->nodetype = LYS_CHOICE;
     choice->prev = (struct lys_node *)choice;
@@ -4195,7 +4199,7 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "choice", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "choice", error);
             c_ext++;
             /* keep it for later processing, skip lyxml_free() */
             continue;
@@ -4229,7 +4233,7 @@
             }
         } else if (!strcmp(sub->name, "default")) {
             if (dflt) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
@@ -4244,7 +4248,7 @@
 
         } else if (!strcmp(sub->name, "mandatory")) {
             if (f_mand) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in leaf is not sufficient, we would allow
@@ -4252,13 +4256,13 @@
              */
             f_mand = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "true")) {
                 choice->flags |= LYS_MAND_TRUE;
             } else if (!strcmp(value, "false")) {
                 choice->flags |= LYS_MAND_FALSE;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             }                   /* else false is the default value, so we can ignore it */
 
@@ -4267,7 +4271,7 @@
             }
         } else if (!strcmp(sub->name, "when")) {
             if (choice->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
@@ -4276,7 +4280,7 @@
                 goto error;
             }
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "choice", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "choice", error);
             c_ftrs++;
 
             /* skip lyxml_free() at the end of the loop, the sub node is processed later */
@@ -4286,7 +4290,7 @@
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
 
@@ -4296,12 +4300,12 @@
 
     if (c_ftrs) {
         choice->iffeature = calloc(c_ftrs, sizeof *choice->iffeature);
-        LY_CHECK_ERR_GOTO(!choice->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!choice->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -4327,14 +4331,14 @@
 
     /* check - default is prohibited in combination with mandatory */
     if (dflt && (choice->flags & LYS_MAND_TRUE)) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "default", "choice");
-        LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\".");
+        LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "default", "choice");
+        LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\".");
         goto error;
     }
 
     /* link default with the case */
     if (dflt) {
-        GETVAL(value, dflt, "value");
+        GETVAL(ctx, value, dflt, "value");
         if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) {
             goto error;
         }
@@ -4342,7 +4346,7 @@
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && choice->when) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && choice->when) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -4357,10 +4361,8 @@
     return retval;
 
 error:
-
     lyxml_free(ctx, dflt);
     lys_node_free(retval, NULL, 0);
-
     return NULL;
 }
 
@@ -4369,6 +4371,7 @@
 read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, LYS_NODE type,
                  int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lys_node *retval;
     struct lys_node_anydata *anyxml;
     struct lyxml_elem *sub, *next;
@@ -4379,7 +4382,7 @@
     void *reallocated;
 
     anyxml = calloc(1, sizeof *anyxml);
-    LY_CHECK_ERR_RETURN(!anyxml, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!anyxml, LOGMEM(ctx), NULL);
 
     anyxml->nodetype = type;
     anyxml->prev = (struct lys_node *)anyxml;
@@ -4387,8 +4390,7 @@
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
             OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
-                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
-            unres)) {
+            (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT), unres)) {
         goto error;
     }
 
@@ -4402,11 +4404,11 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "anydata", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "anydata", error);
             c_ext++;
         } else if (!strcmp(sub->name, "mandatory")) {
             if (f_mand) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in leaf is not sufficient, we would allow
@@ -4414,13 +4416,13 @@
              */
             f_mand = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "true")) {
                 anyxml->flags |= LYS_MAND_TRUE;
             } else if (!strcmp(value, "false")) {
                 anyxml->flags |= LYS_MAND_FALSE;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             }
             /* else false is the default value, so we can ignore it */
@@ -4428,28 +4430,28 @@
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MANDATORY, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "when")) {
             if (anyxml->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
             anyxml->when = read_yin_when(module, sub, unres);
             if (!anyxml->when) {
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, anyxml->must_size, "musts", "anydata", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, anyxml->must_size, "musts", "anydata", error);
             c_must++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "anydata", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "anydata", error);
             c_ftrs++;
 
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -4457,16 +4459,16 @@
     /* middle part - process nodes with cardinality of 0..n */
     if (c_must) {
         anyxml->must = calloc(c_must, sizeof *anyxml->must);
-        LY_CHECK_ERR_GOTO(!anyxml->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!anyxml->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         anyxml->iffeature = calloc(c_ftrs, sizeof *anyxml->iffeature);
-        LY_CHECK_ERR_GOTO(!anyxml->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!anyxml->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -4497,7 +4499,7 @@
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && (anyxml->when || anyxml->must)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && (anyxml->when || anyxml->must)) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -4512,9 +4514,7 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
-
     return NULL;
 }
 
@@ -4523,6 +4523,7 @@
 read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lys_node *retval;
     struct lys_node_leaf *leaf;
     struct lyxml_elem *sub, *next;
@@ -4532,7 +4533,7 @@
     void *reallocated;
 
     leaf = calloc(1, sizeof *leaf);
-    LY_CHECK_ERR_RETURN(!leaf, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!leaf, LOGMEM(ctx), NULL);
 
     leaf->nodetype = LYS_LEAF;
     leaf->prev = (struct lys_node *)leaf;
@@ -4555,12 +4556,12 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "leaf", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "leaf", error);
             c_ext++;
             continue;
         } else if (!strcmp(sub->name, "type")) {
             if (has_type) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* HACK for unres */
@@ -4571,29 +4572,29 @@
             has_type = 1;
         } else if (!strcmp(sub->name, "default")) {
             if (leaf->dflt) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
-            GETVAL(value, sub, "value");
-            leaf->dflt = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, sub, "value");
+            leaf->dflt = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_DEFAULT, 0, unres)) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "units")) {
             if (leaf->units) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
-            GETVAL(value, sub, "name");
-            leaf->units = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, sub, "name");
+            leaf->units = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_UNITS, 0, unres)) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "mandatory")) {
             if (f_mand) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in leaf is not sufficient, we would allow
@@ -4601,13 +4602,13 @@
              */
             f_mand = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "true")) {
                 leaf->flags |= LYS_MAND_TRUE;
             } else if (!strcmp(value, "false")) {
                 leaf->flags |= LYS_MAND_FALSE;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             }                   /* else false is the default value, so we can ignore it */
 
@@ -4616,7 +4617,7 @@
             }
         } else if (!strcmp(sub->name, "when")) {
             if (leaf->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
@@ -4626,16 +4627,16 @@
             }
 
         } else if (!strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, leaf->must_size, "musts", "leaf", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, leaf->must_size, "musts", "leaf", error);
             c_must++;
             continue;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "musts", "leaf", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "musts", "leaf", error);
             c_ftrs++;
             continue;
 
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
 
@@ -4644,12 +4645,12 @@
 
     /* check mandatory parameters */
     if (!has_type) {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
         goto error;
     }
     if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "mandatory", "leaf");
-        LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
+        LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "mandatory", "leaf");
+        LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
                "The \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
         goto error;
     }
@@ -4657,16 +4658,16 @@
     /* middle part - process nodes with cardinality of 0..n */
     if (c_must) {
         leaf->must = calloc(c_must, sizeof *leaf->must);
-        LY_CHECK_ERR_GOTO(!leaf->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!leaf->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         leaf->iffeature = calloc(c_ftrs, sizeof *leaf->iffeature);
-        LY_CHECK_ERR_GOTO(!leaf->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!leaf->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -4704,14 +4705,14 @@
 
     /* check default value (if not defined, there still could be some restrictions
      * that need to be checked against a default value from a derived type) */
-    if (!(options & LYS_PARSE_OPT_INGRP) && !(module->ctx->models.flags & LY_CTX_TRUSTED) &&
+    if (!(options & LYS_PARSE_OPT_INGRP) && !(ctx->models.flags & LY_CTX_TRUSTED) &&
             (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
                                    (struct lys_node *)(&leaf->dflt)) == -1)) {
         goto error;
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && (leaf->when || leaf->must)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && (leaf->when || leaf->must)) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -4726,9 +4727,7 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
-
     return NULL;
 }
 
@@ -4737,6 +4736,7 @@
 read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                   struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lys_node *retval;
     struct lys_node_leaflist *llist;
     struct lyxml_elem *sub, *next;
@@ -4749,7 +4749,7 @@
     void *reallocated;
 
     llist = calloc(1, sizeof *llist);
-    LY_CHECK_ERR_RETURN(!llist, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!llist, LOGMEM(ctx), NULL);
 
     llist->nodetype = LYS_LEAFLIST;
     llist->prev = (struct lys_node *)llist;
@@ -4772,12 +4772,12 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "leaf-list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "leaf-list", error);
             c_ext++;
             continue;
         } else if (!strcmp(sub->name, "type")) {
             if (has_type) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* HACK for unres */
@@ -4788,18 +4788,18 @@
             has_type = 1;
         } else if (!strcmp(sub->name, "units")) {
             if (llist->units) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
-            GETVAL(value, sub, "name");
-            llist->units = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, sub, "name");
+            llist->units = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_UNITS, 0, unres)) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "ordered-by")) {
             if (f_ordr) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in llist is not sufficient, we would
@@ -4811,15 +4811,15 @@
                 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
                  * state data
                  */
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 continue;
             }
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "user")) {
                 llist->flags |= LYS_USERORDERED;
             } else if (strcmp(value, "system")) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             } /* else system is the default value, so we can ignore it */
 
@@ -4827,11 +4827,11 @@
                 goto error;
             }
         } else if (!strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, llist->must_size, "musts", "leaf-list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, llist->must_size, "musts", "leaf-list", error);
             c_must++;
             continue;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "leaf-list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "leaf-list", error);
             c_ftrs++;
             continue;
         } else if ((module->version >= 2) && !strcmp(sub->name, "default")) {
@@ -4840,18 +4840,18 @@
                 goto error;
             }
 
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_dflt, llist->dflt_size, "defaults", "leaf-list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_dflt, llist->dflt_size, "defaults", "leaf-list", error);
             c_dflt++;
             continue;
 
         } else if (!strcmp(sub->name, "min-elements")) {
             if (f_min) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             f_min = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -4861,13 +4861,13 @@
             endptr = NULL;
             val = strtoul(value, &endptr, 10);
             if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             }
             llist->min = (uint32_t) val;
             if (llist->max && (llist->min > llist->max)) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
                 goto error;
             }
 
@@ -4876,12 +4876,12 @@
             }
         } else if (!strcmp(sub->name, "max-elements")) {
             if (f_max) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             f_max = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -4894,13 +4894,13 @@
                 endptr = NULL;
                 val = strtoul(value, &endptr, 10);
                 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
-                    LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                     goto error;
                 }
                 llist->max = (uint32_t) val;
                 if (llist->min > llist->max) {
-                    LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
                     goto error;
                 }
             }
@@ -4910,7 +4910,7 @@
             }
         } else if (!strcmp(sub->name, "when")) {
             if (llist->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
@@ -4919,7 +4919,7 @@
                 goto error;
             }
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
 
@@ -4928,27 +4928,27 @@
 
     /* check constraints */
     if (!has_type) {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
         goto error;
     }
 
     /* middle part - process nodes with cardinality of 0..n */
     if (c_must) {
         llist->must = calloc(c_must, sizeof *llist->must);
-        LY_CHECK_ERR_GOTO(!llist->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!llist->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         llist->iffeature = calloc(c_ftrs, sizeof *llist->iffeature);
-        LY_CHECK_ERR_GOTO(!llist->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!llist->iffeature, LOGMEM(ctx), error);
     }
     if (c_dflt) {
         llist->dflt = calloc(c_dflt, sizeof *llist->dflt);
-        LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -4976,20 +4976,20 @@
                 goto error;
             }
         } else if (!strcmp(sub->name, "default")) {
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
 
             /* check for duplicity in case of configuration data,
              * in case of status data duplicities are allowed */
             if (llist->flags & LYS_CONFIG_W) {
                 for (r = 0; r < llist->dflt_size; r++) {
                     if (ly_strequal(llist->dflt[r], value, 1)) {
-                        LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, "default");
-                        LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Duplicated default value \"%s\".", value);
+                        LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, "default");
+                        LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Duplicated default value \"%s\".", value);
                         goto error;
                     }
                 }
             }
-            llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
+            llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, strlen(value));
         }
     }
 
@@ -5000,8 +5000,8 @@
     }
 
     if (llist->dflt_size && llist->min) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "min-elements", "leaf-list");
-        LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
+        LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "min-elements", "leaf-list");
+        LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
                "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
         goto error;
     }
@@ -5009,7 +5009,7 @@
     /* check default value (if not defined, there still could be some restrictions
      * that need to be checked against a default value from a derived type) */
     for (r = 0; r < llist->dflt_size; r++) {
-        if (!(options & LYS_PARSE_OPT_INGRP) && !(module->ctx->models.flags & LY_CTX_TRUSTED) &&
+        if (!(options & LYS_PARSE_OPT_INGRP) && !(ctx->models.flags & LY_CTX_TRUSTED) &&
                 (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
                                        (struct lys_node *)(&llist->dflt[r])) == -1)) {
             goto error;
@@ -5017,7 +5017,7 @@
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && (llist->when || llist->must)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && (llist->when || llist->must)) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -5032,9 +5032,7 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
-
     return NULL;
 }
 
@@ -5043,6 +5041,7 @@
 read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lys_node *retval, *node;
     struct lys_node_list *list;
     struct lyxml_elem *sub, *next, root, uniq;
@@ -5059,7 +5058,7 @@
     memset(&uniq, 0, sizeof uniq);
 
     list = calloc(1, sizeof *list);
-    LY_CHECK_ERR_RETURN(!list, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!list, LOGMEM(ctx), NULL);
 
     list->nodetype = LYS_LIST;
     list->prev = (struct lys_node *)list;
@@ -5083,7 +5082,7 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "list", error);
             c_ext++;
             continue;
 
@@ -5099,20 +5098,20 @@
                 !strcmp(sub->name, "anydata") ||
                 !strcmp(sub->name, "action") ||
                 !strcmp(sub->name, "notification")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "key")) {
             /* check cardinality 0..1 */
             if (list->keys_size) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, list->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, list->name);
                 goto error;
             }
 
             /* count the number of keys */
-            GETVAL(value, sub, "value");
-            list->keys_str = lydict_insert(module->ctx, value, 0);
+            GETVAL(ctx, value, sub, "value");
+            list->keys_str = lydict_insert(ctx, value, 0);
             while ((value = strpbrk(value, " \t\n"))) {
                 list->keys_size++;
                 while (isspace(*value)) {
@@ -5121,31 +5120,31 @@
             }
             list->keys_size++;
             list->keys = calloc(list->keys_size, sizeof *list->keys);
-            LY_CHECK_ERR_GOTO(!list->keys, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error);
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_KEY, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "unique")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_uniq, list->unique_size, "uniques", "list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_uniq, list->unique_size, "uniques", "list", error);
             c_uniq++;
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &uniq, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &uniq, sub);
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, list->tpdf_size, "typedefs", "list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, list->tpdf_size, "typedefs", "list", error);
             c_tpdf++;
         } else if (!strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, list->must_size, "musts", "list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, list->must_size, "musts", "list", error);
             c_must++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "list", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "list", error);
             c_ftrs++;
 
             /* optional stetments */
         } else if (!strcmp(sub->name, "ordered-by")) {
             if (f_ordr) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             /* just checking the flags in llist is not sufficient, we would
@@ -5157,30 +5156,30 @@
                 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
                  * state data
                  */
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 continue;
             }
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             if (!strcmp(value, "user")) {
                 list->flags |= LYS_USERORDERED;
             } else if (strcmp(value, "system")) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             } /* else system is the default value, so we can ignore it */
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_ORDEREDBY, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "min-elements")) {
             if (f_min) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             f_min = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -5190,28 +5189,28 @@
             auxs = NULL;
             val = strtoul(value, &auxs, 10);
             if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                 goto error;
             }
             list->min = (uint32_t) val;
             if (list->max && (list->min > list->max)) {
-                LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
-                lyxml_free(module->ctx, sub);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
+                lyxml_free(ctx, sub);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MIN, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "max-elements")) {
             if (f_max) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             f_max = 1;
 
-            GETVAL(value, sub, "value");
+            GETVAL(ctx, value, sub, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -5224,34 +5223,34 @@
                 auxs = NULL;
                 val = strtoul(value, &auxs, 10);
                 if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
-                    LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
                     goto error;
                 }
                 list->max = (uint32_t) val;
                 if (list->min > list->max) {
-                    LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
-                    LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
+                    LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
+                    LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
                     goto error;
                 }
             }
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MAX, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "when")) {
             if (list->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
             list->when = read_yin_when(module, sub, unres);
             if (!list->when) {
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -5260,27 +5259,27 @@
      * (but only if we are not in a grouping or augment, then the check is deferred) */
     for (node = retval; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); node = node->parent);
     if (!node && (list->flags & LYS_CONFIG_W) && !list->keys_str) {
-        LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
+        LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
         goto error;
     }
 
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         list->tpdf = calloc(c_tpdf, sizeof *list->tpdf);
-        LY_CHECK_ERR_GOTO(!list->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!list->tpdf, LOGMEM(ctx), error);
     }
     if (c_must) {
         list->must = calloc(c_must, sizeof *list->must);
-        LY_CHECK_ERR_GOTO(!list->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!list->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         list->iffeature = calloc(c_ftrs, sizeof *list->iffeature);
-        LY_CHECK_ERR_GOTO(!list->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!list->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -5341,14 +5340,14 @@
         } else if (!strcmp(sub->name, "notification")) {
             node = read_yin_notif(module, retval, sub, options, unres);
         } else {
-            LOGINT;
+            LOGINT(ctx);
             goto error;
         }
         if (!node) {
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     if (list->keys_str) {
@@ -5362,7 +5361,7 @@
     /* process unique statements */
     if (c_uniq) {
         list->unique = calloc(c_uniq, sizeof *list->unique);
-        LY_CHECK_ERR_GOTO(!list->unique, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error);
 
         LY_TREE_FOR_SAFE(uniq.child, next, sub) {
             r = fill_yin_unique(module, retval, sub, &list->unique[list->unique_size], unres);
@@ -5375,12 +5374,12 @@
                                      LYEXT_SUBSTMT_UNIQUE, list->unique_size - 1, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         }
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && (list->when || list->must)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && (list->when || list->must)) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -5398,10 +5397,10 @@
 
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
     while (uniq.child) {
-        lyxml_free(module->ctx, uniq.child);
+        lyxml_free(ctx, uniq.child);
     }
 
     return NULL;
@@ -5412,6 +5411,7 @@
 read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                    struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
     struct lys_node *retval;
@@ -5425,7 +5425,7 @@
     memset(&root, 0, sizeof root);
 
     cont = calloc(1, sizeof *cont);
-    LY_CHECK_ERR_RETURN(!cont, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!cont, LOGMEM(ctx), NULL);
 
     cont->nodetype = LYS_CONTAINER;
     cont->prev = (struct lys_node *)cont;
@@ -5449,32 +5449,32 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "container", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "container", error);
             c_ext++;
         } else if (!strcmp(sub->name, "presence")) {
             if (cont->presence) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
-            GETVAL(value, sub, "value");
-            cont->presence = lydict_insert(module->ctx, value, strlen(value));
+            GETVAL(ctx, value, sub, "value");
+            cont->presence = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_PRESENCE, 0, unres)) {
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (!strcmp(sub->name, "when")) {
             if (cont->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
             cont->when = read_yin_when(module, sub, unres);
             if (!cont->when) {
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
 
             /* data statements */
         } else if (!strcmp(sub->name, "container") ||
@@ -5488,21 +5488,21 @@
                 !strcmp(sub->name, "anydata") ||
                 !strcmp(sub->name, "action") ||
                 !strcmp(sub->name, "notification")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, cont->tpdf_size, "typedefs", "container", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, cont->tpdf_size, "typedefs", "container", error);
             c_tpdf++;
         } else if (!strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, cont->must_size, "musts", "container", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, cont->must_size, "musts", "container", error);
             c_must++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "container", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "container", error);
             c_ftrs++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -5510,20 +5510,20 @@
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf);
-        LY_CHECK_ERR_GOTO(!cont->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!cont->tpdf, LOGMEM(ctx), error);
     }
     if (c_must) {
         cont->must = calloc(c_must, sizeof *cont->must);
-        LY_CHECK_ERR_GOTO(!cont->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!cont->must, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         cont->iffeature = calloc(c_ftrs, sizeof *cont->iffeature);
-        LY_CHECK_ERR_GOTO(!cont->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!cont->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -5588,11 +5588,11 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && (cont->when || cont->must)) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && (cont->when || cont->must)) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -5607,12 +5607,10 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
-
     return NULL;
 }
 
@@ -5621,6 +5619,7 @@
 read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                   struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
     struct lys_node *retval;
@@ -5633,7 +5632,7 @@
     memset(&root, 0, sizeof root);
 
     grp = calloc(1, sizeof *grp);
-    LY_CHECK_ERR_RETURN(!grp, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!grp, LOGMEM(ctx), NULL);
 
     grp->nodetype = LYS_GROUPING;
     grp->prev = (struct lys_node *)grp;
@@ -5653,7 +5652,7 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "grouping", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "grouping", error);
             c_ext++;
 
         /* data statements */
@@ -5668,15 +5667,15 @@
                 !strcmp(sub->name, "anydata") ||
                 !strcmp(sub->name, "action") ||
                 !strcmp(sub->name, "notification")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, grp->tpdf_size, "typedefs", "grouping", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, grp->tpdf_size, "typedefs", "grouping", error);
             c_tpdf++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -5684,12 +5683,12 @@
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf);
-        LY_CHECK_ERR_GOTO(!grp->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!grp->tpdf, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -5715,7 +5714,7 @@
 
     /* last part - process data nodes */
     if (!root.child) {
-        LOGWRN("Grouping \"%s\" without children.", retval->name);
+        LOGWRN(ctx, "Grouping \"%s\" without children.", retval->name);
     }
     options |= LYS_PARSE_OPT_INGRP;
     LY_TREE_FOR_SAFE(root.child, next, sub) {
@@ -5746,18 +5745,16 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
-
     return NULL;
 }
 
@@ -5766,6 +5763,7 @@
 read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                       int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
     struct lys_node *retval = NULL;
@@ -5777,17 +5775,17 @@
     memset(&root, 0, sizeof root);
 
     inout = calloc(1, sizeof *inout);
-    LY_CHECK_ERR_RETURN(!inout, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!inout, LOGMEM(ctx), NULL);
     inout->prev = (struct lys_node *)inout;
 
     if (!strcmp(yin->name, "input")) {
         inout->nodetype = LYS_INPUT;
-        inout->name = lydict_insert(module->ctx, "input", 0);
+        inout->name = lydict_insert(ctx, "input", 0);
     } else if (!strcmp(yin->name, "output")) {
         inout->nodetype = LYS_OUTPUT;
-        inout->name = lydict_insert(module->ctx, "output", 0);
+        inout->name = lydict_insert(ctx, "output", 0);
     } else {
-        LOGINT;
+        LOGINT(ctx);
         free(inout);
         goto error;
     }
@@ -5806,10 +5804,10 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (!sub->ns) {
             /* garbage */
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions",
                                           inout->nodetype == LYS_INPUT ? "input" : "output", error);
             c_ext++;
         } else if (!strcmp(sub->name, "container") ||
@@ -5821,22 +5819,22 @@
                 !strcmp(sub->name, "grouping") ||
                 !strcmp(sub->name, "anyxml") ||
                 !strcmp(sub->name, "anydata")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, inout->tpdf_size, "typedefs",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, inout->tpdf_size, "typedefs",
                                           inout->nodetype == LYS_INPUT ? "input" : "output", error);
             c_tpdf++;
 
         } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, inout->must_size, "musts",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, inout->must_size, "musts",
                                           inout->nodetype == LYS_INPUT ? "input" : "output", error);
             c_must++;
 
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -5844,15 +5842,15 @@
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf);
-        LY_CHECK_ERR_GOTO(!inout->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!inout->tpdf, LOGMEM(ctx), error);
     }
     if (c_must) {
         inout->must = calloc(c_must, sizeof *inout->must);
-        LY_CHECK_ERR_GOTO(!inout->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!inout->must, LOGMEM(ctx), error);
     }
     if (c_ext) {
         inout->ext = calloc(c_ext, sizeof *inout->ext);
-        LY_CHECK_ERR_GOTO(!inout->ext, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!inout->ext, LOGMEM(ctx), error);
     }
 
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
@@ -5904,11 +5902,11 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && inout->must) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && inout->must) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -5923,12 +5921,10 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
-
     return NULL;
 }
 
@@ -5937,6 +5933,7 @@
 read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
     struct lys_node *retval;
@@ -5946,14 +5943,14 @@
     void *reallocated;
 
     if (parent && (module->version < 2)) {
-        LOGVAL(LYE_INSTMT, LY_VLOG_LYS, parent, "notification");
+        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, parent, "notification");
         return NULL;
     }
 
     memset(&root, 0, sizeof root);
 
     notif = calloc(1, sizeof *notif);
-    LY_CHECK_ERR_RETURN(!notif, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!notif, LOGMEM(ctx), NULL);
 
     notif->nodetype = LYS_NOTIF;
     notif->prev = (struct lys_node *)notif;
@@ -5974,7 +5971,7 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "notification", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "notification", error);
             c_ext++;
             continue;
 
@@ -5988,21 +5985,21 @@
                 !strcmp(sub->name, "grouping") ||
                 !strcmp(sub->name, "anyxml") ||
                 !strcmp(sub->name, "anydata")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, notif->tpdf_size, "typedefs", "notification", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, notif->tpdf_size, "typedefs", "notification", error);
             c_tpdf++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "notification", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "notification", error);
             c_ftrs++;
         } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_must, notif->must_size, "musts", "notification", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_must, notif->must_size, "musts", "notification", error);
             c_must++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -6010,20 +6007,20 @@
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf);
-        LY_CHECK_ERR_GOTO(!notif->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!notif->tpdf, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         notif->iffeature = calloc(c_ftrs, sizeof *notif->iffeature);
-        LY_CHECK_ERR_GOTO(!notif->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!notif->iffeature, LOGMEM(ctx), error);
     }
     if (c_must) {
         notif->must = calloc(c_must, sizeof *notif->must);
-        LY_CHECK_ERR_GOTO(!notif->must, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!notif->must, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -6085,11 +6082,11 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && notif->must) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && notif->must) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -6104,12 +6101,10 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
-
     return NULL;
 }
 
@@ -6118,6 +6113,7 @@
 read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                     int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
     struct lys_node *retval;
@@ -6128,13 +6124,13 @@
 
     if (!strcmp(yin->name, "action")) {
         if (module->version < 2) {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, parent, "action");
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, parent, "action");
             return NULL;
         }
         for (node = parent; node; node = lys_parent(node)) {
             if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
                     || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
-                LOGVAL(LYE_INPAR, LY_VLOG_LYS, parent, strnodetype(node->nodetype), "action");
+                LOGVAL(ctx, LYE_INPAR, LY_VLOG_LYS, parent, strnodetype(node->nodetype), "action");
                 return NULL;
             }
         }
@@ -6144,7 +6140,7 @@
     memset(&root, 0, sizeof root);
 
     rpc = calloc(1, sizeof *rpc);
-    LY_CHECK_ERR_RETURN(!rpc, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!rpc, LOGMEM(ctx), NULL);
 
     rpc->nodetype = (!strcmp(yin->name, "rpc") ? LYS_RPC : LYS_ACTION);
     rpc->prev = (struct lys_node *)rpc;
@@ -6165,43 +6161,43 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions",
                                           rpc->nodetype == LYS_RPC ? "rpc" : "action", error);
             c_ext++;
             continue;
         } else if (!strcmp(sub->name, "input")) {
             if (c_input) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             c_input++;
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
         } else if (!strcmp(sub->name, "output")) {
             if (c_output) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
             c_output++;
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* data statements */
         } else if (!strcmp(sub->name, "grouping")) {
-            lyxml_unlink_elem(module->ctx, sub, 2);
-            lyxml_add_child(module->ctx, &root, sub);
+            lyxml_unlink_elem(ctx, sub, 2);
+            lyxml_add_child(ctx, &root, sub);
 
             /* array counters */
         } else if (!strcmp(sub->name, "typedef")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, rpc->tpdf_size, "typedefs",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, rpc->tpdf_size, "typedefs",
                                           rpc->nodetype == LYS_RPC ? "rpc" : "action", error);
             c_tpdf++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features",
                                           rpc->nodetype == LYS_RPC ? "rpc" : "action", error);
             c_ftrs++;
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -6209,16 +6205,16 @@
     /* middle part - process nodes with cardinality of 0..n except the data nodes */
     if (c_tpdf) {
         rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf);
-        LY_CHECK_ERR_GOTO(!rpc->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!rpc->tpdf, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         rpc->iffeature = calloc(c_ftrs, sizeof *rpc->iffeature);
-        LY_CHECK_ERR_GOTO(!rpc->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!rpc->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -6259,18 +6255,16 @@
             goto error;
         }
 
-        lyxml_free(module->ctx, sub);
+        lyxml_free(ctx, sub);
     }
 
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
     while (root.child) {
-        lyxml_free(module->ctx, root.child);
+        lyxml_free(ctx, root.child);
     }
-
     return NULL;
 }
 
@@ -6284,6 +6278,7 @@
 read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
               int options, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *sub, *next;
     struct lys_node *retval;
     struct lys_node_uses *uses;
@@ -6293,14 +6288,14 @@
     void *reallocated;
 
     uses = calloc(1, sizeof *uses);
-    LY_CHECK_ERR_RETURN(!uses, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!uses, LOGMEM(ctx), NULL);
 
     uses->nodetype = LYS_USES;
     uses->prev = (struct lys_node *)uses;
     retval = (struct lys_node *)uses;
 
-    GETVAL(value, yin, "name");
-    uses->name = lydict_insert(module->ctx, value, 0);
+    GETVAL(ctx, value, yin, "name");
+    uses->name = lydict_insert(ctx, value, 0);
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_MODULE, unres)) {
         goto error;
@@ -6317,32 +6312,32 @@
     LY_TREE_FOR_SAFE(yin->child, next, sub) {
         if (strcmp(sub->ns->value, LY_NSYIN)) {
             /* extension */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, retval->ext_size, "extensions", "uses", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, retval->ext_size, "extensions", "uses", error);
             c_ext++;
             continue;
         } else if (!strcmp(sub->name, "refine")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ref, uses->refine_size, "refines", "uses", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ref, uses->refine_size, "refines", "uses", error);
             c_ref++;
         } else if (!strcmp(sub->name, "augment")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_aug, uses->augment_size, "augments", "uses", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_aug, uses->augment_size, "augments", "uses", error);
             c_aug++;
         } else if (!strcmp(sub->name, "if-feature")) {
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, retval->iffeature_size, "if-features", "uses", error);
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, retval->iffeature_size, "if-features", "uses", error);
             c_ftrs++;
         } else if (!strcmp(sub->name, "when")) {
             if (uses->when) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
                 goto error;
             }
 
             uses->when = read_yin_when(module, sub, unres);
             if (!uses->when) {
-                lyxml_free(module->ctx, sub);
+                lyxml_free(ctx, sub);
                 goto error;
             }
-            lyxml_free(module->ctx, sub);
+            lyxml_free(ctx, sub);
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
             goto error;
         }
     }
@@ -6350,20 +6345,20 @@
     /* process properties with cardinality 0..n */
     if (c_ref) {
         uses->refine = calloc(c_ref, sizeof *uses->refine);
-        LY_CHECK_ERR_GOTO(!uses->refine, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!uses->refine, LOGMEM(ctx), error);
     }
     if (c_aug) {
         uses->augment = calloc(c_aug, sizeof *uses->augment);
-        LY_CHECK_ERR_GOTO(!uses->augment, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!uses->augment, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         uses->iffeature = calloc(c_ftrs, sizeof *uses->iffeature);
-        LY_CHECK_ERR_GOTO(!uses->iffeature, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!uses->iffeature, LOGMEM(ctx), error);
     }
     if (c_ext) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         retval->ext = reallocated;
 
         /* init memory */
@@ -6404,7 +6399,7 @@
     }
 
     /* check XPath dependencies */
-    if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && uses->when) {
+    if (!(ctx->models.flags & LY_CTX_TRUSTED) && uses->when) {
         if (options & LYS_PARSE_OPT_INGRP) {
             if (lyxp_node_check_syntax(retval)) {
                 goto error;
@@ -6419,9 +6414,7 @@
     return retval;
 
 error:
-
     lys_node_free(retval, NULL, 0);
-
     return NULL;
 }
 
@@ -6477,24 +6470,24 @@
             continue;
         } else if (strcmp(child->ns->value, LY_NSYIN)) {
             /* possible extension instance */
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_extinst, trg->ext_size, "extension instances",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_extinst, trg->ext_size, "extension instances",
                                           submodule ? "submodule" : "module", error);
-            lyxml_unlink_elem(module->ctx, child, 2);
-            lyxml_add_child(module->ctx, &exts, child);
+            lyxml_unlink_elem(ctx, child, 2);
+            lyxml_add_child(ctx, &exts, child);
             c_extinst++;
         } else if (!submodule && !strcmp(child->name, "namespace")) {
             if (substmt_group > 0) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
 
             if (trg->ns) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
-            GETVAL(value, child, "uri");
+            GETVAL(ctx, value, child, "uri");
             trg->ns = lydict_insert(ctx, value, strlen(value));
 
             if (lyp_yin_parse_subnode_ext(trg, trg, LYEXT_PAR_MODULE, child, LYEXT_SUBSTMT_NAMESPACE, 0, unres)) {
@@ -6505,18 +6498,18 @@
             substmt_prev = "namespace";
         } else if (!submodule && !strcmp(child->name, "prefix")) {
             if (substmt_group > 0) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
 
             if (trg->prefix) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
-            GETVAL(value, child, "value");
-            if (lyp_check_identifier(value, LY_IDENT_PREFIX, trg, NULL)) {
+            GETVAL(ctx, value, child, "value");
+            if (lyp_check_identifier(ctx, value, LY_IDENT_PREFIX, trg, NULL)) {
                 goto error;
             }
             trg->prefix = lydict_insert(ctx, value, strlen(value));
@@ -6529,19 +6522,19 @@
             substmt_prev = "prefix";
         } else if (submodule && !strcmp(child->name, "belongs-to")) {
             if (substmt_group > 0) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
 
             if (trg->prefix) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
-            GETVAL(value, child, "module");
+            GETVAL(ctx, value, child, "module");
             if (!ly_strequal(value, submodule->belongsto->name, 1)) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
                 goto error;
             }
 
@@ -6551,21 +6544,21 @@
 
             /* get the prefix substatement, start with checks */
             if (!child->child) {
-                LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", child->name);
+                LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", child->name);
                 goto error;
             } else if (strcmp(child->child->name, "prefix")) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->name);
                 goto error;
             } else if (child->child->next) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->next->name);
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->next->name);
                 goto error;
             }
             /* and now finally get the value */
-            GETVAL(value, child->child, "value");
+            GETVAL(ctx, value, child->child, "value");
             /* check here differs from a generic prefix check, since this prefix
              * don't have to be unique
              */
-            if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
+            if (lyp_check_identifier(ctx, value, LY_IDENT_NAME, NULL, NULL)) {
                 goto error;
             }
             submodule->prefix = lydict_insert(ctx, value, strlen(value));
@@ -6582,26 +6575,26 @@
             /* counters (statements with n..1 cardinality) */
         } else if (!strcmp(child->name, "import")) {
             if (substmt_group > 1) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 1;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_imp, trg->imp_size, "imports",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_imp, trg->imp_size, "imports",
                                           submodule ? "submodule" : "module", error);
             c_imp++;
 
             substmt_prev = "import";
         } else if (!strcmp(child->name, "revision")) {
             if (substmt_group > 3) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 3;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_rev, trg->rev_size, "revisions",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_rev, trg->rev_size, "revisions",
                                           submodule ? "submodule" : "module", error);
             c_rev++;
 
@@ -6611,34 +6604,34 @@
             substmt_prev = "revision";
         } else if (!strcmp(child->name, "typedef")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_tpdf, trg->tpdf_size, "typedefs",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_tpdf, trg->tpdf_size, "typedefs",
                                           submodule ? "submodule" : "module", error);
             c_tpdf++;
 
             substmt_prev = "typedef";
         } else if (!strcmp(child->name, "identity")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ident, trg->ident_size, "identities",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ident, trg->ident_size, "identities",
                                           submodule ? "submodule" : "module", error);
             c_ident++;
 
             substmt_prev = "identity";
         } else if (!strcmp(child->name, "include")) {
             if (substmt_group > 1) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 1;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_inc, trg->inc_size, "includes",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_inc, trg->inc_size, "includes",
                                           submodule ? "submodule" : "module", error);
             c_inc++;
 
             substmt_prev = "include";
         } else if (!strcmp(child->name, "augment")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_aug, trg->augment_size, "augments",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_aug, trg->augment_size, "augments",
                                           submodule ? "submodule" : "module", error);
             c_aug++;
             /* keep augments separated, processed last */
@@ -6648,7 +6641,7 @@
             substmt_prev = "augment";
         } else if (!strcmp(child->name, "feature")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ftrs, trg->features_size, "features",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ftrs, trg->features_size, "features",
                                           submodule ? "submodule" : "module", error);
             c_ftrs++;
 
@@ -6682,15 +6675,15 @@
             /* optional statements */
         } else if (!strcmp(child->name, "description")) {
             if (substmt_group > 2) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 2;
 
             if (trg->dsc) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(trg, trg, LYEXT_PAR_MODULE, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
@@ -6705,15 +6698,15 @@
             substmt_prev = "description";
         } else if (!strcmp(child->name, "reference")) {
             if (substmt_group > 2) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 2;
 
             if (trg->ref) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(trg, trg, LYEXT_PAR_MODULE, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
@@ -6728,15 +6721,15 @@
             substmt_prev = "reference";
         } else if (!strcmp(child->name, "organization")) {
             if (substmt_group > 2) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 2;
 
             if (trg->org) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(trg, trg, LYEXT_PAR_MODULE, child, LYEXT_SUBSTMT_ORGANIZATION, 0, unres)) {
@@ -6751,15 +6744,15 @@
             substmt_prev = "organization";
         } else if (!strcmp(child->name, "contact")) {
             if (substmt_group > 2) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
             substmt_group = 2;
 
             if (trg->contact) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
             if (lyp_yin_parse_subnode_ext(trg, trg, LYEXT_PAR_MODULE, child, LYEXT_SUBSTMT_CONTACT, 0, unres)) {
@@ -6774,26 +6767,26 @@
             substmt_prev = "contact";
         } else if (!strcmp(child->name, "yang-version")) {
             if (substmt_group > 0) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
-                LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
+                LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+                LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Statement \"%s\" cannot appear after \"%s\" statement.",
                        child->name, substmt_prev);
                 goto error;
             }
 
             if (version_flag) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
+                LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
                 goto error;
             }
-            GETVAL(value, child, "value");
+            GETVAL(ctx, value, child, "value");
             if (strcmp(value, "1") && strcmp(value, "1.1")) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
+                LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
                 goto error;
             }
             version_flag = 1;
             if (!strcmp(value, "1")) {
                 if (submodule) {
                     if (module->version > 1) {
-                        LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
+                        LOGVAL(ctx, LYE_INVER, LY_VLOG_NONE, NULL);
                         goto error;
                     }
                     submodule->version = 1;
@@ -6803,7 +6796,7 @@
             } else {
                 if (submodule) {
                     if (module->version < 2) {
-                        LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
+                        LOGVAL(ctx, LYE_INVER, LY_VLOG_NONE, NULL);
                         goto error;
                     }
                     submodule->version = 2;
@@ -6820,20 +6813,20 @@
             substmt_prev = "yang-version";
         } else if (!strcmp(child->name, "extension")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_ext, trg->extensions_size, "extensions",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_ext, trg->extensions_size, "extensions",
                                           submodule ? "submodule" : "module", error);
             c_ext++;
 
             substmt_prev = "extension";
         } else if (!strcmp(child->name, "deviation")) {
             substmt_group = 4;
-            YIN_CHECK_ARRAY_OVERFLOW_GOTO(c_dev, trg->deviation_size, "deviations",
+            YIN_CHECK_ARRAY_OVERFLOW_GOTO(ctx, c_dev, trg->deviation_size, "deviations",
                                           submodule ? "submodule" : "module", error);
             c_dev++;
 
             substmt_prev = "deviation";
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
             goto error;
         }
     }
@@ -6841,23 +6834,23 @@
     /* check for mandatory statements */
     if (submodule) {
         if (!submodule->prefix) {
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "belongs-to", "submodule");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "belongs-to", "submodule");
             goto error;
         }
         if (!version_flag) {
             /* check version compatibility with the main module */
             if (module->version > 1) {
-                LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
+                LOGVAL(ctx, LYE_INVER, LY_VLOG_NONE, NULL);
                 goto error;
             }
         }
     } else {
         if (!trg->ns) {
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "namespace", "module");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "namespace", "module");
             goto error;
         }
         if (!trg->prefix) {
-            LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "module");
+            LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "module");
             goto error;
         }
     }
@@ -6865,39 +6858,39 @@
     /* allocate arrays for elements with cardinality of 0..n */
     if (c_imp) {
         trg->imp = calloc(c_imp, sizeof *trg->imp);
-        LY_CHECK_ERR_GOTO(!trg->imp, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->imp, LOGMEM(ctx), error);
     }
     if (c_rev) {
         trg->rev = calloc(c_rev, sizeof *trg->rev);
-        LY_CHECK_ERR_GOTO(!trg->rev, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->rev, LOGMEM(ctx), error);
     }
     if (c_tpdf) {
         trg->tpdf = calloc(c_tpdf, sizeof *trg->tpdf);
-        LY_CHECK_ERR_GOTO(!trg->tpdf, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->tpdf, LOGMEM(ctx), error);
     }
     if (c_ident) {
         trg->ident = calloc(c_ident, sizeof *trg->ident);
-        LY_CHECK_ERR_GOTO(!trg->ident, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->ident, LOGMEM(ctx), error);
     }
     if (c_inc) {
         trg->inc = calloc(c_inc, sizeof *trg->inc);
-        LY_CHECK_ERR_GOTO(!trg->inc, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->inc, LOGMEM(ctx), error);
     }
     if (c_aug) {
         trg->augment = calloc(c_aug, sizeof *trg->augment);
-        LY_CHECK_ERR_GOTO(!trg->augment, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->augment, LOGMEM(ctx), error);
     }
     if (c_ftrs) {
         trg->features = calloc(c_ftrs, sizeof *trg->features);
-        LY_CHECK_ERR_GOTO(!trg->features, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->features, LOGMEM(ctx), error);
     }
     if (c_dev) {
         trg->deviation = calloc(c_dev, sizeof *trg->deviation);
-        LY_CHECK_ERR_GOTO(!trg->deviation, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->deviation, LOGMEM(ctx), error);
     }
     if (c_ext) {
         trg->extensions = calloc(c_ext, sizeof *trg->extensions);
-        LY_CHECK_ERR_GOTO(!trg->extensions, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!trg->extensions, LOGMEM(ctx), error);
     }
 
     /* middle part 1 - process revision and then check whether this (sub)module was not already parsed, add it there */
@@ -6911,7 +6904,7 @@
         /* check uniqueness of the revision date - not required by RFC */
         for (i = 0; i < (trg->rev_size - 1); i++) {
             if (!strcmp(trg->rev[i].date, trg->rev[trg->rev_size - 1].date)) {
-                LOGWRN("Module's revisions are not unique (%s).", trg->rev[trg->rev_size - 1].date);
+                LOGWRN(ctx, "Module's revisions are not unique (%s).", trg->rev[trg->rev_size - 1].date);
                 break;
             }
         }
@@ -6990,7 +6983,7 @@
     if (c_extinst) {
         /* some extensions may be already present from the substatements */
         reallocated = realloc(trg->ext, (c_extinst + trg->ext_size) * sizeof *trg->ext);
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(ctx), error);
         trg->ext = reallocated;
 
         /* init memory */
@@ -7076,7 +7069,7 @@
         lyxml_free(ctx, revs.child);
     }
     while (exts.child) {
-        lyxml_free(module->ctx, exts.child);
+        lyxml_free(ctx, exts.child);
     }
 
     return ret;
@@ -7086,33 +7079,32 @@
 struct lys_submodule *
 yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres)
 {
+    struct ly_ctx *ctx = module->ctx;
     struct lyxml_elem *yin;
     struct lys_submodule *submodule = NULL;
     const char *value;
 
-    assert(module->ctx);
-
-    yin = lyxml_parse_mem(module->ctx, data, LYXML_PARSE_NOMIXEDCONTENT);
+    yin = lyxml_parse_mem(ctx, data, LYXML_PARSE_NOMIXEDCONTENT);
     if (!yin) {
         return NULL;
     }
 
     /* check root element */
     if (!yin->name || strcmp(yin->name, "submodule")) {
-        LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
+        LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
         goto error;
     }
 
-    GETVAL(value, yin, "name");
-    if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
+    GETVAL(ctx, value, yin, "name");
+    if (lyp_check_identifier(ctx, value, LY_IDENT_NAME, NULL, NULL)) {
         goto error;
     }
 
     submodule = calloc(1, sizeof *submodule);
-    LY_CHECK_ERR_GOTO(!submodule, LOGMEM, error);
+    LY_CHECK_ERR_GOTO(!submodule, LOGMEM(ctx), error);
 
-    submodule->ctx = module->ctx;
-    submodule->name = lydict_insert(submodule->ctx, value, strlen(value));
+    submodule->ctx = ctx;
+    submodule->name = lydict_insert(ctx, value, strlen(value));
     submodule->type = 1;
     submodule->implemented = module->implemented;
     submodule->belongsto = module;
@@ -7131,24 +7123,24 @@
     lyp_sort_revisions((struct lys_module *)submodule);
 
     /* cleanup */
-    lyxml_free(module->ctx, yin);
-    lyp_check_circmod_pop(module->ctx);
+    lyxml_free(ctx, yin);
+    lyp_check_circmod_pop(ctx);
 
     LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
     return submodule;
 
 error:
     /* cleanup */
-    lyxml_free(module->ctx, yin);
+    lyxml_free(ctx, yin);
     if (!submodule) {
-        LOGERR(ly_errno, "Submodule parsing failed.");
+        LOGERR(ctx, ly_errno, "Submodule parsing failed.");
         return NULL;
     }
 
-    LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
+    LOGERR(ctx, ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
 
     unres_schema_free((struct lys_module *)submodule, &unres, 0);
-    lyp_check_circmod_pop(module->ctx);
+    lyp_check_circmod_pop(ctx);
     lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
     lys_submodule_module_data_free(submodule);
     lys_submodule_free(submodule, NULL);
@@ -7165,25 +7157,25 @@
     int ret;
 
     unres = calloc(1, sizeof *unres);
-    LY_CHECK_ERR_RETURN(!unres, LOGMEM, NULL);
+    LY_CHECK_ERR_RETURN(!unres, LOGMEM(ctx), NULL);
 
     /* check root element */
     if (!yin->name || strcmp(yin->name, "module")) {
         if (ly_strequal("submodule", yin->name, 0)) {
-            LOGVAL(LYE_SUBMODULE, LY_VLOG_NONE, NULL);
+            LOGVAL(ctx, LYE_SUBMODULE, LY_VLOG_NONE, NULL);
         } else {
-            LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
+            LOGVAL(ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
         }
         goto error;
     }
 
-    GETVAL(value, yin, "name");
-    if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
+    GETVAL(ctx, value, yin, "name");
+    if (lyp_check_identifier(ctx, value, LY_IDENT_NAME, NULL, NULL)) {
         goto error;
     }
 
     module = calloc(1, sizeof *module);
-    LY_CHECK_ERR_GOTO(!module, LOGMEM, error);
+    LY_CHECK_ERR_GOTO(!module, LOGMEM(ctx), error);
 
     module->ctx = ctx;
     module->name = lydict_insert(ctx, value, strlen(value));
@@ -7267,13 +7259,13 @@
     unres_schema_free(module, &unres, 1);
 
     if (!module) {
-        if (ly_vecode != LYVE_SUBMODULE) {
-            LOGERR(ly_errno, "Module parsing failed.");
+        if (ly_vecode(ctx) != LYVE_SUBMODULE) {
+            LOGERR(ctx, ly_errno, "Module parsing failed.");
         }
         return NULL;
     }
 
-    LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
+    LOGERR(ctx, ly_errno, "Module \"%s\" parsing failed.", module->name);
 
     lyp_check_circmod_pop(ctx);
     lys_sub_module_remove_devs_augs(module);
@@ -7291,7 +7283,7 @@
 
     yin = lyxml_parse_mem(ctx, data, LYXML_PARSE_NOMIXEDCONTENT);
     if (!yin) {
-        LOGERR(ly_errno, "Module parsing failed.");
+        LOGERR(ctx, ly_errno, "Module parsing failed.");
         return NULL;
     }
 
@@ -7313,11 +7305,11 @@
 
     val = lys_ext_complex_get_substmt(stmt, ext, &info);
     if (!val) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(mod->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return EXIT_FAILURE;
     }
     if (*val) {
-        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(mod->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return EXIT_FAILURE;
     }
 
@@ -7327,7 +7319,7 @@
 
     str = lyxml_get_attr(node, "value", NULL);
     if (!str) {
-        LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->name);
+        LOGVAL(mod->ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->name);
     } else if (true_val && !strcmp(true_val, str)) {
         /* true value */
         *val = 1;
@@ -7336,7 +7328,7 @@
         *val = 2;
     } else {
         /* unknown value */
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
+        LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
         return EXIT_FAILURE;
     }
 
@@ -7359,11 +7351,11 @@
 
     str = lys_ext_complex_get_substmt(stmt, ext, &info);
     if (!str) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(mod->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return EXIT_FAILURE;
     }
     if (info->cardinality < LY_STMT_CARD_SOME && *str) {
-        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(mod->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return EXIT_FAILURE;
     }
 
@@ -7374,15 +7366,15 @@
         if (!p[0]) {
             /* allocate initial array */
             p[0] = malloc(2 * sizeof(const char *));
-            LY_CHECK_ERR_RETURN(!p[0], LOGMEM, EXIT_FAILURE);
+            LY_CHECK_ERR_RETURN(!p[0], LOGMEM(mod->ctx), EXIT_FAILURE);
             if (stmt == LY_STMT_BELONGSTO) {
                 /* allocate another array for the belongs-to's prefixes */
                 p[1] = malloc(2 * sizeof(const char *));
-                LY_CHECK_ERR_RETURN(!p[1], LOGMEM, EXIT_FAILURE);
+                LY_CHECK_ERR_RETURN(!p[1], LOGMEM(mod->ctx), EXIT_FAILURE);
             } else if (stmt == LY_STMT_ARGUMENT) {
                 /* allocate another array for the yin element */
                 ((uint8_t **)p)[1] = malloc(2 * sizeof(uint8_t));
-                LY_CHECK_ERR_RETURN(!p[1], LOGMEM, EXIT_FAILURE);
+                LY_CHECK_ERR_RETURN(!p[1], LOGMEM(mod->ctx), EXIT_FAILURE);
             }
         } else {
             /* get the index in the array to add new item */
@@ -7402,7 +7394,7 @@
     } else {
         str[c] = lyxml_get_attr(node, argname, NULL);
         if (!str[c]) {
-            LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, argname, node->name);
+            LOGVAL(mod->ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, argname, node->name);
             return EXIT_FAILURE;
         } else {
             str[c] = lydict_insert(mod->ctx, str[c], 0);
@@ -7411,13 +7403,13 @@
         if (stmt == LY_STMT_BELONGSTO) {
             /* get the belongs-to's mandatory prefix substatement */
             if (!node->child) {
-                LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", node->name);
+                LOGVAL(mod->ctx, LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", node->name);
                 return EXIT_FAILURE;
             } else if (strcmp(node->child->name, "prefix")) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->name);
+                LOGVAL(mod->ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->name);
                 return EXIT_FAILURE;
             } else if (node->child->next) {
-                LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->next->name);
+                LOGVAL(mod->ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->next->name);
                 return EXIT_FAILURE;
             }
             /* and now finally get the value */
@@ -7428,7 +7420,7 @@
             }
             str[c] = lyxml_get_attr(node->child, "value", ((void *)0));
             if (!str[c]) {
-                LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->child->name);
+                LOGVAL(mod->ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->child->name);
                 return EXIT_FAILURE;
             }
             str[c] = lydict_insert(mod->ctx, str[c], 0);
@@ -7444,16 +7436,16 @@
             } else {
                 /* get optional yin-element substatement */
                 if (strcmp(node->child->name, "yin-element")) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->name);
+                    LOGVAL(mod->ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->name);
                     return EXIT_FAILURE;
                 } else if (node->child->next) {
-                    LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->next->name);
+                    LOGVAL(mod->ctx, LYE_INSTMT, LY_VLOG_NONE, NULL, node->child->next->name);
                     return EXIT_FAILURE;
                 } else {
                     /* and now finally get the value */
                     value = lyxml_get_attr(node->child, "value", NULL);
                     if (!value) {
-                        LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->child->name);
+                        LOGVAL(mod->ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->child->name);
                         return EXIT_FAILURE;
                     }
                     if (ly_strequal(value, "true", 0)) {
@@ -7461,7 +7453,7 @@
                     } else if (ly_strequal(value, "false", 0)) {
                         ((uint8_t *)str)[c] = 2;
                     } else {
-                        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
+                        LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
                         return EXIT_FAILURE;
                     }
 
@@ -7476,7 +7468,7 @@
         /* enlarge the array(s) */
         reallocated = realloc(p[0], (c + 2) * sizeof(const char *));
         if (!reallocated) {
-            LOGMEM;
+            LOGMEM(mod->ctx);
             lydict_remove(mod->ctx, p[0][c]);
             p[0][c] = NULL;
             return EXIT_FAILURE;
@@ -7488,7 +7480,7 @@
             /* enlarge the second belongs-to's array with prefixes */
             reallocated = realloc(p[1], (c + 2) * sizeof(const char *));
             if (!reallocated) {
-                LOGMEM;
+                LOGMEM(mod->ctx);
                 lydict_remove(mod->ctx, p[1][c]);
                 p[1][c] = NULL;
                 return EXIT_FAILURE;
@@ -7499,7 +7491,7 @@
             /* enlarge the second argument's array with yin element */
             reallocated = realloc(p[1], (c + 2) * sizeof(uint8_t));
             if (!reallocated) {
-                LOGMEM;
+                LOGMEM(mod->ctx);
                 ((uint8_t *)p[1])[c] = 0;
                 return EXIT_FAILURE;
             }
@@ -7520,11 +7512,11 @@
 
     data = lys_ext_complex_get_substmt(stmt, ext, &info);
     if (!data) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(ext->module->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return NULL;
     }
     if (info->cardinality < LY_STMT_CARD_SOME && ((*(uint16_t *)data) & mask)) {
-        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(ext->module->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return NULL;
     }
 
@@ -7547,14 +7539,14 @@
 
     str = lyxml_get_attr(node, "value", NULL);
     if (!str) {
-        LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->name);
+        LOGVAL(mod->ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, "value", node->name);
     } else if (!strcmp(val1_str, str)) {
         *val = *val | val1;
     } else if (!strcmp(val2_str, str)) {
         *val = *val | val2;
     } else {
         /* unknown value */
-        LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
+        LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, str, node->name);
         return EXIT_FAILURE;
     }
     if (lyp_yin_parse_subnode_ext(mod, ext, LYEXT_PAR_EXTINST, node, (LYEXT_SUBSTMT)stmt, 0, unres)) {
@@ -7571,13 +7563,13 @@
 
     snode = lys_ext_complex_get_substmt(stmt, ext, &info);
     if (!snode) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(ext->module->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return NULL;
     }
     if (info->cardinality < LY_STMT_CARD_SOME) {
         LY_TREE_FOR(*snode, siter) {
             if (stmt == lys_snode2stmt(siter->nodetype)) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+                LOGVAL(ext->module->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
                 return NULL;
             }
         }
@@ -7596,11 +7588,11 @@
 
     data = lys_ext_complex_get_substmt(stmt, ext, &info);
     if (!data) {
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(ext->module->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return NULL;
     }
     if (info->cardinality < LY_STMT_CARD_SOME && *data) {
-        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
+        LOGVAL(ext->module->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);
         return NULL;
     }
 
@@ -7613,7 +7605,7 @@
         if (!data) {
             /* allocate initial array */
             *p = data = malloc(2 * sizeof(void *));
-            LY_CHECK_ERR_RETURN(!data, LOGMEM, NULL);
+            LY_CHECK_ERR_RETURN(!data, LOGMEM(ext->module->ctx), NULL);
         } else {
             for (c = 0; *data; data++, c++);
         }
@@ -7622,7 +7614,7 @@
     if (p) {
         /* enlarge the array */
         reallocated = realloc(*p, (c + 2) * sizeof(void *));
-        LY_CHECK_ERR_RETURN(!reallocated, LOGMEM, NULL);
+        LY_CHECK_ERR_RETURN(!reallocated, LOGMEM(ext->module->ctx), NULL);
         *p = reallocated;
         data = *p;
         data[c + 1] = NULL;
@@ -7648,17 +7640,17 @@
 
 #define YIN_STORE_VALUE(TYPE, FROM, TO)           \
     *(TYPE **)TO = malloc(sizeof(TYPE));          \
-    if (!*(TYPE **)TO) { LOGMEM; goto error; }    \
+    if (!*(TYPE **)TO) { LOGMEM(mod->ctx); goto error; }    \
     (**(TYPE **)TO) = (TYPE)FROM;
 
 #define YIN_EXTCOMPLEX_GETPLACE(STMT, TYPE)                                          \
     p = lys_ext_complex_get_substmt(STMT, ext, &info);                               \
     if (!p) {                                                                        \
-        LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name); \
+        LOGVAL(mod->ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->parent->name); \
         goto error;                                                                  \
     }                                                                                \
     if (info->cardinality < LY_STMT_CARD_SOME && (*(TYPE*)p)) {                      \
-        LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);     \
+        LOGVAL(mod->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, node->parent->name);     \
         goto error;                                                                  \
     }                                                                                \
     pp = NULL; i = 0;                                                                \
@@ -7667,7 +7659,7 @@
         pp = p;                                                                      \
         if (!(*pp)) {                                                                \
             *pp = malloc(2 * sizeof(TYPE)); /* allocate initial array */             \
-            LY_CHECK_ERR_GOTO(!*pp, LOGMEM, error);                                  \
+            LY_CHECK_ERR_GOTO(!*pp, LOGMEM(mod->ctx), error);                        \
         } else {                                                                     \
             for (i = 0; (*(TYPE**)pp)[i]; i++);                                      \
         }                                                                            \
@@ -7677,7 +7669,7 @@
     if (pp) {                                                \
         /* enlarge the array */                              \
         reallocated = realloc(*pp, (i + 2) * sizeof(TYPE*)); \
-        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM, error);      \
+        LY_CHECK_ERR_GOTO(!reallocated, LOGMEM(mod->ctx), error);      \
         *pp = reallocated;                                   \
         (*(TYPE**)pp)[i + 1] = 0;                            \
     }
@@ -7687,9 +7679,9 @@
     if (!FUNC(mod, (struct lys_node*)ext, node, ##ARGS, LYS_PARSE_OPT_CFG_NOINHERIT, unres)) { goto error; }
 #define YIN_EXTCOMPLEX_PARSE_RESTR(STMT)                                             \
     YIN_EXTCOMPLEX_GETPLACE(STMT, struct lys_restr*);                                \
-    GETVAL(value, node, "value");                                                    \
+    GETVAL(mod->ctx, value, node, "value");                                                    \
     *(struct lys_restr **)p = calloc(1, sizeof(struct lys_restr));                   \
-    LY_CHECK_ERR_GOTO(!*(struct lys_restr **)p, LOGMEM, error);                      \
+    LY_CHECK_ERR_GOTO(!*(struct lys_restr **)p, LOGMEM(mod->ctx), error);            \
     (*(struct lys_restr **)p)->expr = lydict_insert(mod->ctx, value, 0);             \
     if (read_restr_substmt(mod, *(struct lys_restr **)p, node, unres)) {             \
         goto error;                                                                  \
@@ -7702,7 +7694,7 @@
         } else if (node->ns == yin->ns && (ext->flags & LYS_YINELEM) && ly_strequal(node->name, ext->def->argument, 1)) {
             /* we have the extension's argument */
             if (ext->arg_value) {
-                LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
+                LOGVAL(mod->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
                 goto error;
             }
             ext->arg_value = node->content;
@@ -7731,7 +7723,7 @@
             }
             /* allocate type structure */
             (*type) = calloc(1, sizeof **type);
-            LY_CHECK_ERR_GOTO(!*type, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*type, LOGMEM(mod->ctx), error);
 
             /* HACK for unres */
             lyxml_unlink(mod->ctx, node);
@@ -7750,7 +7742,7 @@
             }
             /* allocate typedef structure */
             (*pp) = calloc(1, sizeof(struct lys_tpdf));
-            LY_CHECK_ERR_GOTO(!*pp, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*pp, LOGMEM(mod->ctx), error);
 
             if (fill_yin_typedef(mod, (struct lys_node *)ext, node, *((struct lys_tpdf **)pp), unres)) {
                 goto error;
@@ -7762,7 +7754,7 @@
             }
             /* allocate iffeature structure */
             (*pp) = calloc(1, sizeof(struct lys_iffeature));
-            LY_CHECK_ERR_GOTO(!*pp, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*pp, LOGMEM(mod->ctx), error);
 
             if (fill_yin_iffeature((struct lys_node *)ext, 0, node, *((struct lys_iffeature **)pp), unres)) {
                 goto error;
@@ -7773,7 +7765,7 @@
                 goto error;
             }
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             if (!strcmp(value, "current")) {
                 *(uint16_t*)p |= LYS_STATUS_CURR;
             } else if (!strcmp(value, "deprecated")) {
@@ -7781,7 +7773,7 @@
             } else if (!strcmp(value, "obsolete")) {
                 *(uint16_t*)p |= LYS_STATUS_OBSLT;
             } else {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                 goto error;
             }
 
@@ -7790,7 +7782,7 @@
             }
         } else if (!strcmp(node->name, "config")) {
             if (yin_parse_extcomplex_flag(mod, node, ext, LY_STMT_MANDATORY, "true", "false", LYS_CONFIG_MASK,
-                                         LYS_CONFIG_W | LYS_CONFIG_SET, LYS_CONFIG_R | LYS_CONFIG_SET, unres)) {
+                                          LYS_CONFIG_W | LYS_CONFIG_SET, LYS_CONFIG_R | LYS_CONFIG_SET, unres)) {
                 goto error;
             }
         } else if (!strcmp(node->name, "argument")) {
@@ -7870,12 +7862,12 @@
         } else if (!strcmp(node->name, "fraction-digits")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_DIGITS, uint8_t);
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             v = strtol(value, NULL, 10);
 
             /* range check */
             if (v < 1 || v > 18) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                 goto error;
             }
 
@@ -7890,7 +7882,7 @@
         } else if (!strcmp(node->name, "max-elements")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_MAX, uint32_t*);
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -7902,7 +7894,7 @@
                 errno = 0; endptr = NULL;
                 u = strtoul(value, &endptr, 10);
                 if (*endptr || value[0] == '-' || errno || u == 0 || u > UINT32_MAX) {
-                    LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                    LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                     goto error;
                 }
             }
@@ -7918,7 +7910,7 @@
         } else if (!strcmp(node->name, "min-elements")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_MIN, uint32_t*);
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -7928,7 +7920,7 @@
             endptr = NULL;
             u = strtoul(value, &endptr, 10);
             if (*endptr || value[0] == '-' || errno || u > UINT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                 goto error;
             }
 
@@ -7943,7 +7935,7 @@
         } else if (!strcmp(node->name, "value")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_VALUE, int32_t*);
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             while (isspace(value[0])) {
                 value++;
             }
@@ -7953,7 +7945,7 @@
 
             /* range check */
             if (ll < INT32_MIN || ll > INT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                 goto error;
             }
 
@@ -7968,12 +7960,12 @@
         } else if (!strcmp(node->name, "position")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_POSITION, uint32_t*);
 
-            GETVAL(value, node, "value");
+            GETVAL(mod->ctx, value, node, "value");
             ll = strtoll(value, NULL, 10);
 
             /* range check */
             if (ll < 0 || ll > UINT32_MAX) {
-                LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
+                LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
                 goto error;
             }
 
@@ -8008,7 +8000,7 @@
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_REVISION, struct lys_revision*);
 
             *(struct lys_revision**)p = calloc(1, sizeof(struct lys_revision));
-            LY_CHECK_ERR_GOTO(!*(struct lys_revision**)p, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*(struct lys_revision**)p, LOGMEM(mod->ctx), error);
             if (fill_yin_revision(mod, node, *(struct lys_revision**)p, unres)) {
                 goto error;
             }
@@ -8017,7 +8009,7 @@
             if (pp) {
                 for (j = 0; j < i; j++) {
                     if (!strcmp((*(struct lys_revision***)pp)[j]->date, (*(struct lys_revision**)p)->date)) {
-                        LOGWRN("Module's revisions are not unique (%s).", (*(struct lys_revision**)p)->date);
+                        LOGWRN(mod->ctx, "Module's revisions are not unique (%s).", (*(struct lys_revision**)p)->date);
                     }
                 }
             }
@@ -8027,7 +8019,7 @@
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_UNIQUE, struct lys_unique*);
 
             *(struct lys_unique**)p = calloc(1, sizeof(struct lys_unique));
-            LY_CHECK_ERR_GOTO(!*(struct lys_unique**)p, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*(struct lys_unique**)p, LOGMEM(mod->ctx), error);
             if (fill_yin_unique(mod, (struct lys_node*)ext, node, *(struct lys_unique**)p, unres)) {
                 goto error;
             }
@@ -8073,20 +8065,20 @@
             }
             /* allocate structure for must */
             (*pp) = calloc(1, sizeof(struct lys_restr));
-            LY_CHECK_ERR_GOTO(!*pp, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*pp, LOGMEM(mod->ctx), error);
 
             if (fill_yin_must(mod, node, *((struct lys_restr **)pp), unres)) {
                 goto error;
             }
         } else if (!strcmp(node->name, "pattern")) {
             YIN_EXTCOMPLEX_GETPLACE(LY_STMT_PATTERN, struct lys_restr*);
-            GETVAL(value, node, "value");
-            if (lyp_check_pattern(value, NULL)) {
+            GETVAL(mod->ctx, value, node, "value");
+            if (lyp_check_pattern(mod->ctx, value, NULL)) {
                 goto error;
             }
 
             *(struct lys_restr **)p = calloc(1, sizeof(struct lys_restr));
-            LY_CHECK_ERR_GOTO(!*(struct lys_restr **)p, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!*(struct lys_restr **)p, LOGMEM(mod->ctx), error);
             (*(struct lys_restr **)p)->expr = lydict_insert(mod->ctx, value, 0);
 
             modifier = 0x06; /* ACK */
@@ -8095,15 +8087,15 @@
                 LY_TREE_FOR(node->child, child) {
                     if (child->ns && !strcmp(child->ns->value, LY_NSYIN) && !strcmp(child->name, "modifier")) {
                         if (name) {
-                            LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
+                            LOGVAL(mod->ctx, LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
                             goto error;
                         }
 
-                        GETVAL(name, child, "value");
+                        GETVAL(mod->ctx, name, child, "value");
                         if (!strcmp(name, "invert-match")) {
                             modifier = 0x15; /* NACK */
                         } else {
-                            LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
+                            LOGVAL(mod->ctx, LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
                             goto error;
                         }
                         /* get extensions of the modifier */
@@ -8117,7 +8109,7 @@
 
             /* store the value: modifier byte + value + terminating NULL byte */
             (*(struct lys_restr **)p)->expr = malloc((strlen(value) + 2) * sizeof(char));
-            LY_CHECK_ERR_GOTO(!(*(struct lys_restr **)p)->expr, LOGMEM, error);
+            LY_CHECK_ERR_GOTO(!(*(struct lys_restr **)p)->expr, LOGMEM(mod->ctx), error);
             ((char *)(*(struct lys_restr **)p)->expr)[0] = modifier;
             strcpy(&((char *)(*(struct lys_restr **)p)->expr)[1], value);
             lydict_insert_zc(mod->ctx, (char *)(*(struct lys_restr **)p)->expr);
@@ -8131,9 +8123,7 @@
         } else if (!strcmp(node->name, "range")) {
             YIN_EXTCOMPLEX_PARSE_RESTR(LY_STMT_RANGE);
         } else {
-            LOGERR(LY_SUCCESS, "Extension's substatement \"%s\" not supported.", node->name);
-            //LOGERR(LY_EINT, "Extension's substatement \"%s\" not supported.", node->name);
-            //return EXIT_FAILURE;
+            LOGERR(mod->ctx, ly_errno, "Extension's substatement \"%s\" not supported.", node->name);
         }
         lyxml_free(mod->ctx, node);
     }