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