CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Memory Buffer Logging

Data Structures

struct  LogMembufData
 

Typedefs

typedef struct LogMembufData LogMembufData
 

Functions

LogDest * logmembufRegister (int maxlevel, strref chanfilter, uint32 size, LogSerializer *ser)
 
LogMembufDatalogmembufData (LogDest *dest)
 
LogMembufDatalogmembufCreate (uint32 size, LogSerializer *ser)
 
void logmembufMsgFunc (const LogRecord *rec, void *userdata)
 
void logmembufCloseFunc (void *userdata)
 

Detailed Description

Memory buffer logging destination that writes log messages to a fixed-size circular buffer in memory. Useful for debugging, testing, and capturing logs in memory-constrained environments. When the buffer fills, new messages wrap around and overwrite the oldest entries.

Basic Usage:

// Register a 4KB buffer with the default compact text serializer
LogDest *dest = logmembufRegister(LOG_Debug, NULL, 4096, NULL);
// Log some messages
logStr(Info, _SL("Test message"));
// Access the buffer contents directly
printf("Buffered logs:\n%.*s\n", (int)lmd->cur, lmd->buf);
// Cleanup -- this frees the buffer, so lmd is invalid afterwards
bool logUnregisterDest(LogDest *dhandle)
struct LogDest LogDest
Opaque handle to a registered log destination.
Definition log.h:187
void logFlush(void)
@ LOG_Debug
Debug messages (compiled out of non-development builds)
Definition log.h:106
#define logStr(level, str)
Definition log.h:612
LogDest * logmembufRegister(int maxlevel, strref chanfilter, uint32 size, LogSerializer *ser)
LogMembufData * logmembufData(LogDest *dest)
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
char * buf
Buffer storage.
Definition logmembuf.h:44
uint32 cur
Current write position (number of bytes written)
Definition logmembuf.h:43

Typedef Documentation

◆ LogMembufData

typedef struct LogMembufData LogMembufData

Memory buffer log destination state

Contains the circular buffer and current write position. When cur reaches size, new messages wrap to the beginning. The buffer is null-terminated when possible.

Function Documentation

◆ logmembufCloseFunc()

void logmembufCloseFunc ( void *  userdata)

Cleanup callback for memory buffer destinations

Frees the buffer and releases resources.

Parameters
userdataLogMembufData pointer from logmembufCreate()

◆ logmembufCreate()

LogMembufData * logmembufCreate ( uint32  size,
LogSerializer ser 
)

Create a memory buffer log destination

Only needed to register the buffer by hand with logRegisterDest()logmembufRegister() does this and the registration together. The returned handle belongs to exactly one destination: it is freed by logmembufCloseFunc() and cannot be registered twice.

Parameters
sizeBuffer size in bytes
serSerializer to render records with; ownership transfers. NULL gets a compact text serializer: short levels, bracketed channels, second-resolution timestamps.
Returns
Memory buffer handle
LogMembufData *lmd = logmembufCreate(8192, NULL);
LogDest * logRegisterDest(int maxlevel, strref chanfilter, LogDestMsg msgfunc, LogDestBatchDone batchfunc, LogDestClose closefunc, void *userdata)
void logmembufCloseFunc(void *userdata)
LogMembufData * logmembufCreate(uint32 size, LogSerializer *ser)
void logmembufMsgFunc(const LogRecord *rec, void *userdata)

◆ logmembufData()

LogMembufData * logmembufData ( LogDest *  dest)

Get the buffer behind a memory buffer destination

The returned struct is owned by the destination and lives exactly as long as it does: logUnregisterDest() frees the buffer, so nothing may read it afterwards.

Reading cur races with the drain thread, which may be appending while this runs. Read it once into a local and use that for both the length and any copy – reading it twice lets it grow in between.

Parameters
destDestination handle from logmembufRegister()
Returns
Buffer state, or NULL if dest is not a memory buffer destination
LogDest *dest = logmembufRegister(LOG_Debug, NULL, 4096, NULL);
uint32 cur = lmd->cur;
printf("%.*s", (int)cur, lmd->buf);

◆ logmembufMsgFunc()

void logmembufMsgFunc ( const LogRecord rec,
void *  userdata 
)

Log message callback for memory buffer destinations

Serializes a log record and appends it to the circular buffer, newline-terminated.

Parameters
recLog record to write
userdataLogMembufData pointer from logmembufCreate()

◆ logmembufRegister()

LogDest * logmembufRegister ( int  maxlevel,
strref  chanfilter,
uint32  size,
LogSerializer ser 
)

Register a memory buffer log destination

Allocates a fixed-size circular buffer and registers it with the logging system in one step. Records are serialized and written to the buffer; messages longer than the buffer size are dropped, and when the buffer fills, writing wraps back to the beginning. The buffer is freed when logUnregisterDest() retires the returned handle.

Use logmembufData() to reach the buffer contents.

Parameters
maxlevelMaximum log level to write to the buffer
chanfilterChannel path pattern, or NULL for every unrestricted channel
sizeBuffer size in bytes
serSerializer to render records with; ownership transfers, including if this call fails. NULL gets a compact text serializer: short levels, bracketed channels, second-resolution timestamps.
Returns
Destination handle for later unregistration, or NULL on failure
LogDest *dest = logmembufRegister(LOG_Debug, NULL, 8192, NULL); // 8KB, compact text
// or capture structured records for a test to parse
LogSerializer * logNdjsonSerializer(LogNdjsonConfig *config)