• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "hilog/log.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
26 #define ZLOGF(LOG_LABEL, fmt, args...) \
27     if (HiLogIsLoggable(LOG_LABEL.domain, LOG_LABEL.tag, LOG_FATAL)) {      \
28         HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_LABEL.domain, LOG_LABEL.tag,    \
29             "%{public}s %{public}d: " fmt, __FUNCTION__, __LINE__, ##args); \
30     }
31 
32 #define ZLOGE(LOG_LABEL, fmt, args...) \
33     if (HiLogIsLoggable(LOG_LABEL.domain, LOG_LABEL.tag, LOG_ERROR)) {      \
34         HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_LABEL.domain, LOG_LABEL.tag,    \
35             "%{public}s %{public}d: " fmt, __FUNCTION__, __LINE__, ##args); \
36     }
37 
38 #define ZLOGW(LOG_LABEL, fmt, args...) \
39     if (HiLogIsLoggable(LOG_LABEL.domain, LOG_LABEL.tag, LOG_WARN)) {       \
40         HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_LABEL.domain, LOG_LABEL.tag,     \
41             "%{public}s %{public}d: " fmt, __FUNCTION__, __LINE__, ##args); \
42     }
43 
44 #define ZLOGI(LOG_LABEL, fmt, args...) \
45     if (HiLogIsLoggable(LOG_LABEL.domain, LOG_LABEL.tag, LOG_INFO)) {       \
46         HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_LABEL.domain, LOG_LABEL.tag,     \
47             "%{public}s %{public}d: " fmt, __FUNCTION__, __LINE__, ##args); \
48     }
49 
50 #define ZLOGD(LOG_LABEL, fmt, args...) \
51     if (HiLogIsLoggable(LOG_LABEL.domain, LOG_LABEL.tag, LOG_DEBUG)) {      \
52         HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_LABEL.domain, LOG_LABEL.tag,    \
53             "%{public}s %{public}d: " fmt, __FUNCTION__, __LINE__, ##args); \
54     }
55 
56 using ErrorMap = std::map<uint32_t, std::string>;
57 class ErrorBase {
58 public:
59     virtual ~ErrorBase() = default;
60     inline const std::string GetErrorDesc(uint32_t error);
61     virtual ErrorMap &GetErrorMap() = 0;
62 
63     static constexpr const char *UNKNOWN_COMMAND = "UNKNOWN COMMAND";
64 };
65 
GetErrorDesc(uint32_t error)66 inline const std::string ErrorBase::GetErrorDesc(uint32_t error)
67 {
68     ErrorMap::iterator found = GetErrorMap().find(error);
69     if (found == GetErrorMap().end()) {
70         return UNKNOWN_COMMAND;
71     } else {
72         return found->second;
73     }
74 }
75 
76 class IPCError : public ErrorBase {
77 public:
78     IPCError() = default;
79     ~IPCError() = default;
80     static const std::string ToString(uint32_t value);
81     virtual ErrorMap &GetErrorMap() override;
82 };
83 } // namespace OHOS
84 #endif // OHOS_IPC_IPC_DEBUG_H
85