• 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 FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H
17 #define FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H
18 
19 #include "hilog/log.h"
20 #include <string>
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 #ifndef LOG_DOMAIN
25 #define LOG_DOMAIN 0xD001100
26 #endif
27 
28 #ifndef APP_LOG_TAG
29 #define APP_LOG_TAG NULL
30 #endif
31 
32 enum class AppLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL };
33 
34 static constexpr OHOS::HiviewDFX::HiLogLabel APP_LABEL = {LOG_CORE, LOG_DOMAIN, APP_LOG_TAG};
35 
36 class AppLogWrapper {
37 public:
38     static bool JudgeLevel(const AppLogLevel &level);
39 
SetLogLevel(const AppLogLevel & level)40     static void SetLogLevel(const AppLogLevel &level)
41     {
42         level_ = level;
43     }
44 
GetLogLevel()45     static const AppLogLevel &GetLogLevel()
46     {
47         return level_;
48     }
49 
50     static std::string GetBriefFileName(const char *str);
51 
52 private:
53     static AppLogLevel level_;
54 };
55 
56 #define AFWK_PRINT_LOG(LEVEL, Level, fmt, ...)                  \
57     if (OHOS::AppExecFwk::AppLogWrapper::JudgeLevel(OHOS::AppExecFwk::AppLogLevel::LEVEL))     \
58     OHOS::HiviewDFX::HiLog::Level(OHOS::AppExecFwk::APP_LABEL,               \
59         "[%{public}s(%{public}s):%{public}d] " fmt,        \
60         OHOS::AppExecFwk::AppLogWrapper::GetBriefFileName(__FILE__).c_str(), \
61         __FUNCTION__,                                      \
62         __LINE__,                                          \
63         ##__VA_ARGS__)
64 
65 #define APP_LOGD(fmt, ...) AFWK_PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
66 #define APP_LOGI(fmt, ...) AFWK_PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
67 #define APP_LOGW(fmt, ...) AFWK_PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
68 #define APP_LOGE(fmt, ...) AFWK_PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
69 #define APP_LOGF(fmt, ...) AFWK_PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
70 }  // namespace AppExecFwk
71 }  // namespace OHOS
72 #endif  // FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H
73