12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci#ifndef LOG_UTILS_H
162498b56bSopenharmony_ci#define LOG_UTILS_H
172498b56bSopenharmony_ci
182498b56bSopenharmony_ci#include <string>
192498b56bSopenharmony_ci#include <vector>
202498b56bSopenharmony_ci#include <unordered_map>
212498b56bSopenharmony_ci
222498b56bSopenharmony_cinamespace OHOS {
232498b56bSopenharmony_cinamespace HiviewDFX {
242498b56bSopenharmony_citemplate<typename K, typename V>
252498b56bSopenharmony_ciclass KVMap {
262498b56bSopenharmony_ciusing ValueCmp = std::function<bool(const V& v1, const V& v2)>;
272498b56bSopenharmony_cipublic:
282498b56bSopenharmony_ci    KVMap(std::unordered_map<K, V> map, K def_k, V def_v,
292498b56bSopenharmony_ci        ValueCmp cmp = [](const V& v1, const V& v2) { return v1 == v2; })
302498b56bSopenharmony_ci        : str_map(map), def_key(def_k), def_value(def_v), compare(cmp)
312498b56bSopenharmony_ci    {
322498b56bSopenharmony_ci    }
332498b56bSopenharmony_ci
342498b56bSopenharmony_ci    const V& GetValue(K key) const
352498b56bSopenharmony_ci    {
362498b56bSopenharmony_ci        auto it = str_map.find(key);
372498b56bSopenharmony_ci        return it == str_map.end() ? def_value : it->second;
382498b56bSopenharmony_ci    }
392498b56bSopenharmony_ci
402498b56bSopenharmony_ci    const K GetKey(const V& value) const
412498b56bSopenharmony_ci    {
422498b56bSopenharmony_ci        for (auto& it : str_map) {
432498b56bSopenharmony_ci            if (compare(value, it.second)) {
442498b56bSopenharmony_ci                return it.first;
452498b56bSopenharmony_ci            }
462498b56bSopenharmony_ci        }
472498b56bSopenharmony_ci        return def_key;
482498b56bSopenharmony_ci    }
492498b56bSopenharmony_ci
502498b56bSopenharmony_ci    std::vector<K> GetAllKeys() const
512498b56bSopenharmony_ci    {
522498b56bSopenharmony_ci        std::vector<K> keys;
532498b56bSopenharmony_ci        for (auto& it : str_map) {
542498b56bSopenharmony_ci            keys.push_back(it.first);
552498b56bSopenharmony_ci        }
562498b56bSopenharmony_ci        return keys;
572498b56bSopenharmony_ci    }
582498b56bSopenharmony_ci
592498b56bSopenharmony_ci    bool IsValidKey(K key) const
602498b56bSopenharmony_ci    {
612498b56bSopenharmony_ci        return (str_map.find(key) != str_map.end());
622498b56bSopenharmony_ci    }
632498b56bSopenharmony_ci
642498b56bSopenharmony_ciprivate:
652498b56bSopenharmony_ci    const std::unordered_map<K, V> str_map;
662498b56bSopenharmony_ci    const K def_key;
672498b56bSopenharmony_ci    const V def_value;
682498b56bSopenharmony_ci    const ValueCmp compare;
692498b56bSopenharmony_ci};
702498b56bSopenharmony_ciusing StringMap = KVMap<uint16_t, std::string>;
712498b56bSopenharmony_cistd::string ErrorCode2Str(int16_t errorCode);
722498b56bSopenharmony_cistd::string LogType2Str(uint16_t logType);
732498b56bSopenharmony_ciuint16_t Str2LogType(const std::string& str);
742498b56bSopenharmony_cistd::string ComboLogType2Str(uint16_t shiftType);
752498b56bSopenharmony_ciuint16_t Str2ComboLogType(const std::string& str);
762498b56bSopenharmony_cistd::vector<uint16_t> GetAllLogTypes();
772498b56bSopenharmony_cistd::string LogLevel2Str(uint16_t logLevel);
782498b56bSopenharmony_ciuint16_t Str2LogLevel(const std::string& str);
792498b56bSopenharmony_cistd::string LogLevel2ShortStr(uint16_t logLevel);
802498b56bSopenharmony_ciuint16_t ShortStr2LogLevel(const std::string& str);
812498b56bSopenharmony_ciuint16_t PrettyStr2LogLevel(const std::string& str);
822498b56bSopenharmony_cistd::string ComboLogLevel2Str(uint16_t shiftLevel);
832498b56bSopenharmony_ciuint16_t Str2ComboLogLevel(const std::string& str);
842498b56bSopenharmony_cistd::string ShowFormat2Str(uint16_t showFormat);
852498b56bSopenharmony_ciuint16_t Str2ShowFormat(const std::string& str);
862498b56bSopenharmony_cistd::string Size2Str(uint64_t size);
872498b56bSopenharmony_ciuint64_t Str2Size(const std::string& str);
882498b56bSopenharmony_ci
892498b56bSopenharmony_ciconstexpr char DEFAULT_SPLIT_DELIMIT[] = ",";
902498b56bSopenharmony_civoid Split(const std::string& src, std::vector<std::string>& dest,
912498b56bSopenharmony_ci           const std::string& separator = DEFAULT_SPLIT_DELIMIT);
922498b56bSopenharmony_ciuint32_t GetBitsCount(uint64_t n);
932498b56bSopenharmony_ciuint16_t GetBitPos(uint64_t n);
942498b56bSopenharmony_ci
952498b56bSopenharmony_cistd::string Uint2DecStr(uint32_t i);
962498b56bSopenharmony_ciuint32_t DecStr2Uint(const std::string& str);
972498b56bSopenharmony_cistd::string Uint2HexStr(uint32_t i);
982498b56bSopenharmony_ciuint32_t HexStr2Uint(const std::string& str);
992498b56bSopenharmony_ci
1002498b56bSopenharmony_ci#if !defined(__WINDOWS__) and !defined(__LINUX__)
1012498b56bSopenharmony_cistd::string GetProgName();
1022498b56bSopenharmony_ci#endif
1032498b56bSopenharmony_cistd::string GetNameByPid(uint32_t pid);
1042498b56bSopenharmony_ciuint32_t GetPPidByPid(uint32_t pid);
1052498b56bSopenharmony_ciuint64_t GenerateHash(const char *p, size_t size);
1062498b56bSopenharmony_civoid PrintErrorno(int err);
1072498b56bSopenharmony_ciint WaitingToDo(int max, const std::string& path, std::function<int(const std::string &path)> func);
1082498b56bSopenharmony_cistd::wstring StringToWstring(const std::string& input);
1092498b56bSopenharmony_ci} // namespace HiviewDFX
1102498b56bSopenharmony_ci} // namespace OHOS
1112498b56bSopenharmony_ci#endif // LOG_UTILS_H