Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_log_tokenized: |
Keir Mierle | bc5a269 | 2020-05-21 16:52:25 -0700 | [diff] [blame] | 2 | |
Wyatt Hepler | cb725c1 | 2020-05-01 11:05:01 -0700 | [diff] [blame] | 3 | ---------------- |
| 4 | pw_log_tokenized |
| 5 | ---------------- |
Wyatt Hepler | 8f4a096 | 2021-03-11 16:39:21 -0800 | [diff] [blame] | 6 | The ``pw_log_tokenized`` module contains utilities for tokenized logging. It |
| 7 | connects ``pw_log`` to ``pw_tokenizer``. |
| 8 | |
| 9 | C++ backend |
| 10 | =========== |
| 11 | ``pw_log_tokenized`` provides a backend for ``pw_log`` that tokenizes log |
Wyatt Hepler | 21ab0f4 | 2021-03-15 09:38:11 -0700 | [diff] [blame] | 12 | messages with the ``pw_tokenizer`` module. By default, log messages are |
| 13 | tokenized with the ``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD`` macro. |
| 14 | The log level, 16-bit tokenized module name, and flags bits are passed through |
| 15 | the payload argument. The macro eventually passes logs to the |
| 16 | ``pw_tokenizer_HandleEncodedMessageWithPayload`` function, which must be |
| 17 | implemented by the application. |
Wyatt Hepler | cb725c1 | 2020-05-01 11:05:01 -0700 | [diff] [blame] | 18 | |
| 19 | Example implementation: |
| 20 | |
| 21 | .. code-block:: cpp |
| 22 | |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 23 | extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload( |
| 24 | pw_tokenizer_Payload payload, const uint8_t message[], size_t size) { |
Wyatt Hepler | cb725c1 | 2020-05-01 11:05:01 -0700 | [diff] [blame] | 25 | // 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 Hepler | 21ab0f4 | 2021-03-15 09:38:11 -0700 | [diff] [blame] | 40 | See the documentation for :ref:`module-pw_tokenizer` for further details. |
| 41 | |
Wyatt Hepler | bcf0735 | 2021-04-05 14:44:30 -0700 | [diff] [blame] | 42 | Using a custom macro |
| 43 | -------------------- |
| 44 | Applications may use their own macro instead of |
Wyatt Hepler | 21ab0f4 | 2021-03-15 09:38:11 -0700 | [diff] [blame] | 45 | ``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD`` by setting the |
| 46 | ``PW_LOG_TOKENIZED_ENCODE_MESSAGE`` config macro. This macro should take |
| 47 | arguments equivalent to ``PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD``: |
| 48 | |
Keir Mierle | b191402 | 2021-04-12 09:08:33 -0700 | [diff] [blame] | 49 | .. 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 Mohr | 640c75c | 2021-05-26 07:22:54 -0700 | [diff] [blame] | 61 | .. _Metadata: https://cs.opensource.google/pigweed/pigweed/+/HEAD:pw_log_tokenized/public/pw_log_tokenized/log_tokenized.h;l=113 |
Wyatt Hepler | cb725c1 | 2020-05-01 11:05:01 -0700 | [diff] [blame] | 62 | |
Wyatt Hepler | bcf0735 | 2021-04-05 14:44:30 -0700 | [diff] [blame] | 63 | For instructions on how to implement a custom tokenization macro, see |
| 64 | :ref:`module-pw_tokenizer-custom-macro`. |
| 65 | |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame] | 66 | Build targets |
| 67 | ------------- |
| 68 | The 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 |
| 71 | implements the backend for the ``pw_log`` facade. ``pw_log_tokenized`` invokes |
Wyatt Hepler | e0575f7 | 2020-10-16 10:47:03 -0700 | [diff] [blame] | 72 | the ``pw_tokenizer:global_handler_with_payload`` facade, which must be |
Wyatt Hepler | 6736887 | 2020-07-30 16:55:38 -0700 | [diff] [blame] | 73 | implemented by the user of ``pw_log_tokenized``. |
| 74 | |
Wyatt Hepler | 8f4a096 | 2021-03-11 16:39:21 -0800 | [diff] [blame] | 75 | Python package |
| 76 | ============== |
Wyatt Hepler | bcf0735 | 2021-04-05 14:44:30 -0700 | [diff] [blame] | 77 | ``pw_log_tokenized`` includes a Python package for decoding tokenized logs. |
Wyatt Hepler | 8f4a096 | 2021-03-11 16:39:21 -0800 | [diff] [blame] | 78 | |
| 79 | pw_log_tokenized |
| 80 | ---------------- |
| 81 | .. automodule:: pw_log_tokenized |
| 82 | :members: |
| 83 | :undoc-members: |