forked from alphaonex86/Ultracopier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogThread.cpp
263 lines (239 loc) · 8.66 KB
/
LogThread.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** \file LogThread.cpp
\brief The thread to do the log but not block the main thread
\author alpha_one_x86
\version 0.3
\date 2010
\licence GPL3, see the file COPYING */
#include "LogThread.h"
#include "ResourcesManager.h"
#include "OptionEngine.h"
#ifdef Q_OS_WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include <QMessageBox>
LogThread::LogThread()
{
sync=false;
connect(OptionEngine::optionEngine,&OptionEngine::newOptionValue, this, &LogThread::newOptionValue);
enabled=false;
moveToThread(this);
start(QThread::IdlePriority);
connect(this, &LogThread::newData, this,&LogThread::realDataWrite,Qt::QueuedConnection);
newOptionValue("Write_log", "transfer", OptionEngine::optionEngine->getOptionValue("Write_log","transfer"));
newOptionValue("Write_log", "error", OptionEngine::optionEngine->getOptionValue("Write_log","error"));
newOptionValue("Write_log", "folder", OptionEngine::optionEngine->getOptionValue("Write_log","folder"));
newOptionValue("Write_log", "sync", OptionEngine::optionEngine->getOptionValue("Write_log","sync"));
newOptionValue("Write_log", "transfer_format", OptionEngine::optionEngine->getOptionValue("Write_log","transfer_format"));
newOptionValue("Write_log", "error_format", OptionEngine::optionEngine->getOptionValue("Write_log","error_format"));
newOptionValue("Write_log", "folder_format", OptionEngine::optionEngine->getOptionValue("Write_log","folder_format"));
newOptionValue("Write_log", "sync", OptionEngine::optionEngine->getOptionValue("Write_log","sync"));
newOptionValue("Write_log", "enabled", OptionEngine::optionEngine->getOptionValue("Write_log","enabled"));
#ifdef Q_OS_WIN32
DWORD size=0;
WCHAR * computerNameW=new WCHAR[size];
if(GetComputerNameW(computerNameW,&size))
computer=QString::fromWCharArray(computerNameW,size-1);
else
computer="Unknown computer";
delete computerNameW;
WCHAR * userNameW=new WCHAR[size];
if(GetUserNameW(userNameW,&size))
user=QString::fromWCharArray(userNameW,size-1);
else
user="Unknown user";
delete userNameW;
#endif
}
LogThread::~LogThread()
{
closeLogs();
quit();
wait();
}
bool LogThread::logTransfer()
{
return enabled && log_enable_transfer;
}
void LogThread::openLogs()
{
if(OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool()==false)
return;
if(log.isOpen())
{
QMessageBox::critical(NULL,tr("Error"),tr("Log file already open, error: %1").arg(log.errorString()));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("log file already open, error: %1").arg(log.errorString()));
return;
}
log.setFileName(OptionEngine::optionEngine->getOptionValue("Write_log","file").toString());
if(sync)
{
if(!log.open(QIODevice::WriteOnly|QIODevice::Unbuffered))
{
QMessageBox::critical(NULL,tr("Error"),tr("Unable to open the log file, error: %1").arg(log.errorString()));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("Unable to open the log file, error: %1").arg(log.errorString()));
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"opened log: "+OptionEngine::optionEngine->getOptionValue("Write_log","file").toString());
}
else
{
if(!log.open(QIODevice::WriteOnly))
{
QMessageBox::critical(NULL,tr("Error"),tr("Unable to open the log file, error: %1").arg(log.errorString()));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("Unable to open the log file, error: %1").arg(log.errorString()));
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"opened log: "+OptionEngine::optionEngine->getOptionValue("Write_log","file").toString());
}
}
void LogThread::closeLogs()
{
if(log.isOpen() && data.size()>0)
log.write(data.toUtf8());
log.close();
}
void LogThread::newTransferStart(const Ultracopier::ItemOfCopyList &item)
{
if(!log_enable_transfer)
return;
QString text;
if(item.mode==Ultracopier::Copy)
text="[Copy] "+transfer_format+"\n";
else
text="[Move] "+transfer_format+"\n";
text=replaceBaseVar(text);
//Variable is %source%, %size%, %destination%
text=text.replace("%source%",item.sourceFullPath);
text=text.replace("%size%",QString::number(item.size));
text=text.replace("%destination%",item.destinationFullPath);
emit newData(text);
}
/** method called when new transfer is started */
void LogThread::transferSkip(const Ultracopier::ItemOfCopyList &item)
{
if(!log_enable_transfer)
return;
QString text="[Skip] "+transfer_format+"\n";
text=replaceBaseVar(text);
//Variable is %source%, %size%, %destination%
text=text.replace("%source%",item.sourceFullPath);
text=text.replace("%size%",QString::number(item.size));
text=text.replace("%destination%",item.destinationFullPath);
emit newData(text);
}
void LogThread::newTransferStop(const Ultracopier::ItemOfCopyList &item)
{
if(!log_enable_transfer)
return;
QString text="[Stop] "+transfer_format+"\n";
text=replaceBaseVar(text);
//Variable is %source%, %size%, %destination%
text=text.replace("%source%",item.sourceFullPath);
text=text.replace("%size%",QString::number(item.size));
text=text.replace("%destination%",item.destinationFullPath);
emit newData(text);
}
void LogThread::error(const QString &path,const quint64 &size,const QDateTime &mtime,const QString &error)
{
if(!log_enable_error)
return;
QString text="[Error] "+error_format+"\n";
text=replaceBaseVar(text);
//Variable is %path%, %size%, %mtime%, %error%
text=text.replace("%path%",path);
text=text.replace("%size%",QString::number(size));
text=text.replace("%mtime%",mtime.toString(Qt::ISODate));
text=text.replace("%error%",error);
emit newData(text);
}
void LogThread::run()
{
exec();
}
void LogThread::realDataWrite(const QString &text)
{
#ifdef ULTRACOPIER_DEBUG
if(!log.isOpen())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"transfer log not open");
return;
}
#endif // ULTRACOPIER_DEBUG
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
if(log.write(text.toUtf8())==-1)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("unable to write into transfer log: %1").arg(log.errorString()));
return;
}
if(sync)
log.flush();
}
void LogThread::newOptionValue(const QString &group,const QString &name,const QVariant &value)
{
if(group!="Write_log")
return;
if(name=="transfer_format")
transfer_format=value.toString();
else if(name=="error_format")
error_format=value.toString();
else if(name=="folder_format")
folder_format=value.toString();
else if(name=="sync")
{
sync=value.toBool();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QString("sync flag is set on: %1").arg(sync));
if(sync)
{
if(log.isOpen())
log.flush();
}
}
else if(name=="transfer")
log_enable_transfer=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool();
else if(name=="error")
log_enable_error=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool();
else if(name=="folder")
log_enable_folder=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool();
if(name=="enabled")
{
enabled=value.toBool();
if(enabled)
openLogs();
else
closeLogs();
}
}
QString LogThread::replaceBaseVar(QString text)
{
text=text.replace("%time%",QDateTime::currentDateTime().toString("dd.MM.yyyy h:m:s"));
#ifdef Q_OS_WIN32
text=text.replace("%computer%",computer);
text=text.replace("%user%",user);
#endif
return text;
}
void LogThread::rmPath(const QString &path)
{
if(!log_enable_folder)
return;
QString text="[RmPath] "+folder_format+"\n";
text=replaceBaseVar(text);
//Variable is %operation% %path%
text=text.replace("%path%",path);
text=text.replace("%operation%","rmPath");
emit newData(text);
}
void LogThread::mkPath(const QString &path)
{
if(!log_enable_folder)
return;
QString text="[MkPath] "+folder_format+"\n";
text=replaceBaseVar(text);
//Variable is %operation% %path%
text=text.replace("%path%",path);
text=text.replace("%operation%","mkPath");
emit newData(text);
}