LogEvent.h revision d10f7b1c7bdb1c66aa04148945cae9733ee4cadf
1c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato/*
2c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Copyright (C) 2017 The Android Open Source Project
3c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato *
4c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
5c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * you may not use this file except in compliance with the License.
6c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * You may obtain a copy of the License at
7c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato *
8c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
9c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato *
10c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Unless required by applicable law or agreed to in writing, software
11c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
12c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * See the License for the specific language governing permissions and
14c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * limitations under the License.
15c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */
16c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
17c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#pragma once
18c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
19c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
20c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
215110bedd787835d4dbc809b7977285cac364bdefYao Chen#include <android/util/ProtoOutputStream.h>
22c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <log/log_event_list.h>
23c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <log/log_read.h>
2480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen#include <private/android_logger.h>
255110bedd787835d4dbc809b7977285cac364bdefYao Chen#include <utils/Errors.h>
26c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
271481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen#include <memory>
28c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <string>
29c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <vector>
30c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
31c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace android {
32c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace os {
33c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace statsd {
34c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
35c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratousing std::string;
36c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratousing std::vector;
37c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
38c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato/**
39c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Wrapper for the log_msg structure.
40c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */
41c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoclass LogEvent {
42c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratopublic:
43c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
44c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Read a LogEvent from a log_msg.
45c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
46a3bf0509e01c6b32a13cfd16060e4a537706f231David Chen    explicit LogEvent(log_msg& msg);
47c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
48c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
4980235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
50c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
5180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    explicit LogEvent(int32_t tagId, uint64_t timestampNs);
521481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen
53c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    ~LogEvent();
54c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
55c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
56c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the timestamp associated with this event.
57c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
58c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    uint64_t GetTimestampNs() const { return mTimestampNs; }
59c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
60c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
61c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the tag for this event.
62c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
63c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    int GetTagId() const { return mTagId; }
64c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
65d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen    uint32_t GetUid() const {
66d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen        return mLogUid;
67d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen    }
68d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen
69c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
70c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the nth value, starting at 1.
71c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     *
72c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Returns BAD_INDEX if the index is larger than the number of elements.
73c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Returns BAD_TYPE if the index is available but the data is the wrong type.
74c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
75c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    int64_t GetLong(size_t key, status_t* err) const;
76c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    const char* GetString(size_t key, status_t* err) const;
77c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    bool GetBool(size_t key, status_t* err) const;
78c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    float GetFloat(size_t key, status_t* err) const;
79c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
80c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
8180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
8280235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
8380235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     */
8480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(uint32_t value);
8580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(int32_t value);
8680235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(uint64_t value);
8780235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(int64_t value);
8880235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(const string& value);
8980235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(float value);
9080235403d2a80eee1c23dbe856d18194b1b93843Yao Chen
9180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    /**
92c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Return a string representation of this event.
93c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
94c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    string ToString() const;
95c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
96c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
975110bedd787835d4dbc809b7977285cac364bdefYao Chen     * Write this object to a ProtoOutputStream.
98c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
995110bedd787835d4dbc809b7977285cac364bdefYao Chen    void ToProto(android::util::ProtoOutputStream& out) const;
100c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
101729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    /*
102729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen     * Get a KeyValuePair proto object.
103729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen     */
104729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen    KeyValuePair GetKeyValueProto(size_t key) const;
105729093df0dd7d7038ad43b16ecdb59a1404f3b50Yao Chen
1061481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen    /**
1071481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
1081481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     * and prepares the list for reading.
1091481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     */
1101481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen    void init();
1111481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen
112a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu    /**
113a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu     * Set timestamp if the original timestamp is missing.
114a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu     */
115a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu    void setTimestampNs(uint64_t timestampNs) {mTimestampNs = timestampNs;}
116a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu
117d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu    int size() const {
118d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu        return mElements.size();
119d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu    }
120d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu
121c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoprivate:
122c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
123c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Don't copy, it's slower. If we really need this we can add it but let's try to
124c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * avoid it.
125c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
126c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    explicit LogEvent(const LogEvent&);
127c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
128c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
129c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Parses a log_msg into a LogEvent object.
130c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
13180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    void init(android_log_context context);
132c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
133c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    vector<android_log_list_element> mElements;
13480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen
13580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    android_log_context mContext;
13693fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen
13793fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen    uint64_t mTimestampNs;
13893fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen
139c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    int mTagId;
140d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen
141d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen    uint32_t mLogUid;
142c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato};
143c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
144c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace statsd
145c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace os
146c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace android
147c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
148