Re-format new C++ files
[babeltrace.git] / src / plugins / ctf / fs-src / metadata.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
6 */
7
8 #define BT_COMP_LOG_SELF_COMP self_comp
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META"
11 #include "logging/comp-logging.h"
12
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include "common/assert.h"
17 #include <glib.h>
18 #include "common/uuid.h"
19 #include "compat/memstream.h"
20 #include <babeltrace2/babeltrace.h>
21
22 #include "fs.hpp"
23 #include "file.hpp"
24 #include "metadata.hpp"
25 #include "../common/metadata/decoder.hpp"
26
27 BT_HIDDEN
28 FILE *ctf_fs_metadata_open_file(const char *trace_path)
29 {
30 GString *metadata_path;
31 FILE *fp = NULL;
32
33 metadata_path = g_string_new(trace_path);
34 if (!metadata_path) {
35 goto end;
36 }
37
38 g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
39 fp = fopen(metadata_path->str, "rb");
40 g_string_free(metadata_path, TRUE);
41 end:
42 return fp;
43 }
44
45 static struct ctf_fs_file *get_file(const char *trace_path, bt_logging_level log_level,
46 bt_self_component *self_comp)
47 {
48 struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp);
49
50 if (!file) {
51 goto error;
52 }
53
54 g_string_append(file->path, trace_path);
55 g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
56
57 if (ctf_fs_file_open(file, "rb")) {
58 goto error;
59 }
60
61 goto end;
62
63 error:
64 if (file) {
65 ctf_fs_file_destroy(file);
66 file = NULL;
67 }
68
69 end:
70 return file;
71 }
72
73 BT_HIDDEN
74 int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace,
75 struct ctf_fs_metadata_config *config)
76 {
77 int ret = 0;
78 struct ctf_fs_file *file = NULL;
79 bt_logging_level log_level = ctf_fs_trace->log_level;
80
81 ctf_metadata_decoder_config decoder_config {};
82 decoder_config.log_level = ctf_fs_trace->log_level, decoder_config.self_comp = self_comp,
83 decoder_config.clock_class_offset_s = config ? config->clock_class_offset_s : 0,
84 decoder_config.clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
85 decoder_config.force_clock_class_origin_unix_epoch =
86 config ? config->force_clock_class_origin_unix_epoch : false,
87 decoder_config.create_trace_class = true,
88
89 file = get_file(ctf_fs_trace->path->str, log_level, self_comp);
90 if (!file) {
91 BT_COMP_LOGE("Cannot create metadata file object.");
92 ret = -1;
93 goto end;
94 }
95
96 ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(&decoder_config);
97 if (!ctf_fs_trace->metadata->decoder) {
98 BT_COMP_LOGE("Cannot create metadata decoder object.");
99 ret = -1;
100 goto end;
101 }
102
103 ret = ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder, file->fp);
104 if (ret) {
105 BT_COMP_LOGE("Cannot update metadata decoder's content.");
106 goto end;
107 }
108
109 ctf_fs_trace->metadata->trace_class =
110 ctf_metadata_decoder_get_ir_trace_class(ctf_fs_trace->metadata->decoder);
111 BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class);
112 ctf_fs_trace->metadata->tc =
113 ctf_metadata_decoder_borrow_ctf_trace_class(ctf_fs_trace->metadata->decoder);
114 BT_ASSERT(ctf_fs_trace->metadata->tc);
115
116 end:
117 ctf_fs_file_destroy(file);
118 return ret;
119 }
120
121 BT_HIDDEN
122 int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
123 {
124 /* Nothing to initialize for the moment. */
125 return 0;
126 }
127
128 BT_HIDDEN
129 void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
130 {
131 free(metadata->text);
132
133 if (metadata->trace_class) {
134 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class);
135 }
136
137 if (metadata->decoder) {
138 ctf_metadata_decoder_destroy(metadata->decoder);
139 }
140 }
This page took 0.032387 seconds and 4 git commands to generate.