1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#include "service_dump_manager.h"
17049e185fSopenharmony_ci#include <unistd.h>
18049e185fSopenharmony_ci#include <locale>
19049e185fSopenharmony_ci#include <codecvt>
20049e185fSopenharmony_ci#include "media_errors.h"
21049e185fSopenharmony_ci
22049e185fSopenharmony_cinamespace OHOS {
23049e185fSopenharmony_cinamespace Media {
24049e185fSopenharmony_ciServiceDumpManager &ServiceDumpManager::GetInstance()
25049e185fSopenharmony_ci{
26049e185fSopenharmony_ci    static ServiceDumpManager ServiceDumpManager;
27049e185fSopenharmony_ci    return ServiceDumpManager;
28049e185fSopenharmony_ci}
29049e185fSopenharmony_ci
30049e185fSopenharmony_civoid ServiceDumpManager::RegisterDfxDumper(std::u16string key, DfxDumper dump)
31049e185fSopenharmony_ci{
32049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
33049e185fSopenharmony_ci    dfxDumper_[key] = dump;
34049e185fSopenharmony_ci}
35049e185fSopenharmony_ci
36049e185fSopenharmony_civoid ServiceDumpManager::UnregisterDfxDumper()
37049e185fSopenharmony_ci{
38049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
39049e185fSopenharmony_ci    dfxDumper_.clear();
40049e185fSopenharmony_ci}
41049e185fSopenharmony_ci
42049e185fSopenharmony_ciint32_t ServiceDumpManager::Dump(int32_t fd, std::unordered_set<std::u16string> args)
43049e185fSopenharmony_ci{
44049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
45049e185fSopenharmony_ci    std::string dumpString = "------------------MediaDfx------------------\n";
46049e185fSopenharmony_ci    for (auto iter : dfxDumper_) {
47049e185fSopenharmony_ci        if (args.find(static_cast<std::u16string>(iter.first)) != args.end()) {
48049e185fSopenharmony_ci            dumpString += "-----";
49049e185fSopenharmony_ci            dumpString += std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> {}.to_bytes(iter.first);
50049e185fSopenharmony_ci            dumpString += "-----\n";
51049e185fSopenharmony_ci            if (fd != -1) {
52049e185fSopenharmony_ci                write(fd, dumpString.c_str(), dumpString.size());
53049e185fSopenharmony_ci                dumpString.clear();
54049e185fSopenharmony_ci            }
55049e185fSopenharmony_ci            if (iter.second(fd) != MSERR_OK) {
56049e185fSopenharmony_ci                return MSERR_INVALID_OPERATION;
57049e185fSopenharmony_ci            }
58049e185fSopenharmony_ci        }
59049e185fSopenharmony_ci    }
60049e185fSopenharmony_ci    return MSERR_OK;
61049e185fSopenharmony_ci}
62049e185fSopenharmony_ci} // namespace Media
63049e185fSopenharmony_ci} // namespace OHOS