/
LogFile.cpp
126 lines (100 loc) · 2.95 KB
/
LogFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "LogFile.h"
#include <iomanip>
#include <thread>
#include <wx/version.h>
#include "imodule.h"
#include "itextstream.h"
#include "version.h"
#include "string/convert.h"
#include "LogWriter.h"
namespace applog
{
namespace
{
const char* const TIME_FMT = "%Y-%m-%d %H:%M:%S";
}
LogFile::LogFile(const std::string& fullPath) :
_logFilePath(fullPath),
_logStream(_logFilePath.c_str())
{}
bool LogFile::isOpen()
{
return _logStream.good();
}
const std::string& LogFile::getFullPath() const
{
return _logFilePath;
}
void LogFile::writeLog(const std::string& outputStr, LogLevel level)
{
_buffer.append(outputStr);
// Hold back until we hit a newline
if (outputStr.rfind('\n') != std::string::npos)
{
#if !defined(__linux__)
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
// Write timestamp and thread information
_logStream << std::put_time(&tm, TIME_FMT);
#endif
_logStream << " (" << std::this_thread::get_id() << ") ";
// Insert the string into the stream and flush the buffer
_logStream << _buffer;
_buffer.clear();
_logStream.flush();
}
}
#if 0
// Creates the singleton logfile with the given filename
void LogFile::create(const std::string& filename)
{
if (InstancePtr() == NULL)
{
// No logfile yet, create one
InstancePtr() = LogFilePtr(new LogFile(filename));
// Write the initialisation info to the logfile.
rMessage() << "Started logging to " << InstancePtr()->_logFilePath << std::endl;
rMessage() << "This is " << RADIANT_APPNAME_FULL() << std::endl;
#if !defined(__linux__)
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
// Write timestamp and thread information
rMessage() << "Today is " << std::put_time(&tm, TIME_FMT) << std::endl;
#endif
// Output the wxWidgets version to the logfile
std::string wxVersion = string::to_string(wxMAJOR_VERSION) + ".";
wxVersion += string::to_string(wxMINOR_VERSION) + ".";
wxVersion += string::to_string(wxRELEASE_NUMBER);
rMessage() << "wxWidgets Version: " << wxVersion << std::endl;
}
}
// Closes the singleton log instance
void LogFile::close() {
// Clear the pointer, this destructs any open logfile instance
InstancePtr() = LogFilePtr();
}
LogFilePtr& LogFile::InstancePtr() {
static LogFilePtr _instancePtr;
return _instancePtr;
}
#endif
void LogFile::close()
{
#if defined(__linux__)
// put_time still unavailable even with GCC 4.9
rMessage() << " Closing log file." << std::endl;
#else
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
rMessage() << std::put_time(&tm, TIME_FMT) << " Closing log file." << std::endl;
#endif
// Insert the last few remaining bytes into the stream
if (!_buffer.empty())
{
_logStream << _buffer << std::endl;
_buffer.clear();
}
_logStream.flush();
_logStream.close();
}
} // namespace applog