Mi: mi backend + mi for command version
[lttng-tools.git] / src / common / mi-lttng.c
CommitLineData
c7e35b03
JR
1/*
2 * Copyright (C) 2014 - Jonathan Rajotte <jonathan.r.julien@gmail.com>
3 * - Olivier Cotte <olivier.cotte@polymtl.ca>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <include/config.h>
20#include <common/config/config.h>
21
22#include "mi-lttng.h"
23
24/* Strings related to command */
25const char * const mi_lttng_element_command = "command";
26const char * const mi_lttng_element_command_version = "version";
27const char * const mi_lttng_element_command_list = "list";
28const char * const mi_lttng_element_command_name = "name";
29const char * const mi_lttng_element_command_output = "output";
30
31/* Strings related to command: version */
32const char * const mi_lttng_element_version = "version";
33const char * const mi_lttng_element_version_str = "string";
34const char * const mi_lttng_element_version_web = "url";
35const char * const mi_lttng_element_version_major = "major";
36const char * const mi_lttng_element_version_minor = "minor";
37const char * const mi_lttng_element_version_license = "license";
38const char * const mi_lttng_element_version_patch_level = "patchLevel";
39const char * const mi_lttng_element_version_description = "description";
40
41LTTNG_HIDDEN
42struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
43{
44 struct mi_writer *mi_writer;
45
46 mi_writer = zmalloc(sizeof(struct mi_writer));
47 if (!mi_writer) {
48 PERROR("zmalloc mi_writer_create");
49 goto end;
50 }
51 if (mi_output_type == LTTNG_MI_XML) {
52 mi_writer->writer = config_writer_create(fd_output);
53 if (!mi_writer->writer) {
54 goto err_destroy;
55 }
56 mi_writer->type = LTTNG_MI_XML;
57 } else {
58 goto err_destroy;
59 }
60
61end:
62 return mi_writer;
63
64err_destroy:
65 free(mi_writer);
66 return NULL;
67}
68
69LTTNG_HIDDEN
70int mi_lttng_writer_destroy(struct mi_writer *writer)
71{
72 int ret;
73
74 if (!writer) {
75 ret = -EINVAL;
76 goto end;
77 }
78
79 ret = config_writer_destroy(writer->writer);
80 if (ret < 0) {
81 goto end;
82 }
83
84 free(writer);
85end:
86 return ret;
87}
88
89LTTNG_HIDDEN
90int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
91{
92 int ret;
93
94 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command);
95 if (ret) {
96 goto end;
97 }
98 ret = mi_lttng_writer_write_element_string(writer,
99 mi_lttng_element_command_name, command);
100end:
101 return ret;
102}
103
104LTTNG_HIDDEN
105int mi_lttng_writer_command_close(struct mi_writer *writer)
106{
107 return mi_lttng_writer_close_element(writer);
108}
109
110LTTNG_HIDDEN
111int mi_lttng_writer_open_element(struct mi_writer *writer,
112 const char *element_name)
113{
114 return config_writer_open_element(writer->writer, element_name);
115}
116
117LTTNG_HIDDEN
118int mi_lttng_writer_close_element(struct mi_writer *writer)
119{
120 return config_writer_close_element(writer->writer);
121}
122
123LTTNG_HIDDEN
124int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
125 const char *element_name, uint64_t value)
126{
127 return config_writer_write_element_unsigned_int(writer->writer,
128 element_name, value);
129}
130
131LTTNG_HIDDEN
132int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
133 const char *element_name, int64_t value)
134{
135 return config_writer_write_element_signed_int(writer->writer,
136 element_name, value);
137}
138
139LTTNG_HIDDEN
140int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
141 const char *element_name, int value)
142{
143 return config_writer_write_element_bool(writer->writer,
144 element_name, value);
145}
146
147LTTNG_HIDDEN
148int mi_lttng_writer_write_element_string(struct mi_writer *writer,
149 const char *element_name, const char *value)
150{
151 return config_writer_write_element_string(writer->writer,
152 element_name, value);
153}
154
155LTTNG_HIDDEN
156int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
157 const char *lttng_description, const char *lttng_license)
158{
159 int ret;
160
161 /* Open version */
162 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
163 if (ret) {
164 goto end;
165 }
166
167 /* Version string (contain info like rc etc.) */
168 ret = mi_lttng_writer_write_element_string(writer,
169 mi_lttng_element_version_str, VERSION);
170 if (ret) {
171 goto end;
172 }
173
174 /* Major version number */
175 ret = mi_lttng_writer_write_element_unsigned_int(writer,
176 mi_lttng_element_version_major, version->version_major);
177 if (ret) {
178 goto end;
179 }
180
181 /* Minor version number */
182 ret = mi_lttng_writer_write_element_unsigned_int(writer,
183 mi_lttng_element_version_minor, version->version_minor);
184 if (ret) {
185 goto end;
186 }
187
188 /* Patch number */
189 ret = mi_lttng_writer_write_element_unsigned_int(writer,
190 mi_lttng_element_version_patch_level, version->version_patchlevel);
191 if (ret) {
192 goto end;
193 }
194
195 /* Name of the version */
196 ret = mi_lttng_writer_write_element_string(writer,
197 config_element_name, version->version_name);
198 if (ret) {
199 goto end;
200 }
201
202 /* Description mostly related to beer... */
203 ret = mi_lttng_writer_write_element_string(writer,
204 mi_lttng_element_version_description, lttng_description);
205 if (ret) {
206 goto end;
207 }
208
209 /* url */
210 ret = mi_lttng_writer_write_element_string(writer,
211 mi_lttng_element_version_web, version->package_url);
212 if (ret) {
213 goto end;
214 }
215
216 /* License: free as in free beer...no...*speech* */
217 ret = mi_lttng_writer_write_element_string(writer,
218 mi_lttng_element_version_license, lttng_license);
219 if (ret) {
220 goto end;
221 }
222
223 /* Close version element */
224 ret = mi_lttng_writer_close_element(writer);
225
226end:
227 return ret;
228}
229
230LTTNG_HIDDEN
231int mi_lttng_session(struct mi_writer *writer,
232 struct lttng_session *session, int is_open)
233{
234 int ret;
235
236 /* open sessions element */
237 ret = mi_lttng_writer_open_element(writer,
238 config_element_session);
239 if (ret) {
240 goto end;
241 }
242
243 /* Name of the session */
244 ret = mi_lttng_writer_write_element_string(writer,
245 config_element_name, session->name);
246 if (ret) {
247 goto end;
248 }
249
250 /* path */
251 ret = mi_lttng_writer_write_element_string(writer,
252 config_element_path, session->path);
253 if (ret) {
254 goto end;
255 }
256
257 /* enabled ? */
258 ret = mi_lttng_writer_write_element_unsigned_int(writer,
259 config_element_enabled, session->enabled);
260 if (ret) {
261 goto end;
262 }
263
264 /* snapshot mode */
265 ret = mi_lttng_writer_write_element_unsigned_int(writer,
266 config_element_snapshot_mode, session->snapshot_mode);
267 if (ret) {
268 goto end;
269 }
270
271 /* live timer interval in usec */
272 ret = mi_lttng_writer_write_element_unsigned_int(writer,
273 config_element_live_timer_interval,
274 session->live_timer_interval);
275 if (ret) {
276 goto end;
277 }
278
279 if (!is_open) {
280 /* Closing session element */
281 ret = mi_lttng_writer_close_element(writer);
282 }
283end:
284 return ret;
285
286}
This page took 0.033525 seconds and 5 git commands to generate.