• 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 #ifndef PREFERENCES_JSKIT_COMMON_H
16 #define PREFERENCES_JSKIT_COMMON_H
17 
18 #include <string>
19 #include <vector>
20 
21 #include "hilog/log.h"
22 #include "preferences_errno.h"
23 
24 namespace OHOS {
25 namespace PreferencesJsKit {
26 static const OHOS::HiviewDFX::HiLogLabel PREFIX_LABEL = { LOG_CORE, 0xD001650, "PreferencesJsKit" };
27 
28 #define LOG_DEBUG(...) ((void)OHOS::HiviewDFX::HiLog::Debug(PREFIX_LABEL, __VA_ARGS__))
29 #define LOG_INFO(...) ((void)OHOS::HiviewDFX::HiLog::Info(PREFIX_LABEL, __VA_ARGS__))
30 #define LOG_WARN(...) ((void)OHOS::HiviewDFX::HiLog::Warn(PREFIX_LABEL, __VA_ARGS__))
31 #define LOG_ERROR(...) ((void)OHOS::HiviewDFX::HiLog::Error(PREFIX_LABEL, __VA_ARGS__))
32 #define LOG_FATAL(...) ((void)OHOS::HiviewDFX::HiLog::Fatal(PREFIX_LABEL, __VA_ARGS__))
33 
34 static const std::vector<std::string> ERR_INFO {
35     "E_ERROR",
36     "E_STALE",
37     "E_INVALID_ARGS",
38     "E_OUT_OF_MEMORY",
39     "E_NOT_PERMIT",
40     "E_KEY_EMPTY",
41     "E_KEY_EXCEED_MAX_LENGTH",
42     "E_PTR_EXIST_ANOTHER_HOLD",
43     "E_DELETE_FILE_FAIL",
44     "E_EMPTY_FILE_PATH",
45     "E_RELATIVE_PATH",
46     "E_EMPTY_FILE_NAME",
47     "E_INVALID_FILE_PATH",
48     "E_PATH_EXCEED_MAX_LENGTH"
49 };
50 
GetErrStr(int err)51 static inline const char *GetErrStr(int err)
52 {
53     if (err == OHOS::NativePreferences::E_OK) {
54         return nullptr;
55     }
56     size_t index = err - OHOS::NativePreferences::E_BASE - 1;
57     if (index >= ERR_INFO.size() || index < 0) {
58         return "Unknown error";
59     }
60     return ERR_INFO.at(index).c_str();
61 }
62 } // namespace PreferencesJsKit
63 } // namespace OHOS
64 
65 #endif
66