LogEvent.h revision 1481fe142d36d5f0b36eeebc358d5a8aef7bf28a
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
21#include <utils/Errors.h>
22#include <log/log_event_list.h>
23#include <log/log_read.h>
24
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace android {
30namespace os {
31namespace statsd {
32
33using std::string;
34using std::vector;
35
36/**
37 * Wrapper for the log_msg structure.
38 */
39class LogEvent {
40public:
41    /**
42     * Read a LogEvent from a log_msg.
43     */
44    explicit LogEvent(log_msg msg);
45
46    /**
47     * Constructs a LogEvent with the specified tag and creates an android_log_event_list in write
48     * mode. Obtain this list with the getter. Make sure to call init() before attempting to read
49     * any of the values. This constructor is useful for unit-testing since we can't pass in an
50     * android_log_event_list since there is no copy constructor or assignment operator available.
51     */
52    explicit LogEvent(int tag);
53
54    ~LogEvent();
55
56    /**
57     * Get the timestamp associated with this event.
58     */
59    uint64_t GetTimestampNs() const { return mTimestampNs; }
60
61    /**
62     * Get the tag for this event.
63     */
64    int GetTagId() const { return mTagId; }
65
66    /**
67     * Get the nth value, starting at 1.
68     *
69     * Returns BAD_INDEX if the index is larger than the number of elements.
70     * Returns BAD_TYPE if the index is available but the data is the wrong type.
71     */
72    int64_t GetLong(size_t key, status_t* err) const;
73    const char* GetString(size_t key, status_t* err) const;
74    bool GetBool(size_t key, status_t* err) const;
75    float GetFloat(size_t key, status_t* err) const;
76
77    /**
78     * Return a string representation of this event.
79     */
80    string ToString() const;
81
82    /**
83     * Write this object as an EventMetricData proto object.
84     * TODO: Use the streaming output generator to do this instead of this proto lite object?
85     */
86    void ToProto(EventMetricData* out) const;
87
88    /*
89     * Get a KeyValuePair proto object.
90     */
91    KeyValuePair GetKeyValueProto(size_t key) const;
92
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
106private:
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;
124    // Need a copy of the android_log_event_list so the strings are not cleared.
125    android_log_event_list mList;
126    long mTimestampNs;
127    int mTagId;
128};
129
130}  // namespace statsd
131}  // namespace os
132}  // namespace android
133
134