Tokenized log arguments#

pw_log: Robust and portable logging for embedded projects

The pw_log facade is intended to make the logging backend invisible to the user, but some backend features require additional frontend support, necessitating a break in the abstraction. One of these features is the logging of nested token arguments in tokenized logs, because only the user is able to know which log arguments can be tokens (see pw_tokenizer for context on tokenization, and pw_log_tokenized for an example of a tokenized logging backend). Arguments that have already been tokenized are just unsigned integers, and not all strings can be compile-time constants, so users must be provided with a way of manually marking token arguments.

To this end, pw_log/tokenized_args.h aliases macros from pw_tokenizer to enable logging nested tokens when the active backend uses tokenization. These alias macros revert to plain string logging otherwise, allowing projects to take advantage of nested token logging without breaking readable logs when the project is built with a different logging backend. To support logging nested token arguments, a pw_log backend must add an empty file log_backend_uses_pw_tokenizer.h under public_overrides/pw_log_backend/.

Although the detokenizing backend accepts several different numeric bases, the macros currently only support formatting nested tokens in hexadecimal, which affects how the arguments appear in final logs if they cannot be detokenized for any reason. Undetokenized tokens will appear inline as hex integers prefixed with $#, e.g. $#34d16466.

Inline string arguments#

#include "pw_log/tokenized_args.h"

#include "pw_log/log.h"

// bool active_
PW_LOG_INFO("Component is " PW_LOG_TOKEN_FMT(),
            active_ ? PW_LOG_TOKEN_EXPR("active") : PW_LOG_TOKEN_EXPR("idle"));

Logging enums and status codes#

PW_LOG_ENUM and PW_LOG_ENUM_VERSIONED log enumerators as tokens on tokenizing backends and strings on non-tokenizing backends, evaluating their argument once. With pw_log/status.h (//pw_log:status) included, they also log the status code of pw::Status, other statuses with enum codes, pw::StatusWithSize, and pw::Result:

I2cStatus and the MY_DRIVER_I2C_STATUS_* macros in the examples below are generated by pw_enum. See Logging statuses.

#include "pw_log/log.h"
#include "pw_log/status.h"

void LogTransferCode(pw::Status status) {
  // Static domain (1 argument):
  PW_LOG_INFO("Transfer code: " PW_STATUS_CODE_FMT, PW_LOG_ENUM(status));
}

template <typename EnumOrStatus>
void LogGenericCode(const EnumOrStatus& value) {
  // Versioned domain (works for any pw_enum enum or status):
  PW_LOG_INFO("Code: " PW_LOG_ENUM_VERSIONED_FMT(),
              PW_LOG_ENUM_VERSIONED(value));
}

Logging statuses#

PW_LOG_STATUS from pw_log/status.h logs a pw::Status or other pw::StatusBase-derived status as CODE. If the status has a message created with PW_STATUS_MSG("...") (see Status messages), it logs as CODE (message). Pair PW_LOG_STATUS with PW_STATUS_FMT for pw::Status, or with a generated <STATUS>_FMT macro. See the pw_status logging guide for how to use it.

#include "pw_log/log.h"
#include "pw_log/status.h"

void HandleStatus(pw::Status status, const I2cStatus& i2c_status) {
  // Logs "Read failed: NOT_FOUND". If the status has a message, logs
  // "Read failed: NOT_FOUND (No such key)".
  PW_LOG_WARN("Read failed: " PW_STATUS_FMT, PW_LOG_STATUS(status));
  PW_LOG_WARN("I2C transfer failed: " MY_DRIVER_I2C_STATUS_FMT,
              PW_LOG_STATUS(i2c_status));
}

PW_LOG_STATUS evaluates its argument once and expands to one log argument. To log the status of a pw::Result or pw::StatusWithSize, pass the object itself (PW_LOG_STATUS(result)).

How the status is logged depends on how status messages are stored, which is set with PW_STATUS_CFG_MESSAGES (see Enabling messages):

  • PW_STATUS_CFG_MESSAGES_NONE (the default): the status code, as with PW_LOG_ENUM.

  • PW_STATUS_CFG_MESSAGES_TOKENIZED: one 32-bit value with the code in its low bits and the message’s token in the rest, logged as a bit field token. The message’s token is in the domain of the status’s code followed by ::Message (e.g. ::pw::Status::Message). Bit field tokens are currently only decoded by the Python detokenizer.

  • PW_STATUS_CFG_MESSAGES_STRING: one string, formatted into a temporary buffer of PW_LOG_STATUS_STRING_SIZE bytes that exists until the end of the log statement. Longer text is truncated. The backend must use the string before the log statement returns.

PW_STATUS_CFG_MESSAGES must match the pw_log backend: tokenized messages for backends that tokenize logs, string messages otherwise. pw_log/status.h fails to compile with an explanation if they do not match.

API reference#

Moved: Tokenized args