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 HIVIEW_BASE_LOGGER_H
16 #define HIVIEW_BASE_LOGGER_H
17 
18 #include <cinttypes>
19 #include <cstdarg>
20 #include <string>
21 #include "hilog/log.h"
22 #include "i_logger.h"
23 
24 #define PUBLIC "{public}"
25 // All Log domain in hiview should has the prefix of 0xD002D
26 // And every src file use this header should define LOG_DOMAIN and LOG_TAG
27 #define DEFINE_LOG_TAG(name) \
28     static unsigned int logLabelDomain = 0xD002D10; \
29     static const char *logLabelTag = name
30 
31 #define DEFINE_LOG_LABEL(region, name) \
32     static unsigned int logLabelDomain = region; \
33     static const char *logLabelTag = name
34 
35 #define HIVIEW_LOGD(format, ...) \
36     HILOG_IMPL(LOG_CORE, LOG_DEBUG, logLabelDomain, logLabelTag, "%" PUBLIC "s: " format, __func__, ##__VA_ARGS__)
37 #define HIVIEW_LOGI(format, ...) \
38     HILOG_IMPL(LOG_CORE, LOG_INFO, logLabelDomain, logLabelTag, "%" PUBLIC "s: " format, __func__, ##__VA_ARGS__)
39 #define HIVIEW_LOGW(format, ...) \
40     HILOG_IMPL(LOG_CORE, LOG_WARN, logLabelDomain, logLabelTag, "%" PUBLIC "s: " format, __func__, ##__VA_ARGS__)
41 #define HIVIEW_LOGE(format, ...) \
42     HILOG_IMPL(LOG_CORE, LOG_ERROR, logLabelDomain, logLabelTag, "%" PUBLIC "s: " format, __func__, ##__VA_ARGS__)
43 #define HIVIEW_LOGF(format, ...) \
44     HILOG_IMPL(LOG_CORE, LOG_FATAL, logLabelDomain, logLabelTag, "%" PUBLIC "s: " format, __func__, ##__VA_ARGS__)
45 
46 
47 namespace OHOS::HiviewDFX {
48 class Logger final {
49 public:
50     static Logger& GetInstance();
51     bool SetUserLogger(std::unique_ptr<ILogger> logger);
52     void Print(uint32_t level, uint32_t domain, const char* tag, const char*, ...);
53 private:
54     Logger();
55     virtual ~Logger() = default;
56     std::unique_ptr<ILogger> m_logger;
57 };
58 }
59 #endif // HIVIEW_BASE_LOGGER_H
60