blob: 68ca0853229c7c5ebed8a0eb930651845ce14773 [file] [log] [blame]
Hector Dearman20b3c1c2018-01-15 15:34:03 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Hector Dearman20b3c1c2018-01-15 15:34:03 +000017#include <fstream>
18#include <iostream>
Hector Dearman20b3c1c2018-01-15 15:34:03 +000019
Hector Dearman20b3c1c2018-01-15 15:34:03 +000020#include "perfetto/base/logging.h"
Primiano Tucci20dc8f72018-10-23 12:28:29 +010021#include "tools/trace_to_text/trace_to_systrace.h"
22#include "tools/trace_to_text/trace_to_text.h"
Hector Dearman20b3c1c2018-01-15 15:34:03 +000023
24namespace {
25
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010026int Usage(const char* argv0) {
Lalit Magantic27975c2018-04-10 21:42:18 +010027 printf(
Hector Dearman22679eb2018-11-12 18:08:32 +000028 "Usage: %s systrace|json|text [trace.proto] "
Primiano Tucci1c752c12018-10-23 09:27:19 +010029 "[trace.txt]\n",
Lalit Magantic27975c2018-04-10 21:42:18 +010030 argv0);
Hector Dearman20b3c1c2018-01-15 15:34:03 +000031 return 1;
32}
33
34} // namespace
35
36int main(int argc, char** argv) {
Primiano Tucci1c752c12018-10-23 09:27:19 +010037 if (argc < 2)
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010038 return Usage(argv[0]);
Hector Dearman20b3c1c2018-01-15 15:34:03 +000039
Primiano Tucci1c752c12018-10-23 09:27:19 +010040 std::istream* input_stream;
41 std::ifstream file_istream;
42 if (argc > 2) {
43 const char* file_path = argv[2];
44 file_istream.open(file_path, std::ios_base::in | std::ios_base::binary);
45 if (!file_istream.is_open())
46 PERFETTO_FATAL("Could not open %s", file_path);
47 input_stream = &file_istream;
48 } else {
49 if (isatty(STDIN_FILENO)) {
50 PERFETTO_ELOG("Reading from stdin but it's connected to a TTY");
51 PERFETTO_LOG("It is unlikely that you want to type in some binary.");
52 PERFETTO_LOG("Either pass a file path to the cmdline or pipe stdin");
53 return Usage(argv[0]);
54 }
55 input_stream = &std::cin;
56 }
57
58 std::ostream* output_stream;
59 std::ofstream file_ostream;
60 if (argc > 3) {
61 const char* file_path = argv[3];
62 file_ostream.open(file_path, std::ios_base::out | std::ios_base::trunc);
63 if (!file_ostream.is_open())
64 PERFETTO_FATAL("Could not open %s", file_path);
65 output_stream = &file_ostream;
66 } else {
67 output_stream = &std::cout;
Primiano Tucci1c752c12018-10-23 09:27:19 +010068 }
69
Hector Dearman20b3c1c2018-01-15 15:34:03 +000070 std::string format(argv[1]);
71
Primiano Tucci45c9b182018-03-29 14:10:51 +010072 if (format == "json")
Primiano Tucci20dc8f72018-10-23 12:28:29 +010073 return perfetto::trace_to_text::TraceToSystrace(input_stream, output_stream,
74 /*wrap_in_json=*/true);
Primiano Tucci45c9b182018-03-29 14:10:51 +010075 if (format == "systrace")
Primiano Tucci20dc8f72018-10-23 12:28:29 +010076 return perfetto::trace_to_text::TraceToSystrace(input_stream, output_stream,
77 /*wrap_in_json=*/false);
Primiano Tucci45c9b182018-03-29 14:10:51 +010078 if (format == "text")
Primiano Tucci20dc8f72018-10-23 12:28:29 +010079 return perfetto::trace_to_text::TraceToText(input_stream, output_stream);
Primiano Tuccif0269902018-04-13 11:13:19 +010080
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010081 return Usage(argv[0]);
Hector Dearman20b3c1c2018-01-15 15:34:03 +000082}