1 /*
2 * Copyright (c) 2023 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/dump_manager.h"
16 namespace OHOS {
17 namespace DistributedData {
GetInstance()18 DumpManager &DumpManager::GetInstance()
19 {
20     static DumpManager instance;
21     return instance;
22 }
23 
AddConfig(const std::string & name,Config & config)24 void DumpManager::AddConfig(const std::string &name, Config &config)
25 {
26     factory_.Insert(name, config);
27     indexTable_.Insert(config.fullCmd, name);
28     indexTable_.Insert(config.abbrCmd, name);
29 }
30 
GetConfig(const std::string & name)31 DumpManager::Config DumpManager::GetConfig(const std::string &name)
32 {
33     auto it = factory_.Find(name);
34     if (it.first) {
35         return (it.second);
36     }
37     auto itIndex = indexTable_.Find(name);
38     if (itIndex.first) {
39         return GetConfig(itIndex.second);
40     }
41     return {};
42 }
43 
LoadConfig()44 ConcurrentMap<std::string, DumpManager::Config> DumpManager::LoadConfig()
45 {
46     return factory_;
47 }
48 
AddHandler(const std::string & infoName,uintptr_t ptr,const Handler & handler)49 void DumpManager::AddHandler(const std::string &infoName, uintptr_t ptr, const Handler &handler)
50 {
51     handlers_.Compute(infoName, [ptr, &handler](const auto &key, auto &value) {
52         value.emplace(ptr, handler);
53         return true;
54     });
55 }
56 
GetHandler(const std::string & infoName)57 std::vector<DumpManager::Handler> DumpManager::GetHandler(const std::string &infoName)
58 {
59     auto it = handlers_.Find(infoName);
60     std::vector<Handler> handlers;
61     if (it.first) {
62         for (auto &handler : it.second) {
63             handlers.emplace_back(handler.second);
64         }
65         return handlers;
66     }
67     auto itIndex = indexTable_.Find(infoName);
68     if (itIndex.first) {
69         return GetHandler(itIndex.second);
70     }
71     return handlers;
72 }
73 
RemoveHandler(const std::string & infoName,uintptr_t ptr)74 void DumpManager::RemoveHandler(const std::string &infoName, uintptr_t ptr)
75 {
76     handlers_.ComputeIfPresent(infoName, [ptr](const auto &key, auto &value) {
77         value.erase(ptr);
78         return !value.empty();
79     });
80 }
81 } // namespace DistributedData
82 } // namespace OHOS