Reputation: 310
Is there any good way to turn a std.stdio.File
into something that is an instance of a stream from std.stream
?
Reason: I find myself wanting a generic logging utility working on streams, and I want to pass it std.stdio.stderr
which is a std.stdio.File
.
Upvotes: 1
Views: 153
Reputation: 26579
Instead of the deprecated std.stream
module, use ranges.
import std.stdio;
import std.range;
import std.algorithm;
import std.typecons;
import std.conv;
// Log levels
enum LEVEL {
DEBUG,
INFO,
WARN
};
alias LogMsg = Tuple!(LEVEL, string); // Should be a struct, but I'm lazy
void main() {
// Get a writer, which is an OutputRange
auto writer = stderr.lockingTextWriter();
// Some messages. Can be any InputRange, not just an array
auto messages = [
LogMsg(LEVEL.DEBUG, "Log message 1"),
LogMsg(LEVEL.INFO, "Log message 2"),
LogMsg(LEVEL.WARN, "Log message 3"),
];
// Write each message to the writer
put(writer, messages
// transform LogMsg's into strings to write.
// Bonus points: use chain instead of ~ to avoid allocation
.map!(msg => msg[0].to!string ~ ": " ~ msg[1] ~ "\n")
);
}
Upvotes: 4
Reputation: 2413
you can use derr from cstream.
example:
import std.stream;
import std.cstream;
void main() {
ubyte[] data = cast(ubyte[])"someData";
OutputStream stream = derr;
stream.write(data);
}
Btw. there is a logging module in D standard lib
Upvotes: 2