• 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 #include <cstdint>
17 #include <ctime>
18 #include <mutex>
19 #include <string>
20 #include <vector>
21 
22 #include "constants.h"
23 #include "faultlog_info.h"
24 #include "string_util.h"
25 #include "time_util.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 constexpr int DEFAULT_BUFFER_SIZE = 64;
31 } // namespace
32 
GetFormatedTime(uint64_t target)33 std::string GetFormatedTime(uint64_t target)
34 {
35     time_t now = time(nullptr);
36     if (target > static_cast<uint64_t>(now)) {
37         target = target / 1000; // 1000 : convert millisecond to seconds
38     }
39 
40     time_t out = static_cast<time_t>(target);
41     struct tm tmStruct {0};
42     struct tm* timeInfo = localtime_r(&out, &tmStruct);
43     if (timeInfo == nullptr) {
44         return "00000000000000";
45     }
46 
47     char buf[DEFAULT_BUFFER_SIZE] = {0};
48     strftime(buf, DEFAULT_BUFFER_SIZE - 1, "%Y%m%d%H%M%S", timeInfo);
49     return std::string(buf, strlen(buf));
50 }
51 
GetFaultNameByType(int32_t faultType,bool asFileName)52 std::string GetFaultNameByType(int32_t faultType, bool asFileName)
53 {
54     switch (faultType) {
55         case FaultLogType::JS_CRASH:
56             return asFileName ? "jscrash" : "JS_ERROR";
57         case FaultLogType::CPP_CRASH:
58             return asFileName ? "cppcrash" : "CPP_CRASH";
59         case FaultLogType::APP_FREEZE:
60             return asFileName ? "appfreeze" : "APP_FREEZE";
61         case FaultLogType::SYS_FREEZE:
62             return asFileName ? "sysfreeze" : "SYS_FREEZE";
63         default:
64             break;
65     }
66     return "Unknown";
67 }
68 
GetFaultLogName(const FaultLogInfo & info)69 std::string GetFaultLogName(const FaultLogInfo& info)
70 {
71     std::string name = info.module;
72     if (name.find("/") != std::string::npos) {
73         name = info.module.substr(info.module.find_last_of("/") + 1);
74     }
75 
76     std::string ret = "";
77     ret.append(GetFaultNameByType(info.faultLogType, true));
78     ret.append("-");
79     ret.append(name);
80     ret.append("-");
81     ret.append(std::to_string(info.id));
82     ret.append("-");
83     ret.append(GetFormatedTime(info.time));
84     return ret;
85 }
86 
GetLogTypeByName(const std::string & type)87 int32_t GetLogTypeByName(const std::string& type)
88 {
89     if (type == "jscrash") {
90         return FaultLogType::JS_CRASH;
91     } else if (type == "cppcrash") {
92         return FaultLogType::CPP_CRASH;
93     } else if (type == "appfreeze") {
94         return FaultLogType::APP_FREEZE;
95     } else if (type == "sysfreeze") {
96         return FaultLogType::SYS_FREEZE;
97     } else if (type == "all" || type == "ALL") {
98         return FaultLogType::ALL;
99     } else {
100         return -1;
101     }
102 }
103 
ExtractInfoFromFileName(const std::string & fileName)104 FaultLogInfo ExtractInfoFromFileName(const std::string& fileName)
105 {
106     // FileName LogType-PackageName-Uid-YYYYMMDDHHMMSS
107     FaultLogInfo info;
108     std::vector<std::string> splitStr;
109     const int32_t expectedVecSize = 4;
110     StringUtil::SplitStr(fileName, "-", splitStr);
111     if (splitStr.size() == expectedVecSize) {
112         info.faultLogType = GetLogTypeByName(splitStr[0]);                 // 0 : index of log type
113         info.module = splitStr[1];                                         // 1 : index of module name
114         StringUtil::ConvertStringTo<int32_t>(splitStr[2], info.id);        // 2 : index of uid
115         info.time = TimeUtil::StrToTimeStamp(splitStr[3], "%Y%m%d%H%M%S"); // 3 : index of timestamp
116     }
117     info.pid = 0;
118     return info;
119 }
120 
ExtractInfoFromTempFile(const std::string & fileName)121 FaultLogInfo ExtractInfoFromTempFile(const std::string& fileName)
122 {
123     // FileName LogType-pid-time
124     FaultLogInfo info;
125     std::vector<std::string> splitStr;
126     const int32_t expectedVecSize = 3;
127     StringUtil::SplitStr(fileName, "-", splitStr);
128     if (splitStr.size() == expectedVecSize) {
129         info.faultLogType = GetLogTypeByName(splitStr[0]);                 // 0 : index of log type
130         StringUtil::ConvertStringTo<int32_t>(splitStr[1], info.pid);       // 1 : index of pid
131         StringUtil::ConvertStringTo<int64_t>(splitStr[2], info.time);      // 2 : index of timestamp
132     }
133     return info;
134 }
135 
RegulateModuleNameIfNeed(const std::string & name)136 std::string RegulateModuleNameIfNeed(const std::string& name)
137 {
138     std::vector<std::string> splitStr;
139     StringUtil::SplitStr(name, "/\\", splitStr);
140     auto size = splitStr.size();
141     if (size > 0) {
142         return splitStr[size - 1];
143     }
144     return name;
145 }
146 } // namespace HiviewDFX
147 } // namespace OHOS
148