cli/babeltrace-log.c: include <string.h> for strdup()
[babeltrace.git] / cli / babeltrace-log.c
1 /*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <babeltrace/babeltrace.h>
29 #include <popt.h>
30 #include <glib.h>
31
32 static
33 void print_usage(FILE *fp)
34 {
35 fprintf(stderr, "Usage: babeltrace-log [OPTIONS] OUTPUT-PATH\n");
36 fprintf(stderr, "\n");
37 fprintf(stderr, "Options:\n");
38 fprintf(stderr, "\n");
39 fprintf(stderr, " -t, --with-timestamps Extract timestamps from lines and map them to\n");
40 fprintf(stderr, " a CTF clock class\n");
41 }
42
43 static
44 int parse_params(int argc, char *argv[], char **output_path,
45 bool *no_extract_ts)
46 {
47 enum {
48 OPT_WITH_TIMESTAMPS = 1,
49 OPT_HELP = 2,
50 };
51 static struct poptOption opts[] = {
52 { "with-timestamps", 't', POPT_ARG_NONE, NULL, OPT_WITH_TIMESTAMPS, NULL, NULL },
53 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
54 { NULL, '\0', 0, NULL, 0, NULL, NULL },
55 };
56 poptContext pc = NULL;
57 int opt;
58 int ret = 0;
59 const char *leftover;
60
61 *no_extract_ts = true;
62 pc = poptGetContext(NULL, argc, (const char **) argv, opts, 0);
63 if (!pc) {
64 fprintf(stderr, "Cannot get popt context\n");
65 goto error;
66 }
67
68 poptReadDefaultConfig(pc, 0);
69
70 while ((opt = poptGetNextOpt(pc)) > 0) {
71 switch (opt) {
72 case OPT_HELP:
73 print_usage(stdout);
74 goto end;
75 case OPT_WITH_TIMESTAMPS:
76 *no_extract_ts = false;
77 break;
78 default:
79 fprintf(stderr, "Unknown command-line option specified (option code %d)\n",
80 opt);
81 goto error;
82 }
83 }
84
85 if (opt < -1) {
86 fprintf(stderr, "While parsing command-line options, at option %s: %s\n",
87 poptBadOption(pc, 0), poptStrerror(opt));
88 goto error;
89 }
90
91 leftover = poptGetArg(pc);
92 if (!leftover) {
93 fprintf(stderr, "Command line error: Missing output path\n");
94 print_usage(stderr);
95 goto error;
96 }
97
98 *output_path = strdup(leftover);
99 assert(*output_path);
100 goto end;
101
102 error:
103 ret = 1;
104
105 end:
106 if (pc) {
107 poptFreeContext(pc);
108 }
109
110 return ret;
111 }
112
113 int main(int argc, char *argv[])
114 {
115 char *output_path = NULL;
116 bool no_extract_ts;
117 int retcode;
118 GError *error = NULL;
119 gchar *bt_argv[] = {
120 BT_CLI_PATH,
121 "run",
122 "--component",
123 "dmesg:src.text.dmesg",
124 "--params",
125 "read-from-stdin=yes",
126 "--params",
127 NULL, /* no-extract-timestamp=? placeholder */
128 "--component",
129 "ctf:sink.ctf.fs",
130 "--key",
131 "path",
132 "--value",
133 NULL, /* output path placeholder */
134 "--params",
135 "single-trace=yes",
136 "--connect",
137 "dmesg:ctf",
138 NULL, /* sentinel */
139 };
140
141 retcode = parse_params(argc, argv, &output_path, &no_extract_ts);
142 if (retcode) {
143 goto end;
144 }
145
146 if (no_extract_ts) {
147 bt_argv[7] = "no-extract-timestamp=yes";
148 } else {
149 bt_argv[7] = "no-extract-timestamp=no";
150 }
151
152 bt_argv[13] = output_path;
153 (void) g_spawn_sync(NULL, bt_argv, NULL,
154 G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL,
155 NULL, NULL, &retcode, &error);
156
157 if (error) {
158 fprintf(stderr, "Failed to execute \"%s\": %s (%d)\n",
159 bt_argv[0], error->message, error->code);
160 }
161
162 end:
163 free(output_path);
164
165 if (error) {
166 g_error_free(error);
167 }
168
169 return retcode;
170 }
This page took 0.035307 seconds and 4 git commands to generate.