AxlFormat – Printf Engine
AxlFormat – Printf Engine
Callback-driven printf engine. Format text directly into any sink (buffer, network socket, file, hash) without intermediate allocation.
This is the engine behind axl_printf, axl_snprintf, axl_asprintf,
and axl_string_append_printf. It has zero dependencies (no memory
allocator, no I/O) – it breaks the Log -> Data circular dependency by
being self-contained.
Header: <axl/axl-format.h>
Callback-Driven Formatting
The core API takes a write callback that receives formatted output in chunks. No memory is allocated – all formatting uses a small stack buffer.
#include <axl.h>
// Write directly to a TCP socket
void net_write(const char *data, size_t len, void *ctx) {
axl_tcp_send((AxlTcpSocket *)ctx, data, len);
}
// Format an HTTP request line directly into the socket
axl_format(net_write, sock, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n",
path, host);
API
Two functions:
axl_format(write_fn, ctx, fmt, ...)– variadic convenienceaxl_vformat(write_fn, ctx, fmt, args)– va_list version
The callback type:
typedef void (*AxlWriteFunc)(const char *data, size_t len, void *ctx);
Use Cases
Format directly into a network send buffer (no intermediate string)
Stream formatted output into a hash computation
Write a custom logging backend that formats in-place
Build protocol messages without allocating temporary strings
Supported Format Specifiers
Specifier |
Type |
Example |
|---|---|---|
|
|
|
|
signed int |
|
|
unsigned int |
|
|
hex (lower/upper) |
|
|
|
|
|
|
|
|
char |
|
|
pointer |
|
|
literal |
|
Width and zero-padding are supported: %08x, %-20s, %5d.
API Reference
Typedefs
-
typedef void (*AxlWriteFunc)(const char *data, size_t len, void *ctx)
AxlWriteFunc:
Callback invoked by the format engine to emit output. May be called multiple times per format call (once per literal segment and once per formatted argument).
Functions
-
void axl_vformat(AxlWriteFunc write_fn, void *ctx, const char *fmt, va_list args)
Format with va_list into a write callback.
The engine calls
write_fnone or more times with formatted output segments. No memory is allocated — all formatting uses a small stack buffer.void my_write(const char *data, size_t len, void *ctx) { axl_tcp_send((AxlTcpSocket *)ctx, data, len); } va_list args; va_start(args, fmt); axl_vformat(my_write, sock, fmt, args); va_end(args);
- Parameters:
write_fn – output callback
ctx – passed to write_fn
fmt – printf-style format string
args – format arguments
-
void axl_format(AxlWriteFunc write_fn, void *ctx, const char *fmt, ...)
Format into a write callback (variadic wrapper).
Convenience wrapper around axl_vformat.
void buf_write(const char *data, size_t len, void *ctx) { // append to a custom buffer } axl_format(buf_write, &my_buf, "count=%d name=%s", 42, "AXL");
- Parameters:
write_fn – output callback
ctx – passed to write_fn
fmt – printf-style format string
- Param :
format arguments