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