blob: 464afca1361f4343eb7e9f524f64acfc2915921e [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
21#include <utils/Errors.h>
22#include <log/log_event_list.h>
23#include <log/log_read.h>
24
25#include <string>
26#include <vector>
27
28namespace android {
29namespace os {
30namespace statsd {
31
32using std::string;
33using std::vector;
34
35/**
36 * Wrapper for the log_msg structure.
37 */
38class LogEvent {
39public:
40 /**
41 * Read a LogEvent from a log_msg.
42 */
43 explicit LogEvent(const log_msg& msg);
44
45 /**
46 * Read a LogEvent from an android_log_context.
47 */
48 explicit LogEvent(int64_t timestampNs, android_log_event_list* reader);
49 ~LogEvent();
50
51 /**
52 * Get the timestamp associated with this event.
53 */
54 uint64_t GetTimestampNs() const { return mTimestampNs; }
55
56 /**
57 * Get the tag for this event.
58 */
59 int GetTagId() const { return mTagId; }
60
61 /**
62 * Get the nth value, starting at 1.
63 *
64 * Returns BAD_INDEX if the index is larger than the number of elements.
65 * Returns BAD_TYPE if the index is available but the data is the wrong type.
66 */
67 int64_t GetLong(size_t key, status_t* err) const;
68 const char* GetString(size_t key, status_t* err) const;
69 bool GetBool(size_t key, status_t* err) const;
70 float GetFloat(size_t key, status_t* err) const;
71
72 /**
73 * Return a string representation of this event.
74 */
75 string ToString() const;
76
77 /**
78 * Write this object as an EventMetricData proto object.
79 * TODO: Use the streaming output generator to do this instead of this proto lite object?
80 */
81 void ToProto(EventMetricData* out) const;
82
Yao Chen729093d2017-10-16 10:33:26 -070083 /*
84 * Get a KeyValuePair proto object.
85 */
86 KeyValuePair GetKeyValueProto(size_t key) const;
87
Joe Onoratoc4dfae52017-10-17 23:38:21 -070088private:
89 /**
90 * Don't copy, it's slower. If we really need this we can add it but let's try to
91 * avoid it.
92 */
93 explicit LogEvent(const LogEvent&);
94
95 /**
96 * Parses a log_msg into a LogEvent object.
97 */
98 void init(const log_msg& msg);
99
100 /**
101 * Parses a log_msg into a LogEvent object.
102 */
103 void init(int64_t timestampNs, android_log_event_list* reader);
104
105 vector<android_log_list_element> mElements;
106 long mTimestampNs;
107 int mTagId;
108};
109
110} // namespace statsd
111} // namespace os
112} // namespace android
113