c19f326b59233dd88cb7e926d71cecd300570a5b
[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 <assert.h>
27 #include <babeltrace/babeltrace.h>
28 #include <popt.h>
29 #include <glib.h>
30
31 static
32 void print_usage(FILE *fp)
33 {
34 fprintf(stderr, "Usage: babeltrace-log [OPTIONS] OUTPUT-PATH\n");
35 fprintf(stderr, "\n");
36 fprintf(stderr, "Options:\n");
37 fprintf(stderr, "\n");
38 fprintf(stderr, " -t, --with-timestamps Extract timestamps from lines and map them to\n");
39 fprintf(stderr, " a CTF clock class\n");
40 }
41
42 static
43 int parse_params(int argc, char *argv[], char **output_path,
44 bool *no_extract_ts)
45 {
46 enum {
47 OPT_WITH_TIMESTAMPS = 1,
48 OPT_HELP = 2,
49 };
50 static struct poptOption opts[] = {
51 { "with-timestamps", 't', POPT_ARG_NONE, NULL, OPT_WITH_TIMESTAMPS, NULL, NULL },
52 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
53 { NULL, '\0', 0, NULL, 0, NULL, NULL },
54 };
55 poptContext pc = NULL;
56 int opt;
57 int ret = 0;
58 const char *leftover;
59
60 *no_extract_ts = true;
61 pc = poptGetContext(NULL, argc, (const char **) argv, opts, 0);
62 if (!pc) {
63 fprintf(stderr, "Cannot get popt context\n");
64 goto error;
65 }
66
67 poptReadDefaultConfig(pc, 0);
68
69 while ((opt = poptGetNextOpt(pc)) > 0) {
70 switch (opt) {
71 case OPT_HELP:
72 print_usage(stdout);
73 goto end;
74 case OPT_WITH_TIMESTAMPS:
75 *no_extract_ts = false;
76 break;
77 default:
78 fprintf(stderr, "Unknown command-line option specified (option code %d)\n",
79 opt);
80 goto error;
81 }
82 }
83
84 if (opt < -1) {
85 fprintf(stderr, "While parsing command-line options, at option %s: %s\n",
86 poptBadOption(pc, 0), poptStrerror(opt));
87 goto error;
88 }
89
90 leftover = poptGetArg(pc);
91 if (!leftover) {
92 fprintf(stderr, "Command line error: Missing output path\n");
93 print_usage(stderr);
94 goto error;
95 }
96
97 *output_path = strdup(leftover);
98 assert(*output_path);
99 goto end;
100
101 error:
102 ret = 1;
103
104 end:
105 if (pc) {
106 poptFreeContext(pc);
107 }
108
109 return ret;
110 }
111
112 int main(int argc, char *argv[])
113 {
114 char *output_path = NULL;
115 bool no_extract_ts;
116 int retcode;
117 GError *error = NULL;
118 gchar *bt_argv[] = {
119 BT_CLI_PATH,
120 "run",
121 "--component",
122 "dmesg:src.text.dmesg",
123 "--params",
124 "read-from-stdin=yes",
125 "--params",
126 NULL, /* no-extract-timestamp=? placeholder */
127 "--component",
128 "ctf:sink.ctf.fs",
129 "--key",
130 "path",
131 "--value",
132 NULL, /* output path placeholder */
133 "--params",
134 "single-trace=yes",
135 "--connect",
136 "dmesg:ctf",
137 NULL, /* sentinel */
138 };
139
140 retcode = parse_params(argc, argv, &output_path, &no_extract_ts);
141 if (retcode) {
142 goto end;
143 }
144
145 if (no_extract_ts) {
146 bt_argv[7] = "no-extract-timestamp=yes";
147 } else {
148 bt_argv[7] = "no-extract-timestamp=no";
149 }
150
151 bt_argv[13] = output_path;
152 (void) g_spawn_sync(NULL, bt_argv, NULL,
153 G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL,
154 NULL, NULL, &retcode, &error);
155
156 if (error) {
157 fprintf(stderr, "Failed to execute \"%s\": %s (%d)\n",
158 bt_argv[0], error->message, error->code);
159 }
160
161 end:
162 free(output_path);
163
164 if (error) {
165 g_error_free(error);
166 }
167
168 return retcode;
169 }
This page took 0.032932 seconds and 3 git commands to generate.