-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.cpp
More file actions
173 lines (143 loc) · 4.37 KB
/
LogFile.cpp
File metadata and controls
173 lines (143 loc) · 4.37 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#include <fstream>
#include <chrono>
#include <locale>
#include "LogFile.h"
#if defined(_WIN32) || defined(_WIN64)
#define FN_STR(x) x
#else
#include <thread>
#include <codecvt>
#define FN_STR(x) wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x).c_str()
#endif
using namespace std::chrono;
map<wstring, CLogFile>CLogFile::s_lstLogFiles;
thread_local stringstream CLogFile::m_ssMsg;
thread_local bool CLogFile::s_bDontLog = false;
CLogFile& CLogFile::GetInstance(const wstring& strLogfileName)
{
if (m_ssMsg.getloc().name() != "C")
m_ssMsg.imbue(locale("C"));
const auto instance = s_lstLogFiles.find(strLogfileName);
if (instance == s_lstLogFiles.end())
{
s_lstLogFiles.emplace(strLogfileName, CLogFile(strLogfileName));
return s_lstLogFiles.find(strLogfileName)->second;
}
return instance->second;
}
CLogFile::CLogFile(const CLogFile& src) : m_strFileName(src.m_strFileName), m_lstMessages(src.m_lstMessages)
{
m_atThrRunning.store(src.m_atThrRunning);
}
CLogFile::~CLogFile()
{
m_mtxBacklog.lock();
while (m_lstMessages.empty() == false || m_atThrRunning == true)
{
m_mtxBacklog.unlock();
this_thread::sleep_for(milliseconds(10));
m_mtxBacklog.lock();
}
m_mtxBacklog.unlock();
}
CLogFile& CLogFile::operator << (const LOGTYPES lt)
{
if (lt == LOGTYPES::END)
{
if (m_strFileName.empty() == false && s_bDontLog == false)
{
m_ssMsg << "\r\n";
StartWriteThread(m_ssMsg.str());
}
stringstream().swap(m_ssMsg);
}
else if (lt == LOGTYPES::PUTTIME && m_strFileName.empty() == false)
{
const auto in_time_t = system_clock::to_time_t(system_clock::now());
const struct tm* stTime = ::localtime(&in_time_t);
const static string pattern = "%d/%b/%Y:%H:%M:%S %z";
use_facet <time_put <char> >(locale("C")).put(m_ssMsg.rdbuf(), m_ssMsg, ' ', stTime, pattern.c_str(), pattern.c_str() + pattern.size());
}
return *this;
}
CLogFile& CLogFile::operator << (const uint64_t nSize)
{
if (m_strFileName.empty() == false)
m_ssMsg << nSize;
return *this;
}
CLogFile& CLogFile::operator << (const uint32_t nSize)
{
if (m_strFileName.empty() == false)
m_ssMsg << nSize;
return *this;
}
CLogFile& CLogFile::operator << (const string& strItem)
{
if (m_strFileName.empty() == false)
m_ssMsg << strItem;
return *this;
}
CLogFile& CLogFile::operator << (const char* const strItem)
{
if (m_strFileName.empty() == false)
m_ssMsg << strItem;
return *this;
}
CLogFile& CLogFile::WriteToLog()
{
if (m_strFileName.empty() == false && s_bDontLog == false)
{
m_ssMsg << "\r\n";
StartWriteThread(m_ssMsg.str());
}
stringstream().swap(m_ssMsg);
return *this;
}
void CLogFile::SetDontLog(bool bDontLog/* = true*/) noexcept
{
s_bDontLog = bDontLog;
}
void CLogFile::StartWriteThread(const string& szMessage)
{
lock_guard<mutex>lock(m_mtxBacklog);
m_lstMessages.emplace_back(szMessage);
bool bTmp = false;
if (m_atThrRunning.compare_exchange_strong(bTmp, true) == true)
{
thread([&]() {
fstream fout;
fout.open(FN_STR(m_strFileName), ios::out | ios::app | ios::binary);
if (fout.is_open() == true)
{
m_mtxBacklog.lock();
while (m_lstMessages.empty() == false)
{
auto msg = *begin(m_lstMessages);
m_lstMessages.pop_front();
m_mtxBacklog.unlock();
fout.write(msg.c_str(), msg.size());
m_mtxBacklog.lock();
}
fout.close();
atomic_exchange(&m_atThrRunning, false);
m_mtxBacklog.unlock();
}
else
{
m_mtxBacklog.lock();
m_lstMessages.clear();
atomic_exchange(&m_atThrRunning, false);
m_mtxBacklog.unlock();
}
}).detach();
}
}