ctf: use `bt2c::Logger` throughout `src.ctf.fs`, `src.ctf.lttng-live`
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / parser.ypp
index b05836956258fc38a738f0594c104ce4f9eeb98d..7ae25b69d3d8fa2233e2d60194a4ba64a089983c 100644 (file)
@@ -7,18 +7,6 @@
  * Common Trace Format Metadata Grammar.
  */
 
-#include <assert.h>
-#include <ctype.h>
-#include <errno.h>
-#include <glib.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#define BT_LOG_OUTPUT_LEVEL ctf_plugin_metadata_log_level
-#define BT_LOG_TAG "PLUGIN/CTF/META/PARSER"
 #include "plugins/ctf/common/src/metadata/tsdl/logging.hpp"
 
 #include "common/list.h"
  */
 BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
 
+thread_local const ctf_scanner *currentCtfScanner;
+
+#define YYFPRINTF(_stream, _fmt, args...)                                                          \
+    do {                                                                                           \
+        int size = snprintf(NULL, 0, (_fmt), ##args);                                              \
+        std::string str(size, '\0');                                                               \
+        int written = snprintf(&str[0], size + 1, (_fmt), ##args);                                 \
+        BT_ASSERT(size == written);                                                                \
+        BT_CPPLOGT_STR_SPEC(currentCtfScanner->logger, str.c_str());                               \
+    } while (0)
+
 /* Join two lists, put "add" at the end of "head".  */
 static inline void
 _bt_list_splice_tail (struct bt_list_head *add, struct bt_list_head *head)
@@ -268,9 +267,10 @@ int import_string(struct ctf_scanner *scanner, YYSTYPE *lvalp,
        lvalp->s = (char *) objstack_alloc(scanner->objstack, len);
        if (src[0] == 'L') {
                // TODO: import wide string
-               _BT_LOGE_APPEND_CAUSE_LINENO(yyget_lineno(scanner),
+               _BT_CPPLOGE_APPEND_CAUSE_LINENO(currentCtfScanner->logger,
+                       yyget_lineno(scanner),
                        "wide characters are not supported as of this version: "
-                       "scanner-addr=%p", scanner);
+                       "scanner-addr={}", fmt::ptr(scanner));
                return -1;
        } else {
                return import_basic_string(scanner, lvalp, len, src, delim);
@@ -294,7 +294,8 @@ static void push_scope(struct ctf_scanner *scanner)
 {
        struct ctf_scanner_scope *ns;
 
-       BT_LOGT("Pushing scope: scanner-addr=%p", scanner);
+       BT_CPPLOGT_SPEC(currentCtfScanner->logger,
+               "Pushing scope: scanner-addr={}", fmt::ptr(scanner));
        ns = (ctf_scanner_scope *) malloc(sizeof(struct ctf_scanner_scope));
        init_scope(ns, scanner->cs);
        scanner->cs = ns;
@@ -304,7 +305,8 @@ static void pop_scope(struct ctf_scanner *scanner)
 {
        struct ctf_scanner_scope *os;
 
-       BT_LOGT("Popping scope: scanner-addr=%p", scanner);
+       BT_CPPLOGT_SPEC(currentCtfScanner->logger,
+               "Popping scope: scanner-addr={}", fmt::ptr(scanner));
        os = scanner->cs;
        scanner->cs = os->parent;
        finalize_scope(os);
@@ -316,8 +318,9 @@ static int lookup_type(struct ctf_scanner_scope *s, const char *id)
        int ret;
 
        ret = GPOINTER_TO_INT(g_hash_table_lookup(s->classes, id));
-       BT_LOGT("Looked up type: scanner-addr=%p, id=\"%s\", ret=%d",
-               s, id, ret);
+       BT_CPPLOGT_SPEC(currentCtfScanner->logger,
+               "Looked up type: scanner-addr={}, id=\"{}\", ret={}",
+               fmt::ptr(s), id, ret);
        return ret;
 }
 
@@ -332,15 +335,17 @@ int is_type(struct ctf_scanner *scanner, const char *id)
                        break;
                }
        }
-       BT_LOGT("Found if ID is type: scanner-addr=%p, id=\"%s\", ret=%d",
-               scanner, id, ret);
+       BT_CPPLOGT_SPEC(currentCtfScanner->logger,
+               "Found if ID is type: scanner-addr={}, id=\"{}\", ret={}",
+               fmt::ptr(scanner), id, ret);
        return ret;
 }
 
 static void add_type(struct ctf_scanner *scanner, char *id)
 {
-       BT_LOGT("Adding type: scanner-addr=%p, id=\"%s\"",
-               scanner, id);
+       BT_CPPLOGT_SPEC(currentCtfScanner->logger,
+               "Adding type: scanner-addr={}, id=\"{}\"", fmt::ptr(scanner),
+               id);
        if (lookup_type(scanner->cs, id))
                return;
        g_hash_table_insert(scanner->cs->classes, id, id);
@@ -353,9 +358,10 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
 
        node = (ctf_node *) objstack_alloc(scanner->objstack, sizeof(*node));
        if (!node) {
-               _BT_LOGE_APPEND_CAUSE_LINENO(yyget_lineno(scanner->scanner),
+               _BT_CPPLOGE_APPEND_CAUSE_LINENO(currentCtfScanner->logger,
+                       yyget_lineno(scanner->scanner),
                        "failed to allocate one stack entry: "
-                       "scanner-addr=%p", scanner);
+                       "scanner-addr={}", fmt::ptr(scanner));
                return &error_node;
        }
        node->type = type;
@@ -366,8 +372,8 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
        switch (type) {
        case NODE_ROOT:
                node->type = NODE_ERROR;
-               BT_LOGE("Trying to create root node: scanner-addr=%p",
-                       scanner);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Trying to create root node: scanner-addr={}",
+                       fmt::ptr(scanner));
                break;
        case NODE_EVENT:
                BT_INIT_LIST_HEAD(&node->u.event.declaration_list);
@@ -442,8 +448,8 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
        case NODE_UNKNOWN:
        default:
                node->type = NODE_ERROR;
-               BT_LOGE("Unknown node type: scanner-addr=%p, node-type=%d",
-                       scanner, type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: scanner-addr={}, node-type={}",
+                       fmt::ptr(scanner), type);
                break;
        }
 
@@ -502,7 +508,7 @@ static int reparent_ctf_expression(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -559,7 +565,7 @@ static int reparent_typedef(struct ctf_node *node, struct ctf_node *parent)
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -616,7 +622,7 @@ static int reparent_field_class_alias(struct ctf_node *node, struct ctf_node *pa
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -657,7 +663,7 @@ static int reparent_field_class_specifier(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -723,7 +729,7 @@ static int reparent_field_class_specifier_list(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -774,7 +780,7 @@ static int reparent_field_class_declarator(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -797,7 +803,8 @@ static int set_parent_node(struct ctf_node *node,
 
        switch (node->type) {
        case NODE_ROOT:
-               BT_LOGE_STR("Trying to reparent root node.");
+               BT_CPPLOGE_STR_SPEC(currentCtfScanner->logger,
+                       "Trying to reparent root node.");
                return -EINVAL;
 
        case NODE_EVENT:
@@ -914,7 +921,7 @@ static int set_parent_node(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               BT_LOGE("Unknown node type: node-type=%d", parent->type);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "Unknown node type: node-type={}", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -923,8 +930,9 @@ static int set_parent_node(struct ctf_node *node,
 static
 void yyerror(struct ctf_scanner *scanner, yyscan_t yyscanner, const char *str)
 {
-       _BT_LOGE_APPEND_CAUSE_LINENO(yyget_lineno(scanner->scanner),
-               "%s: token=\"%s\"", str, yyget_text(scanner->scanner));
+       _BT_CPPLOGE_APPEND_CAUSE_LINENO(currentCtfScanner->logger,
+               yyget_lineno(scanner->scanner),
+               "{}: token=\"{}\"", str, yyget_text(scanner->scanner));
 }
 
 #define reparent_error(scanner, str)                           \
@@ -955,25 +963,26 @@ static struct ctf_ast *ctf_ast_alloc(struct ctf_scanner *scanner)
 int ctf_scanner_append_ast(struct ctf_scanner *scanner, FILE *input)
 {
        /* Start processing new stream */
+       struct ClearCurrentCtfScanner {
+               ~ClearCurrentCtfScanner() {
+                       currentCtfScanner = nullptr;
+               }
+       } clearMoiLa;
+
+       currentCtfScanner = scanner;
        yyrestart(input, scanner->scanner);
        return yyparse(scanner, scanner->scanner);
 }
 
-struct ctf_scanner *ctf_scanner_alloc(void)
+struct ctf_scanner *ctf_scanner_alloc(const bt2c::Logger &parentLogger)
 {
-       struct ctf_scanner *scanner;
-       int ret;
-
-       scanner = (ctf_scanner *) malloc(sizeof(*scanner));
-       if (!scanner)
-               return NULL;
-       memset(scanner, 0, sizeof(*scanner));
-       ret = yylex_init_extra(scanner, &scanner->scanner);
+       ctf_scanner *scanner = new ctf_scanner {parentLogger};
+       int ret = yylex_init_extra(scanner, &scanner->scanner);
        if (ret) {
-               BT_LOGE("yylex_init_extra() failed: ret=%d", ret);
+               BT_CPPLOGE_SPEC(scanner->logger, "yylex_init_extra() failed: ret={}", ret);
                goto cleanup_scanner;
        }
-       scanner->objstack = objstack_create();
+       scanner->objstack = objstack_create(scanner->logger);
        if (!scanner->objstack)
                goto cleanup_lexer;
        scanner->ast = ctf_ast_alloc(scanner);
@@ -989,10 +998,10 @@ cleanup_objstack:
 cleanup_lexer:
        ret = yylex_destroy(scanner->scanner);
        if (!ret)
-               BT_LOGE("yylex_destroy() failed: scanner-addr=%p, ret=%d",
-                       scanner, ret);
+               BT_CPPLOGE_SPEC(scanner->logger, "yylex_destroy() failed: scanner-addr={}, ret={}",
+                       fmt::ptr(scanner), ret);
 cleanup_scanner:
-       free(scanner);
+       delete scanner;
        return NULL;
 }
 
@@ -1023,9 +1032,9 @@ void ctf_scanner_free(struct ctf_scanner *scanner)
        objstack_destroy(scanner->objstack);
        ret = yylex_destroy(scanner->scanner);
        if (ret)
-               BT_LOGE("yylex_destroy() failed: scanner-addr=%p, ret=%d",
-                       scanner, ret);
-       free(scanner);
+               BT_CPPLOGE_SPEC(currentCtfScanner->logger, "yylex_destroy() failed: scanner-addr={}, ret={}",
+                       fmt::ptr(scanner), ret);
+       delete scanner;
 }
 
 /*
This page took 0.028337 seconds and 4 git commands to generate.