LogEvent.h revision d40053eb8bcb19c7c3b080a36714566bb4e4a748
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 192087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac#include "field_util.h" 20c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" 21c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 225110bedd787835d4dbc809b7977285cac364bdefYao Chen#include <android/util/ProtoOutputStream.h> 23c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <log/log_event_list.h> 24c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <log/log_read.h> 2580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen#include <private/android_logger.h> 265110bedd787835d4dbc809b7977285cac364bdefYao Chen#include <utils/Errors.h> 272087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac#include <utils/JenkinsHash.h> 28c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 291481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen#include <memory> 30c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <string> 312087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac#include <map> 32c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato#include <vector> 33c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 34c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace android { 35c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace os { 36c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratonamespace statsd { 37c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 38c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratousing std::string; 39c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratousing std::vector; 40c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 41c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato/** 42c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Wrapper for the log_msg structure. 43c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 44c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoclass LogEvent { 45c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratopublic: 46c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 47c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Read a LogEvent from a log_msg. 48c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 49a3bf0509e01c6b32a13cfd16060e4a537706f231David Chen explicit LogEvent(log_msg& msg); 50c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 51c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 5280235403d2a80eee1c23dbe856d18194b1b93843Yao Chen * Constructs a LogEvent with synthetic data for testing. Must call init() before reading. 53c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 5480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen explicit LogEvent(int32_t tagId, uint64_t timestampNs); 551481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen 56c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato ~LogEvent(); 57c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 58c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 59c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Get the timestamp associated with this event. 60c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 61c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato uint64_t GetTimestampNs() const { return mTimestampNs; } 62c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 63c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 64c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Get the tag for this event. 65c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 66c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato int GetTagId() const { return mTagId; } 67c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 68d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen uint32_t GetUid() const { 69d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen return mLogUid; 70d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen } 71d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen 72c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 73c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Get the nth value, starting at 1. 74c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * 75c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Returns BAD_INDEX if the index is larger than the number of elements. 76c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Returns BAD_TYPE if the index is available but the data is the wrong type. 77c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 78c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato int64_t GetLong(size_t key, status_t* err) const; 79c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato const char* GetString(size_t key, status_t* err) const; 80c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato bool GetBool(size_t key, status_t* err) const; 81c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato float GetFloat(size_t key, status_t* err) const; 82c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 832087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac /* 842087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac * Get DimensionsValue proto objects from FieldMatcher. 852087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac */ 862087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac void GetAtomDimensionsValueProtos( 872087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac const FieldMatcher& matcher, std::vector<DimensionsValue> *dimensionsValues) const; 882087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac bool GetAtomDimensionsValueProto( 892087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac const FieldMatcher& matcher, DimensionsValue* dimensionsValue) const; 902087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac 912087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac /* 922087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac * Get a DimensionsValue proto objects from Field. 932087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac */ 942087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac bool GetSimpleAtomDimensionsValueProto(size_t field, DimensionsValue* dimensionsValue) const; 952087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac DimensionsValue GetSimpleAtomDimensionsValueProto(size_t atomField) const; 962087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac 97c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 9880235403d2a80eee1c23dbe856d18194b1b93843Yao Chen * Write test data to the LogEvent. This can only be used when the LogEvent is constructed 9980235403d2a80eee1c23dbe856d18194b1b93843Yao Chen * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it. 10080235403d2a80eee1c23dbe856d18194b1b93843Yao Chen */ 10180235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(uint32_t value); 10280235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(int32_t value); 10380235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(uint64_t value); 10480235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(int64_t value); 10580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(const string& value); 10680235403d2a80eee1c23dbe856d18194b1b93843Yao Chen bool write(float value); 1072087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac bool write(const std::vector<AttributionNode>& nodes); 1082087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac bool write(const AttributionNode& node); 10980235403d2a80eee1c23dbe856d18194b1b93843Yao Chen 11080235403d2a80eee1c23dbe856d18194b1b93843Yao Chen /** 111c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Return a string representation of this event. 112c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 113c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato string ToString() const; 114c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 115c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 1165110bedd787835d4dbc809b7977285cac364bdefYao Chen * Write this object to a ProtoOutputStream. 117c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 1185110bedd787835d4dbc809b7977285cac364bdefYao Chen void ToProto(android::util::ProtoOutputStream& out) const; 119c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 1201481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen /** 1211481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen * Used with the constructor where tag is passed in. Converts the log_event_list to read mode 1221481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen * and prepares the list for reading. 1231481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen */ 1241481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen void init(); 1251481fe142d36d5f0b36eeebc358d5a8aef7bf28aDavid Chen 126a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu /** 127a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu * Set timestamp if the original timestamp is missing. 128a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu */ 129a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu void setTimestampNs(uint64_t timestampNs) {mTimestampNs = timestampNs;} 130a7259abde4e89fd91404b14b4845113cd313d1ecChenjie Yu 1312087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac inline int size() const { 1322087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac return mFieldValueMap.size(); 133d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu } 134d9dfda76e0aa8b8aa4d2562a7ff4259f4500e355Chenjie Yu 135d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac /** 136d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac * Returns the mutable DimensionsValue proto for the specific the field. 137d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac */ 138d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac DimensionsValue* findFieldValueOrNull(const Field& field); 139d40053eb8bcb19c7c3b080a36714566bb4e4a748Yangster-mac 1402087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac inline const FieldValueMap& getFieldValueMap() const { return mFieldValueMap; } 1412087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac 142c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onoratoprivate: 143c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 144c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Don't copy, it's slower. If we really need this we can add it but let's try to 145c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * avoid it. 146c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 147c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato explicit LogEvent(const LogEvent&); 148c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 149c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato /** 150c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato * Parses a log_msg into a LogEvent object. 151c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato */ 15280235403d2a80eee1c23dbe856d18194b1b93843Yao Chen void init(android_log_context context); 153c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 1542087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac FieldValueMap mFieldValueMap; 15580235403d2a80eee1c23dbe856d18194b1b93843Yao Chen 1562087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac // This field is used when statsD wants to create log event object and write fields to it. After 1572087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac // calling init() function, this object would be destroyed to save memory usage. 1582087716f2bdca90c7c3034d556ac12911bd8018eYangster-mac // When the log event is created from log msg, this field is never initiated. 15980235403d2a80eee1c23dbe856d18194b1b93843Yao Chen android_log_context mContext; 16093fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen 16193fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen uint64_t mTimestampNs; 16293fe3a34a02c673eaee4a2d18565ba8df20685cbYao Chen 163c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato int mTagId; 164d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen 165d10f7b1c7bdb1c66aa04148945cae9733ee4cadfYao Chen uint32_t mLogUid; 166c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato}; 167c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 168c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato} // namespace statsd 169c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato} // namespace os 170c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato} // namespace android 171c4dfae56c10a1dd571baa78c750f2e68c919d74fJoe Onorato 172