Class CircularAppender

Circular appender which appends to an internal buffer

class CircularAppender(ulong BufferSize = 2 ^^ 20) ;

Constructors

NameDescription
this () Ctor

Fields

NameTypeDescription
buffer char[BufferSize]Backing store for the cyclic buffer
cyclic Cycle!(typeof(buffer))Cyclic Output range over buffer
mask_ MaskMask
used_length size_tUsed length of the buffer, only grows, up to buffer.length

Methods

NameDescription
append (event) Append an event to the buffer
clear ()
mask () Return the fingerprint for this class
name ()
print (output) Print the contents of the appender to the console

Example

static class MockLayout : Appender.Layout
{
    /// Format the message
    public override void format (LogEvent event, scope FormatterSink dg)
    {
        sformat(dg, "{}", event.msg);
    }
}

scope get_log_output = (string log_msg){
    import dtext.log.ILogger;

    immutable buffer_size = 7;
    char[buffer_size + 1] result_buff;
    scope appender = new CircularAppender!(buffer_size);
    LogEvent event = {
        msg: log_msg,
    };
    appender.layout(new MockLayout());
    appender.append(event);
    appender.print(result_buff[]);
    return result_buff[0 .. min(log_msg.length, buffer_size)].dup;
};

// case 1
// the internal buffer size is greater than the length of the messages
// that we are trying to log
assert(get_log_output("01234") == "01234");

// case 2
// the internal buffer size is equal to the length of the messages
// that we are trying to log(there is a terminating newline)
assert(get_log_output("012345") == "012345");

// case 3
// the internal buffer size is smaller than the length of the messages
// that we are trying to log
assert(get_log_output("0123456789") == "3456789");

// Make sure we don't trip on stray format specifiers
assert(get_log_output("Thou shalt ignore random %s in the string") == " string");

// log a map over a range
import std.algorithm;
import std.range;
assert(get_log_output(format("{}", iota(2).map!(i => i + 1))) == "[1, 2]");