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