1 Star 0 Fork 4.9K

戈英祯 / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
subsys-dfx-hilog-rich.md 15.97 KB
一键复制 编辑 原始数据 按行查看 历史
duangavin123 提交于 2021-07-20 22:06 . update 导入OpenHarmony工程

Development Guidelines on HiLog

Overview

HiLog is the log system of OpenHarmony that provides logging for the system framework, services, and applications to record information on user operations and system running status.

This development guide is applicable to Standard-System Devices (reference memory ≥ 128 MB).

Available APIs

Table 1 Description of C++ and C APIs

C++

C

Class

API

API/Macro

HiLog

int Debug(const HiLogLabel &label, const char *fmt, ...)

HILOG_DEBUG(type, ...)

int Info(const HiLogLabel &label, const char *fmt, ...)

HILOG_INFO(type, ...)

int Warn(const HiLogLabel &label, const char *fmt, ...)

HILOG_WARN(type, ...)

int Error(const HiLogLabel &label, const char *fmt, ...)

HILOG_ERROR(type, ...)

int Fatal(const HiLogLabel &label, const char *fmt, ...)

HILOG_FATAL(type, ...)

NA

int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)

boolean IsLoggable(unsigned int domain, const char *tag, LogLevel level)

bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level)

HiLogLabel

struct HiLogLabel

LOG_DOMAIN

LOG_TAG

Table 2 Parameters of C++ APIs

Class

API

Description

HiLog

int Debug(const HiLogLabel &label, const char *fmt, ...)

Generates debug logs.

Input arguments:

  • label: Identifies the log type, service domain, and tag.
  • format: Indicates the constant format, including the parameter type and privacy identifier. If the privacy identifier is not specified, a parameter is treated as a privacy parameter by default.
  • fmt: Indicates the string describing the format variable parameter.

Output arguments: none

Return value: Returns a value greater than or equal to 0 if the operation is successful; returns a value less than 0 otherwise.

int Info(const HiLogLabel &label, const char *fmt, ...)

Generates info logs.

Arguments: See argument description of the Debug function.

int Warn(const HiLogLabel &label, const char *fmt, ...)

Generates warn logs.

Arguments: See argument description of the Debug function.

int Error(const HiLogLabel &label, const char *fmt, ...)

Generates error logs.

Arguments: See argument description of the Debug function.

int Fatal(const HiLogLabel &label, const char *fmt, ...)

Generates fatal logs.

Arguments: See argument description of the Debug function.

boolean IsLoggable(unsigned int domain, const char *tag, LogLevel level)

Checks whether logs of the specified service domain, tag, and level can be printed.

Input arguments:

  • domain: Indicates the service domain of logs.
  • tag: Indicates the log tag.
  • level: Indicates the log level.

Output arguments: none

Return value: Returns true if the specified logs can be printed; returns false otherwise.

HiLogLabel

struct HiLogLabel

Initializes log tag parameters.

Members:

  • domain: Indicates the service domain of logs.
  • tag: Indicates the log tag.
  • level: Indicates the log level.

How to Develop

C

  1. Include the hilog header file in the .c source file.

    #include "hilog/log.h"

    Construct domain and tag.

    #undef LOG_DOMAIN
    #undef LOG_TAG
    #define LOG_DOMAIN 0  // Indicates the service domain. The value ranges from 0x0 to 0xFFFFF.
    #define LOG_TAG "MY_TAG"

    Print logs.

    HILOG_INFO(LOG_CORE, "Failed to visit %{private}s, reason:%{public}d.", url, errno);
  2. Configure compilation information. Specifically, add the subsystem SDK dependency to BUILD.gn.

    external_deps = [ "hiviewdfx_hilog_native:libhilog" ]

C++

  1. Include the hilog header file in the .h class definition header file.

    #include "hilog/log.h"

    If log printing is required for the class header file, define LABEL at the beginning of the class definition in the header file.

    class MyClass {
    static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "MY_TAG"}; 
    ...
    }

    If log printing is not required for the class definition header file, define LABEL in the class implementation file.

    using namespace OHOS::HiviewDFX;
    static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "MY_TAG"}; 

    Print logs.

    HiLog::Info(LABEL, "Failed to visit %{private}s, reason:%{public}d.", url, errno);
  2. Configure compilation information. Specifically, add the subsystem SDK dependency to BUILD.gn.

    external_deps = [ "hiviewdfx:libhilog" ]
1
https://gitee.com/geyingzhen/docs.git
git@gitee.com:geyingzhen/docs.git
geyingzhen
docs
docs
master

搜索帮助