Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #pragma once |
| 18 | |
| 19 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 20 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 21 | #include <android/util/ProtoOutputStream.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 22 | #include <log/log_event_list.h> |
| 23 | #include <log/log_read.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 24 | #include <utils/Errors.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 25 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 26 | #include <memory> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
| 34 | using std::string; |
| 35 | using std::vector; |
| 36 | |
| 37 | /** |
| 38 | * Wrapper for the log_msg structure. |
| 39 | */ |
| 40 | class LogEvent { |
| 41 | public: |
| 42 | /** |
| 43 | * Read a LogEvent from a log_msg. |
| 44 | */ |
David Chen | a3bf050 | 2017-11-01 16:57:43 -0700 | [diff] [blame^] | 45 | explicit LogEvent(log_msg& msg); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 48 | * Constructs a LogEvent with the specified tag and creates an android_log_event_list in write |
| 49 | * mode. Obtain this list with the getter. Make sure to call init() before attempting to read |
| 50 | * any of the values. This constructor is useful for unit-testing since we can't pass in an |
| 51 | * android_log_event_list since there is no copy constructor or assignment operator available. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 52 | */ |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 53 | explicit LogEvent(int tag); |
| 54 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 55 | ~LogEvent(); |
| 56 | |
| 57 | /** |
| 58 | * Get the timestamp associated with this event. |
| 59 | */ |
| 60 | uint64_t GetTimestampNs() const { return mTimestampNs; } |
| 61 | |
| 62 | /** |
| 63 | * Get the tag for this event. |
| 64 | */ |
| 65 | int GetTagId() const { return mTagId; } |
| 66 | |
| 67 | /** |
| 68 | * Get the nth value, starting at 1. |
| 69 | * |
| 70 | * Returns BAD_INDEX if the index is larger than the number of elements. |
| 71 | * Returns BAD_TYPE if the index is available but the data is the wrong type. |
| 72 | */ |
| 73 | int64_t GetLong(size_t key, status_t* err) const; |
| 74 | const char* GetString(size_t key, status_t* err) const; |
| 75 | bool GetBool(size_t key, status_t* err) const; |
| 76 | float GetFloat(size_t key, status_t* err) const; |
| 77 | |
| 78 | /** |
| 79 | * Return a string representation of this event. |
| 80 | */ |
| 81 | string ToString() const; |
| 82 | |
| 83 | /** |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 84 | * Write this object to a ProtoOutputStream. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 85 | */ |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 86 | void ToProto(android::util::ProtoOutputStream& out) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 87 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 88 | /* |
| 89 | * Get a KeyValuePair proto object. |
| 90 | */ |
| 91 | KeyValuePair GetKeyValueProto(size_t key) const; |
| 92 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 93 | /** |
| 94 | * A pointer to the contained log_event_list. |
| 95 | * |
| 96 | * @return The android_log_event_list contained within. |
| 97 | */ |
| 98 | android_log_event_list* GetAndroidLogEventList(); |
| 99 | |
| 100 | /** |
| 101 | * Used with the constructor where tag is passed in. Converts the log_event_list to read mode |
| 102 | * and prepares the list for reading. |
| 103 | */ |
| 104 | void init(); |
| 105 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 106 | private: |
| 107 | /** |
| 108 | * Don't copy, it's slower. If we really need this we can add it but let's try to |
| 109 | * avoid it. |
| 110 | */ |
| 111 | explicit LogEvent(const LogEvent&); |
| 112 | |
| 113 | /** |
| 114 | * Parses a log_msg into a LogEvent object. |
| 115 | */ |
| 116 | void init(const log_msg& msg); |
| 117 | |
| 118 | /** |
| 119 | * Parses a log_msg into a LogEvent object. |
| 120 | */ |
| 121 | void init(int64_t timestampNs, android_log_event_list* reader); |
| 122 | |
| 123 | vector<android_log_list_element> mElements; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 124 | // Need a copy of the android_log_event_list so the strings are not cleared. |
| 125 | android_log_event_list mList; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 126 | long mTimestampNs; |
| 127 | int mTagId; |
| 128 | }; |
| 129 | |
| 130 | } // namespace statsd |
| 131 | } // namespace os |
| 132 | } // namespace android |
| 133 | |