blob: df75d9f2e0b15c77701b24cf3448cd33090c5fdd [file] [log] [blame]
Joe Onoratoc4dfae52017-10-17 23:38:21 -07001/*
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 Chen5110bed2017-10-23 12:50:02 -070021#include <android/util/ProtoOutputStream.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022#include <log/log_event_list.h>
23#include <log/log_read.h>
Yao Chen5110bed2017-10-23 12:50:02 -070024#include <utils/Errors.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070025
David Chen1481fe12017-10-16 13:16:34 -070026#include <memory>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070027#include <string>
28#include <vector>
29
30namespace android {
31namespace os {
32namespace statsd {
33
34using std::string;
35using std::vector;
36
37/**
38 * Wrapper for the log_msg structure.
39 */
40class LogEvent {
41public:
42 /**
43 * Read a LogEvent from a log_msg.
44 */
David Chena3bf0502017-11-01 16:57:43 -070045 explicit LogEvent(log_msg& msg);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070046
47 /**
David Chen1481fe12017-10-16 13:16:34 -070048 * 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 Onoratoc4dfae52017-10-17 23:38:21 -070052 */
David Chen1481fe12017-10-16 13:16:34 -070053 explicit LogEvent(int tag);
54
Joe Onoratoc4dfae52017-10-17 23:38:21 -070055 ~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 Chen5110bed2017-10-23 12:50:02 -070084 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070085 */
Yao Chen5110bed2017-10-23 12:50:02 -070086 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070087
Yao Chen729093d2017-10-16 10:33:26 -070088 /*
89 * Get a KeyValuePair proto object.
90 */
91 KeyValuePair GetKeyValueProto(size_t key) const;
92
David Chen1481fe12017-10-16 13:16:34 -070093 /**
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 Onoratoc4dfae52017-10-17 23:38:21 -0700106private:
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 Chen1481fe12017-10-16 13:16:34 -0700124 // Need a copy of the android_log_event_list so the strings are not cleared.
125 android_log_event_list mList;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700126 long mTimestampNs;
127 int mTagId;
128};
129
130} // namespace statsd
131} // namespace os
132} // namespace android
133