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