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
198a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen#include "FieldValue.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
27c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <string>
28c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <vector>
29c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
30c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace android {
31c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace os {
32c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace statsd {
33c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
349c1debe330006b2b4d5c08a55905789c57369609Yao Chenstruct AttributionNodeInternal {
359c1debe330006b2b4d5c08a55905789c57369609Yao Chen    void set_uid(int32_t id) {
369c1debe330006b2b4d5c08a55905789c57369609Yao Chen        mUid = id;
379c1debe330006b2b4d5c08a55905789c57369609Yao Chen    }
389c1debe330006b2b4d5c08a55905789c57369609Yao Chen
399c1debe330006b2b4d5c08a55905789c57369609Yao Chen    void set_tag(const std::string& value) {
409c1debe330006b2b4d5c08a55905789c57369609Yao Chen        mTag = value;
419c1debe330006b2b4d5c08a55905789c57369609Yao Chen    }
429c1debe330006b2b4d5c08a55905789c57369609Yao Chen
439c1debe330006b2b4d5c08a55905789c57369609Yao Chen    int32_t uid() const {
449c1debe330006b2b4d5c08a55905789c57369609Yao Chen        return mUid;
459c1debe330006b2b4d5c08a55905789c57369609Yao Chen    }
469c1debe330006b2b4d5c08a55905789c57369609Yao Chen
479c1debe330006b2b4d5c08a55905789c57369609Yao Chen    const std::string& tag() const {
489c1debe330006b2b4d5c08a55905789c57369609Yao Chen        return mTag;
499c1debe330006b2b4d5c08a55905789c57369609Yao Chen    }
509c1debe330006b2b4d5c08a55905789c57369609Yao Chen
519c1debe330006b2b4d5c08a55905789c57369609Yao Chen    int32_t mUid;
529c1debe330006b2b4d5c08a55905789c57369609Yao Chen    std::string mTag;
539c1debe330006b2b4d5c08a55905789c57369609Yao Chen};
54c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato/**
55c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Wrapper for the log_msg structure.
56c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */
57c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoclass LogEvent {
58c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratopublic:
59c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
60c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Read a LogEvent from a log_msg.
61c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
62a3bf0509e01c6b32a13cfd16060e4a537706f231David Chen    explicit LogEvent(log_msg& msg);
63c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
64c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
6580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
66c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
67330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs);
68330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac
69330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    // For testing. The timestamp is used as both elapsed real time and logd timestamp.
70330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    explicit LogEvent(int32_t tagId, int64_t timestampNs);
711481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen
72c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    ~LogEvent();
73c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
74c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
75c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the timestamp associated with this event.
76c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
77330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; }
78330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; }
79c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
80c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
81c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the tag for this event.
82c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
8368985805f26f5da6cc6b63934cb00fbff2741ca9Yangster-mac    inline int GetTagId() const { return mTagId; }
84c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
8568985805f26f5da6cc6b63934cb00fbff2741ca9Yangster-mac    inline uint32_t GetUid() const {
86d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen        return mLogUid;
87d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen    }
88d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen
89c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
90c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Get the nth value, starting at 1.
91c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     *
92c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Returns BAD_INDEX if the index is larger than the number of elements.
93c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Returns BAD_TYPE if the index is available but the data is the wrong type.
94c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
95c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    int64_t GetLong(size_t key, status_t* err) const;
9680f9112aecf08845ef0b2b132d721b7ce850970fChenjie Yu    int GetInt(size_t key, status_t* err) const;
97c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    const char* GetString(size_t key, status_t* err) const;
98c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    bool GetBool(size_t key, status_t* err) const;
99c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    float GetFloat(size_t key, status_t* err) const;
100c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
101c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
10280235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
10380235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
10480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen     */
10580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(uint32_t value);
10680235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(int32_t value);
10780235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(uint64_t value);
10880235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(int64_t value);
1099c1debe330006b2b4d5c08a55905789c57369609Yao Chen    bool write(const std::string& value);
11080235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    bool write(float value);
1119c1debe330006b2b4d5c08a55905789c57369609Yao Chen    bool write(const std::vector<AttributionNodeInternal>& nodes);
1129c1debe330006b2b4d5c08a55905789c57369609Yao Chen    bool write(const AttributionNodeInternal& node);
11380235403d2a80eee1c23dbe856d18194b1b93843Yao Chen
11480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    /**
115c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Return a string representation of this event.
116c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
1179c1debe330006b2b4d5c08a55905789c57369609Yao Chen    std::string ToString() const;
118c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
119c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
1205110bedd787835d4dbc809b7977285cac364bdefYao Chen     * Write this object to a ProtoOutputStream.
121c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
1225110bedd787835d4dbc809b7977285cac364bdefYao Chen    void ToProto(android::util::ProtoOutputStream& out) const;
123c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
1241481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen    /**
1251481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
1261481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     * and prepares the list for reading.
1271481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen     */
1281481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen    void init();
1291481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen
130a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu    /**
131330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac     * Set elapsed timestamp if the original timestamp is missing.
132a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu     */
133330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    void setElapsedTimestampNs(int64_t timestampNs) {
134330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac        mElapsedTimestampNs = timestampNs;
135330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    }
136330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac
137330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    /**
138330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac     * Set the timestamp if the original logd timestamp is missing.
139330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac     */
140330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    void setLogdWallClockTimestampNs(int64_t timestampNs) {
141330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac        mLogdTimestampNs = timestampNs;
142330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    }
143a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu
1442087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac    inline int size() const {
1458a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen        return mValues.size();
146d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu    }
147d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu
1488a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    const std::vector<FieldValue>& getValues() const {
1498a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen        return mValues;
1508a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    }
151d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac
1528a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    std::vector<FieldValue>* getMutableValues() {
1538a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen        return &mValues;
1548a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    }
1552087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac
156c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoprivate:
157c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
158c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Don't copy, it's slower. If we really need this we can add it but let's try to
159c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * avoid it.
160c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
161c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    explicit LogEvent(const LogEvent&);
162c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
163c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    /**
164c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     * Parses a log_msg into a LogEvent object.
165c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato     */
16680235403d2a80eee1c23dbe856d18194b1b93843Yao Chen    void init(android_log_context context);
167c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
1688a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    // The items are naturally sorted in DFS order as we read them. this allows us to do fast
1698a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    // matching.
1708a8d16ceea1e5b7a2f8c41e17b5d993035f50f5dYao Chen    std::vector<FieldValue> mValues;
17180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen
1722087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac    // This field is used when statsD wants to create log event object and write fields to it. After
1732087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac    // calling init() function, this object would be destroyed to save memory usage.
1742087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac    // When the log event is created from log msg, this field is never initiated.
17548d75189ccbfc06f24f315ff5b8a1a42cf8207c5Yao Chen    android_log_context mContext = NULL;
17693fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen
177330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    // The timestamp set by the logd.
178330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    int64_t mLogdTimestampNs;
179330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac
180330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    // The elapsed timestamp set by statsd log writer.
181330af58f2b8582b855085655fae553cdfaf44e6cYangster-mac    int64_t mElapsedTimestampNs;
18293fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen
183c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato    int mTagId;
184d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen
185d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen    uint32_t mLogUid;
186c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato};
187c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
188c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace statsd
189c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace os
190c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}  // namespace android
191c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato
192