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
16 #ifndef OHOS_IPC_IPC_DEBUG_H
17 #define OHOS_IPC_IPC_DEBUG_H
18
19 #include <map>
20 #include <string>
21 #include "hilog/log.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 // if need enable ipc debug log, use '#define CONFIG_IPC_DEBUG'
26 #define ZLOGW(TAG, ...) (void)HiviewDFX::HiLog::Warn(TAG, __VA_ARGS__)
27 #define ZLOGE(TAG, ...) (void)HiviewDFX::HiLog::Error(TAG, __VA_ARGS__)
28
29 #if (defined CONFIG_IPC_DEBUG)
30 #define ZLOGI(TAG, ...) (void)HiviewDFX::HiLog::Info(TAG, __VA_ARGS__)
31 #else
32 #define ZLOGI(TAG, ...)
33 #endif /* CONFIG_IPC_DEBUG */
34
35 using ErrorMap = std::map<uint32_t, std::string>;
36 class ErrorBase {
37 public:
38 virtual ~ErrorBase() = default;
39 inline const std::string &GetErrorDesc(uint32_t error);
40 virtual ErrorMap &GetErrorMap() = 0;
41 };
42
GetErrorDesc(uint32_t error)43 inline const std::string &ErrorBase::GetErrorDesc(uint32_t error)
44 {
45 static const std::string unknowCommand = "UNKNOWN COMMAND";
46 ErrorMap::iterator found = GetErrorMap().find(error);
47 if (found == GetErrorMap().end()) {
48 return unknowCommand;
49 } else {
50 return found->second;
51 }
52 }
53
54 class IPCError : public ErrorBase {
55 public:
56 IPCError() = default;
57 ~IPCError() = default;
58 static const std::string &ToString(int value);
59 virtual ErrorMap &GetErrorMap() override;
60 };
61 } // namespace OHOS
62 #endif // OHOS_IPC_IPC_DEBUG_H
63