diff --git a/frameworks/hilog_ndk/hilog_ndk.c b/frameworks/hilog_ndk/hilog_ndk.c index 792cca33b1bb36f77ef5ba3dd7f83ead87d7224c..9abce6c8079c80ed081c94cde925272e78830a79 100644 --- a/frameworks/hilog_ndk/hilog_ndk.c +++ b/frameworks/hilog_ndk/hilog_ndk.c @@ -13,11 +13,16 @@ * limitations under the License. */ +#include #include +#include #include "hilog_inner.h" #include "hilog/log_c.h" +#define MAX_LOG_LEN 4096 /* maximum length of a log, include '\0' */ +#define MAX_TAG_LEN 32 /* log tag size, include '\0' */ + int OH_LOG_Print(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) { int ret; @@ -28,6 +33,35 @@ int OH_LOG_Print(LogType type, LogLevel level, unsigned int domain, const char * return ret; } +int OH_LOG_PrintMsg(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *message) +{ + return OH_LOG_Print(type, level, domain, tag, "%{public}s", message); +} + +int OH_LOG_PrintMsgByLen(LogType type, LogLevel level, unsigned int domain, const char *tag, size_t tagLen, + const char *message, size_t messageLen) +{ + if (tag == NULL || message == NULL) { + return -1; + } + size_t copyTagLen = tagLen < MAX_TAG_LEN - 1 ? tagLen : MAX_TAG_LEN - 1; + size_t copyMsgLen = messageLen < MAX_LOG_LEN - 1 ? messageLen : MAX_LOG_LEN - 1; + char newTag[copyTagLen + 1]; + char newMessage[copyMsgLen + 1]; + (void)memset_s(newTag, copyTagLen + 1, 0, copyTagLen + 1); + (void)memset_s(newMessage, copyMsgLen + 1, 0, copyMsgLen + 1); + if (strncpy_s(newTag, copyTagLen + 1, tag, copyTagLen) < 0 + || strncpy_s(newMessage, copyMsgLen + 1, message, copyMsgLen) < 0) { + return -1; + } + return OH_LOG_Print(type, level, domain, newTag, "%{public}s", newMessage); +} + +int OH_LOG_VPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, va_list ap) +{ + return HiLogPrintArgs(type, level, domain, tag, fmt, ap); +} + bool OH_LOG_IsLoggable(unsigned int domain, const char *tag, LogLevel level) { return HiLogIsLoggable(domain, tag, level); diff --git a/interfaces/native/kits/include/hilog/log.h b/interfaces/native/kits/include/hilog/log.h index 7452137c280d212fc271cca2f31466f0dc11bd6f..1bb60f32b27a6ab68186ecd38dc3b70ada97da93 100644 --- a/interfaces/native/kits/include/hilog/log.h +++ b/interfaces/native/kits/include/hilog/log.h @@ -58,6 +58,7 @@ * * @since 8 */ +#include #include #include @@ -150,6 +151,69 @@ typedef enum { int OH_LOG_Print(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) __attribute__((__format__(os_log, 5, 6))); +/** + * @brief Outputs logs. + * + * You can use this function to output logs based on the specified log type, log level, service domain, log tag, + * and message text. + * + * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}. + * @param level Indicates the log level, which can be LOG_DEBUG, LOG_INFO, LOG_WARN, + * LOG_ERROR, and LOG_FATAL. + * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. + * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior. + * @param message Indicates the log string. + * @return Returns 0 or a larger value if the operation is successful; returns a value smaller + * than 0 otherwise. + * @since 16 + */ +int OH_LOG_PrintMsg(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *message); + +/** + * @brief Outputs logs. + * + * You can use this function to output logs based on the specified log type, log level, service domain, log tag, + * message text and message length. + * + * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}. + * @param level Indicates the log level, which can be LOG_DEBUG, LOG_INFO, LOG_WARN, + * LOG_ERROR, and LOG_FATAL. + * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. + * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior. + * @param tagLen Indicates the length of tag. + * @param message Indicates the log string. + * @param messageLen Indicates the length of message. + * @return Returns 0 or a larger value if the operation is successful; returns a value smaller + * than 0 otherwise. + * @since 16 + */ +int OH_LOG_PrintMsgByLen(LogType type, LogLevel level, unsigned int domain, const char *tag, size_t tagLen, + const char *message, size_t messageLen); + +/** + * @brief Outputs logs. + * + * You can use this function to output logs based on the specified log type, log level, service domain, log tag, + * and a va_list instead of variable parameters determined by the format specifier and privacy identifier in the printf + * format. + * + * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}. + * @param level Indicates the log level, which can be LOG_DEBUG, LOG_INFO, LOG_WARN, + * LOG_ERROR, and LOG_FATAL. + * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. + * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior. + * @param fmt Indicates the format string, which is an enhancement of a printf format string and supports the privacy + * identifier. Specifically, {public} or {private} is added between the % character and the format specifier + * in each parameter. \n + * @param ap Indicates a list of parameters. The number and type of parameters must map onto the format specifiers + * in the format string. + * @return Returns 0 or a larger value if the operation is successful; returns a value smaller + * than 0 otherwise. + * @since 16 + */ +int OH_LOG_VPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, va_list ap) + __attribute__((__format__(os_log, 5, 0))); + /** * @brief Checks whether logs of the specified service domain, log tag, and log level can be output. *