• 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 #ifndef LOG_UTILS_H
16 #define LOG_UTILS_H
17 
18 #include <string>
19 #include <vector>
20 #include <unordered_map>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 template<typename K, typename V>
25 class KVMap {
26 using ValueCmp = std::function<bool(const V& v1, const V& v2)>;
27 public:
28     KVMap(std::unordered_map<K, V> map, K def_k, V def_v,
29         ValueCmp cmp = [](const V& v1, const V& v2) { return v1 == v2; })
str_map(map)30         : str_map(map), def_key(def_k), def_value(def_v), compare(cmp)
31     {
32     }
33 
GetValue(K key)34     const V& GetValue(K key) const
35     {
36         auto it = str_map.find(key);
37         return it == str_map.end() ? def_value : it->second;
38     }
39 
GetKey(const V & value)40     const K GetKey(const V& value) const
41     {
42         for (auto& it : str_map) {
43             if (compare(value, it.second)) {
44                 return it.first;
45             }
46         }
47         return def_key;
48     }
49 
GetAllKeys()50     std::vector<K> GetAllKeys() const
51     {
52         std::vector<K> keys;
53         for (auto& it : str_map) {
54             keys.push_back(it.first);
55         }
56         return keys;
57     }
58 
IsValidKey(K key)59     bool IsValidKey(K key) const
60     {
61         return (str_map.find(key) != str_map.end());
62     }
63 
64 private:
65     const std::unordered_map<K, V> str_map;
66     const K def_key;
67     const V def_value;
68     const ValueCmp compare;
69 };
70 using StringMap = KVMap<uint16_t, std::string>;
71 std::string ErrorCode2Str(int16_t errorCode);
72 std::string LogType2Str(uint16_t logType);
73 uint16_t Str2LogType(const std::string& str);
74 std::string ComboLogType2Str(uint16_t shiftType);
75 uint16_t Str2ComboLogType(const std::string& str);
76 std::vector<uint16_t> GetAllLogTypes();
77 std::string LogLevel2Str(uint16_t logLevel);
78 uint16_t Str2LogLevel(const std::string& str);
79 std::string LogLevel2ShortStr(uint16_t logLevel);
80 uint16_t ShortStr2LogLevel(const std::string& str);
81 uint16_t PrettyStr2LogLevel(const std::string& str);
82 std::string ComboLogLevel2Str(uint16_t shiftLevel);
83 uint16_t Str2ComboLogLevel(const std::string& str);
84 std::string ShowFormat2Str(uint16_t showFormat);
85 uint16_t Str2ShowFormat(const std::string& str);
86 std::string Size2Str(uint64_t size);
87 uint64_t Str2Size(const std::string& str);
88 
89 constexpr char DEFAULT_SPLIT_DELIMIT[] = ",";
90 void Split(const std::string& src, std::vector<std::string>& dest,
91            const std::string& separator = DEFAULT_SPLIT_DELIMIT);
92 uint32_t GetBitsCount(uint64_t n);
93 uint16_t GetBitPos(uint64_t n);
94 
95 std::string Uint2DecStr(uint32_t i);
96 uint32_t DecStr2Uint(const std::string& str);
97 std::string Uint2HexStr(uint32_t i);
98 uint32_t HexStr2Uint(const std::string& str);
99 
100 #if !defined(__WINDOWS__) and !defined(__LINUX__)
101 std::string GetProgName();
102 #endif
103 std::string GetNameByPid(uint32_t pid);
104 uint32_t GetPPidByPid(uint32_t pid);
105 uint64_t GenerateHash(const char *p, size_t size);
106 void PrintErrorno(int err);
107 int WaitingToDo(int max, const std::string& path, std::function<int(const std::string &path)> func);
108 } // namespace HiviewDFX
109 } // namespace OHOS
110 #endif // LOG_UTILS_H