官方文档里有输出调试信息的方法。包含两种,一种是调用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