LogBuffer

Wirte formatted error messages into a memory buffer.

This module is not thread safe.

Summary
LogBufferWirte formatted error messages into a memory buffer.
CopyrightThis program is free software.
Files
C-kern/api/io/writer/log/logbuffer.hHeader file LogBuffer.
C-kern/io/writer/log/logbuffer.cImplementation file LogBuffer impl.
Types
struct logbuffer_tExport logbuffer_t into global namespace.
Functions
test
unittest_io_writer_log_logbufferTest logbuffer_t functionality.
logbuffer_tA logbuffer_t writes error messages to a buffer.
addrHolds start address of memory buffer.
sizeHolds size in bytes of memory buffer.
logsizeStores the size in bytes of the buffered log entries.
ioHolds iochannel the log is written to.
lifetime
logbuffer_FREEStatic initializer.
logbuffer_INITStatic initializer.
init_logbufferInitializes object.
free_logbufferClears all members.
query
sizefree_logbufferReturns free size usable by the next written entry.
io_logbufferReturns the iochannel_t the content of the buffer is written to.
getbuffer_logbufferReturns the start address of the memory buffer and size of written log.
compare_logbufferReturns 0 if logbuffer compares equal to content in logbuf.
update
clear_logbufferClears the buffer without writting it out.
write_logbufferWrites (flushes) the buffer to the configured io channel.
printf_logbufferWrites new log entry to log buffer.
printheader_logbufferAppends header to log buffer.
vprintf_logbufferSame as printf_logbuffer.
inline implementation
Macros
clear_logbufferImplements logbuffer_t.clear_logbuffer.
getbuffer_logbufferImplements logbuffer_t.getbuffer_logbuffer.
io_logbufferImplements logbuffer_t.io_logbuffer.
sizefree_logbufferImplements logbuffer_t.sizefree_logbuffer.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/io/writer/log/logbuffer.h

Header file LogBuffer.

C-kern/io/writer/log/logbuffer.c

Implementation file LogBuffer impl.

Types

struct logbuffer_t

typedef struct logbuffer_t logbuffer_t

Export logbuffer_t into global namespace.

Functions

test

unittest_io_writer_log_logbuffer

int unittest_io_writer_log_logbuffer(void)

Test logbuffer_t functionality.

logbuffer_t

struct logbuffer_t

A logbuffer_t writes error messages to a buffer.  If a new messages is always appended and it is truncated if there is no more space in the buffer.  If new messages should not be appended and less then log_config_MINSIZE plus “terminating \0 byte” bytes are free the buffer is written to a configured iochannel_t before the new message is written into the buffer.

Summary
addrHolds start address of memory buffer.
sizeHolds size in bytes of memory buffer.
logsizeStores the size in bytes of the buffered log entries.
ioHolds iochannel the log is written to.
lifetime
logbuffer_FREEStatic initializer.
logbuffer_INITStatic initializer.
init_logbufferInitializes object.
free_logbufferClears all members.
query
sizefree_logbufferReturns free size usable by the next written entry.
io_logbufferReturns the iochannel_t the content of the buffer is written to.
getbuffer_logbufferReturns the start address of the memory buffer and size of written log.
compare_logbufferReturns 0 if logbuffer compares equal to content in logbuf.
update
clear_logbufferClears the buffer without writting it out.
write_logbufferWrites (flushes) the buffer to the configured io channel.
printf_logbufferWrites new log entry to log buffer.
printheader_logbufferAppends header to log buffer.
vprintf_logbufferSame as printf_logbuffer.

addr

uint8_t * addr

Holds start address of memory buffer.

size

uint32_t size

Holds size in bytes of memory buffer.

logsize

uint32_t logsize

Stores the size in bytes of the buffered log entries.  If the buffer is empty logsize is 0.

io

sys_iochannel_t io

Holds iochannel the log is written to.

lifetime

logbuffer_FREE

#define logbuffer_FREE { 0, 0, 0, sys_iochannel_FREE }

Static initializer.

logbuffer_INIT

#define logbuffer_INIT(size,
addr,
io) { (addr), (size), ((addr)[0] = 0), (io) }

Static initializer.  You do not need to free an object initialized in such a manner but you can.

Parameter

sizeSize of a temporary or static buffer.
addrStart address of the buffer.
ioiochannel_t the buffer is written to.

init_logbuffer

int init_logbuffer(/*out*/logbuffer_t *logbuf,
uint32_t buffer_size,
uint8_t buffer_addr[buffer_size],
sys_iochannel_t io)

Initializes object.  No additional resources are allocated.

free_logbuffer

int free_logbuffer(logbuffer_t *logbuf)

Clears all members.  The memory is not freed it is considered managed by the calling object.  The configured iochannel_t is freed if it does not equal iochannel_STDERR or iochannel_STDOUT.

query

sizefree_logbuffer

uint32_t sizefree_logbuffer(const logbuffer_t *logbuf)

Returns free size usable by the next written entry.  Call write_logbuffer if this value is less than log_config_MINSIZE+”terminating \0 byte”.

io_logbuffer

sys_iochannel_t io_logbuffer(const logbuffer_t *logbuf)

Returns the iochannel_t the content of the buffer is written to.

getbuffer_logbuffer

void getbuffer_logbuffer(const logbuffer_t *logbuf,
/*out*/uint8_t **addr,
/*out*/size_t *logsize)

Returns the start address of the memory buffer and size of written log.

compare_logbuffer

int compare_logbuffer(const logbuffer_t *logbuf,
size_t logsize,
const uint8_t logbuffer[logsize])

Returns 0 if logbuffer compares equal to content in logbuf.  The compare equal the logsize must much and all written texts.  The timestamps are not compared so they are allowed to differ.  The value EINVAL is returned in case the comparison is not equal.

update

clear_logbuffer

void clear_logbuffer(logbuffer_t *logbuf)

Clears the buffer without writting it out.

write_logbuffer

int write_logbuffer(logbuffer_t *logbuf)

Writes (flushes) the buffer to the configured io channel.  If an error occurs no logging is done only the error code is returned.

printf_logbuffer

void printf_logbuffer(
   logbuffer_t *logbuf,
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 2, 3)))

Writes new log entry to log buffer.  If the written content is bigger than sizefree_logbuffer it is truncated.  A truncated message is indicated by “ ...” as last characters in the buffer.

printheader_logbuffer

void printheader_logbuffer(logbuffer_t *logbuf,
const struct log_header_t *header)

Appends header to log buffer.  The header looks like “[thread_id: timestamp] funcname() filename:linenr\nError NR - Description”.

vprintf_logbuffer

void vprintf_logbuffer(logbuffer_t *logbuf,
const char *format,
va_list args)

Same as printf_logbuffer.  The argument after format must be of type va_list and replaces the variable number of argumens in printf_logbuffer.

Macros

clear_logbuffer

#define clear_logbuffer(
   logbuf
) ( __extension__ ({ logbuffer_t * _lb ; _lb = (logbuf) ; _lb->addr[0] = 0 ; _lb->logsize = 0 ; }))

Implements logbuffer_t.clear_logbuffer.

getbuffer_logbuffer

#define getbuffer_logbuffer(
   logbuf,
   _addr,
   _logsize
) ( __extension__ ({ const logbuffer_t * _lb ; _lb = (logbuf) ; *(_addr) = _lb->addr ; *(_logsize) = _lb->logsize ; }))

Implements logbuffer_t.getbuffer_logbuffer.

io_logbuffer

#define io_logbuffer(logbuf) ((logbuf)->io)

Implements logbuffer_t.io_logbuffer.

sizefree_logbuffer

#define sizefree_logbuffer(
   logbuf
) ( __extension__ ({ const logbuffer_t * _lb ; _lb = (logbuf) ; _lb->size - _lb->logsize ; }))

Implements logbuffer_t.sizefree_logbuffer.

Wirte formatted error messages into a memory buffer.
Implements LogBuffer.
typedef struct logbuffer_t logbuffer_t
Export logbuffer_t into global namespace.
struct logbuffer_t
A logbuffer_t writes error messages to a buffer.
int unittest_io_writer_log_logbuffer(void)
Test logbuffer_t functionality.
uint8_t * addr
Holds start address of memory buffer.
uint32_t size
Holds size in bytes of memory buffer.
uint32_t logsize
Stores the size in bytes of the buffered log entries.
sys_iochannel_t io
Holds iochannel the log is written to.
#define logbuffer_FREE { 0, 0, 0, sys_iochannel_FREE }
Static initializer.
#define logbuffer_INIT(size,
addr,
io) { (addr), (size), ((addr)[0] = 0), (io) }
Static initializer.
int init_logbuffer(/*out*/logbuffer_t *logbuf,
uint32_t buffer_size,
uint8_t buffer_addr[buffer_size],
sys_iochannel_t io)
Initializes object.
int free_logbuffer(logbuffer_t *logbuf)
Clears all members.
uint32_t sizefree_logbuffer(const logbuffer_t *logbuf)
Returns free size usable by the next written entry.
sys_iochannel_t io_logbuffer(const logbuffer_t *logbuf)
Returns the iochannel_t the content of the buffer is written to.
typedef sys_iochannel_t iochannel_t
Describes an input/output channel like a file or network connection.
void getbuffer_logbuffer(const logbuffer_t *logbuf,
/*out*/uint8_t **addr,
/*out*/size_t *logsize)
Returns the start address of the memory buffer and size of written log.
int compare_logbuffer(const logbuffer_t *logbuf,
size_t logsize,
const uint8_t logbuffer[logsize])
Returns 0 if logbuffer compares equal to content in logbuf.
void clear_logbuffer(logbuffer_t *logbuf)
Clears the buffer without writting it out.
int write_logbuffer(logbuffer_t *logbuf)
Writes (flushes) the buffer to the configured io channel.
void printf_logbuffer(
   logbuffer_t *logbuf,
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 2, 3)))
Writes new log entry to log buffer.
void printheader_logbuffer(logbuffer_t *logbuf,
const struct log_header_t *header)
Appends header to log buffer.
void vprintf_logbuffer(logbuffer_t *logbuf,
const char *format,
va_list args)
Same as printf_logbuffer.
#define clear_logbuffer(
   logbuf
) ( __extension__ ({ logbuffer_t * _lb ; _lb = (logbuf) ; _lb->addr[0] = 0 ; _lb->logsize = 0 ; }))
Implements logbuffer_t.clear_logbuffer.
#define getbuffer_logbuffer(
   logbuf,
   _addr,
   _logsize
) ( __extension__ ({ const logbuffer_t * _lb ; _lb = (logbuf) ; *(_addr) = _lb->addr ; *(_logsize) = _lb->logsize ; }))
Implements logbuffer_t.getbuffer_logbuffer.
#define io_logbuffer(logbuf) ((logbuf)->io)
Implements logbuffer_t.io_logbuffer.
#define sizefree_logbuffer(
   logbuf
) ( __extension__ ({ const logbuffer_t * _lb ; _lb = (logbuf) ; _lb->size - _lb->logsize ; }))
Implements logbuffer_t.sizefree_logbuffer.
The default standard error (output) channel.
The default standard output channel.
Close