1 /*
2  * Copyright (C) 2016 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 EVENT_LOG_LIST_BUILDER_H_
18 #define EVENT_LOG_LIST_BUILDER_H_
19 
20 #include <cstdint>
21 #include <memory>
22 
23 #include <android-base/macros.h>
24 
25 // EventLogListBuilder provides a mechanism to build an EventLog list
26 // consisting of int and string EventLog values.
27 //
28 // NOTE: This class does not provide the ability to append an embedded list,
29 // i.e., a list containing a list.
30 class EventLogListBuilder {
31  public:
32   EventLogListBuilder();
33 
34   // Append a single value of a specified type.
35   bool Append(int value);
36   bool Append(const std::string& value);
37 
38   // Finalizes construction of the EventLog list and releases the data
39   // to the caller. Caller takes ownership of the payload. No further calls
40   // to append* may be made once the payload is acquired by the caller.
41   void Release(std::unique_ptr<uint8_t[]>* log, size_t* size);
42 
43  private:
44   // Appends |data| of the given |size| to the payload.
45   void AppendData(const void* data, size_t size);
46 
47   // Appends a single byte to the payload.
48   void AppendByte(uint8_t byte);
49 
50   // Returns true iff the remaining capacity in |payload_| is large enough to
51   // accommodate |value_size| bytes. The space required to log the event type
52   // is included in the internal calculation so must not be passed in to
53   // |value_size|.
54   bool IsSpaceAvailable(size_t value_size);
55 
56   // The number of items in the EventLog list.
57   size_t payload_count_;
58 
59   // The size of the data stored in |payload_|. Used to track where to insert
60   // new data.
61   size_t payload_size_;
62 
63   // The payload constructed by calls to log*. The payload may only contain
64   // MAX_EVENT_PAYLOAD (512) bytes.
65   std::unique_ptr<uint8_t[]> payload_;
66 
67   DISALLOW_COPY_AND_ASSIGN(EventLogListBuilder);
68 };
69 
70  #endif  // EVENT_LOG_LIST_BUILDER_H_
71