src.ctf.fs: honor component's initial log level
[babeltrace.git] / src / plugins / ctf / fs-src / metadata.c
CommitLineData
e98a2d6e
PP
1/*
2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
4 *
5 * Some functions are based on older functions written by Mathieu Desnoyers.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
98903a3e
PP
26#define BT_LOG_OUTPUT_LEVEL log_level
27#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META"
28#include "logging/log.h"
29
e98a2d6e
PP
30#include <stdio.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <stdbool.h>
578e048b 34#include "common/assert.h"
e98a2d6e 35#include <glib.h>
578e048b
MJ
36#include "compat/uuid.h"
37#include "compat/memstream.h"
3fadfbc0 38#include <babeltrace2/babeltrace.h>
e98a2d6e 39
56a1cced
JG
40#include "fs.h"
41#include "file.h"
42#include "metadata.h"
1e649dff 43#include "../common/metadata/decoder.h"
e98a2d6e 44
33f93973
PP
45BT_HIDDEN
46FILE *ctf_fs_metadata_open_file(const char *trace_path)
47{
4dd8c9bf 48 GString *metadata_path;
33f93973
PP
49 FILE *fp = NULL;
50
4dd8c9bf 51 metadata_path = g_string_new(trace_path);
33f93973 52 if (!metadata_path) {
4dd8c9bf 53 goto end;
33f93973
PP
54 }
55
3743a302 56 g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
33f93973 57 fp = fopen(metadata_path->str, "rb");
33f93973 58 g_string_free(metadata_path, TRUE);
4dd8c9bf 59end:
33f93973
PP
60 return fp;
61}
62
98903a3e
PP
63static struct ctf_fs_file *get_file(const char *trace_path,
64 bt_logging_level log_level)
e98a2d6e 65{
98903a3e 66 struct ctf_fs_file *file = ctf_fs_file_create(log_level);
e98a2d6e
PP
67
68 if (!file) {
69 goto error;
70 }
71
72 g_string_append(file->path, trace_path);
3743a302 73 g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
e98a2d6e 74
55314f2a 75 if (ctf_fs_file_open(file, "rb")) {
e98a2d6e
PP
76 goto error;
77 }
78
79 goto end;
80
81error:
82 if (file) {
83 ctf_fs_file_destroy(file);
84 file = NULL;
85 }
86
87end:
88 return file;
89}
90
41693723
PP
91BT_HIDDEN
92int ctf_fs_metadata_set_trace_class(
93 bt_self_component_source *self_comp,
94 struct ctf_fs_trace *ctf_fs_trace,
a2a54545 95 struct ctf_fs_metadata_config *config)
e98a2d6e
PP
96{
97 int ret = 0;
1e649dff 98 struct ctf_fs_file *file = NULL;
a2a54545 99 struct ctf_metadata_decoder_config decoder_config = {
98903a3e 100 .log_level = ctf_fs_trace->log_level,
f7b785ac 101 .self_comp = bt_self_component_source_as_self_component(self_comp),
297941e3
JG
102 .clock_class_offset_s = config ? config->clock_class_offset_s : 0,
103 .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
a2a54545 104 };
98903a3e 105 bt_logging_level log_level = ctf_fs_trace->log_level;
e98a2d6e 106
98903a3e 107 file = get_file(ctf_fs_trace->path->str, log_level);
e98a2d6e 108 if (!file) {
55314f2a 109 BT_LOGE("Cannot create metadata file object");
8318bbaa 110 ret = -1;
1e649dff 111 goto end;
e98a2d6e
PP
112 }
113
f7b785ac
PP
114 ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(
115 &decoder_config);
44c440bc 116 if (!ctf_fs_trace->metadata->decoder) {
55314f2a 117 BT_LOGE("Cannot create metadata decoder object");
8318bbaa 118 ret = -1;
1e649dff 119 goto end;
e98a2d6e
PP
120 }
121
44c440bc
PP
122 ret = ctf_metadata_decoder_decode(ctf_fs_trace->metadata->decoder,
123 file->fp);
1e649dff 124 if (ret) {
55314f2a 125 BT_LOGE("Cannot decode metadata file");
1e649dff 126 goto end;
e98a2d6e
PP
127 }
128
862ca4ed
PP
129 ctf_fs_trace->metadata->trace_class =
130 ctf_metadata_decoder_get_ir_trace_class(
131 ctf_fs_trace->metadata->decoder);
41693723 132 BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class);
44c440bc
PP
133 ctf_fs_trace->metadata->tc =
134 ctf_metadata_decoder_borrow_ctf_trace_class(
135 ctf_fs_trace->metadata->decoder);
136 BT_ASSERT(ctf_fs_trace->metadata->tc);
4f1f88a6 137
e98a2d6e 138end:
1e649dff 139 ctf_fs_file_destroy(file);
4f1f88a6 140 return ret;
e98a2d6e
PP
141}
142
41693723 143BT_HIDDEN
e98a2d6e
PP
144int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
145{
5b29e799 146 /* Nothing to initialize for the moment. */
e98a2d6e
PP
147 return 0;
148}
149
41693723 150BT_HIDDEN
413bc2c4 151void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
e98a2d6e
PP
152{
153 if (metadata->text) {
154 free(metadata->text);
155 }
156
862ca4ed 157 if (metadata->trace_class) {
c5b9b441 158 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class);
e98a2d6e 159 }
44c440bc
PP
160
161 if (metadata->decoder) {
162 ctf_metadata_decoder_destroy(metadata->decoder);
163 }
e98a2d6e 164}
This page took 0.060787 seconds and 4 git commands to generate.