1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# The following logging mechanisms are to be used in bash functions of recipes.
8# They are intended to map one to one in intention and output format with the
9# python recipe logging functions of a similar naming convention: bb.plain(),
10# bb.note(), etc.
11
12LOGFIFO = "${T}/fifo.${@os.getpid()}"
13
14# Print the output exactly as it is passed in. Typically used for output of
15# tasks that should be seen on the console. Use sparingly.
16# Output: logs console
17bbplain() {
18	if [ -p ${LOGFIFO} ] ; then
19		printf "%b\0" "bbplain $*" > ${LOGFIFO}
20	else
21		echo "$*"
22	fi
23}
24
25# Notify the user of a noteworthy condition.
26# Output: logs
27bbnote() {
28	if [ -p ${LOGFIFO} ] ; then
29		printf "%b\0" "bbnote $*" > ${LOGFIFO}
30	else
31		echo "NOTE: $*"
32	fi
33}
34
35# Print a warning to the log. Warnings are non-fatal, and do not
36# indicate a build failure.
37# Output: logs console
38bbwarn() {
39	if [ -p ${LOGFIFO} ] ; then
40		printf "%b\0" "bbwarn $*" > ${LOGFIFO}
41	else
42		echo "WARNING: $*"
43	fi
44}
45
46# Print an error to the log. Errors are non-fatal in that the build can
47# continue, but they do indicate a build failure.
48# Output: logs console
49bberror() {
50	if [ -p ${LOGFIFO} ] ; then
51		printf "%b\0" "bberror $*" > ${LOGFIFO}
52	else
53		echo "ERROR: $*"
54	fi
55}
56
57# Print a fatal error to the log. Fatal errors indicate build failure
58# and halt the build, exiting with an error code.
59# Output: logs console
60bbfatal() {
61	if [ -p ${LOGFIFO} ] ; then
62		printf "%b\0" "bbfatal $*" > ${LOGFIFO}
63	else
64		echo "ERROR: $*"
65	fi
66	exit 1
67}
68
69# Like bbfatal, except prevents the suppression of the error log by
70# bitbake's UI.
71# Output: logs console
72bbfatal_log() {
73	if [ -p ${LOGFIFO} ] ; then
74		printf "%b\0" "bbfatal_log $*" > ${LOGFIFO}
75	else
76		echo "ERROR: $*"
77	fi
78	exit 1
79}
80
81# Print debug messages. These are appropriate for progress checkpoint
82# messages to the logs. Depending on the debug log level, they may also
83# go to the console.
84# Output: logs console
85# Usage: bbdebug 1 "first level debug message"
86#        bbdebug 2 "second level debug message"
87bbdebug() {
88	USAGE='Usage: bbdebug [123] "message"'
89	if [ $# -lt 2 ]; then
90		bbfatal "$USAGE"
91	fi
92
93	# Strip off the debug level and ensure it is an integer
94	DBGLVL=$1; shift
95	NONDIGITS=$(echo "$DBGLVL" | tr -d "[:digit:]")
96	if [ "$NONDIGITS" ]; then
97		bbfatal "$USAGE"
98	fi
99
100	# All debug output is printed to the logs
101	if [ -p ${LOGFIFO} ] ; then
102		printf "%b\0" "bbdebug $DBGLVL $*" > ${LOGFIFO}
103	else
104		echo "DEBUG: $*"
105	fi
106}
107
108