052b5425e752aaf9bf5ff8a04b8919b7f40f12be
[lttng-tools.git] / src / common / mi-lttng.c
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 */
25 const char * const mi_lttng_element_command = "command";
26 const char * const mi_lttng_element_command_version = "version";
27 const char * const mi_lttng_element_command_list = "list";
28 const char * const mi_lttng_element_command_name = "name";
29 const char * const mi_lttng_element_command_output = "output";
30
31 /* Strings related to command: version */
32 const char * const mi_lttng_element_version = "version";
33 const char * const mi_lttng_element_version_str = "string";
34 const char * const mi_lttng_element_version_web = "url";
35 const char * const mi_lttng_element_version_major = "major";
36 const char * const mi_lttng_element_version_minor = "minor";
37 const char * const mi_lttng_element_version_license = "license";
38 const char * const mi_lttng_element_version_patch_level = "patchLevel";
39 const char * const mi_lttng_element_version_description = "description";
40
41 LTTNG_HIDDEN
42 struct 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
61 end:
62 return mi_writer;
63
64 err_destroy:
65 free(mi_writer);
66 return NULL;
67 }
68
69 LTTNG_HIDDEN
70 int 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);
85 end:
86 return ret;
87 }
88
89 LTTNG_HIDDEN
90 int 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);
100 end:
101 return ret;
102 }
103
104 LTTNG_HIDDEN
105 int mi_lttng_writer_command_close(struct mi_writer *writer)
106 {
107 return mi_lttng_writer_close_element(writer);
108 }
109
110 LTTNG_HIDDEN
111 int 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
117 LTTNG_HIDDEN
118 int mi_lttng_writer_close_element(struct mi_writer *writer)
119 {
120 return config_writer_close_element(writer->writer);
121 }
122
123 LTTNG_HIDDEN
124 int 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
131 LTTNG_HIDDEN
132 int 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
139 LTTNG_HIDDEN
140 int 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
147 LTTNG_HIDDEN
148 int 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
155 LTTNG_HIDDEN
156 int 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
226 end:
227 return ret;
228 }
229
230 LTTNG_HIDDEN
231 int 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 }
283 end:
284 return ret;
285
286 }
This page took 0.050593 seconds and 3 git commands to generate.