xref: /aosp_15_r20/external/pigweed/pw_assert_tokenized/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert_tokenized:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker===================
4*61c4878aSAndroid Build Coastguard Workerpw_assert_tokenized
5*61c4878aSAndroid Build Coastguard Worker===================
6*61c4878aSAndroid Build Coastguard Worker
7*61c4878aSAndroid Build Coastguard Worker--------
8*61c4878aSAndroid Build Coastguard WorkerOverview
9*61c4878aSAndroid Build Coastguard Worker--------
10*61c4878aSAndroid Build Coastguard WorkerThe ``pw_assert_tokenized`` module provides ``PW_ASSERT()`` and ``PW_CHECK_*()``
11*61c4878aSAndroid Build Coastguard Workerbackends for the ``pw_assert`` module. These backends are much more space
12*61c4878aSAndroid Build Coastguard Workerefficient than using ``pw_assert_log`` with ``pw_log_tokenized`` The tradeoff,
13*61c4878aSAndroid Build Coastguard Workerhowever, is that ``PW_CHECK_*()`` macros are much more limited as all argument
14*61c4878aSAndroid Build Coastguard Workervalues are discarded. This means only constant string information is captured in
15*61c4878aSAndroid Build Coastguard Workerthe reported tokens.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker* **PW_ASSERT()**: The ``PW_ASSERT()`` macro will capture the file name and line
18*61c4878aSAndroid Build Coastguard Worker  number of the assert statement. By default, it is passed to the logging system
19*61c4878aSAndroid Build Coastguard Worker  to produce a string like this:
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker  .. code-block:: text
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker     PW_ASSERT() or PW_DASSERT() failure at
24*61c4878aSAndroid Build Coastguard Worker     pw_result/public/pw_result/result.h:63
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Worker* **PW_CHECK_\*()**: The ``PW_CHECK_*()`` macros work in contexts where
27*61c4878aSAndroid Build Coastguard Worker  tokenization is fully supported, so they are able to capture the CHECK
28*61c4878aSAndroid Build Coastguard Worker  statement expression and any provided string literal in addition to the file
29*61c4878aSAndroid Build Coastguard Worker  name in the pw_log_tokenized key/value format:
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker  .. code-block:: text
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker     "■msg♦Check failure: \*unoptimizable >= 0, Ensure this CHECK logic
34*61c4878aSAndroid Build Coastguard Worker     stays■module♦KVS■file♦pw_kvs/size_report/base.cc"
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker  Evaluated values of ``PW_CHECK_*()`` statements are not captured, and any
37*61c4878aSAndroid Build Coastguard Worker  string formatting arguments are also not captured. This minimizes call-site
38*61c4878aSAndroid Build Coastguard Worker  cost as only two arguments are ever passed to the handler (the calculated
39*61c4878aSAndroid Build Coastguard Worker  token, and the line number of the statement).
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker  Note that the line number is passed to the tokenized logging system as
42*61c4878aSAndroid Build Coastguard Worker  metadata, but is not part of the tokenized string. This is to ensure the
43*61c4878aSAndroid Build Coastguard Worker  CHECK callsite maximizes efficiency by only passing two arguments to the
44*61c4878aSAndroid Build Coastguard Worker  handler.
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard WorkerIn both cases, the assert handler is only called with two arguments: a 32-bit
47*61c4878aSAndroid Build Coastguard Workertoken to represent a string, and the integer line number of the callsite.
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker-----
50*61c4878aSAndroid Build Coastguard WorkerSetup
51*61c4878aSAndroid Build Coastguard Worker-----
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker#. Set ``pw_assert_BACKEND = "$dir_pw_assert_tokenized:check_backend"`` and
54*61c4878aSAndroid Build Coastguard Worker   ``pw_assert_LITE_BACKEND = "$dir_pw_assert_tokenized:assert_backend"`` in
55*61c4878aSAndroid Build Coastguard Worker   your target configuration.
56*61c4878aSAndroid Build Coastguard Worker#. Ensure your target provides
57*61c4878aSAndroid Build Coastguard Worker   ``pw_log_tokenized_HANDLER_BACKEND``. By default, pw_assert_tokenized will
58*61c4878aSAndroid Build Coastguard Worker   forward assert failures to the log system. The tokenizer handler should check
59*61c4878aSAndroid Build Coastguard Worker   for ``LOG_LEVEL_FATAL`` and properly divert to a crash handler.
60*61c4878aSAndroid Build Coastguard Worker#. Add file name tokens to your token database. pw_assert_tokenized can't create
61*61c4878aSAndroid Build Coastguard Worker   file name tokens that can be parsed out of the final compiled binary. The
62*61c4878aSAndroid Build Coastguard Worker   ``pw_relative_source_file_names``
63*61c4878aSAndroid Build Coastguard Worker   :ref:`GN template<module-pw_build-relative-source-file-names>` can be used to
64*61c4878aSAndroid Build Coastguard Worker   collect the names of all source files used in your final executable into a
65*61c4878aSAndroid Build Coastguard Worker   JSON file, which can then be included in the creation of a tokenizer
66*61c4878aSAndroid Build Coastguard Worker   database.
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard WorkerExample file name token database setup
69*61c4878aSAndroid Build Coastguard Worker--------------------------------------
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker.. code-block::
72*61c4878aSAndroid Build Coastguard Worker
73*61c4878aSAndroid Build Coastguard Worker   pw_executable("main") {
74*61c4878aSAndroid Build Coastguard Worker     deps = [
75*61c4878aSAndroid Build Coastguard Worker       # ...
76*61c4878aSAndroid Build Coastguard Worker     ]
77*61c4878aSAndroid Build Coastguard Worker     sources = [ "main.cc" ]
78*61c4878aSAndroid Build Coastguard Worker   }
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker   pw_tokenizer_database("log_tokens") {
81*61c4878aSAndroid Build Coastguard Worker     database = "tools/tokenized_logs.csv"
82*61c4878aSAndroid Build Coastguard Worker     deps = [
83*61c4878aSAndroid Build Coastguard Worker       ":source_file_names",
84*61c4878aSAndroid Build Coastguard Worker       ":main",
85*61c4878aSAndroid Build Coastguard Worker     ]
86*61c4878aSAndroid Build Coastguard Worker     optional_paths = [ "$root_build_dir/**/*.elf" ]
87*61c4878aSAndroid Build Coastguard Worker     input_databases = [ "$target_gen_dir/source_file_names.json" ]
88*61c4878aSAndroid Build Coastguard Worker   }
89*61c4878aSAndroid Build Coastguard Worker
90*61c4878aSAndroid Build Coastguard Worker   # Extracts all source/header file names from "main" and its transitive
91*61c4878aSAndroid Build Coastguard Worker   # dependencies for tokenization.
92*61c4878aSAndroid Build Coastguard Worker   pw_relative_source_file_names("source_file_names") {
93*61c4878aSAndroid Build Coastguard Worker     deps = [ ":main" ]
94*61c4878aSAndroid Build Coastguard Worker     outputs = [ "$target_gen_dir/source_file_names.json" ]
95*61c4878aSAndroid Build Coastguard Worker   }
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker.. warning::
99*61c4878aSAndroid Build Coastguard Worker  This module is experimental and does not provide a stable API.
100