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 SERVICES_INCLUDE_GLOBAL_H
17 #define SERVICES_INCLUDE_GLOBAL_H
18 
19 #include <errno.h>
20 #include <functional>
21 #include <sys/time.h>
22 #include <time.h>
23 
24 #include "hilog/log.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 
29 #define LOG_INFO(fmt, args...) \
30     LogTimeStamp();            \
31     printf("I %s:%d  %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args)
32 
33 #define LOG_ERROR(fmt, args...) \
34     LogTimeStamp();             \
35     printf("E %s:%d  %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args)
36 
37 #define LOG_WARNING(fmt, args...) \
38     LogTimeStamp();               \
39     printf("W %s:%d  %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args)
40 
41 #if DEBUG
42 #define LOG_DEBUG(fmt, args...) \
43     LogTimeStamp();             \
44     printf("D %s:%d  %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args)
45 #else
46 #define LOG_DEBUG(fmt, args...)
47 #endif
48 
49 void LogTimeStamp();
50 
51 // Error Code
52 namespace ErrorCode {
53 // Error Code definition in the input method management system
54 enum {
55     ERROR_STATUS_PERMISSION_DENIED = -EPERM,     // permission denied
56     ERROR_STATUS_UNKNOWN_TRANSACTION = -EBADMSG, // unknown transaction
57 
58     // binder exception error code from Status.h
59     ERROR_EX_ILLEGAL_ARGUMENT = -3,      // illegal argument exception
60     ERROR_EX_NULL_POINTER = -4,          // null pointer exception
61     ERROR_EX_ILLEGAL_STATE = -5,         // illegal state exception
62     ERROR_EX_PARCELABLE = -6,            // parcelable exception
63     ERROR_EX_UNSUPPORTED_OPERATION = -7, // unsupported operation exception
64     ERROR_EX_SERVICE_SPECIFIC = -8,      // service specific exception
65     // no error
66     NO_ERROR = 0, // no error
67 
68     // system service error
69     ERROR_NULL_POINTER = 1,   // null pointer
70     ERROR_BAD_PARAMETERS = 2, // bad parameters
71     ERROR_CLIENT_NOT_FOUND = 3,
72     ERROR_CLIENT_NULL_POINTER = 4,
73     ERROR_SUBSCRIBE_KEYBOARD_EVENT = 5,
74     ERROR_IME_NOT_STARTED = 6,
75     ERROR_SERVICE_START_FAILED = 7,
76 
77     ERROR_CONTROLLER_INVOKING_FAILED = 8,
78     ERROR_PERSIST_CONFIG = 9,
79     ERROR_KBD_HIDE_FAILED = 10,
80     ERROR_SWITCH_IME = 11,
81     ERROR_PACKAGE_MANAGER = 12,
82     ERROR_REMOTE_CLIENT_DIED = 13,
83     ERROR_IME_START_FAILED = 14, // failed to start IME service
84     ERROR_KBD_SHOW_FAILED = 15,  // failed to show keyboard
85     ERROR_CLIENT_NOT_BOUND = 16,
86     ERROR_CLIENT_NOT_EDITABLE = 17,
87     ERROR_CLIENT_NOT_FOCUSED = 18,
88     ERROR_CLIENT_ADD_FAILED = 19,
89     ERROR_OPERATE_PANEL = 20,
90     ERROR_NOT_CURRENT_IME = 21,
91     ERROR_NOT_IME = 22,
92     ERROR_ADD_DEATH_RECIPIENT_FAILED = 23,
93     ERROR_STATUS_SYSTEM_PERMISSION = 24, // not system application
94     ERROR_IME = 25,
95     ERROR_PARAMETER_CHECK_FAILED = 26,
96     ERROR_IME_START_INPUT_FAILED = 27,
97     ERROR_KEYWORD_NOT_FOUND = 28,
98     ERROR_ENABLE_IME = 29,
99     ERROR_PARSE_CONFIG_FILE = 30,
100     ERROR_NOT_DEFAULT_IME = 31,
101     ERROR_ENABLE_SECURITY_MODE = 32,
102     ERROR_DISPATCH_KEY_EVENT = 33,
103     ERROR_INVALID_PRIVATE_COMMAND_SIZE = 34,
104     ERROR_TEXT_LISTENER_ERROR = 35,
105     ERROR_PANEL_NOT_FOUND = 36,
106     ERROR_WINDOW_MANAGER = 37,
107     ERROR_GET_TEXT_CONFIG = 38,
108     ERROR_TEXT_PREVIEW_NOT_SUPPORTED = 39,
109     ERROR_INVALID_RANGE = 40,
110     ERROR_CMD_LISTENER_ERROR = 41,
111     ERROR_SYSTEM_CMD_CHANNEL_ERROR = 42,
112     ERROR_INVALID_PRIVATE_COMMAND = 43,
113     ERROR_OS_ACCOUNT = 44,
114     ERROR_TASK_MANAGER_PEND_FAILED = 45,
115     ERROR_RES_ERROR = 46,
116 };
117 }; // namespace ErrorCode
118 
119 static constexpr HiviewDFX::HiLogLabel g_SMALL_SERVICES_LABEL = { LOG_CORE, 0xD001C10, "ImsaKit" };
120 
121 #define IMSA_HILOGD(fmt, ...)                                                                                    \
122     (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain,                     \
123         OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \
124         __FUNCTION__, ##__VA_ARGS__)
125 #define IMSA_HILOGE(fmt, ...)                                                                                    \
126     (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain,                     \
127         OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \
128         __FUNCTION__, ##__VA_ARGS__)
129 #define IMSA_HILOGF(fmt, ...)                                                                                    \
130     (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain,                     \
131         OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \
132         __FUNCTION__, ##__VA_ARGS__)
133 #define IMSA_HILOGI(fmt, ...)                                                                                    \
134     (void)HILOG_IMPL(LOG_CORE, LOG_INFO, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain,                      \
135         OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \
136         __FUNCTION__, ##__VA_ARGS__)
137 #define IMSA_HILOGW(fmt, ...)                                                                                    \
138     (void)HILOG_IMPL(LOG_CORE, LOG_WARN, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain,                      \
139         OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \
140         __FUNCTION__, ##__VA_ARGS__)
141 using Function = std::function<bool()>;
142 bool BlockRetry(uint32_t interval, uint32_t maxRetryTimes, Function func);
143 } // namespace MiscServices
144 } // namespace OHOS
145 #endif // SERVICES_INCLUDE_GLOBAL_H
146