1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-tokenized-args: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker----------------------- 4*61c4878aSAndroid Build Coastguard WorkerTokenized log arguments 5*61c4878aSAndroid Build Coastguard Worker----------------------- 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_log 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard WorkerThe ``pw_log`` facade is intended to make the logging backend invisible to the 10*61c4878aSAndroid Build Coastguard Workeruser, but some backend features require additional frontend support, 11*61c4878aSAndroid Build Coastguard Workernecessitating a break in the abstraction. One of these features is the logging 12*61c4878aSAndroid Build Coastguard Workerof nested token arguments in tokenized logs, because only the user is able to 13*61c4878aSAndroid Build Coastguard Workerknow which log arguments can be tokens (see :ref:`module-pw_tokenizer` for 14*61c4878aSAndroid Build Coastguard Workercontext on tokenization, and :ref:`module-pw_log_tokenized` for an example of 15*61c4878aSAndroid Build Coastguard Workera tokenized logging backend). Arguments that have already been tokenized are 16*61c4878aSAndroid Build Coastguard Workerjust unsigned integers, and not all strings can be compile-time constants, so 17*61c4878aSAndroid Build Coastguard Workerusers must be provided with a way of manually marking token arguments. 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard WorkerTo this end, ``pw_log/tokenized_args.h`` aliases macros from ``pw_tokenizer`` 20*61c4878aSAndroid Build Coastguard Workerto enable logging nested tokens when the active backend uses tokenization. 21*61c4878aSAndroid Build Coastguard WorkerThese alias macros revert to plain string logging otherwise, allowing projects 22*61c4878aSAndroid Build Coastguard Workerto take advantage of nested token logging without breaking readable logs when 23*61c4878aSAndroid Build Coastguard Workerthe project is built with a different logging backend. To support logging 24*61c4878aSAndroid Build Coastguard Workernested token arguments, a ``pw_log`` backend must add an empty file 25*61c4878aSAndroid Build Coastguard Worker``log_backend_uses_pw_tokenizer.h`` under ``public_overrides/pw_log_backend/``. 26*61c4878aSAndroid Build Coastguard Worker 27*61c4878aSAndroid Build Coastguard WorkerAlthough the detokenizing backend accepts several different numeric bases, the 28*61c4878aSAndroid Build Coastguard Workermacros currently only support formatting nested tokens in hexadecimal, which 29*61c4878aSAndroid Build Coastguard Workeraffects how the arguments appear in final logs if they cannot be detokenized 30*61c4878aSAndroid Build Coastguard Workerfor any reason. Undetokenized tokens will appear inline as hex integers 31*61c4878aSAndroid Build Coastguard Workerprefixed with ``$#``, e.g. ``$#34d16466``. 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_LOG_TOKEN_TYPE 34*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_LOG_TOKEN 35*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_LOG_TOKEN_EXPR 36*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_LOG_TOKEN_FMT 37*61c4878aSAndroid Build Coastguard Worker 38*61c4878aSAndroid Build Coastguard WorkerExample usage with inline string arguments: 39*61c4878aSAndroid Build Coastguard Worker 40*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h" 43*61c4878aSAndroid Build Coastguard Worker #include "pw_log/tokenized_args.h" 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard Worker // bool active_ 46*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("Component is " PW_LOG_TOKEN_FMT(), 47*61c4878aSAndroid Build Coastguard Worker active_ ? PW_LOG_TOKEN_EXPR("active") 48*61c4878aSAndroid Build Coastguard Worker : PW_LOG_TOKEN_EXPR("idle")); 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard WorkerExample usage with enums: 51*61c4878aSAndroid Build Coastguard Worker 52*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 53*61c4878aSAndroid Build Coastguard Worker 54*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h" 55*61c4878aSAndroid Build Coastguard Worker #include "pw_log/tokenized_args.h" 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard Worker namespace foo { 58*61c4878aSAndroid Build Coastguard Worker 59*61c4878aSAndroid Build Coastguard Worker enum class Color { kRed, kGreen, kBlue }; 60*61c4878aSAndroid Build Coastguard Worker 61*61c4878aSAndroid Build Coastguard Worker PW_LOG_TOKEN_TYPE ColorToToken(Color color) { 62*61c4878aSAndroid Build Coastguard Worker switch (color) { 63*61c4878aSAndroid Build Coastguard Worker case Color::kRed: 64*61c4878aSAndroid Build Coastguard Worker return PW_LOG_TOKEN_EXPR("kRed"); 65*61c4878aSAndroid Build Coastguard Worker case Color::kGreen: 66*61c4878aSAndroid Build Coastguard Worker return PW_LOG_TOKEN_EXPR("kGreen"); 67*61c4878aSAndroid Build Coastguard Worker case Color::kBlue: 68*61c4878aSAndroid Build Coastguard Worker return PW_LOG_TOKEN_EXPR("kBlue"); 69*61c4878aSAndroid Build Coastguard Worker default: 70*61c4878aSAndroid Build Coastguard Worker return PW_LOG_TOKEN_EXPR("kUnknown"); 71*61c4878aSAndroid Build Coastguard Worker } 72*61c4878aSAndroid Build Coastguard Worker } 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker } // namespace foo 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard Worker void LogColor(foo::Color color) { 77*61c4878aSAndroid Build Coastguard Worker PW_LOG("Color: [" PW_LOG_TOKEN_FMT() "]", color) 78*61c4878aSAndroid Build Coastguard Worker } 79