harmony 鸿蒙HiLog Development

  • 2022-08-09
  • 浏览 (871)

HiLog Development

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 List 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:

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

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 0xD003200  // Indicates the service domain. The value ranges from 0xD000000 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 = [ "hilog: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, 0xD003200, "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, 0xD003200, "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" ]
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Subsystems

harmony 鸿蒙AI Framework Development Guide

harmony 鸿蒙NNRt Access Adaptation

harmony 鸿蒙Application Privilege Configuration

harmony 鸿蒙Development Example

harmony 鸿蒙Setting Up a Development Environment

harmony 鸿蒙Development Guidelines

harmony 鸿蒙Application Framework Overview

harmony 鸿蒙ArkCompiler Development

harmony 鸿蒙Custom Window Title Bar Development

0  赞