Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <iostream> |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 19 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 20 | #include "perfetto/base/logging.h" |
Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [diff] [blame] | 21 | #include "tools/trace_to_text/trace_to_systrace.h" |
| 22 | #include "tools/trace_to_text/trace_to_text.h" |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 26 | int Usage(const char* argv0) { |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 27 | printf( |
Hector Dearman | 22679eb | 2018-11-12 18:08:32 +0000 | [diff] [blame] | 28 | "Usage: %s systrace|json|text [trace.proto] " |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 29 | "[trace.txt]\n", |
Lalit Maganti | c27975c | 2018-04-10 21:42:18 +0100 | [diff] [blame] | 30 | argv0); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | int main(int argc, char** argv) { |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 37 | if (argc < 2) |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 38 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 39 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 40 | 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 Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 70 | std::string format(argv[1]); |
| 71 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 72 | if (format == "json") |
Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [diff] [blame] | 73 | return perfetto::trace_to_text::TraceToSystrace(input_stream, output_stream, |
| 74 | /*wrap_in_json=*/true); |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 75 | if (format == "systrace") |
Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [diff] [blame] | 76 | return perfetto::trace_to_text::TraceToSystrace(input_stream, output_stream, |
| 77 | /*wrap_in_json=*/false); |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 78 | if (format == "text") |
Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [diff] [blame] | 79 | return perfetto::trace_to_text::TraceToText(input_stream, output_stream); |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 80 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 81 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 82 | } |