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 <hilog_common.h> 27 28 #include "log_data.h" 29 #include "log_filter.h" 30 #include "log_stats.h" 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 class HilogBuffer { 35 public: 36 using LogMsgContainer = std::list<HilogData>; 37 using ReaderId = uintptr_t; 38 39 HilogBuffer(); 40 ~HilogBuffer(); 41 42 size_t Insert(const HilogMsg& msg); 43 std::optional<HilogData> Query(const LogFilter& filter, const ReaderId& id, int tailCount = 0); 44 45 ReaderId CreateBufReader(std::function<void()> onNewDataCallback); 46 void RemoveBufReader(const ReaderId& id); 47 48 int32_t Delete(uint16_t logType); 49 50 void InitBuffLen(); 51 void InitBuffHead(); 52 int64_t GetBuffLen(uint16_t logType); 53 int32_t SetBuffLen(uint16_t logType, uint64_t buffSize); 54 55 void CountLog(const StatsInfo &info); 56 void ResetStats(); 57 LogStats& GetStatsInfo(); 58 59 private: 60 struct BufferReader { 61 LogMsgContainer::iterator m_pos; 62 LogMsgContainer* m_msgList = nullptr; 63 uint32_t skipped; 64 std::function<void()> m_onNewDataCallback; 65 }; 66 enum class DeleteReason { 67 BUFF_OVERFLOW, 68 CMD_CLEAR 69 }; 70 void OnDeleteItem(LogMsgContainer::iterator itemPos, DeleteReason reason); 71 void OnPushBackedItem(LogMsgContainer& msgList); 72 void OnNewItem(LogMsgContainer& msgList); 73 std::shared_ptr<BufferReader> GetReader(const ReaderId& id); 74 75 size_t sizeByType[LOG_TYPE_MAX]; 76 LogMsgContainer hilogDataList; 77 LogMsgContainer hilogKlogList; 78 std::shared_mutex hilogBufferMutex; 79 std::map<ReaderId, std::shared_ptr<BufferReader>> m_logReaders; 80 std::shared_mutex m_logReaderMtx; 81 LogStats stats; 82 }; 83 } // namespace HiviewDFX 84 } // namespace OHOS 85 #endif 86