`ctf` plugin: metadata: use BT_COMP_LOG*() instead of BT_LOG*()
[babeltrace.git] / src / plugins / ctf / fs-src / metadata.c
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
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include "common/assert.h"
31 #include <glib.h>
32 #include "compat/uuid.h"
33 #include "compat/memstream.h"
34 #include <babeltrace2/babeltrace.h>
35
36 #include "fs.h"
37 #include "file.h"
38 #include "metadata.h"
39 #include "../common/metadata/decoder.h"
40
41 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META"
42 #include "logging.h"
43
44 BT_HIDDEN
45 FILE *ctf_fs_metadata_open_file(const char *trace_path)
46 {
47 GString *metadata_path;
48 FILE *fp = NULL;
49
50 metadata_path = g_string_new(trace_path);
51 if (!metadata_path) {
52 goto end;
53 }
54
55 g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
56 fp = fopen(metadata_path->str, "rb");
57 g_string_free(metadata_path, TRUE);
58 end:
59 return fp;
60 }
61
62 static struct ctf_fs_file *get_file(const char *trace_path)
63 {
64 struct ctf_fs_file *file = ctf_fs_file_create();
65
66 if (!file) {
67 goto error;
68 }
69
70 g_string_append(file->path, trace_path);
71 g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
72
73 if (ctf_fs_file_open(file, "rb")) {
74 goto error;
75 }
76
77 goto end;
78
79 error:
80 if (file) {
81 ctf_fs_file_destroy(file);
82 file = NULL;
83 }
84
85 end:
86 return file;
87 }
88
89 BT_HIDDEN
90 int ctf_fs_metadata_set_trace_class(
91 bt_self_component_source *self_comp,
92 struct ctf_fs_trace *ctf_fs_trace,
93 struct ctf_fs_metadata_config *config)
94 {
95 int ret = 0;
96 struct ctf_fs_file *file = NULL;
97 struct ctf_metadata_decoder_config decoder_config = {
98 .log_level = BT_LOG_OUTPUT_LEVEL,
99 .self_comp = bt_self_component_source_as_self_component(self_comp),
100 .clock_class_offset_s = config ? config->clock_class_offset_s : 0,
101 .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
102 };
103
104 file = get_file(ctf_fs_trace->path->str);
105 if (!file) {
106 BT_LOGE("Cannot create metadata file object");
107 ret = -1;
108 goto end;
109 }
110
111 ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(
112 &decoder_config);
113 if (!ctf_fs_trace->metadata->decoder) {
114 BT_LOGE("Cannot create metadata decoder object");
115 ret = -1;
116 goto end;
117 }
118
119 ret = ctf_metadata_decoder_decode(ctf_fs_trace->metadata->decoder,
120 file->fp);
121 if (ret) {
122 BT_LOGE("Cannot decode metadata file");
123 goto end;
124 }
125
126 ctf_fs_trace->metadata->trace_class =
127 ctf_metadata_decoder_get_ir_trace_class(
128 ctf_fs_trace->metadata->decoder);
129 BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class);
130 ctf_fs_trace->metadata->tc =
131 ctf_metadata_decoder_borrow_ctf_trace_class(
132 ctf_fs_trace->metadata->decoder);
133 BT_ASSERT(ctf_fs_trace->metadata->tc);
134
135 end:
136 ctf_fs_file_destroy(file);
137 return ret;
138 }
139
140 BT_HIDDEN
141 int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
142 {
143 /* Nothing to initialize for the moment. */
144 return 0;
145 }
146
147 BT_HIDDEN
148 void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
149 {
150 if (metadata->text) {
151 free(metadata->text);
152 }
153
154 if (metadata->trace_class) {
155 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class);
156 }
157
158 if (metadata->decoder) {
159 ctf_metadata_decoder_destroy(metadata->decoder);
160 }
161 }
This page took 0.034412 seconds and 4 git commands to generate.