1 /* 2 * Copyright (C) 2023 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 #ifndef SRC_TRACE_PROCESSOR_UTIL_PROTOZERO_TO_JSON_H_ 18 #define SRC_TRACE_PROCESSOR_UTIL_PROTOZERO_TO_JSON_H_ 19 20 #include <cstdint> 21 #include <string> 22 #include <vector> 23 24 #include "perfetto/protozero/field.h" 25 26 namespace perfetto::trace_processor { 27 28 class DescriptorPool; 29 30 namespace protozero_to_json { 31 32 enum Flags { 33 kNone = 0, 34 35 // Produce nice json (newlines, 1 space post :, 2 space indents) 36 kPretty = 1 << 0, 37 38 // Report errors as an extra key on the root json object. For example 39 // the output with this flag might look like: 40 // { 41 // "foo": { ... }, 42 // "baz": { ... }, 43 // "__error": "Failed to decode key 'bar' due to <some error>" 44 // } 45 kInlineErrors = 1 << 1, 46 47 // Report annotations as an extra key on the root json object. For example 48 // the output with this flag might look like: 49 // { 50 // "foo": { ... }, 51 // "baz": { ... }, 52 // "__annotations": { 53 // "foo": { 54 // "__field_options": { "unit": "ms_smallerIsBetter" } 55 // } 56 // } 57 // } 58 kInlineAnnotations = 1 << 2, 59 }; 60 61 // Given a protozero message |protobytes| which is of fully qualified name 62 // |type|, convert this into a text proto format string. All types used in 63 // message definition of |type| must be available in |pool|. 64 std::string ProtozeroToJson(const DescriptorPool& pool, 65 const std::string& type, 66 protozero::ConstBytes protobytes, 67 int flags); 68 69 std::string ProtozeroToJson(const DescriptorPool& pool, 70 const std::string& type, 71 const std::vector<uint8_t>& protobytes, 72 int flags); 73 74 } // namespace protozero_to_json 75 } // namespace perfetto::trace_processor 76 77 #endif // SRC_TRACE_PROCESSOR_UTIL_PROTOZERO_TO_JSON_H_ 78