• 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 #include "dump_log_manager.h"
16 #include "directory_ex.h"
17 #include "common/dumper_constant.h"
18 #include "hilog_wrapper.h"
19 namespace OHOS {
20 namespace HiviewDFX {
21 namespace {
22 static const std::string TMP_FOLDER = "/data/log/hidumper/tmp/";
23 static const int LOGFILE_MAX = 10;
24 } // namespace
DumpLogManager()25 DumpLogManager::DumpLogManager()
26 {
27 }
28 
~DumpLogManager()29 DumpLogManager::~DumpLogManager()
30 {
31 }
32 
Init()33 bool DumpLogManager::Init()
34 {
35     DUMPER_HILOGD(MODULE_COMMON, "Init enter|");
36 
37     ForceRemoveDirectory(TMP_FOLDER);
38     ForceCreateDirectory(TMP_FOLDER);
39     ForceCreateDirectory(ZIP_FOLDER);
40     EraseLogs();
41 
42     DUMPER_HILOGD(MODULE_COMMON, "Init leave|");
43     return true;
44 }
45 
Uninit()46 void DumpLogManager::Uninit()
47 {
48     ForceRemoveDirectory(TMP_FOLDER);
49     EraseLogs();
50 }
51 
EraseLogs()52 void DumpLogManager::EraseLogs()
53 {
54     DUMPER_HILOGD(MODULE_COMMON, "EraseLogs enter|");
55 
56     std::vector<std::string> allFiles;
57     GetDirFiles(ZIP_FOLDER, allFiles);
58 
59     std::vector<std::string> zipFiles;
60     for (std::string str : allFiles) {
61         std::string filePath = IncludeTrailingPathDelimiter(ExtractFilePath(str));
62         std::string fileName = ExtractFileName(str);
63         std::string fileExt = ExtractFileExt(fileName);
64         DUMPER_HILOGD(MODULE_COMMON, "EraseLogs debug|"
65             "str=[%{public}s], filePath=[%{public}s], fileName=[%{public}s], fileExt=[%{public}s]",
66             str.c_str(), filePath.c_str(), fileName.c_str(), fileExt.c_str());
67 
68         if ((filePath != ZIP_FOLDER) || (fileExt != ZIP_FILEEXT)) {
69             DUMPER_HILOGD(MODULE_COMMON, "EraseLogs debug|skip, str=[%{public}s]", str.c_str());
70             continue;
71         }
72 
73         DUMPER_HILOGD(MODULE_COMMON, "EraseLogs debug|add, str=[%{public}s]", str.c_str());
74         zipFiles.push_back(str);
75     }
76     allFiles.clear();
77 
78     std::sort(zipFiles.begin(), zipFiles.end(),
79         [] (const std::string &left, const std::string &right) {
80         return (right.compare(left) > 0);
81     });
82 
83     int sum = static_cast<int>(zipFiles.size()) - LOGFILE_MAX;
84     DUMPER_HILOGD(MODULE_COMMON, "EraseLogs debug|sum=%{public}d", sum);
85     for (int i = 0; i < sum; i++) {
86         std::string &str = zipFiles[i];
87         DUMPER_HILOGD(MODULE_COMMON, "EraseLogs debug|del, str=[%{public}s]", str.c_str());
88         RemoveFile(str);
89     }
90 
91     DUMPER_HILOGD(MODULE_COMMON, "EraseLogs leave|");
92 }
93 
CreateTmpFolder(uint32_t id)94 std::string DumpLogManager::CreateTmpFolder(uint32_t id)
95 {
96     DUMPER_HILOGD(MODULE_COMMON, "CreateTmpFolder enter|id=%{public}d", id);
97 
98     std::string ret = IncludeTrailingPathDelimiter(TMP_FOLDER + std::to_string(id));
99     bool res = ForceCreateDirectory(ret);
100 
101     DUMPER_HILOGD(MODULE_COMMON, "CreateTmpFolder leave|ret=%{public}s, res=[%{public}d]", ret.c_str(), res);
102     return ret;
103 }
104 
EraseTmpFolder(uint32_t id)105 bool DumpLogManager::EraseTmpFolder(uint32_t id)
106 {
107     DUMPER_HILOGD(MODULE_COMMON, "EraseTmpFolder enter|id=%{public}d", id);
108 
109     std::string folder = IncludeTrailingPathDelimiter(TMP_FOLDER + std::to_string(id));
110     bool ret = ForceRemoveDirectory(folder);
111 
112     DUMPER_HILOGD(MODULE_COMMON, "EraseTmpFolder leave|ret=[%{public}d]", ret);
113     return ret;
114 }
115 } // namespace HiviewDFX
116 } // namespace OHOS
117