third_party.pigweed.src/pw_console/embedding.rst
Anthony DiGirolamo 427edb2eb7 pw_console: Serial bytes logging
- Move pw_console python logging related functions to python_logging.py

- Add a PwConsoleEmbed.setup_python_logging to:
  - Silence debug messages from prompt_toolkit and asyncio.
  - Disable all Stdout and Stderr handlers.

- log_line.py and table.py:
  Strip trailing white space to prevent extra line breaks showing up in
  the log window.

- pw_console/py/pw_console/pyserial_wrapper.py
  - SerialWithLogging class that wrapps read and write to add logging
  - Logs with metadata columns (byte length and Read/Write) and hex
    numbers aligned with characters.

- pw_hdlc/py/pw_hdlc/rpc_console.py:
  - Add two additional log panes for serial debug and host log messages
    (hidden by default).
  - New --serial-debug to turn on SerialWithLogging

Bug: 403
Test: openocd -f targets/stm32f429i_disc1/py/stm32f429i_disc1_utils/openocd_stm32f4xx.cfg -c "program out/stm32f429i_disc1_debug/obj/pw_hdlc/rpc_example/bin/rpc_example.elf verify reset exit"
Test: pw rpc -d /dev/ttyACM0 -b 115200 --proto-globs pw_rpc/echo.proto
Change-Id: I579d93eb77dea0825c21d91ff2251080bac2186d
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/56784
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
2021-10-06 21:33:35 +00:00

101 lines
2.9 KiB
ReStructuredText

.. _module-pw_console-embedding:
Embedding Guide
===============
Using embed()
-------------
``pw console`` is invoked by calling ``PwConsoleEmbed().embed()`` in your
own Python script.
.. automodule:: pw_console.embed
:members: PwConsoleEmbed
:undoc-members:
:show-inheritance:
Adding Log Metadata
-------------------
``pw_console`` can display log messages in a table with justified columns for
metadata fields provided by :ref:`module-pw_log_tokenized`.
It is also possible to manually add values that should be displayed in columns
using the ``extra`` keyword argument when logging from Python. See the `Python's
logging documentation`_ for how ``extra`` works. A dict of name, value pairs can
be passed in as the ``extra_metadata_fields`` variable. For example, the
following code will create a log message with two custom columns titled
``module`` and ``timestamp``.
.. code-block:: python
import logging
LOG = logging.getLogger('log_source_1')
LOG.info(
'Hello there!',
extra={
'extra_metadata_fields': {
'module': 'cool',
'timestamp': 1.2345,
}
}
)
.. _Python's logging documentation: https://docs.python.org/3/library/logging.html#logging.Logger.debug
Debugging Serial Data
---------------------
``pw_console`` is often used to communicate with devices using `pySerial
<https://pythonhosted.org/pyserial/>`_ and it may be necessary to monitor the
raw data flowing over the wire to help with debugging. ``pw_console`` provides a
simple wrapper for a pySerial instances that log data for each read and write
call.
.. code-block:: python
# Instead of 'import serial' use this import:
from pw_console.pyserial_wrapper import SerialWithLogging
serial_device = SerialWithLogging('/dev/ttyUSB0', 115200, timeout=1)
With the above example each ``serial_device.read`` and ``write`` call will
create a log message to the ``pw_console.serial_debug_logger`` Python
logger. This logger can then be included as a log window pane in the
``PwConsoleEmbed()`` call.
.. code-block:: python
import logging
from pw_console import PwConsoleEmbed
console = PwConsoleEmbed(
global_vars=globals(),
local_vars=locals(),
loggers={
'Host Logs': [
# Root Python logger
logging.getLogger(''),
# Your current Python package logger.
logging.getLogger(__package__)
],
'Device Logs': [
logging.getLogger('usb_gadget')
],
'Serial Debug': [
# New log window to display serial read and writes
logging.getLogger('pw_console.serial_debug_logger')
],
},
app_title='CoolConsole',
)
# Then run the console with:
console.embed()
.. figure:: images/serial_debug.svg
:alt: Serial debug pw_console screenshot.
Screenshot of issuing an Echo RPC with serial debug logging.