[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 07/27] python/aqmp: Add logging utility helpers
From: |
John Snow |
Subject: |
[PATCH v4 07/27] python/aqmp: Add logging utility helpers |
Date: |
Wed, 15 Sep 2021 12:29:35 -0400 |
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
python/qemu/aqmp/util.py | 56 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/python/qemu/aqmp/util.py b/python/qemu/aqmp/util.py
index 5b8f968969..52a1532188 100644
--- a/python/qemu/aqmp/util.py
+++ b/python/qemu/aqmp/util.py
@@ -4,10 +4,15 @@
This module provides asyncio utilities and compatibility wrappers for
Python 3.6 to provide some features that otherwise become available in
Python 3.7+.
+
+Various logging and debugging utilities are also provided, such as
+`exception_summary()` and `pretty_traceback()`, used primarily for
+adding information into the logging stream.
"""
import asyncio
import sys
+import traceback
from typing import (
Any,
Coroutine,
@@ -140,3 +145,54 @@ async def wait_closed(writer: asyncio.StreamWriter) ->
None:
while sock.fileno() != -1:
await asyncio.sleep(0)
+
+
+# ----------------------------
+# Section: Logging & Debugging
+# ----------------------------
+
+
+def exception_summary(exc: BaseException) -> str:
+ """
+ Return a summary string of an arbitrary exception.
+
+ It will be of the form "ExceptionType: Error Message", if the error
+ string is non-empty, and just "ExceptionType" otherwise.
+ """
+ name = type(exc).__qualname__
+ smod = type(exc).__module__
+ if smod not in ("__main__", "builtins"):
+ name = smod + '.' + name
+
+ error = str(exc)
+ if error:
+ return f"{name}: {error}"
+ return name
+
+
+def pretty_traceback(prefix: str = " | ") -> str:
+ """
+ Formats the current traceback, indented to provide visual distinction.
+
+ This is useful for printing a traceback within a traceback for
+ debugging purposes when encapsulating errors to deliver them up the
+ stack; when those errors are printed, this helps provide a nice
+ visual grouping to quickly identify the parts of the error that
+ belong to the inner exception.
+
+ :param prefix: The prefix to append to each line of the traceback.
+ :return: A string, formatted something like the following::
+
+ | Traceback (most recent call last):
+ | File "foobar.py", line 42, in arbitrary_example
+ | foo.baz()
+ | ArbitraryError: [Errno 42] Something bad happened!
+ """
+ output = "".join(traceback.format_exception(*sys.exc_info()))
+
+ exc_lines = []
+ for line in output.split('\n'):
+ exc_lines.append(prefix + line)
+
+ # The last line is always empty, omit it
+ return "\n".join(exc_lines[:-1])
--
2.31.1
- [PATCH v4 00/27] python: introduce Asynchronous QMP package, John Snow, 2021/09/15
- [PATCH v4 01/27] python/aqmp: add asynchronous QMP (AQMP) subpackage, John Snow, 2021/09/15
- [PATCH v4 02/27] python/aqmp: add error classes, John Snow, 2021/09/15
- [PATCH v4 03/27] python/pylint: Add exception for TypeVar names ('T'), John Snow, 2021/09/15
- [PATCH v4 04/27] python/aqmp: add asyncio compatibility wrappers, John Snow, 2021/09/15
- [PATCH v4 06/27] python/aqmp: add runstate state machine to AsyncProtocol, John Snow, 2021/09/15
- [PATCH v4 05/27] python/aqmp: add generic async message-based protocol support, John Snow, 2021/09/15
- [PATCH v4 07/27] python/aqmp: Add logging utility helpers,
John Snow <=
- [PATCH v4 08/27] python/aqmp: add logging to AsyncProtocol, John Snow, 2021/09/15
- [PATCH v4 09/27] python/aqmp: add AsyncProtocol.accept() method, John Snow, 2021/09/15
- [PATCH v4 10/27] python/aqmp: add configurable read buffer limit, John Snow, 2021/09/15
- [PATCH v4 11/27] python/aqmp: add _cb_inbound and _cb_outbound logging hooks, John Snow, 2021/09/15
- [PATCH v4 12/27] python/aqmp: add AsyncProtocol._readline() method, John Snow, 2021/09/15
- [PATCH v4 13/27] python/aqmp: add QMP Message format, John Snow, 2021/09/15
- [PATCH v4 14/27] python/aqmp: add well-known QMP object models, John Snow, 2021/09/15
- [PATCH v4 16/27] python/pylint: disable too-many-function-args, John Snow, 2021/09/15
- [PATCH v4 17/27] python/aqmp: add QMP protocol support, John Snow, 2021/09/15
- [PATCH v4 19/27] python/aqmp: Add message routing to QMP protocol, John Snow, 2021/09/15