summaryrefslogtreecommitdiff
path: root/LogThread.cpp
blob: 234ccbfa6bf95e64fedddf4f2d992608fbdda296 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/** \file LogThread.cpp
\brief The thread to do the log but not block the main thread
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#include "LogThread.h"
#include "ResourcesManager.h"
#include "OptionEngine.h"
#include "cpp11addition.h"

#ifdef Q_OS_WIN32
    #ifndef NOMINMAX
        #define NOMINMAX
    #endif
    #include <windows.h>
#endif
#include <QMessageBox>

std::string LogThread::text_header_copy="[Copy] ";
std::string LogThread::text_header_move="[Move] ";
std::string LogThread::text_header_skip="[Skip] ";
std::string LogThread::text_header_stop="[Stop] ";
std::string LogThread::text_header_error="[Error] ";
std::string LogThread::text_header_MkPath="[MkPath] ";
std::string LogThread::text_header_RmPath="[RmPath] ";

std::string LogThread::text_var_source="%source%";
std::string LogThread::text_var_size="%size%";
std::string LogThread::text_var_destination="%destination%";
std::string LogThread::text_var_path="%path%";
std::string LogThread::text_var_error="%error%";
std::string LogThread::text_var_mtime="%mtime%";
std::string LogThread::text_var_time="%time%";
std::string LogThread::text_var_timestring="%dd.MM.yyyy h:m:s%";
#ifdef Q_OS_WIN32
std::string LogThread::text_var_computer="%computer%";
std::string LogThread::text_var_user="%user%";
#endif
std::string LogThread::text_var_operation="%operation%";
std::string LogThread::text_var_rmPath="%rmPath%";
std::string LogThread::text_var_mkPath="%mkPath%";

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).toStdString();
    else
        computer="Unknown computer";
    delete computerNameW;

    WCHAR * userNameW=new WCHAR[size];
    if(GetUserNameW(userNameW,&size))
        user=QString::fromWCharArray(userNameW,size-1).toStdString();
    else
        user="Unknown user";
    delete userNameW;
    #endif

    #ifdef Q_OS_WIN32
    lineReturn="\r\n";
    #else
    lineReturn="\n";
    #endif
}

LogThread::~LogThread()
{
    closeLogs();
    quit();
    wait();
}

bool LogThread::logTransfer() const
{
    return enabled && log_enable_transfer;
}

void LogThread::openLogs()
{
    if(stringtobool(OptionEngine::optionEngine->getOptionValue("Write_log","enabled"))==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,"log file already open, error: "+log.errorString().toStdString());
        return;
    }
    log.setFileName(QString::fromStdString(OptionEngine::optionEngine->getOptionValue("Write_log","file")));
    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,"Unable to open the log file, error: "+log.errorString().toStdString());
        }
        else
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"opened log: "+OptionEngine::optionEngine->getOptionValue("Write_log","file"));
    }
    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,"Unable to open the log file, error: "+log.errorString().toStdString());
        }
        else
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"opened log: "+OptionEngine::optionEngine->getOptionValue("Write_log","file"));
    }
}

void LogThread::closeLogs()
{
    if(log.isOpen() && data.size()>0)
        log.write(data.data(),data.size());
    log.close();
}

void LogThread::newTransferStart(const Ultracopier::ItemOfCopyList &item)
{
    if(!logTransfer())
        return;
    std::string text;
    if(item.mode==Ultracopier::Copy)
        text=LogThread::text_header_copy+transfer_format+lineReturn;
    else
        text=LogThread::text_header_move+transfer_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %source%, %size%, %destination%
    stringreplaceAll(text,LogThread::text_var_source,item.sourceFullPath);
    stringreplaceAll(text,LogThread::text_var_size,std::to_string(item.size));
    stringreplaceAll(text,LogThread::text_var_destination,item.destinationFullPath);
    stringreplaceAll(text,LogThread::text_var_time,QDateTime::currentDateTime().toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString());
    emit newData(text);
}

/** method called when new transfer is started */
void LogThread::transferSkip(const Ultracopier::ItemOfCopyList &item)
{
    if(!logTransfer())
        return;
    std::string text=LogThread::text_header_skip+transfer_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %source%, %size%, %destination%
    stringreplaceAll(text,LogThread::text_var_source,item.sourceFullPath);
    stringreplaceAll(text,LogThread::text_var_size,std::to_string(item.size));
    stringreplaceAll(text,LogThread::text_var_destination,item.destinationFullPath);
    stringreplaceAll(text,LogThread::text_var_time,QDateTime::currentDateTime().toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString());
    emit newData(text);
}

void LogThread::newTransferStop(const Ultracopier::ItemOfCopyList &item)
{
    if(!logTransfer())
        return;
    std::string text=LogThread::text_header_stop+transfer_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %source%, %size%, %destination%
    stringreplaceAll(text,LogThread::text_var_source,item.sourceFullPath);
    stringreplaceAll(text,LogThread::text_var_size,std::to_string(item.size));
    stringreplaceAll(text,LogThread::text_var_destination,item.destinationFullPath);
    stringreplaceAll(text,LogThread::text_var_time,QDateTime::currentDateTime().toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString());
    emit newData(text);
}

void LogThread::error(const std::string &path,const uint64_t &size,const uint64_t &mtime,const std::string &error)
{
    if(!log_enable_error)
        return;
    std::string text=LogThread::text_header_error+error_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %path%, %size%, %mtime%, %error%
    stringreplaceAll(text,LogThread::text_var_path,path);
    stringreplaceAll(text,LogThread::text_var_size,std::to_string(size));
    stringreplaceAll(text,LogThread::text_var_mtime,QDateTime::fromTime_t(static_cast<unsigned int>(mtime)).toString(Qt::ISODate).toStdString());
    stringreplaceAll(text,LogThread::text_var_error,error);
    stringreplaceAll(text,LogThread::text_var_time,QDateTime::fromTime_t(static_cast<unsigned int>(mtime)).toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString());
    emit newData(text);
}

void LogThread::run()
{
    exec();
}

void LogThread::realDataWrite(const std::string &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.data(),text.size())==-1)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to write into transfer log: "+log.errorString().toStdString());
        return;
    }
    if(sync)
        log.flush();
}

void LogThread::newOptionValue(const std::string &group,const std::string &name,const std::string &value)
{
    if(group!="Write_log")
        return;

    if(name=="transfer_format")
        transfer_format=value;
    else if(name=="error_format")
        error_format=value;
    else if(name=="folder_format")
        folder_format=value;
    else if(name=="sync")
    {
        sync=stringtobool(value);
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"sync flag is set on: "+value);
        if(sync)
        {
            if(log.isOpen())
                log.flush();
        }
    }
    else if(name=="transfer")
        log_enable_transfer=stringtobool(OptionEngine::optionEngine->getOptionValue("Write_log","enabled")) && stringtobool(value);
    else if(name=="error")
        log_enable_error=stringtobool(OptionEngine::optionEngine->getOptionValue("Write_log","enabled")) && stringtobool(value);
    else if(name=="folder")
        log_enable_folder=stringtobool(OptionEngine::optionEngine->getOptionValue("Write_log","enabled")) && stringtobool(value);
    if(name=="enabled")
    {
        enabled=stringtobool(value);
        if(enabled)
            openLogs();
        else
            closeLogs();
    }
}

std::string LogThread::replaceBaseVar(std::string text)
{
    stringreplaceAll(text,LogThread::text_var_time,QDateTime::currentDateTime().toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString());
    #ifdef Q_OS_WIN32
    stringreplaceAll(text,LogThread::text_var_computer,computer);
    stringreplaceAll(text,LogThread::text_var_user,user);
    #endif
    return text;
}

void LogThread::rmPath(const std::string &path)
{
    if(!logTransfer())
        return;
    std::string text=LogThread::text_header_RmPath+folder_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %operation% %path%
    stringreplaceAll(text,LogThread::text_var_path,path);
    stringreplaceAll(text,LogThread::text_var_operation,LogThread::text_var_rmPath);
    emit newData(text);
}

void LogThread::mkPath(const std::string &path)
{
    if(!logTransfer())
        return;
    std::string text=LogThread::text_header_MkPath+folder_format+lineReturn;
    text=replaceBaseVar(text);
    //Variable is %operation% %path%
    stringreplaceAll(text,LogThread::text_var_path,path);
    stringreplaceAll(text,LogThread::text_var_operation,LogThread::text_var_mkPath);
    emit newData(text);
}