1 /*
2  * Copyright (C) 2018 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 #ifndef SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_
17 #define SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_
18 
19 #include <vector>
20 
21 #include "perfetto/ext/tracing/core/trace_writer.h"
22 #include "perfetto/protozero/message_handle.h"
23 #include "perfetto/protozero/root_message.h"
24 #include "perfetto/protozero/scattered_heap_buffer.h"
25 #include "protos/perfetto/trace/trace_packet.gen.h"
26 
27 namespace perfetto {
28 
29 // A specialization of TraceWriter for testing which writes into memory
30 // allocated by the ScatteredHeapBuffer.
31 // See //include/perfetto/tracing/core/trace_writer.h for docs.
32 class TraceWriterForTesting : public TraceWriter {
33  public:
34   TraceWriterForTesting();
35   ~TraceWriterForTesting() override;
36 
37   // TraceWriter implementation. See documentation in trace_writer.h.
38   // TracePacketHandle is defined in trace_writer.h
39   TracePacketHandle NewTracePacket() override;
40   void Flush(std::function<void()> callback = {}) override;
41 
42   std::vector<protos::gen::TracePacket> GetAllTracePackets();
43   protos::gen::TracePacket GetOnlyTracePacket();
44 
45   WriterID writer_id() const override;
46   uint64_t written() const override;
47 
48  private:
49   TraceWriterForTesting(const TraceWriterForTesting&) = delete;
50   TraceWriterForTesting& operator=(const TraceWriterForTesting&) = delete;
51 
52   protozero::ScatteredHeapBuffer delegate_;
53   protozero::ScatteredStreamWriter stream_;
54 
55   // The packet returned via NewTracePacket(). Its owned by this class,
56   // TracePacketHandle has just a pointer to it.
57   std::unique_ptr<protozero::RootMessage<protos::pbzero::TracePacket>>
58       cur_packet_;
59 };
60 
61 }  // namespace perfetto
62 
63 #endif  // SRC_TRACING_CORE_TRACE_WRITER_FOR_TESTING_H_
64