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 _HILOG_PERSISTER_H
17 #define _HILOG_PERSISTER_H
18 
19 #include <pthread.h>
20 #include <zlib.h>
21 
22 #include <condition_variable>
23 #include <chrono>
24 #include <fstream>
25 #include <iostream>
26 #include <list>
27 #include <memory>
28 #include <string>
29 #include <thread>
30 #include <variant>
31 
32 #include "log_buffer.h"
33 #include "log_filter.h"
34 #include "log_persister_rotator.h"
35 #include "log_compress.h"
36 
37 namespace OHOS {
38 namespace HiviewDFX {
39 using LogPersistQueryResult = struct {
40     int32_t result;
41     uint32_t jobId;
42     uint16_t logType;
43     uint16_t compressAlg;
44     char filePath[FILE_PATH_MAX_LEN];
45     uint32_t fileSize;
46     uint32_t fileNum;
47 } __attribute__((__packed__));
48 
49 class LogPersister : public std::enable_shared_from_this<LogPersister> {
50 public:
51     [[nodiscard]] static std::shared_ptr<LogPersister> CreateLogPersister(HilogBuffer &buffer);
52 
53     ~LogPersister();
54 
55     static int Kill(uint32_t id);
56     static int Query(std::list<LogPersistQueryResult> &results);
57     static int Refresh(uint32_t id);
58     static void Clear();
59 
60     int Init(const PersistRecoveryInfo& msg, bool restore);
61     int Deinit();
62 
63     void Start();
64     void Stop();
65 
66     void FillInfo(LogPersistQueryResult &response);
67 
68 private:
69     explicit LogPersister(HilogBuffer &buffer);
70 
71     static bool CheckRegistered(uint32_t id, const std::string& logPath);
72     static std::shared_ptr<LogPersister> GetLogPersisterById(uint32_t id);
73     static void RegisterLogPersister(const std::shared_ptr<LogPersister>& obj);
74     static void DeregisterLogPersister(const std::shared_ptr<LogPersister>& obj);
75 
76     void NotifyNewLogAvailable();
77 
78     int ReceiveLogLoop();
79 
80     int InitCompression();
81     int InitFileRotator(const PersistRecoveryInfo& msg, bool restore);
82     int WriteLogData(const HilogData& logData);
83     bool WriteUncompressedLogs(std::string& logLine);
84     void WriteCompressedLogs();
85 
86     int PrepareUncompressedFile(const std::string& parentPath, bool restore);
87 
88     std::string m_plainLogFilePath;
89     LogPersisterBuffer *m_mappedPlainLogFile;
90     uint32_t m_plainLogSize = 0;
91     std::unique_ptr<LogCompress> m_compressor;
92     std::unique_ptr<LogPersisterBuffer> m_compressBuffer;
93     std::unique_ptr<LogPersisterRotator> m_fileRotator;
94 
95     std::mutex m_receiveLogCvMtx;
96     std::condition_variable m_receiveLogCv;
97 
98     volatile bool m_stopThread = false;
99     std::thread m_persisterThread;
100 
101     HilogBuffer &m_hilogBuffer;
102     HilogBuffer::ReaderId m_bufReader;
103     LogPersistStartMsg m_startMsg;
104 
105     std::mutex m_initMtx;
106     volatile bool m_inited = false;
107 
108     static std::recursive_mutex s_logPersistersMtx;
109     static std::list<std::shared_ptr<LogPersister>> s_logPersisters;
110 };
111 
112 std::list<std::string> LogDataToFormatedStrings(HilogData *data);
113 } // namespace HiviewDFX
114 } // namespace OHOS
115 #endif
116