Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module log;
- import std.stdio;
- import std.array;
- import std.conv;
- import std.string;
- import std.datetime;
- class Log
- {
- string recordTemplate = `$t - $m`;// if not empty, then used as a template for output record;
- public LogEngine[] engines;
- bool IsEnabled = true;// Set to false if logging is not needed
- public this(LogEngine[] eng ...)
- {
- engines = eng;
- if (engines.length == 0) engines ~= new LogEngine();
- }
- version(log) {
- void opCall(T...)(string msg, T args)
- {
- if (!IsEnabled) return;
- if (recordTemplate != ``) {
- msg = replace(recordTemplate, `$m`,
- (args.length > 0 ? format(msg, args) : msg)
- );
- msg = replace(msg, `$t`, (cast(DateTime)Clock.currTime()).toSimpleString());
- }
- foreach(eng; engines)
- eng.Write(msg);
- }
- } else {
- void opCall(...){}// hope D optimizes empty function calls?
- }
- }
- class LogEngine
- {
- public void Write(string msg)
- {
- writeln(msg);// default implementation, just output to console
- }
- }
- class FileLogEngine : LogEngine
- {
- File f;
- public this(string FileName)
- {
- f = File(FileName, `a`);
- }
- override public void Write(string msg)
- {
- f.write(msg ~ "\n");
- f.flush();
- }
- }
- class DBLogEngine : LogEngine
- {
- public this(string ConnectionString)
- {
- // open database connection
- }
- override public void Write(string msg)
- {
- // write record
- }
- }
- class EmailLogEngine : LogEngine
- {
- public this(string host, int port = 25)
- {
- // initialize mail server structures
- }
- override public void Write(string msg)
- {
- // send message
- }
- }
- class WebLogEngine : LogEngine
- {
- public this(string URL)
- {
- }
- override public void Write(string msg)
- {
- // call webservice in URL with msg
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement