Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [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 | |
| 17 | #include "tools/trace_to_text/trace_to_text.h" |
| 18 | |
| 19 | #include <google/protobuf/compiler/importer.h> |
| 20 | #include <google/protobuf/dynamic_message.h> |
| 21 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 22 | #include <google/protobuf/text_format.h> |
| 23 | |
| 24 | #include "perfetto/base/logging.h" |
| 25 | #include "tools/trace_to_text/proto_full_utils.h" |
| 26 | |
| 27 | namespace perfetto { |
| 28 | namespace trace_to_text { |
| 29 | |
| 30 | namespace { |
| 31 | using google::protobuf::Descriptor; |
| 32 | using google::protobuf::DynamicMessageFactory; |
| 33 | using google::protobuf::FileDescriptor; |
| 34 | using google::protobuf::Message; |
| 35 | using google::protobuf::TextFormat; |
| 36 | using google::protobuf::compiler::DiskSourceTree; |
| 37 | using google::protobuf::compiler::Importer; |
| 38 | using google::protobuf::io::OstreamOutputStream; |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | int TraceToText(std::istream* input, std::ostream* output) { |
| 43 | DiskSourceTree dst; |
| 44 | dst.MapPath("perfetto", "protos/perfetto"); |
| 45 | MultiFileErrorCollectorImpl mfe; |
| 46 | Importer importer(&dst, &mfe); |
| 47 | const FileDescriptor* parsed_file = |
| 48 | importer.Import("perfetto/trace/trace.proto"); |
| 49 | |
| 50 | DynamicMessageFactory dmf; |
| 51 | const Descriptor* trace_descriptor = parsed_file->message_type(0); |
| 52 | const Message* msg_root = dmf.GetPrototype(trace_descriptor); |
| 53 | Message* msg = msg_root->New(); |
| 54 | |
| 55 | if (!msg->ParseFromIstream(input)) { |
| 56 | PERFETTO_ELOG("Could not parse input."); |
| 57 | return 1; |
| 58 | } |
| 59 | OstreamOutputStream zero_copy_output(output); |
| 60 | TextFormat::Print(*msg, &zero_copy_output); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | } // namespace trace_to_text |
| 65 | } // namespace perfetto |