Skip to content

Authoring custom structured data

RFC 5424 lets a message carry structured data: one or more SD-ELEMENTs, each a named bag of PARAM="value" pairs:

[example@32473 detail="Hello World"]

SolidSyslog ships the three standard elements (meta, timeQuality, origin). This guide is for adding your own element (an access-control event, a device reading, a config change) with the library's safe authoring API.

You write only the content. The library owns the framing: the brackets, the SD-ID spelling, the PARAM-NAME="..." punctuation, and the RFC 5424 section 6.3.3 value escaping. A value can never break out of its quotes; whatever bytes you hand it stay inside the "...". The names are yours to keep valid, as described below. The framing holds while the header and structured data fit: the formatter holds SOLIDSYSLOG_MAX_MESSAGE_SIZE bytes, one of them kept for a terminating NUL, and writes the header and structured data before the message text. If those two do not fit, the record is truncated inside an element and loses its closing quote and bracket. Keep elements well within the cap.

The complete worked example below is drawn from the BDD target source at Bdd/Targets/Common/BddTargetCustomSd.c, which is test infrastructure rather than shipped example code.

The two writer types

Authoring uses two opaque, stack-transient writers; you never touch a raw buffer:

Type You get it from What it does
SolidSyslogSdElement handed to your Format callback Opens/closes one element: SolidSyslogSdElement_Begin(name, enterprise), SolidSyslogSdElement_Param(name), SolidSyslogSdElement_End. Owns the SD-ID and PARAM-NAME syntax.
SolidSyslogSdValue SolidSyslogSdElement_Param(...) returns one Writes one PARAM value: SolidSyslogSdValue_String, SolidSyslogSdValue_BoundedString, SolidSyslogSdValue_Uint32. Applies the section 6.3.3 escaping.

A value producer is only ever handed a SolidSyslogSdValue*; it physically cannot open a parameter or reach the framing.

Implementing a custom SD

A custom SD is a SolidSyslogStructuredData, a one-function vtable. Implement Format, which the library calls with an SD-ELEMENT writer when it builds a message:

#include "SolidSyslogSdElement.h"
#include "SolidSyslogSdValue.h"
#include "SolidSyslogStructuredDataDefinition.h"

static void ExampleSd_Format(struct SolidSyslogStructuredData* base, struct SolidSyslogSdElement* element)
{
    (void) base; /* stateless here - see "Carrying instance or per-call data" below */

    SolidSyslogSdElement_Begin(element, "example", 32473U);
    SolidSyslogSdValue_String(SolidSyslogSdElement_Param(element, "detail"), "Hello World");
    SolidSyslogSdElement_End(element);
}

static struct SolidSyslogStructuredData exampleSd = {ExampleSd_Format};

Begin → Param/value → End. Emit as many parameters as you like between Begin and End; emit more than one element by repeating the Begin…End cycle.

Carrying instance or per-call data

Format receives the SD object itself as base, so a stateful SD reads its data through base. Embed the vtable as the first member and downcast:

struct ExampleSd
{
    struct SolidSyslogStructuredData base; /* must be first */
    const struct Event* event;            /* whatever your element reports */
};

static void ExampleSd_Format(struct SolidSyslogStructuredData* base, struct SolidSyslogSdElement* element)
{
    const struct ExampleSd* self = (const struct ExampleSd*) base;
    SolidSyslogSdElement_Begin(element, "example", 32473U);
    SolidSyslogSdValue_String(SolidSyslogSdElement_Param(element, "user"), self->event->User);
    SolidSyslogSdElement_End(element);
}

The library never allocates your SD; it lives in your storage (static, stack, or your own pool). An SD passed to SolidSyslog_LogWithSd only needs to stay valid for that call. An SD in Config.Sd[], and the array itself, are read on every record, so both must outlive the logger.

SD-IDs and enterprise numbers

SolidSyslogSdElement_Begin(name, enterpriseNumber) builds the SD-ID:

  • Enterprise number 0 → an IANA-registered name, emitted verbatim: [meta …].
  • Non-zero → a private name@number: [example@32473 …]. The number is your IANA Private Enterprise Number (PEN); 32473 is the reserved example/documentation PEN.

Register for a PEN through IANA before shipping a private SD-ID of your own; until then 32473 is the correct placeholder.

The library's own element, solidsyslog@67478 on the records it writes about itself, is under the PEN registered to Cozens Software Solutions Limited. That number is fixed in the library and is not something you supply: those records go out under it whatever PEN your own elements use.

Keep your SD-ID and PARAM-NAMEs within RFC 5424 section 6.3.2: printable US-ASCII excluding =, ], ", and space. You write these as compile-time constants, so this is yours to get right. The library bounds each name to 32 bytes and substitutes non-printable bytes and spaces, and a NULL name suppresses the element (or skips that parameter). It writes =, ] and " as given, and a ] in a name breaks the framing.

Registering your SD

There are two ways to attach a custom SD, and they compose: per-instance elements emit first, then per-message ones.

Per-instance, on every message. Put it in the config Sd[] array at create time:

/* read on every record, so it must outlive the logger */
static struct SolidSyslogStructuredData* sds[2];

sds[0] = metaSd;
sds[1] = &exampleSd;
struct SolidSyslogConfig config = { /* ... */ .Sd = sds, .SdCount = 2 };

Per-message, on one call. Pass it to SolidSyslog_LogWithSd, which attaches the array to just that message after the per-instance set:

struct SolidSyslogStructuredData* sds[] = {&exampleSd};
SolidSyslog_LogWithSd(handle, &message, sds, 1);

SolidSyslog_Log(handle, &message) is exactly SolidSyslog_LogWithSd(handle, &message, NULL, 0). A NULL array entry is skipped, so you can leave conditional elements out without a placeholder.

Reentrancy

A stateful SD reads its data through base while Format runs. Because a log call formats synchronously, the object only has to be valid across the call, but don't share one mutable SD object across threads that log concurrently: if one thread repoints self->event while another is mid-Format, the second thread emits the wrong data. Give each call site its own SD instance, or keep the object immutable. A stateless SD (like the first example) is never affected. An SD in Config.Sd[] is formatted for every record, including the logger's reports of its own events from SolidSyslog_Service, so it is always shared; the Format contract states what that requires.

Values and escaping

Write values with SolidSyslogSdValue:

  • SolidSyslogSdValue_String(value, source): a NUL-terminated string.
  • SolidSyslogSdValue_BoundedString(value, source, maxDecodedLength): capped for a receiver that parses into a width-limited field. The bound counts what the reader's un-escaping decoder extracts, not the on-wire bytes.
  • SolidSyslogSdValue_Uint32(value, number): decimal digits.

The library applies the RFC 5424 section 6.3.3 escaping for you (", \, and ] are backslash-escaped) and substitutes ill-formed UTF-8 with U+FFFD; you pass the raw value and a receiver that un-escapes gets well-formed text back. Output is bounded by the message buffer, so a value can never overrun it.

What the library owns, and what you own

The library owns You own
Brackets, SD-ID, PARAM-NAME="..." punctuation The element and parameter names
Section 6.3.3 value escaping + UTF-8 validation The parameter values
Name length bound + NULL-name suppression Valid name characters (section 6.3.2)
Buffer bounding Registering the SD (Config.Sd[] or LogWithSd), and its storage/lifetime