1 /*
2  * Copyright (C) 2021 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 INCLUDE_PERFETTO_TEST_TRACED_VALUE_TEST_SUPPORT_H_
18 #define INCLUDE_PERFETTO_TEST_TRACED_VALUE_TEST_SUPPORT_H_
19 
20 #include "perfetto/base/export.h"
21 #include "perfetto/protozero/scattered_heap_buffer.h"
22 #include "perfetto/tracing/traced_value.h"
23 #include "protos/perfetto/trace/track_event/debug_annotation.pbzero.h"
24 
25 namespace perfetto {
26 
27 namespace internal {
28 PERFETTO_EXPORT std::string DebugAnnotationToString(
29     const std::string& proto_message);
30 }  // namespace internal
31 
32 // Leverage TracedValue support for the given value to convert it to a JSON-like
33 // representation. Note: this should be _only_ for testing TracedValue
34 // conversion and providing extra information for human consumption (e.g. when
35 // the test fails).
36 // Please do not rely on this to compare the object values in
37 // tests and implement explicit comparison operators for the objects you want to
38 // test as the stability of this representation is not guaranteed.
39 template <typename T>
TracedValueToString(T && value)40 std::string TracedValueToString(T&& value) {
41   protozero::HeapBuffered<protos::pbzero::DebugAnnotation> message;
42   WriteIntoTracedValue(internal::CreateTracedValueFromProto(message.get()),
43                        std::forward<T>(value));
44   return internal::DebugAnnotationToString(message.SerializeAsString());
45 }
46 
47 }  // namespace perfetto
48 
49 #endif  // INCLUDE_PERFETTO_TEST_TRACED_VALUE_TEST_SUPPORT_H_
50