1
2
3
4
5
6 #include <soulng/util/LogFileWriter.hpp>
7 #include <soulng/util/Time.hpp>
8
9 namespace soulng { namespace util {
10
11 LogFileWriter::LogFileWriter(const std::string& logFilePath_, bool open_) : logFilePath(logFilePath_), open(open_)
12 {
13 if (open)
14 {
15 std::ios_base::openmode mode = std::ios_base::app;
16 logFile.open(logFilePath, mode);
17 }
18 }
19
20 LogFileWriter::LogFileWriter(const std::string& logFilePath) : LogFileWriter(logFilePath, true)
21 {
22 }
23
24 LogFileWriter& LogFileWriter::operator<<(StandardEndLine manip)
25 {
26 if (!open) return *this;
27 WriteLine();
28 return *this;
29 }
30
31 void LogFileWriter::WriteCurrentDateTime()
32 {
33 if (!open) return;
34 DateTime timestamp = GetCurrentDateTime();
35 logFile << timestamp.ToString() << std::endl;
36 }
37
38 void LogFileWriter::WriteLine()
39 {
40 if (!open) return;
41 logFile << std::endl;
42 }
43
44 void LogFileWriter::WriteLine(const std::string& line)
45 {
46 if (!open) return;
47 logFile << line << std::endl;
48 }
49
50 void LogFileWriter::Write(const std::string& s)
51 {
52 if (!open) return;
53 logFile << s;
54 }
55
56 LogFileWriter& operator<<(LogFileWriter& writer, const std::string& s)
57 {
58 writer.Write(s);
59 return writer;
60 }
61
62 LogFileWriter& operator<<(LogFileWriter& writer, const char* s)
63 {
64 writer.Write(s);
65 return writer;
66 }
67
68 LogFileWriter& operator<<(LogFileWriter& writer, char c)
69 {
70 writer.Write(std::string(1, c));
71 return writer;
72 }
73
74 LogFileWriter& operator<<(LogFileWriter& writer, bool b)
75 {
76 if (b)
77 {
78 writer.Write("true");
79 }
80 else
81 {
82 writer.Write("false");
83 }
84 return writer;
85
86 }
87
88 LogFileWriter& operator<<(LogFileWriter& writer, int x)
89 {
90 writer.Write(std::to_string(x));
91 return writer;
92 }
93
94 LogFileWriter& operator<<(LogFileWriter& writer, double x)
95 {
96 writer.Write(std::to_string(x));
97 return writer;
98 }
99
100 LogFileWriter& operator<<(LogFileWriter& writer, int64_t x)
101 {
102 writer.Write(std::to_string(x));
103 return writer;
104 }
105
106 LogFileWriter& operator<<(LogFileWriter& writer, uint64_t x)
107 {
108 writer.Write(std::to_string(x));
109 return writer;
110 }
111
112 } }