Search code examples
yoctobitbakeopenembeddedrecipe

Yocto : Custom colored output in recipes?


Context

When I'm using bitbake to process recipes with Yocto, I'd like to be able to print useful output on the screen. There exist a few ways to do this so far, but they're not very practical. Let me enumerate the default options:

  1. Using: bbinfo "my message" with bitbake --verbose
  2. Using: bbdebug 1 "my message" with bitbake -D (scaling is possible for more debug levels)
  3. Using: bbwarn "my message" with no extra flags

Problem

Using option (1) outputs a lot of unnecessary clutter on the terminal, and also suffers from having no colored output. Option (2) is colored, but unfortunately also captures a lot of debugging messages from the other build components, which floods the output. I've been using option (3) for now, as it makes my messages stand out without also enabling other irrelevant information from appearing.

So my question is, how can I enable custom logging (i.e. in green, or blue) for my own messages? It would make it easier for me to distinguish important output I want without accidentally enabling unnecessary information from appearing


Solution

  • Bitbake is the one taking care of printing log messages in their appropriate colors. You could add new kinds of log messages if you want with different colors by modifying: https://gitlab.com/yoctoproject/poky/-/blob/master/bitbake/lib/bb/build.py#L475 seems to be where the bbwarn/bbdebug/bbplain/etc... shell commands are translated to Python's bb.warn, bb.debug, bb.plain, etc.. defined here: https://gitlab.com/yoctoproject/poky/-/blob/master/bitbake/lib/bb/__init__.py#L132-165 and whose colors and loglevels are defined in BBLogFormatter class defined here: https://gitlab.com/yoctoproject/poky/-/blob/master/bitbake/lib/bb/msg.py#L22

    I'd personally try to add warning levels the same way as different debug levels are handled and then you can associate a warning level to a particular color.

    This however modifies bitbake itself, which is not possible via bbappends or other btibake mechanisms as they are Python scripts.

    Otherwise, I'm wondering if it would not be possible to just have your own logger defined in/for the recipes/tasks/functions you want to use it, e.g. have a bbclass which sets up everything you need with the Python logging module and just use this instead of bbwarn or bb.warn.