1 module gccbuild.log;
2 
3 import scriptlike;
4 
5 private File logFile;
6 
7 void writelnLog(T...)(T args)
8 {
9     if (!logFile.isOpen)
10         return;
11 
12     logFile.writeln(args);
13 }
14 
15 void writeLogCMD(string text)
16 {
17     if (!logFile.isOpen)
18         return;
19 
20     foreach (line; text.lineSplitter())
21         logFile.writeln("           ", line);
22     logFile.flush();
23 }
24 
25 void intializeLog(Path path)
26 {
27     void echoLogger(string text)
28     {
29         if (!logFile.isOpen)
30             return;
31 
32         logFile.writeln("       ", text);
33         logFile.flush();
34     }
35 
36     scriptlikeEcho = true;
37     scriptlikeCustomEcho = &echoLogger;
38     logFile = File(path.toString(), "w");
39 }
40 
41 private SysTime startTime;
42 
43 void startSectionLog(string text)
44 {
45     if (!logFile.isOpen)
46         return;
47 
48     startTime = Clock.currTime;
49     static bool first = true;
50 
51     if (first)
52         first = false;
53     else
54     {
55         logFile.writeln();
56         logFile.writeln();
57     }
58 
59     logFile.writeln("==> ", text);
60 }
61 
62 void endSectionLog()
63 {
64     endSectionLog(Clock.currTime - startTime);
65 }
66 
67 void endSectionLog(Duration dur)
68 {
69     if (logFile.isOpen)
70         logFile.writefln(":   (%s)", dur);
71 }
72 
73 private SysTime bulletStartTime;
74 
75 void writeBulletPointLog(string text)
76 {
77     enum prefix = "  -> ";
78     bulletStartTime = Clock.currTime;
79     writelnLog(prefix, text);
80 }
81 
82 void endBulletPointLog()
83 {
84     endBulletPointLog(Clock.currTime - bulletStartTime);
85 }
86 
87 void endBulletPointLog(Duration dur)
88 {
89     writelnLog("  :  (", Clock.currTime - bulletStartTime, ")");
90 }
91 
92 void closeLog()
93 {
94     logFile.close();
95 }
96 
97 string runCollectLog(string command)
98 {
99     auto result = tryRunCollectLog(command);
100     if (result.status != 0)
101         throw new ErrorLevelException(result.status, command, result.output);
102     return result.output;
103 }
104 
105 auto tryRunCollectLog(string command)
106 {
107     auto result = tryRunCollect(command);
108     result.output.writeLogCMD();
109     return result;
110 }