• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LOG_BUFFER_H
17 #define LOG_BUFFER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <map>
23 #include <memory>
24 #include <shared_mutex>
25 
26 #include "log_data.h"
27 #include "log_filter.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 class HilogBuffer {
32 public:
33     using LogMsgContainer = std::list<HilogData>;
34     using ReaderId = uintptr_t;
35     using OnFound = std::function<void(const HilogData&)>;
36 
37     HilogBuffer();
38     ~HilogBuffer();
39 
40     size_t Insert(const HilogMsg& msg);
41     bool Query(const LogFilterExt& filter, const ReaderId& id, OnFound onFound);
42 
43     ReaderId CreateBufReader(std::function<void()> onNewDataCallback);
44     void RemoveBufReader(const ReaderId& id);
45 
46     int32_t Delete(uint16_t logType);
47     int64_t GetBuffLen(uint16_t logType);
48     int32_t SetBuffLen(uint16_t logType, uint64_t buffSize);
49     int32_t GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped);
50     int32_t GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped);
51     int32_t ClearStatisticInfoByLog(uint16_t logType);
52     int32_t ClearStatisticInfoByDomain(uint32_t domain);
53 
54     static bool LogMatchFilter(const LogFilterExt& filter, const HilogData& logData);
55 private:
56     struct BufferReader {
57         LogMsgContainer::iterator m_pos;
58         LogMsgContainer* m_msgList = nullptr;
59         std::function<void()> m_onNewDataCallback;
60     };
61 
62     void UpdateStatistics(const HilogData& logData);
63     void OnDeleteItem(LogMsgContainer::iterator itemPos);
64     void OnPushBackedItem(LogMsgContainer& msgList);
65     void OnNewItem(LogMsgContainer& msgList);
66     std::shared_ptr<BufferReader> GetReader(const ReaderId& id);
67 
68     size_t size;
69     size_t sizeByType[LOG_TYPE_MAX];
70     LogMsgContainer hilogDataList;
71     LogMsgContainer hilogKlogList;
72     std::shared_mutex hilogBufferMutex;
73     std::map<uint32_t, uint64_t> cacheLenByDomain;
74     std::map<uint32_t, uint64_t> printLenByDomain;
75     std::map<uint32_t, uint64_t> droppedByDomain;
76     uint64_t cacheLenByType[LOG_TYPE_MAX];
77     uint64_t droppedByType[LOG_TYPE_MAX];
78     uint64_t printLenByType[LOG_TYPE_MAX];
79 
80     std::map<ReaderId, std::shared_ptr<BufferReader>> m_logReaders;
81     std::shared_mutex m_logReaderMtx;
82 };
83 } // namespace HiviewDFX
84 } // namespace OHOS
85 #endif
86