Commit | Line | Data |
---|---|---|
8c572eba | 1 | /* |
899a1d74 | 2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
8c572eba MD |
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 | * | |
c462e188 MD |
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. | |
8c572eba MD |
21 | */ |
22 | ||
989c73bc | 23 | #include <stdlib.h> |
899a1d74 PP |
24 | #include <stdio.h> |
25 | #include <stdbool.h> | |
26 | #include <assert.h> | |
27 | #include <babeltrace/babeltrace.h> | |
28 | #include <popt.h> | |
95fb061d | 29 | #include <glib.h> |
8c572eba | 30 | |
b522ac18 | 31 | static |
899a1d74 | 32 | void print_usage(FILE *fp) |
b522ac18 | 33 | { |
899a1d74 PP |
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"); | |
b522ac18 MD |
40 | } |
41 | ||
8c572eba | 42 | static |
899a1d74 PP |
43 | int parse_params(int argc, char *argv[], char **output_path, |
44 | bool *no_extract_ts) | |
8c572eba | 45 | { |
899a1d74 PP |
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"); | |
70fd5a51 | 64 | goto error; |
ca8b2b02 | 65 | } |
08c82b90 | 66 | |
899a1d74 | 67 | poptReadDefaultConfig(pc, 0); |
185987b1 | 68 | |
899a1d74 PP |
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; | |
08c82b90 | 76 | break; |
899a1d74 PP |
77 | default: |
78 | fprintf(stderr, "Unknown command-line option specified (option code %d)\n", | |
79 | opt); | |
80 | goto error; | |
8c572eba | 81 | } |
8c572eba MD |
82 | } |
83 | ||
899a1d74 PP |
84 | if (opt < -1) { |
85 | fprintf(stderr, "While parsing command-line options, at option %s: %s\n", | |
86 | poptBadOption(pc, 0), poptStrerror(opt)); | |
70fd5a51 | 87 | goto error; |
f824ae04 | 88 | } |
899a1d74 PP |
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; | |
f824ae04 | 95 | } |
90219914 | 96 | |
899a1d74 PP |
97 | *output_path = strdup(leftover); |
98 | assert(*output_path); | |
99 | goto end; | |
989c73bc | 100 | |
899a1d74 PP |
101 | error: |
102 | ret = 1; | |
ca8b2b02 | 103 | |
899a1d74 PP |
104 | end: |
105 | if (pc) { | |
106 | poptFreeContext(pc); | |
ca8b2b02 | 107 | } |
899a1d74 PP |
108 | |
109 | return ret; | |
ca8b2b02 MD |
110 | } |
111 | ||
899a1d74 | 112 | int main(int argc, char *argv[]) |
90219914 | 113 | { |
899a1d74 PP |
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 */ | |
c5a565d9 PP |
133 | "--params", |
134 | "single-trace=yes", | |
899a1d74 PP |
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; | |
90219914 | 143 | } |
90219914 | 144 | |
899a1d74 PP |
145 | if (no_extract_ts) { |
146 | bt_argv[7] = "no-extract-timestamp=yes"; | |
147 | } else { | |
148 | bt_argv[7] = "no-extract-timestamp=no"; | |
39592eae MD |
149 | } |
150 | ||
899a1d74 PP |
151 | bt_argv[13] = output_path; |
152 | (void) g_spawn_sync(NULL, bt_argv, NULL, | |
2723b834 | 153 | G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL, |
899a1d74 | 154 | NULL, NULL, &retcode, &error); |
989c73bc | 155 | |
899a1d74 PP |
156 | if (error) { |
157 | fprintf(stderr, "Failed to execute \"%s\": %s (%d)\n", | |
158 | bt_argv[0], error->message, error->code); | |
989c73bc | 159 | } |
ebd04048 | 160 | |
899a1d74 PP |
161 | end: |
162 | free(output_path); | |
90219914 | 163 | |
899a1d74 PP |
164 | if (error) { |
165 | g_error_free(error); | |
ebd04048 | 166 | } |
989c73bc | 167 | |
899a1d74 | 168 | return retcode; |
90219914 | 169 | } |