LogEvent.h revision d10f7b1c7bdb1c66aa04148945cae9733ee4cadf
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 <android/util/ProtoOutputStream.h>
22#include <log/log_event_list.h>
23#include <log/log_read.h>
24#include <private/android_logger.h>
25#include <utils/Errors.h>
26
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace android {
32namespace os {
33namespace statsd {
34
35using std::string;
36using std::vector;
37
38/**
39 * Wrapper for the log_msg structure.
40 */
41class LogEvent {
42public:
43    /**
44     * Read a LogEvent from a log_msg.
45     */
46    explicit LogEvent(log_msg& msg);
47
48    /**
49     * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
50     */
51    explicit LogEvent(int32_t tagId, uint64_t timestampNs);
52
53    ~LogEvent();
54
55    /**
56     * Get the timestamp associated with this event.
57     */
58    uint64_t GetTimestampNs() const { return mTimestampNs; }
59
60    /**
61     * Get the tag for this event.
62     */
63    int GetTagId() const { return mTagId; }
64
65    uint32_t GetUid() const {
66        return mLogUid;
67    }
68
69    /**
70     * Get the nth value, starting at 1.
71     *
72     * Returns BAD_INDEX if the index is larger than the number of elements.
73     * Returns BAD_TYPE if the index is available but the data is the wrong type.
74     */
75    int64_t GetLong(size_t key, status_t* err) const;
76    const char* GetString(size_t key, status_t* err) const;
77    bool GetBool(size_t key, status_t* err) const;
78    float GetFloat(size_t key, status_t* err) const;
79
80    /**
81     * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
82     * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
83     */
84    bool write(uint32_t value);
85    bool write(int32_t value);
86    bool write(uint64_t value);
87    bool write(int64_t value);
88    bool write(const string& value);
89    bool write(float value);
90
91    /**
92     * Return a string representation of this event.
93     */
94    string ToString() const;
95
96    /**
97     * Write this object to a ProtoOutputStream.
98     */
99    void ToProto(android::util::ProtoOutputStream& out) const;
100
101    /*
102     * Get a KeyValuePair proto object.
103     */
104    KeyValuePair GetKeyValueProto(size_t key) const;
105
106    /**
107     * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
108     * and prepares the list for reading.
109     */
110    void init();
111
112    /**
113     * Set timestamp if the original timestamp is missing.
114     */
115    void setTimestampNs(uint64_t timestampNs) {mTimestampNs = timestampNs;}
116
117    int size() const {
118        return mElements.size();
119    }
120
121private:
122    /**
123     * Don't copy, it's slower. If we really need this we can add it but let's try to
124     * avoid it.
125     */
126    explicit LogEvent(const LogEvent&);
127
128    /**
129     * Parses a log_msg into a LogEvent object.
130     */
131    void init(android_log_context context);
132
133    vector<android_log_list_element> mElements;
134
135    android_log_context mContext;
136
137    uint64_t mTimestampNs;
138
139    int mTagId;
140
141    uint32_t mLogUid;
142};
143
144}  // namespace statsd
145}  // namespace os
146}  // namespace android
147
148