GC free Writer

Sean Kelly sean at invisibleduck.org
Fri Feb 8 12:36:48 PST 2013


On Feb 8, 2013, at 7:57 AM, David <d at dav1d.de> wrote:

> I am currently implementing a logging module, I want to make logging to
> stderr/stdout/"any file" possible, also during runtime-shutdown (logging
> from dtors)
> 
> Atm it lookes like this:
> 
> ----
> void log(LogLevel level, Args...)(Args args) {
>    string message = format(args);
> 
>    ... pass string to writer
> }
> ---
> 
> But format allocates! That makes it throw an InvalidMemoryOperationError
> when calling the logging function from a dtor. So I need a GC-free
> writer for std.format.formattedWrite, similiar to std.stdio's
> LockingTextWriter but with a buffer instead of a file
> (std.array.Appender GC free something like that). I couldn't come up
> with a working solution, I hope you have ideas.

Does your IO layer require one call per log line or can you do multiple writes followed by an "end log line" terminator?  Assuming the former, the most obvious approach would be for the writer to have a static array equal to the max log line size, accumulate until done and then issue one write to the output layer.


More information about the Digitalmars-d-learn mailing list