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 #define ZLOGE(LOG_LABEL, fmt, args...) \
26 (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
27 #define ZLOGW(LOG_LABEL, fmt, args...) \
28 (void)OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
29 #define ZLOGI(LOG_LABEL, fmt, args...) \
30 (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
31 #define ZLOGD(LOG_LABEL, fmt, args...) \
32 (void)OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
33
34 using ErrorMap = std::map<uint32_t, std::string>;
35 class ErrorBase {
36 public:
37 virtual ~ErrorBase() = default;
38 inline const std::string &GetErrorDesc(uint32_t error);
39 virtual ErrorMap &GetErrorMap() = 0;
40 };
41
GetErrorDesc(uint32_t error)42 inline const std::string &ErrorBase::GetErrorDesc(uint32_t error)
43 {
44 static const std::string unknowCommand = "UNKNOWN COMMAND";
45 ErrorMap::iterator found = GetErrorMap().find(error);
46 if (found == GetErrorMap().end()) {
47 return unknowCommand;
48 } else {
49 return found->second;
50 }
51 }
52
53 class IPCError : public ErrorBase {
54 public:
55 IPCError() = default;
56 ~IPCError() = default;
57 static const std::string &ToString(int value);
58 ErrorMap &GetErrorMap() override;
59 };
60 } // namespace OHOS
61 #endif // OHOS_IPC_IPC_DEBUG_H
62