1 
2 module logd.log;
3 
4 import std.file;
5 import std.path;
6 import std.stdio;
7 import std.string;
8 import std.format;
9 import std.datetime;
10 
11 static class Logd {
12 	
13 	static private __gshared File log;
14 	static private __gshared bool isLogging = true;
15 
16 	public enum Level {
17 		success = "success",
18 		event = "event",
19 		warning = "warning",
20 		error = "error",
21 		update = "update",
22 		user = "user",
23 	}
24 
25 	static this() {
26 		log.open("log".setExtension("html"), "w");
27 		WriteHtmlHeader();
28 	}
29 
30 	static ~this() {
31 		Close();
32 	}
33 
34 	@property static public void Filename(string filename) {
35 		Close();
36 		log.open(filename.setExtension("html"), "w");
37 		WriteHtmlHeader();
38 	}
39 
40 	@property static public string Filename() {
41 		return log.name;
42 	}
43 
44 	@property static public void IsLogging(bool logging) {
45 		isLogging = logging;
46 	}
47 
48 	@property static public bool IsLogging() {
49 		return isLogging;
50 	}
51 
52 	@property static public bool IsOpen() {
53 		return log.isOpen;
54 	}
55 
56 	static public void Write()() {
57 		writeln();
58 		if(isLogging) 
59 			log.writeln();
60 	}
61 
62 	static public void Write(Level, T...)(Level level, T t) {
63 		writeln(t);
64 		if(isLogging) 
65 			log.writeln("<p class=\"", level, "\">", CurrentTime(),t,"</p>");
66 	}
67 
68 	static public void WriteWithTag(Level, string, T...)(Level level, string fmt, T t){
69 		writeln(t);
70 		if(isLogging) 
71 			log.writeln("<",fmt," class=\"", level, "\">",t,"</",fmt,">");
72 	}
73 
74 	static public void Flush() {
75 		std.stdio.stdout.flush();
76 		log.flush();
77 		log.sync();
78 	}
79 
80 	static public void Close() {
81 		if(log.isOpen) {
82 			WriteHtmlFooter();
83 			Flush();
84 			log.close();
85 		}
86 	}
87 
88 	static public void Open() {
89 		log.open(log.name, "w");
90 		WriteHtmlHeader();
91 	}
92 
93 	static public string CurrentTime() {
94 		auto time = Clock.currTime;
95 		return format("(%s/%s/%s %s:%s:%s) - ", time.day, cast(int)time.month, time.year, time.hour, time.minute, time.second);
96 	}
97 
98 	static private void WriteHtmlHeader(){
99 		log.writeln("<!DOCTYPE html>");
100 		log.writeln("<html>");
101 		log.writeln("<head>");
102 		log.writeln("<title>Log</title>");
103 		log.writeln("<style>");
104 		log.writeln("body{background-color: black;} p{margin-top: 5px; margin-bottom: 5px;} .success {color: lime;} .warning {color: orange;} .error {color: red;} .event {color: yellow;} .user{color: aqua;} .update {color: white;}");
105 		log.writeln("</style>");
106 		log.writeln("</head>");
107 		log.writeln("<body>");	
108 	}
109 
110 	static private void WriteHtmlFooter() {
111 		log.writeln("</body>");
112 		log.writeln("</html>");
113 	}	
114 }