xref: /aosp_15_r20/external/perfetto/src/trace_processor/util/protozero_to_text.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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_TEXT_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_PROTOZERO_TO_TEXT_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_text {
30 
31 // If |new_lines_modes| == kIncludeNewLines, new lines will be used between
32 // fields, otherwise only a space will be used.
33 enum NewLinesMode {
34   kIncludeNewLines = 0,
35   kSkipNewLines,
36 };
37 
38 // Given a protozero message |protobytes| which is of fully qualified name
39 // |type| within TrackEvent proto messages, we will convert this into a text
40 // proto format string.
41 //
42 // DebugTrackEventProtozeroToText will use new lines between fields, and
43 // ShortDebugTrackEventProtozeroToText will use only a single space.
44 std::string DebugTrackEventProtozeroToText(const std::string& type,
45                                            protozero::ConstBytes protobytes);
46 std::string ShortDebugTrackEventProtozeroToText(
47     const std::string& type,
48     protozero::ConstBytes protobytes);
49 
50 // Given a protozero message |protobytes| which is of fully qualified name
51 // |type|, convert this into a text proto format string. All types used in
52 // message definition of |type| must be available in |pool|.
53 std::string ProtozeroToText(
54     const DescriptorPool& pool,
55     const std::string& type,
56     protozero::ConstBytes protobytes,
57     NewLinesMode new_lines_mode = NewLinesMode::kIncludeNewLines,
58     uint32_t initial_indent_depth = 0);
59 
60 std::string ProtozeroToText(const DescriptorPool& pool,
61                             const std::string& type,
62                             const std::vector<uint8_t>& protobytes,
63                             NewLinesMode new_lines_mode);
64 
65 // Allow the conversion from a protozero enum to a string. The template is just
66 // to allow easy enum passing since we will do the explicit cast to a int32_t
67 // for the user.
68 std::string ProtozeroEnumToText(const std::string& type, int32_t enum_value);
69 template <typename Enum>
ProtozeroEnumToText(const std::string & type,Enum enum_value)70 std::string ProtozeroEnumToText(const std::string& type, Enum enum_value) {
71   return ProtozeroEnumToText(type, static_cast<int32_t>(enum_value));
72 }
73 
74 }  // namespace protozero_to_text
75 }  // namespace trace_processor
76 }  // namespace perfetto
77 
78 #endif  // SRC_TRACE_PROCESSOR_UTIL_PROTOZERO_TO_TEXT_H_
79