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 "BundleMgrService"
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 FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
57 #define APP_LOGD(fmt, ...)            \
58     ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, APP_LOG_TAG, \
59     "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
60 #define APP_LOGI(fmt, ...)            \
61     ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, APP_LOG_TAG, \
62     "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
63 #define APP_LOGW(fmt, ...)            \
64     ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, APP_LOG_TAG, \
65     "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
66 #define APP_LOGE(fmt, ...)            \
67     ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, APP_LOG_TAG, \
68     "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
69 #define APP_LOGF(fmt, ...)            \
70     ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, APP_LOG_TAG, \
71     "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
72 
73 #define APP_LOGI_NOFUNC(fmt, ...)            \
74     ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__))
75 #define APP_LOGW_NOFUNC(fmt, ...)            \
76     ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__))
77 #define APP_LOGE_NOFUNC(fmt, ...)            \
78     ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__))
79 }  // namespace AppExecFwk
80 }  // namespace OHOS
81 #endif  // FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H
82