Fuchsia uses a wire format for structured logs inspired by its [trace format](https://fuchsia.dev/docs/reference/tracing/trace-format). This transmission
format allows consumption and propagation of arbitrary data structures as log records.

## Validation

The size of a log record is capped at 32kb.

Writers that send oversize or invalid records to the diagnostics service will have their streams
closed and an error recorded by the diagnostics service.

## Primitives

Integers have little endian encoding. Signed integers are twos-complement.

Timestamps are signed 64-bit integers measured in nanoseconds, as recorded by
[`zx_clock_get_monotonic`](https://fuchsia.dev/reference/syscalls/clock_get_monotonic).

Strings are denoted by a 16-bit "string ref". If the most significant bit (MSB) is 0, the string is
empty. If the MSB is 1, the remaining bits of the ref indicate the length of the subsequent UTF-8
byte stream. All string refs with an MSB of 0 other than the empty string are reserved for future
extensions. Strings are padded with zeroes until 8-byte aligned.

Records consist of a number of 8-byte words and are 8-byte aligned.

### Header

The required metadata for a log record are a record type, the overall length of the record, and a
timestamp. These are encoded in the first 16 bytes of a record:
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_1cf908bc4754798b545ab8aa51f6a076c0a560172f282a7650c984cca906e8e3.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

Notes:

- The "size words" field includes the header word.
- The timestamp is in nanoseconds.

Currently all records are expected to have type=9. This was chosen to mirror the [trace format](https://fuchsia.dev/docs/reference/tracing/trace-format) but
may require a change before these records can be processed by tracing tools.
Values for severity are defined in
[/sdk/fidl/fuchsia.diagnostics/severity.fidl](https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/fidl/fuchsia.diagnostics/severity.fidl)

## Arguments

Log record data is conveyed via a list of typed key-value pairs. The keys are always non-empty
strings, which supports different types of arguments, and the values can have several types.

### Argument header

Each argument has an 8-byte header, followed by the argument name, followed by the argument's value.
The name is padded with zeroes to 8-byte alignment before the argument's content is written.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_68396755570c20997f954a0e769c7c44dd9c98cbfe42e57e53987b6e0d61f892.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

Notes:

- The "size words" field includes the header word.
- The "name ref" is a string ref for the argument name.
- The "varies" section varies by argument type and must be 0 if unused.
- The "name length" is "64 \* nameref".
- The "name" is the name of the argument, the total length (not in diagram) is `namelen` and is padded to 8-byte alignment.

The first 4 bits of the argument header determine which type the argument has:

| T (type) | name |
|---|---|
| `3` | signed 64-bit integers |
| `4` | unsigned 64-bit integers |
| `5` | double-precision floats |
| `6` | UTF-8 strings |
| `9` | booleans |

### Signed 64-bit integer arguments

Signed integers are appended after the argument name is terminated.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_d183d6972c2e47702241d49c0336a991ad8455bad024e982eec4427b251d7835.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

### Unsigned 64-bit integer arguments

Unsigned integers are appended after the argument name is terminated.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_d5ce6db372a46505a56f132e1fb005ac4e3c0004de79c74ee81db1964167c1bf.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

### 64-bit floating point arguments

Floats are appended after the argument name is terminated.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_11c8d25404856233c023ad4061c4bd4725928ebb4caf0dda82f811547e177d3d.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

### String arguments

Strings are encoded in UTF-8, padded with zeroes until 8-byte aligned, and appended after the
argument name.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_4dd6023fc7d81a3fba5158eef81db0c97d42c6028cc98910b731153a19aa3953.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

### Boolean arguments

Booleans are appended after the `NameRef` field in the argument header.
<iframe src="https:///frame/fuchsia-src/reference/platform-spec/diagnostics/logs-encoding_a50f4e51a9565dfea0dcf4cf2703730600e116673da2fe2c12080662807b5598.frame" class="framebox inherit-locale " allow="clipboard-write https://" allowfullscreen is-upgraded></iframe>

# Encoding "legacy" format messages

Components that call [`LogSink.Connect`](https://fuchsia.dev/reference/fidl/fuchsia.logger#Connect) are expected to pass a socket in "datagram" mode (as
opposed to "streaming") and to write the "legacy" wire format into it. This uses little endian
integers and a mix of length-prefixed and null-terminated UTF-8 strings.
[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/reference/platform-spec/diagnostics/logs-encoding.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/reference/platform-spec/diagnostics/logs-encoding.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/reference/platform-spec/diagnostics/logs-encoding.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/reference/platform-spec/diagnostics/logs-encoding.md "Edit the source code of this page")