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 HIVIEWDFX_HILOG_CPP_H 17 #define HIVIEWDFX_HILOG_CPP_H 18 19 #include "hilog/log_c.h" 20 21 #ifdef __cplusplus 22 namespace OHOS { 23 namespace HiviewDFX { 24 /** 25 * @brief Initialize log parameters: type, domain and tag. 26 * domain: Indicates the log service domainID. Different LogType have different domainID ranges, 27 * Log type of LOG_APP: 0-0xFFFF, 28 * Log type of LOG_CORE & LOG_INIT: 0xD000000-0xD0FFFFF. 29 * tag: Indicates the log service tag, you can customize the tag as needed, usually the keyword of the module. 30 */ 31 using HiLogLabel = struct { 32 LogType type; 33 unsigned int domain; 34 const char *tag; 35 }; 36 37 /** 38 * @brief Hilog C++ class interface of different log level. 39 * Debug: Designates lower priority log. 40 * Info: Designates useful information. 41 * Warn: Designates hazardous situations. 42 * Error: Designates very serious errors. 43 * Fatal: Designates major fatal anomaly. 44 * 45 * label: HiLogLabel for the log 46 * fmt: Format string for the log 47 */ 48 class HiLog final { 49 public: 50 static int Debug(const HiLogLabel &label, const char *fmt, ...) __attribute__((__format__(os_log, 2, 3))); 51 static int Info(const HiLogLabel &label, const char *fmt, ...) __attribute__((__format__(os_log, 2, 3))); 52 static int Warn(const HiLogLabel &label, const char *fmt, ...) __attribute__((__format__(os_log, 2, 3))); 53 static int Error(const HiLogLabel &label, const char *fmt, ...) __attribute__((__format__(os_log, 2, 3))); 54 static int Fatal(const HiLogLabel &label, const char *fmt, ...) __attribute__((__format__(os_log, 2, 3))); 55 }; 56 } // namespace HiviewDFX 57 } // namespace OHOS 58 59 /** 60 * @brief Hilog C++ macro interface of different log level. 61 * HiLogDebug: Designates lower priority log. 62 * HiLogInfo: Designates useful information. 63 * HiLogWarn: Designates hazardous situations. 64 * HiLogError: Designates very serious errors. 65 * HiLogFatal: Designates major fatal anomaly. 66 * 67 * @param label HiLogLabel for the log 68 * @param fmt Format string for the log 69 */ 70 #define HiLogDebug(label, fmt, ...) HILOG_IMPL(label.type, LOG_DEBUG, label.domain, label.tag, fmt, ##__VA_ARGS__) 71 #define HiLogInfo(label, fmt, ...) HILOG_IMPL(label.type, LOG_INFO, label.domain, label.tag, fmt, ##__VA_ARGS__) 72 #define HiLogWarn(label, fmt, ...) HILOG_IMPL(label.type, LOG_WARN, label.domain, label.tag, fmt, ##__VA_ARGS__) 73 #define HiLogError(label, fmt, ...) HILOG_IMPL(label.type, LOG_ERROR, label.domain, label.tag, fmt, ##__VA_ARGS__) 74 #define HiLogFatal(label, fmt, ...) HILOG_IMPL(label.type, LOG_FATAL, label.domain, label.tag, fmt, ##__VA_ARGS__) 75 76 #endif // __cplusplus 77 #endif // HIVIEWDFX_HILOG_CPP_H 78