1
2
3
4
5
6 using System;
7
8 namespace System.IO
9 {
10 public class LogWriter : IOBase
11 {
12 public LogWriter() : logFilePath(), writer(&Console.Out()), ownWriter(false)
13 {
14 }
15 public LogWriter(const string& logFilePath_) :
16 logFilePath(logFilePath_), writer(new StreamWriter(new FileStream(logFilePath, cast<OpenMode>(OpenMode.append)))), ownWriter(true)
17 {
18 writer->Own(writer->GetStream());
19 if (writer->Error())
20 {
21 SetErrorId(writer->GetErrorId());
22 }
23 else if (writer->GetStream()->Error())
24 {
25 SetErrorId(writer->GetStream()->GetErrorId());
26 }
27 }
28 public override ~LogWriter()
29 {
30 if (ownWriter)
31 {
32 delete writer;
33 }
34 }
35 [nodiscard]
36 public Result<bool> Write(const string& str)
37 {
38 if (Error())
39 {
40 return Result<bool>(ErrorId(GetErrorId()));
41 }
42 if (writer->Error())
43 {
44 SetErrorId(writer->GetErrorId());
45 return Result<bool>(ErrorId(writer->GetErrorId()));
46 }
47 auto result = writer->Write(str);
48 if (result.Error())
49 {
50 SetErrorId(result.GetErrorId());
51 return result;
52 }
53 return Result<bool>(true);
54 }
55 [nodiscard]
56 public Result<bool> WriteLine(const string& str)
57 {
58 if (Error())
59 {
60 return Result<bool>(ErrorId(GetErrorId()));
61 }
62 if (writer->Error())
63 {
64 SetErrorId(writer->GetErrorId());
65 return Result<bool>(ErrorId(writer->GetErrorId()));
66 }
67 auto result = writer->WriteLine(str);
68 if (result.Error())
69 {
70 SetErrorId(result.GetErrorId());
71 return result;
72 }
73 result = writer->Flush();
74 if (result.Error())
75 {
76 SetErrorId(result.GetErrorId());
77 return result;
78 }
79 return Result<bool>(true);
80 }
81 [nodiscard]
82 public Result<bool> WriteLine()
83 {
84 if (Error())
85 {
86 return Result<bool>(ErrorId(GetErrorId()));
87 }
88 if (writer->Error())
89 {
90 SetErrorId(writer->GetErrorId());
91 return Result<bool>(ErrorId(writer->GetErrorId()));
92 }
93 auto result = writer->WriteLine();
94 if (result.Error())
95 {
96 return result;
97 }
98 result = writer->Flush();
99 if (result.Error())
100 {
101 SetErrorId(result.GetErrorId());
102 return result;
103 }
104 return Result<bool>(true);
105 }
106 public inline const string& LogFilePath() const
107 {
108 return logFilePath;
109 }
110 public inline StreamWriter& GetStreamWriter()
111 {
112 return *writer;
113 }
114 private string logFilePath;
115 private StreamWriter* writer;
116 private bool ownWriter;
117 }
118
119 public LogWriter& operator<<(LogWriter& writer, const string& str)
120 {
121 if (writer.Error()) return writer;
122 auto result = writer.Write(str);
123 return writer;
124 }
125
126 public LogWriter& operator<<(LogWriter& writer, char c)
127 {
128 if (writer.Error()) return writer;
129 auto result = writer.Write(ToString(c));
130 return writer;
131 }
132
133 public LogWriter& operator<<(LogWriter& writer, bool b)
134 {
135 if (writer.Error()) return writer;
136 auto result = writer.Write(ToString(b));
137 return writer;
138 }
139
140 public LogWriter& operator<<(LogWriter& writer, int i)
141 {
142 if (writer.Error()) return writer;
143 auto result = writer.Write(ToString(i));
144 return writer;
145 }
146
147 public LogWriter& operator<<(LogWriter& writer, uint u)
148 {
149 if (writer.Error()) return writer;
150 auto result = writer.Write(ToString(u));
151 return writer;
152 }
153
154 public LogWriter& operator<<(LogWriter& writer, long l)
155 {
156 if (writer.Error()) return writer;
157 auto result = writer.Write(ToString(l));
158 return writer;
159 }
160
161 public LogWriter& operator<<(LogWriter& writer, ulong u)
162 {
163 if (writer.Error()) return writer;
164 auto result = writer.Write(ToString(u));
165 return writer;
166 }
167
168 public LogWriter& operator<<(LogWriter& writer, const System.Endl& endl)
169 {
170 if (writer.Error()) return writer;
171 auto result = writer.WriteLine();
172 return writer;
173 }