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
22namespace OHOS {
23namespace HiviewDFX {
24template<typename K, typename V>
25class KVMap {
26using ValueCmp = std::function<bool(const V& v1, const V& v2)>;
27public:
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; })
30        : str_map(map), def_key(def_k), def_value(def_v), compare(cmp)
31    {
32    }
33
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
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
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
59    bool IsValidKey(K key) const
60    {
61        return (str_map.find(key) != str_map.end());
62    }
63
64private:
65    const std::unordered_map<K, V> str_map;
66    const K def_key;
67    const V def_value;
68    const ValueCmp compare;
69};
70using StringMap = KVMap<uint16_t, std::string>;
71std::string ErrorCode2Str(int16_t errorCode);
72std::string LogType2Str(uint16_t logType);
73uint16_t Str2LogType(const std::string& str);
74std::string ComboLogType2Str(uint16_t shiftType);
75uint16_t Str2ComboLogType(const std::string& str);
76std::vector<uint16_t> GetAllLogTypes();
77std::string LogLevel2Str(uint16_t logLevel);
78uint16_t Str2LogLevel(const std::string& str);
79std::string LogLevel2ShortStr(uint16_t logLevel);
80uint16_t ShortStr2LogLevel(const std::string& str);
81uint16_t PrettyStr2LogLevel(const std::string& str);
82std::string ComboLogLevel2Str(uint16_t shiftLevel);
83uint16_t Str2ComboLogLevel(const std::string& str);
84std::string ShowFormat2Str(uint16_t showFormat);
85uint16_t Str2ShowFormat(const std::string& str);
86std::string Size2Str(uint64_t size);
87uint64_t Str2Size(const std::string& str);
88
89constexpr char DEFAULT_SPLIT_DELIMIT[] = ",";
90void Split(const std::string& src, std::vector<std::string>& dest,
91           const std::string& separator = DEFAULT_SPLIT_DELIMIT);
92uint32_t GetBitsCount(uint64_t n);
93uint16_t GetBitPos(uint64_t n);
94
95std::string Uint2DecStr(uint32_t i);
96uint32_t DecStr2Uint(const std::string& str);
97std::string Uint2HexStr(uint32_t i);
98uint32_t HexStr2Uint(const std::string& str);
99
100#if !defined(__WINDOWS__) and !defined(__LINUX__)
101std::string GetProgName();
102#endif
103std::string GetNameByPid(uint32_t pid);
104uint32_t GetPPidByPid(uint32_t pid);
105uint64_t GenerateHash(const char *p, size_t size);
106void PrintErrorno(int err);
107int WaitingToDo(int max, const std::string& path, std::function<int(const std::string &path)> func);
108std::wstring StringToWstring(const std::string& input);
109} // namespace HiviewDFX
110} // namespace OHOS
111#endif // LOG_UTILS_H