Skip to contentSkip to conversation

Logging str() instead of repr() on exceptions

PATCH In review Troels Dalsgaard Hoffmeyer 2 participants 3 messages Aug 9, 2024 – Aug 15, 2024

lore
Share & tools
lore
#0 lore raw

cover Logging str() instead of repr() on exceptions

I was trying to create a recipe for a go binary and was struggling a lot in getting it working. One of the reasons for me struggling was some poor error messages, i kept getting an error message with just "FileNotFoundError(2, "file or directory not found")", with no description of which file or directory. After some digging i found that bitbake mostly logs repr(exception) instead of str(exception), and repr() is usually used when you want a string representation that can be interpreted as python again, whereas str() is for more human readable strings.

Talking with RP on IRC, he mentioned that they might have encountered situations in the past where repr() was providing more information that str(), but wanted to see the specific case i was running into. This patch only changes the specific log messages that i needed, but there is an overall pattern of logging repr() instead of str()

Troels Dalsgaard Hoffmeyer (1):
bb: Log str() instead of repr() for exceptions in build

 lib/bb/build.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/1 bb: Log str() instead of repr() for exceptions in build

When getting errors during build, they would be printed using repr(), which doesnt have a lot of context in some cases.
For example FileNotFoundError(2, "file or directory not found"), would be printed, without the path of the file not found.
This changes the build logging to use str() instead, which according to the spec is fore human readable strings, whereas repr() is for string representations that can be be used as valid python.

---
 lib/bb/build.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/bb/build.py +2 -2
index ab8bce3d5..097730047 100644
@@ -743,7 +743,7 @@ def _exec_task(fn, task, d, quieterr):
 
         if quieterr:
             if not handled:
-                logger.warning(repr(exc))
+                logger.warning(str(exc))
             event.fire(TaskFailedSilent(task, fn, logfn, localdata), localdata)
         else:
             errprinted = errchk.triggered
@@ -752,7 +752,7 @@ def _exec_task(fn, task, d, quieterr):
             if verboseStdoutLogging or handled:
                 errprinted = True
             if not handled:
-                logger.error(repr(exc))
+                logger.error(str(exc))
             event.fire(TaskFailed(task, fn, logfn, localdata, errprinted), localdata)
         return 1
 
Signature · 1 line
2.40.1
#2 lore raw

Re: [bitbake-devel] [PATCH 1/1] bb: Log str() instead of repr() for exceptions in build

Quoted 27 lines · Troels Dalsgaard Hoffmeyer via lists.openembedded.org

When getting errors during build, they would be printed using repr(), which doesnt have a lot of context in some cases.
For example FileNotFoundError(2, "file or directory not found"), would be printed, without the path of the file not found.
This changes the build logging to use str() instead, which according to the spec is fore human readable strings, whereas repr() is for string representations that can be be used as valid python.

---
 lib/bb/build.py | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
lib/bb/build.py +0 -0
index ab8bce3d5..097730047 100644
@@ -743,7 +743,7 @@ def _exec_task(fn, task, d, quieterr):


         if quieterr:
             if not handled:
-                logger.warning(repr(exc))
+                logger.warning(str(exc)) event.fire(TaskFailedSilent(task, fn, logfn, localdata), localdata) else:
             errprinted = errchk.triggered

lib/bb/build.py +0 -0
@@ -752,7 +752,7 @@ def _exec_task(fn, task, d, quieterr):
Quoted 8 lines

             if verboseStdoutLogging or handled:
                 errprinted = True
             if not handled:
-                logger.error(repr(exc))
+                logger.error(str(exc))
             event.fire(TaskFailed(task, fn, logfn, localdata, errprinted), localdata) return 1

Going by the python docs, this is the correct thing to do. I also know we've had hard to debug problems due to str() not giving enough information to debug exceptions though which does mean I'm a bit conflicted about doing this, it feels like we're going around in circles :(

Cheers,

Richard