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