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 FOUNDATION_ACE_NAPI_UTILS_LOG_H 17 #define FOUNDATION_ACE_NAPI_UTILS_LOG_H 18 19 #ifdef LINUX_PLATFORM 20 #include <cstdint> 21 #endif 22 #include <cstring> 23 #include <string> 24 25 #include "utils/macros.h" 26 27 #define __FILENAME__ strrchr(__FILE__, '/') + 1 28 29 #if defined(MAC_PLATFORM) || defined(WINDOWS_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM) || \ 30 defined(LINUX_PLATFORM) 31 enum class LogLevel : uint32_t { 32 Debug = 0, 33 Info, 34 Warn, 35 Error, 36 Fatal, 37 }; 38 39 NAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...); 40 41 #define HILOG_PRINT(Level, fmt, ...) \ 42 PrintLog(LogLevel::Level, "[%-20s(%s)] " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__); 43 44 #else 45 46 #include "hilog/log.h" 47 48 #undef LOG_DOMAIN 49 #undef LOG_TAG 50 #undef HILOG_FATAL 51 #undef HILOG_ERROR 52 #undef HILOG_WARN 53 #undef HILOG_INFO 54 #undef HILOG_DEBUG 55 56 #define LOG_DOMAIN 0xD003900 57 #define LOG_TAG "NAPI" 58 59 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, LOG_TAG }; 60 61 #define HILOG_PRINT(Level, fmt, ...) \ 62 (void)OHOS::HiviewDFX::HiLog::Level( \ 63 LOG_LABEL, "[(%{public}s:%{public}d)(%{public}s)] " fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__) 64 65 #endif 66 67 #define HILOG_FATAL(fmt, ...) HILOG_PRINT(Fatal, fmt, ##__VA_ARGS__) 68 #define HILOG_ERROR(fmt, ...) HILOG_PRINT(Error, fmt, ##__VA_ARGS__) 69 #define HILOG_WARN(fmt, ...) HILOG_PRINT(Warn, fmt, ##__VA_ARGS__) 70 #define HILOG_INFO(fmt, ...) HILOG_PRINT(Info, fmt, ##__VA_ARGS__) 71 #define HILOG_DEBUG(fmt, ...) HILOG_PRINT(Debug, fmt, ##__VA_ARGS__) 72 73 #endif /* FOUNDATION_ACE_NAPI_UTILS_LOG_H */ 74