12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#ifndef LOG_BUFFER_H
172498b56bSopenharmony_ci#define LOG_BUFFER_H
182498b56bSopenharmony_ci
192498b56bSopenharmony_ci#include <cstdint>
202498b56bSopenharmony_ci#include <functional>
212498b56bSopenharmony_ci#include <list>
222498b56bSopenharmony_ci#include <map>
232498b56bSopenharmony_ci#include <memory>
242498b56bSopenharmony_ci#include <shared_mutex>
252498b56bSopenharmony_ci
262498b56bSopenharmony_ci#include <hilog_common.h>
272498b56bSopenharmony_ci
282498b56bSopenharmony_ci#include "log_data.h"
292498b56bSopenharmony_ci#include "log_filter.h"
302498b56bSopenharmony_ci#include "log_stats.h"
312498b56bSopenharmony_ci
322498b56bSopenharmony_cinamespace OHOS {
332498b56bSopenharmony_cinamespace HiviewDFX {
342498b56bSopenharmony_ciclass HilogBuffer {
352498b56bSopenharmony_cipublic:
362498b56bSopenharmony_ci    using LogMsgContainer = std::list<HilogData>;
372498b56bSopenharmony_ci    using ReaderId = uintptr_t;
382498b56bSopenharmony_ci
392498b56bSopenharmony_ci    HilogBuffer(bool isSupportSkipLog);
402498b56bSopenharmony_ci    ~HilogBuffer();
412498b56bSopenharmony_ci
422498b56bSopenharmony_ci    size_t Insert(const HilogMsg& msg, bool& isFull);
432498b56bSopenharmony_ci    std::optional<HilogData> Query(const LogFilter& filter, const ReaderId& id, int tailCount = 0);
442498b56bSopenharmony_ci
452498b56bSopenharmony_ci    ReaderId CreateBufReader(std::function<void()> onNewDataCallback);
462498b56bSopenharmony_ci    void RemoveBufReader(const ReaderId& id);
472498b56bSopenharmony_ci
482498b56bSopenharmony_ci    int32_t Delete(uint16_t logType);
492498b56bSopenharmony_ci
502498b56bSopenharmony_ci    void InitBuffLen();
512498b56bSopenharmony_ci    void InitBuffHead();
522498b56bSopenharmony_ci    int64_t GetBuffLen(uint16_t logType);
532498b56bSopenharmony_ci    int32_t SetBuffLen(uint16_t logType, uint64_t buffSize);
542498b56bSopenharmony_ci
552498b56bSopenharmony_ci    void CountLog(const StatsInfo &info);
562498b56bSopenharmony_ci    void ResetStats();
572498b56bSopenharmony_ci    LogStats& GetStatsInfo();
582498b56bSopenharmony_ci
592498b56bSopenharmony_ciprivate:
602498b56bSopenharmony_ci    struct BufferReader {
612498b56bSopenharmony_ci        LogMsgContainer::iterator m_pos;
622498b56bSopenharmony_ci        LogMsgContainer* m_msgList = nullptr;
632498b56bSopenharmony_ci        uint32_t skipped;
642498b56bSopenharmony_ci        std::function<void()> m_onNewDataCallback;
652498b56bSopenharmony_ci    };
662498b56bSopenharmony_ci    enum class DeleteReason {
672498b56bSopenharmony_ci        BUFF_OVERFLOW,
682498b56bSopenharmony_ci        CMD_CLEAR
692498b56bSopenharmony_ci    };
702498b56bSopenharmony_ci    bool IsItemUsed(LogMsgContainer::iterator itemPos);
712498b56bSopenharmony_ci    void OnDeleteItem(LogMsgContainer::iterator itemPos, DeleteReason reason);
722498b56bSopenharmony_ci    void OnPushBackedItem(LogMsgContainer& msgList);
732498b56bSopenharmony_ci    void OnNewItem(LogMsgContainer& msgList);
742498b56bSopenharmony_ci    std::shared_ptr<BufferReader> GetReader(const ReaderId& id);
752498b56bSopenharmony_ci
762498b56bSopenharmony_ci    size_t sizeByType[LOG_TYPE_MAX];
772498b56bSopenharmony_ci    LogMsgContainer hilogDataList;
782498b56bSopenharmony_ci    std::shared_mutex hilogBufferMutex;
792498b56bSopenharmony_ci    std::map<ReaderId, std::shared_ptr<BufferReader>> m_logReaders;
802498b56bSopenharmony_ci    std::shared_mutex m_logReaderMtx;
812498b56bSopenharmony_ci    LogStats stats;
822498b56bSopenharmony_ci    bool m_isSupportSkipLog;
832498b56bSopenharmony_ci};
842498b56bSopenharmony_ci} // namespace HiviewDFX
852498b56bSopenharmony_ci} // namespace OHOS
862498b56bSopenharmony_ci#endif
87