blob: 0871c6bbc449c955b7d184b3f88be7af9c316cb5 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_log_tokenized:
Keir Mierlebc5a2692020-05-21 16:52:25 -07002
Wyatt Heplercb725c12020-05-01 11:05:01 -07003----------------
4pw_log_tokenized
5----------------
Wyatt Hepler8f4a0962021-03-11 16:39:21 -08006The ``pw_log_tokenized`` module contains utilities for tokenized logging. It
7connects ``pw_log`` to ``pw_tokenizer``.
8
9C++ backend
10===========
11``pw_log_tokenized`` provides a backend for ``pw_log`` that tokenizes log
Wyatt Hepler21ab0f42021-03-15 09:38:11 -070012messages with the ``pw_tokenizer`` module. By default, log messages are
13tokenized with the ``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD`` macro.
14The log level, 16-bit tokenized module name, and flags bits are passed through
15the payload argument. The macro eventually passes logs to the
16``pw_tokenizer_HandleEncodedMessageWithPayload`` function, which must be
17implemented by the application.
Wyatt Heplercb725c12020-05-01 11:05:01 -070018
19Example implementation:
20
21.. code-block:: cpp
22
Wyatt Hepler7a5e4d62020-08-31 08:39:16 -070023 extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload(
24 pw_tokenizer_Payload payload, const uint8_t message[], size_t size) {
Wyatt Heplercb725c12020-05-01 11:05:01 -070025 // The metadata object provides the log level, module token, and flags.
26 // These values can be recorded and used for runtime filtering.
27 pw::log_tokenized::Metadata metadata(payload);
28
29 if (metadata.level() < current_log_level) {
30 return;
31 }
32
33 if (metadata.flags() & HIGH_PRIORITY_LOG != 0) {
34 EmitHighPriorityLog(metadata.module(), message, size);
35 } else {
36 EmitLowPriorityLog(metadata.module(), message, size);
37 }
38 }
39
Wyatt Hepler21ab0f42021-03-15 09:38:11 -070040See the documentation for :ref:`module-pw_tokenizer` for further details.
41
Wyatt Heplerbcf07352021-04-05 14:44:30 -070042Using a custom macro
43--------------------
44Applications may use their own macro instead of
Wyatt Hepler21ab0f42021-03-15 09:38:11 -070045``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD`` by setting the
46``PW_LOG_TOKENIZED_ENCODE_MESSAGE`` config macro. This macro should take
47arguments equivalent to ``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD``:
48
Keir Mierleb1914022021-04-12 09:08:33 -070049.. c:macro:: PW_LOG_TOKENIZED_ENCODE_MESSAGE(log_metadata, message, ...)
50
51 :param log_metadata:
52
53 Packed metadata for the log message. See the Metadata_ class for how to
54 unpack the details.
55
56 :type log_metadata: pw_tokenizer_Payload
57
58 :param message: The log message format string (untokenized)
59 :type message: :c:texpr:`const char*`
60
Rob Mohr640c75c2021-05-26 07:22:54 -070061 .. _Metadata: https://cs.opensource.google/pigweed/pigweed/+/HEAD:pw_log_tokenized/public/pw_log_tokenized/log_tokenized.h;l=113
Wyatt Heplercb725c12020-05-01 11:05:01 -070062
Wyatt Heplerbcf07352021-04-05 14:44:30 -070063For instructions on how to implement a custom tokenization macro, see
64:ref:`module-pw_tokenizer-custom-macro`.
65
Wyatt Hepler67368872020-07-30 16:55:38 -070066Build targets
67-------------
68The GN build for ``pw_log_tokenized`` has two targets: ``pw_log_tokenized`` and
69``log_backend``. The ``pw_log_tokenized`` target provides the
70``pw_log_tokenized/log_tokenized.h`` header. The ``log_backend`` target
71implements the backend for the ``pw_log`` facade. ``pw_log_tokenized`` invokes
Wyatt Heplere0575f72020-10-16 10:47:03 -070072the ``pw_tokenizer:global_handler_with_payload`` facade, which must be
Wyatt Hepler67368872020-07-30 16:55:38 -070073implemented by the user of ``pw_log_tokenized``.
74
Wyatt Hepler8f4a0962021-03-11 16:39:21 -080075Python package
76==============
Wyatt Heplerbcf07352021-04-05 14:44:30 -070077``pw_log_tokenized`` includes a Python package for decoding tokenized logs.
Wyatt Hepler8f4a0962021-03-11 16:39:21 -080078
79pw_log_tokenized
80----------------
81.. automodule:: pw_log_tokenized
82 :members:
83 :undoc-members: