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 enum NewLinesMode {
32   kIncludeNewLines = 0,
33   kSkipNewLines,
34 };
35 
36 // Given a protozero message |protobytes| which is of fully qualified name
37 // |type| within TrackEvent proto messages, we will convert this into a text
38 // proto format string.
39 //
40 // DebugTrackEventProtozeroToText will use new lines between fields, and
41 // ShortDebugTrackEventProtozeroToText will use only a single space.
42 std::string DebugTrackEventProtozeroToText(const std::string& type,
43                                            protozero::ConstBytes protobytes);
44 std::string ShortDebugTrackEventProtozeroToText(
45     const std::string& type,
46     protozero::ConstBytes protobytes);
47 
48 // Given a protozero message |protobytes| which is of fully qualified name
49 // |type|, convert this into a text proto format string. All types used in
50 // message definition of |type| must be available in |pool|. If
51 // |new_lines_modes| == kIncludeNewLines, new lines will be used between fields,
52 // otherwise only a space will be used.
53 std::string ProtozeroToText(const DescriptorPool& pool,
54                             const std::string& type,
55                             protozero::ConstBytes protobytes,
56                             NewLinesMode new_lines_mode);
57 
58 std::string ProtozeroToText(const DescriptorPool& pool,
59                             const std::string& type,
60                             const std::vector<uint8_t>& protobytes,
61                             NewLinesMode new_lines_mode);
62 
63 // Allow the conversion from a protozero enum to a string. The template is just
64 // to allow easy enum passing since we will do the explicit cast to a int32_t
65 // for the user.
66 std::string ProtozeroEnumToText(const std::string& type, int32_t enum_value);
67 template <typename Enum>
ProtozeroEnumToText(const std::string & type,Enum enum_value)68 std::string ProtozeroEnumToText(const std::string& type, Enum enum_value) {
69   return ProtozeroEnumToText(type, static_cast<int32_t>(enum_value));
70 }
71 
72 std::string BytesToHexEncodedStringForTesting(const std::string&);
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