Yocto - 如何在配方文件中输出log

官方文档里有输出调试信息的方法。包含两种,一种是调用python的,一种是bash的。

另外,在python 函数里,调用python的log输出;在bash 函数里调用bash的log输出;不然会出错。

输出log的语句直接写在bb脚本里,但不在可被执行的python或bash函数里,会报错。

Python举例:

bsp_number = "FF002201BAA"

bb.warn(bsp_number)

33.12 Recipe Logging Mechanisms

Yocto 项目提供了多个日志函数,用于生成调试输出以及报告错误和警告。对于 Python 函数,以下日志函数可用。所有这些函数都会将日志记录到 ${T}/log.do_task,如果设置正确,还可以记录到标准输出 (stdout):

The Yocto Project provides several logging functions for producing debugging output and reporting errors and warnings. For Python functions, the following logging functions are available. All of these functions log to ${T}/log.do_task, and can also log to standard output (stdout) with the right settings:

* bb.plain(msg): 将 msg 原样写入日志,同时记录到 stdout。

* bb.note(msg): 将 "NOTE: msg "写入日志。如果使用"-v "调用 BitBake,也会将日志记录到 stdout。

* bb.debug(level,msg): 将 "DEBUG: msg "写入日志。如果日志级别大于或等于级别,也会将日志写入 stdout。更多信息请参阅《BitBake 用户手册》中的 "用法和语法 "选项。(1 Overview — Bitbake dev documentation)

* bb.warn(msg): 向日志写入 "WARNING: msg",同时将日志记录到 stdout。

* bb.error(msg): 向日志写入 "ERROR: msg",同时记录到标准输出(stdout)。

注意: 

调用此函数不会导致任务失败。

* bb.fatal(msg): 此日志函数与 bb.error(msg) 类似,但也会导致调用任务失败。

注意

bb.fatal() 引发异常,这意味着无需在函数后添加 "return" 语句。

* bb.plain(msg): Writes msg as is to the log while also logging to stdout.

* bb.note(msg): Writes “NOTE: msg” to the log. Also logs to stdout if BitBake is called with “-v”.

* bb.debug(level, msg): Writes “DEBUG: msg” to the log. Also logs to stdout if the log level is greater than or equal to level. See the “Usage and syntax” option in the BitBake User Manual for more information.

* bb.warn(msg): Writes “WARNING: msg” to the log while also logging to stdout.

* bb.error(msg): Writes “ERROR: msg” to the log while also logging to standard out (stdout).

Note

Calling this function does not cause the task to fail.

* bb.fatal(msg): This logging function is similar to bb.error(msg) but also causes the calling task to fail.

Note

bb.fatal() raises an exception, which means you do not need to put a “return” statement after the function.

bb.debug里的level是和bitbake命令启动时在命令行设定的Debug level相比较的。

同样的日志记录函数也可以在 shell 函数中使用,名称分别为 bbplain、bbnote、bbdebug、bbwarn、bberror 和 bbfatal。logging类实现了这些函数。有关信息,请参见源代码目录下 pokymeta/classes-global 文件夹中的日志类。所有包含 base.bbclass 的配方都默认继承日志类。base class的特殊性在于每个 .bb 文件都隐含地继承了该类。即不需要在bb文件的开头加上 inherit base。

The same logging functions are also available in shell functions, under the names bbplain, bbnote, bbdebug, bbwarn, bberror, and bbfatal. The logging class implements these functions. See that class in the meta/classes folder of the Source Directory for information.

The logging class is inherited by default by all recipes containing base.bbclass. The base class is special in that every .bb file implicitly inherits the class.

33.12.1 Logging With Python

在使用 Python 创建配方并插入处理构建日志的代码时,请记住,目标是在尽可能保持控制台 "静默 "的情况下提供信息丰富的日志。此外,如果您希望在日志中显示状态信息,请使用 "调试 "日志级别。

下面是一个用 Python 编写的示例。该代码为一个函数处理日志,该函数用于确定需要运行的任务数。有关详细信息,请参阅 "do_listtasks "部分:

(6 Tasks — The Yocto Project ® 4.3.999 documentation)

When creating recipes using Python and inserting code that handles build logs, keep in mind the goal is to have informative logs while keeping the console as “silent” as possible. Also, if you want status messages in the log, use the “debug” loglevel.

Here is an example written in Python. The code handles logging for a function that determines the number of tasks needed to be run. See the “do_listtasks” section for additional information:

python do_listtasks() {

    bb.debug(2, "Starting to figure out the task list")

    if noteworthy_condition:

        bb.note("There are 47 tasks to run")

    bb.debug(2, "Got to point xyz")

    if warning_trigger:

        bb.warn("Detected warning_trigger, this might be a problem later.")

    if recoverable_error:

        bb.error("Hit recoverable_error, you really need to fix this!")

    if fatal_error:

        bb.fatal("fatal_error detected, unable to print the task list")

    bb.plain("The tasks present are abc")

    bb.debug(2, "Finished figuring out the tasklist")

}

33.12.2 Logging With Bash

在使用 Bash 创建配方并插入处理构建日志的代码时,您的目标是相同的--以最少的控制台输出提供信息。使用 Bash 编写的配方所使用的语法与上一节描述的使用 Python 编写的配方类似。

下面是一个用 Bash 编写的示例。代码记录了 do_my_function 函数的执行进度:

When creating recipes using Bash and inserting code that handles build logs, you have the same goals — informative with minimal console output. The syntax you use for recipes written in Bash is similar to that of recipes written in Python described in the previous section.

Here is an example written in Bash. The code logs the progress of the do_my_function function:

do_my_function() {

    bbdebug 2 "Running do_my_function"

    if [ exceptional_condition ]; then

        bbnote "Hit exceptional_condition"

    fi

    bbdebug 2  "Got to point xyz"

    if [ warning_trigger ]; then

        bbwarn "Detected warning_trigger, this might cause a problem later."

    fi

    if [ recoverable_error ]; then

        bberror "Hit recoverable_error, correcting"

    fi

    if [ fatal_error ]; then

        bbfatal "fatal_error detected"

    fi

    bbdebug 2 "Completed do_my_function"

}

参考:

1,Yocto

33 Debugging Tools and Techniques — The Yocto Project ® 4.3.999 documentation

2,Oreilly

Adding logging to recipes - Embedded Linux Development Using Yocto Project Cookbook - Second Edition [Book]

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值