Add shared copyright to EfficiOS Inc. and Linux Foundation
[babeltrace.git] / converter / babeltrace.c
1 /*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/babeltrace.h>
22 #include <babeltrace/format.h>
23 #include <popt.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30
31 static char *opt_input_format;
32 static char *opt_output_format;
33
34 static const char *opt_input_path;
35 static const char *opt_output_path;
36
37 int babeltrace_verbose, babeltrace_debug;
38 int opt_field_names;
39
40 void strlower(char *str)
41 {
42 while (*str) {
43 *str = tolower(*str);
44 str++;
45 }
46 }
47
48 enum {
49 OPT_NONE = 0,
50 OPT_HELP,
51 OPT_LIST,
52 OPT_VERBOSE,
53 OPT_DEBUG,
54 OPT_NAMES,
55 };
56
57 static struct poptOption long_options[] = {
58 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
59 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
60 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
61 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
62 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
63 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
64 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
65 { "names", 'n', POPT_ARG_NONE, NULL, OPT_NAMES, NULL, NULL },
66 { NULL, 0, 0, NULL, 0, NULL, NULL },
67 };
68
69 static void list_formats(FILE *fp)
70 {
71 fprintf(fp, "\n");
72 bt_fprintf_format_list(fp);
73 }
74
75 static void usage(FILE *fp)
76 {
77 fprintf(fp, "BabelTrace Trace Converter %u.%u\n\n",
78 BABELTRACE_VERSION_MAJOR,
79 BABELTRACE_VERSION_MINOR);
80 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
81 fprintf(fp, "\n");
82 fprintf(fp, " INPUT Input trace path\n");
83 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
84 fprintf(fp, "\n");
85 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
86 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
87 fprintf(fp, "\n");
88 fprintf(fp, " -h, --help This help message\n");
89 fprintf(fp, " -l, --list List available formats\n");
90 fprintf(fp, " -v, --verbose Verbose mode\n");
91 fprintf(fp, " -d, --debug Debug mode\n");
92 fprintf(fp, " -n, --names Print field names\n");
93 list_formats(fp);
94 fprintf(fp, "\n");
95 }
96
97 /*
98 * Return 0 if caller should continue, < 0 if caller should return
99 * error, > 0 if caller should exit without reporting error.
100 */
101 static int parse_options(int argc, char **argv)
102 {
103 poptContext pc;
104 int opt, ret = 0;
105
106 if (argc == 1) {
107 usage(stdout);
108 return 1; /* exit cleanly */
109 }
110
111 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
112 poptReadDefaultConfig(pc, 0);
113
114 while ((opt = poptGetNextOpt(pc)) != -1) {
115 switch (opt) {
116 case OPT_HELP:
117 usage(stdout);
118 ret = 1; /* exit cleanly */
119 goto end;
120 case OPT_LIST:
121 list_formats(stdout);
122 ret = 1;
123 goto end;
124 case OPT_VERBOSE:
125 babeltrace_verbose = 1;
126 break;
127 case OPT_DEBUG:
128 babeltrace_debug = 1;
129 break;
130 case OPT_NAMES:
131 opt_field_names = 1;
132 break;
133 default:
134 ret = -EINVAL;
135 goto end;
136 }
137 }
138
139 opt_input_path = poptGetArg(pc);
140 if (!opt_input_path) {
141 ret = -EINVAL;
142 goto end;
143 }
144 opt_output_path = poptGetArg(pc);
145 end:
146 if (pc) {
147 poptFreeContext(pc);
148 }
149 return ret;
150 }
151
152 int main(int argc, char **argv)
153 {
154 int ret;
155 struct format *fmt_read, *fmt_write;
156 struct trace_descriptor *td_read, *td_write;
157
158 ret = parse_options(argc, argv);
159 if (ret < 0) {
160 fprintf(stdout, "Error parsing options.\n\n");
161 usage(stdout);
162 exit(EXIT_FAILURE);
163 } else if (ret > 0) {
164 exit(EXIT_SUCCESS);
165 }
166 printf_verbose("Verbose mode active.\n");
167 printf_debug("Debug mode active.\n");
168
169 if (opt_input_format)
170 strlower(opt_input_format);
171 if (opt_output_format)
172 strlower(opt_output_format);
173
174 printf_verbose("Converting from file: %s\n", opt_input_path);
175 printf_verbose("Converting from format: %s\n",
176 opt_input_format ? : "ctf <default>");
177 printf_verbose("Converting to file: %s\n",
178 opt_output_path ? : "<stdout>");
179 printf_verbose("Converting to format: %s\n",
180 opt_output_format ? : "text <default>");
181
182 if (!opt_input_format)
183 opt_input_format = "ctf";
184 if (!opt_output_format)
185 opt_output_format = "text";
186 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
187 if (!fmt_read) {
188 fprintf(stdout, "[error] Format \"%s\" is not supported.\n\n",
189 opt_input_format);
190 exit(EXIT_FAILURE);
191 }
192 if (!opt_output_format)
193 opt_output_format = "ctf";
194 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
195 if (!fmt_write) {
196 fprintf(stdout, "[error] format \"%s\" is not supported.\n\n",
197 opt_output_format);
198 exit(EXIT_FAILURE);
199 }
200
201 td_read = fmt_read->open_trace(opt_input_path, O_RDONLY);
202 if (!td_read) {
203 fprintf(stdout, "[error] opening trace \"%s\" for reading.\n\n",
204 opt_input_path);
205 goto error_td_read;
206 }
207
208 td_write = fmt_write->open_trace(opt_output_path, O_RDWR);
209 if (!td_write) {
210 fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n",
211 opt_output_path ? : "<none>");
212 goto error_td_write;
213 }
214
215 ret = convert_trace(td_write, td_read);
216 if (ret) {
217 fprintf(stdout, "Error printing trace.\n\n");
218 goto error_copy_trace;
219 }
220
221 fmt_write->close_trace(td_write);
222 fmt_read->close_trace(td_read);
223 exit(EXIT_SUCCESS);
224
225 /* Error handling */
226 error_copy_trace:
227 fmt_write->close_trace(td_write);
228 error_td_write:
229 fmt_read->close_trace(td_read);
230 error_td_read:
231 exit(EXIT_FAILURE);
232 }
This page took 0.033171 seconds and 4 git commands to generate.