• 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 _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 
58     int Init(const PersistRecoveryInfo& msg, bool restore);
59     int Deinit();
60 
61     void Start();
62     void Stop();
63 
64     void FillInfo(LogPersistQueryResult &response);
65 
66 private:
67     explicit LogPersister(HilogBuffer &buffer);
68 
69     static bool CheckRegistered(uint32_t id, const std::string& logPath);
70     static std::shared_ptr<LogPersister> GetLogPersisterById(uint32_t id);
71     static void RegisterLogPersister(const std::shared_ptr<LogPersister>& obj);
72     static void DeregisterLogPersister(const std::shared_ptr<LogPersister>& obj);
73 
74     void NotifyNewLogAvailable();
75 
76     int ReceiveLogLoop();
77 
78     int InitCompression();
79     int InitFileRotator(const PersistRecoveryInfo& msg, bool restore);
80     int WriteLogData(const HilogData& logData);
81     bool WriteUncompressedLogs(std::string& logLine);
82     void WriteCompressedLogs();
83 
84     int PrepareUncompressedFile(const std::string& parentPath, bool restore);
85 
86     std::string m_plainLogFilePath;
87     LogPersisterBuffer *m_mappedPlainLogFile;
88     uint32_t m_plainLogSize = 0;
89     std::unique_ptr<LogCompress> m_compressor;
90     std::unique_ptr<LogPersisterBuffer> m_compressBuffer;
91     std::unique_ptr<LogPersisterRotator> m_fileRotator;
92 
93     std::mutex m_receiveLogCvMtx;
94     std::condition_variable m_receiveLogCv;
95 
96     volatile bool m_stopThread = false;
97     std::thread m_persisterThread;
98 
99     HilogBuffer &m_hilogBuffer;
100     HilogBuffer::ReaderId m_bufReader;
101     LogPersistStartMsg m_startMsg;
102 
103     std::mutex m_initMtx;
104     volatile bool m_inited = false;
105 
106     static std::recursive_mutex s_logPersistersMtx;
107     static std::list<std::shared_ptr<LogPersister>> s_logPersisters;
108 };
109 
110 std::list<std::string> LogDataToFormatedStrings(HilogData *data);
111 } // namespace HiviewDFX
112 } // namespace OHOS
113 #endif
114