Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. Observability
  5. pw_log
  6. Tokenized args
Tokenized args

Overview

Learn more: Tokenized log arguments

Namespaces

namespace  pw
 The Pigweed namespace.
 

Macros

#define PW_STATUS_DOMAIN   "::pw::Status"
 Tokenizer domain for pw_Status and pw::Status codes.
 
#define PW_STATUS_CODE_FMT   PW_LOG_TOKEN_FMT(PW_STATUS_DOMAIN)
 
#define PW_STATUS_FMT   PW_LOG_STATUS_DOMAIN_FMT(PW_STATUS_DOMAIN, 5)
 
#define PW_LOG_STATUS_DOMAIN_FMT(domain, code_bits)   PW_LOG_TOKEN_FMT(domain)
 
#define PW_LOG_STATUS(...)    PW_LOG_ENUM(::pw::log::internal::LoggedStatus(__VA_ARGS__))
 
#define PW_LOG_ENUM_FMT(enum)   PW_LOG_TOKEN_FMT(#enum)
 
#define PW_LOG_TOKEN_TYPE   const char*
 
#define PW_LOG_TOKEN_DEFAULT_VALUE   nullptr
 
#define PW_LOG_TOKEN(...)    PW_DELEGATE_BY_ARG_COUNT(_PW_STRING_OPTIONAL_DOMAIN_, __VA_ARGS__)
 
#define PW_LOG_TOKEN_EXPR(...)    PW_DELEGATE_BY_ARG_COUNT(_PW_STRING_OPTIONAL_DOMAIN_, __VA_ARGS__)
 
#define PW_LOG_TOKEN_FMT(...)   "%s"
 
#define PW_LOG_ENUM(...)    ::pw::EnumToString(::pw::log::internal::ToEnum(__VA_ARGS__))
 
#define PW_LOG_NESTED_TOKEN_FMT(...)   "%s::%s"
 
#define PW_LOG_ENUM_VERSIONED_FMT(...)   "%s"
 
#define PW_LOG_ENUM_VERSIONED(...)   PW_LOG_ENUM(__VA_ARGS__)
 

Macro Definition Documentation

◆ PW_LOG_ENUM

#define PW_LOG_ENUM (   ...)     ::pw::EnumToString(::pw::log::internal::ToEnum(__VA_ARGS__))

Converts an enum to the log argument for a static-domain format specifier (such as a generated <ENUM>_FMT macro). Evaluates its argument once.

If nested tokenization is supported by the logging backend, returns the 32-bit enum token. For non-tokenizing backends, returns the string name of the enumerator (const char*).

With pw_log/status.h included, also accepts statuses with enum codes, pw::StatusWithSize, and pw::Result, and logs their status code.

◆ PW_LOG_ENUM_FMT

#define PW_LOG_ENUM_FMT (   enum)    PW_LOG_TOKEN_FMT(#enum)

Format specifier for logging a PW_TOKENIZE_ENUM enum with PW_LOG_ENUM.

If nested tokenization is supported by the logging backend, this expands to a nested token format specifier using #enum as the domain. For non-tokenizing backends, defaults to s.

Note
pw_enum types use versioned tokenizer domains. Log them with the generated <ENUM>_FMT macro or PW_LOG_ENUM_VERSIONED_FMT() instead.

◆ PW_LOG_ENUM_VERSIONED

#define PW_LOG_ENUM_VERSIONED (   ...)    PW_LOG_ENUM(__VA_ARGS__)

Converts an enum to the arguments needed to log its enumerator with PW_LOG_ENUM_VERSIONED_FMT(). Accepts the same types as PW_LOG_ENUM and evaluates its argument once.

If nested tokenization is supported by the logging backend, expands to two arguments: the 32-bit versioned domain token and the enum value token. For non-tokenizing backends, expands to the enumerator string (const char*).

◆ PW_LOG_ENUM_VERSIONED_FMT

#define PW_LOG_ENUM_VERSIONED_FMT (   ...)    "%s"

Type-agnostic format specifier for logging a versioned enum or status code with PW_LOG_ENUM_VERSIONED.

On both tokenizing and non-tokenizing backends, formats as the enumerator string (for example, "SECOND" or "NOT_FOUND").

◆ PW_LOG_NESTED_TOKEN_FMT

#define PW_LOG_NESTED_TOKEN_FMT (   ...)    "%s::%s"

If nested tokenization is supported by the logging backend, this is an alias for PW_NESTED_TOKEN_FMT.

For non-tokenizing backends, defaults to s::s.

◆ PW_LOG_STATUS

#define PW_LOG_STATUS (   ...)     PW_LOG_ENUM(::pw::log::internal::LoggedStatus(__VA_ARGS__))

Expands to the argument needed to log a status and its optional message with <STATUS>_FMT or PW_STATUS_FMT. Accepts pw::Status, other statuses with enum codes, pw::StatusWithSize, and pw::Result. Evaluates its argument once.

The status logs as CODE, or as CODE (message) if it has a message:

// 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));
// Statuses generated by pw_enum have their own format macro.
PW_LOG_WARN("Transfer failed: " MY_DRIVER_I2C_STATUS_FMT,
PW_LOG_STATUS(i2c_status));
#define PW_LOG_STATUS(...)
Definition: status.h:148
#define PW_STATUS_FMT
Definition: status.h:69

With a non-tokenizing pw_log backend and status messages enabled, the argument is a string in a temporary buffer that only exists until the end of the log statement.

◆ PW_LOG_STATUS_DOMAIN_FMT

#define PW_LOG_STATUS_DOMAIN_FMT (   domain,
  code_bits 
)    PW_LOG_TOKEN_FMT(domain)

Format specifier for logging a status with PW_LOG_STATUS. Generated pw_enum statuses define <STATUS>_FMT with this macro.

Formats as CODE, or as CODE (message) if the status has a message.

Parameters
[in]domainString literal with the tokenizer domain of the status's code enum.
[in]code_bitsNumber of low bits that the status's code occupies when it shares a 32-bit word with a message token: the bit width of the code enum (pw::EnumTraits<Code>::kBitWidth), or 32 if the code enum does not support messages. Must be a decimal integer literal. If it is wrong, tokenized logs of the status cannot be decoded.

◆ PW_LOG_TOKEN

#define PW_LOG_TOKEN (   ...)     PW_DELEGATE_BY_ARG_COUNT(_PW_STRING_OPTIONAL_DOMAIN_, __VA_ARGS__)

If nested tokenization is supported by the logging backend, this is an alias for PW_TOKENIZE_STRING. No-op otherwise.

◆ PW_LOG_TOKEN_DEFAULT_VALUE

#define PW_LOG_TOKEN_DEFAULT_VALUE   nullptr

An "empty" value for a token.

If nested tokenization is supported by the logging backend, this is 0. Otherwise, it is nullptr.

◆ PW_LOG_TOKEN_EXPR

#define PW_LOG_TOKEN_EXPR (   ...)     PW_DELEGATE_BY_ARG_COUNT(_PW_STRING_OPTIONAL_DOMAIN_, __VA_ARGS__)

If nested tokenization is supported by the logging backend, this is an alias for PW_TOKENIZE_STRING_EXPR. No-op otherwise.

◆ PW_LOG_TOKEN_FMT

#define PW_LOG_TOKEN_FMT (   ...)    "%s"

If nested tokenization is supported by the logging backend, this is an alias for PW_TOKEN_FMT.

For non-tokenizing backends, defaults to the string specifier s.

◆ PW_LOG_TOKEN_TYPE

#define PW_LOG_TOKEN_TYPE   const char*

If nested tokenization is supported by the logging backend, this is an alias for pw_tokenizer_Token.

For non-tokenizing backends, defaults to const char*.

◆ PW_STATUS_CODE_FMT

#define PW_STATUS_CODE_FMT   PW_LOG_TOKEN_FMT(PW_STATUS_DOMAIN)

Format specifier for logging the code of a pw::Status, pw_Status, pw::StatusWithSize, or pw::Result<T> with PW_LOG_ENUM.

◆ PW_STATUS_FMT

#define PW_STATUS_FMT   PW_LOG_STATUS_DOMAIN_FMT(PW_STATUS_DOMAIN, 5)

Format specifier for logging a pw::Status, pw::StatusWithSize, or pw::Result<T> with PW_LOG_STATUS.

Describe what failed, then log the 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));