-
Notifications
You must be signed in to change notification settings - Fork 19
/
CircularLog.cpp
135 lines (116 loc) · 3.61 KB
/
CircularLog.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
127
128
129
130
131
132
133
134
135
// sktoolslib - common files for SK tools
// Copyright (C) 2013, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "CircularLog.h"
#include "codecvt.h"
#include "StringUtils.h"
#include "UnicodeUtils.h"
#include <fstream>
#include <vector>
#include <time.h>
CCircularLog::CCircularLog()
: m_maxLines(10000)
{
}
CCircularLog::~CCircularLog()
{
Save();
}
CCircularLog& CCircularLog::Instance()
{
static CCircularLog instance;
return instance;
}
bool CCircularLog::Init(const std::wstring& path, int maxlines)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_path = path;
m_maxLines = maxlines;
try
{
std::ifstream file;
file.open(m_path);
if (!file.good())
{
CreateDirectory(path.substr(0, path.find_last_of('\\')).c_str(), nullptr);
HANDLE hFile = CreateFile(path.c_str(), GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
return false;
}
std::string line;
while (std::getline(file, line))
{
m_lines.push_back(CUnicodeUtils::StdGetUnicode(line));
};
file.close();
}
catch (std::exception&)
{
return false;
}
return true;
}
bool CCircularLog::AddLine(const std::wstring& line)
{
std::unique_lock<std::mutex> lock(m_mutex);
wchar_t tmpBuf1[128] = {0};
_wstrtime_s(tmpBuf1, 128);
wchar_t tmpBuf2[128] = {0};
_wstrdate_s(tmpBuf2, 128);
m_lines.push_back(CStringUtils::Format(L"%s %s : %s", tmpBuf2, tmpBuf1, line.c_str()));
while (static_cast<int>(m_lines.size()) > m_maxLines)
m_lines.pop_front();
return true;
}
bool CCircularLog::Save()
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_path.empty())
return false;
try
{
std::ofstream file;
file.open(m_path);
if (!file.good())
{
return false;
}
for (const auto& line : m_lines)
{
file << CUnicodeUtils::StdGetUTF8(line) << std::endl;
}
file.close();
}
catch (std::exception&)
{
return false;
}
return true;
}
void CCircularLog::operator()(LPCWSTR pszFormat, ...)
{
va_list marker;
// Initialize variable arguments
va_start(marker, pszFormat);
// Get formatted string length adding one for the NULL
size_t len = _vscwprintf(pszFormat, marker) + 1;
// Create a char vector to hold the formatted string.
std::vector<wchar_t> buffer(len, L'\0');
_vsnwprintf_s(&buffer[0], buffer.size(), len, pszFormat, marker);
// Reset variable arguments
va_end(marker);
AddLine(&buffer[0]);
}