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_DATA_STORAGE_LOG_WRAPPER_H 17 #define OHOS_DATA_STORAGE_LOG_WRAPPER_H 18 19 #include "hilog/log_c.h" 20 #include "hilog/log_cpp.h" 21 #include "iosfwd" 22 23 namespace OHOS { 24 namespace Telephony { 25 enum class DataStorageLogLevel { 26 DEBUG = 0, 27 INFO, 28 WARN, 29 ERROR, 30 FATAL, 31 }; 32 33 class DataStorageLogWrapper { 34 public: 35 static bool JudgeLevel(const DataStorageLogLevel &level); 36 SetLogLevel(const DataStorageLogLevel & level)37 static void SetLogLevel(const DataStorageLogLevel &level) 38 { 39 level_ = level; 40 } 41 GetLogLevel()42 static const DataStorageLogLevel &GetLogLevel() 43 { 44 return level_; 45 } 46 47 static std::string GetBriefFileName(const std::string &file); 48 49 private: 50 static DataStorageLogLevel level_; 51 }; 52 53 #define CONFIG_HILOG 54 #ifdef CONFIG_HILOG 55 56 #ifndef TELEPHONY_LOG_TAG 57 #define TELEPHONY_LOG_TAG "DataStorage" 58 #endif 59 60 #ifndef LOG_DOMAIN 61 #define LOG_DOMAIN 0xD001F08 62 #endif 63 64 static constexpr OHOS::HiviewDFX::HiLogLabel DATA_STORAGE_LABEL = {LOG_CORE, LOG_DOMAIN, 65 TELEPHONY_LOG_TAG}; 66 67 #define DATA_STORAGE_FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) 68 69 #define OHOS_DEBUG 70 #ifndef OHOS_DEBUG 71 #define PRINT_DATASTORAGE_LOG(op, fmt, ...) (void)OHOS::HiviewDFX::HiLog::op(DATA_STORAGE_LABEL, fmt, ##__VA_ARGS__) 72 #else 73 #define PRINT_DATASTORAGE_LOG(op, fmt, ...) \ 74 (void)OHOS::HiviewDFX::HiLog::op(DATA_STORAGE_LABEL, "[%{public}s-(%{public}s:%{public}d)] " fmt, __FUNCTION__, \ 75 DATA_STORAGE_FILENAME, __LINE__, ##__VA_ARGS__) 76 #endif 77 78 #define DATA_STORAGE_LOGE(fmt, ...) PRINT_DATASTORAGE_LOG(Error, fmt, ##__VA_ARGS__) 79 #define DATA_STORAGE_LOGW(fmt, ...) PRINT_DATASTORAGE_LOG(Warn, fmt, ##__VA_ARGS__) 80 #define DATA_STORAGE_LOGI(fmt, ...) PRINT_DATASTORAGE_LOG(Info, fmt, ##__VA_ARGS__) 81 #define DATA_STORAGE_LOGF(fmt, ...) PRINT_DATASTORAGE_LOG(Fatal, fmt, ##__VA_ARGS__) 82 #define DATA_STORAGE_LOGD(fmt, ...) PRINT_DATASTORAGE_LOG(Debug, fmt, ##__VA_ARGS__) 83 84 #else 85 #endif // CONFIG_HILOG 86 } // namespace Telephony 87 } // namespace OHOS 88 #endif // OHOS_DATA_STORAGE_LOG_WRAPPER_H 89