Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Log UDP Handler

Log handler that sends each record over UDP to a collector. Useful for aggregating logs from many yunos without touching local disk.

Source code:

udpc_close()

Closes the UDP client instance referenced by udpc, releasing all associated resources.

void udpc_close(
    udpc_t udpc
);

Parameters

KeyTypeDescription
udpcudpc_tA handle to the UDP client instance to be closed.

Returns

This function does not return a value.

Notes

If udpc is NULL, the function returns immediately without performing any action. The function removes the UDP client from the internal list and deallocates its memory. If the client has an open socket, it is closed before deallocation. On ESP32 platforms, the associated event loop is deleted before freeing resources.


udpc_end()

Closes all active UDP client handlers and releases associated resources.

void udpc_end(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

This function does not return a value.

Notes

This function iterates through all active UDP clients and calls udpc_close() on each one to properly release resources.


udpc_fwrite()

udpc_fwrite() formats and sends a log message over UDP using a specified format string and arguments.

int udpc_fwrite(
    udpc_t      udpc,
    int         priority,
    const char  *format,
    ...
);

Parameters

KeyTypeDescription
udpcudpc_tA handle to the UDP client instance.
priorityintThe priority level of the log message.
formatconst char *A format string specifying how subsequent arguments are formatted.
...variadicAdditional arguments corresponding to the format string.

Returns

Returns 0 on success, or -1 if an error occurs.

Notes

Internally, udpc_fwrite() formats the message using vsnprintf() and then calls udpc_write() to send the formatted log message.


udpc_open()

udpc_open() initializes and opens a UDP client for logging, configuring its buffer size, frame size, and output format.

udpc_t udpc_open(
    const char       *url,
    const char       *bindip,
    const char       *if_name,
    size_t            bf_size,
    size_t            udp_frame_size,
    output_format_t   output_format,
    BOOL              exit_on_fail
);

Parameters

KeyTypeDescription
urlconst char *The destination URL for the UDP client.
bindipconst char *The local IP address to bind the socket, or NULL for default.
if_nameconst char *The network interface name to bind the socket, or NULL for default.
bf_sizesize_tThe buffer size in bytes; 0 defaults to 256 KB.
udp_frame_sizesize_tThe maximum UDP frame size; 0 defaults to 1500 bytes.
output_formatoutput_format_tThe output format for logging; defaults to OUTPUT_FORMAT_YUNETA if invalid.
exit_on_failBOOLIf TRUE, the process exits on failure; otherwise, it continues.

Returns

Returns a udpc_t handle to the UDP client on success, or NULL on failure.

Notes

If url is empty or invalid, udpc_open() returns NULL. Memory is allocated dynamically for the buffer; ensure proper cleanup with udpc_close(). If the socket cannot be created, the function logs an error and returns NULL.


udpc_start_up()

udpc_start_up() initializes the UDP client system by setting process-related metadata and preparing internal structures.

int udpc_start_up(
    const char *process_name,
    const char *hostname,
    int         pid
);

Parameters

KeyTypeDescription
process_nameconst char *The name of the process using the UDP client.
hostnameconst char *The hostname of the system running the UDP client.
pidintThe process ID of the calling process.

Returns

Returns 0 on success, or -1 if the system is already initialized.

Notes

This function must be called before using udpc_open() or other UDP client functions.


udpc_write()

udpc_write() sends a log message over UDP, formatting it according to the specified output format.

int udpc_write(
    udpc_t      udpc,
    int         priority,
    const char *bf,
    size_t      len
);

Parameters

KeyTypeDescription
udpcudpc_tA handle to the UDP client instance.
priorityintThe priority level of the log message, ranging from LOG_EMERG to LOG_MONITOR.
bfconst char *The log message to be sent, which must be a null-terminated string.
lensize_tThe length of the log message in bytes.

Returns

Returns 0 on success, or -1 if an error occurs.

Notes

If the message length exceeds the buffer size, the function returns an error. The function ensures that the message is properly formatted based on the selected output_format_t. If the UDP socket is not open, udpc_write() attempts to reopen it before sending the message. Messages are sent in chunks of udp_frame_size if they exceed the frame size.