2014-03-26 21:01:26 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* 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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-03-26 21:01:26 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-04-03 18:56:36 +04:00
|
|
|
#include <QRegExp>
|
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "syncrunfilelog.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2014-04-02 18:36:32 +04:00
|
|
|
#include "filesystem.h"
|
2014-03-28 13:13:35 +04:00
|
|
|
#include <qfileinfo.h>
|
2014-03-26 21:01:26 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-03-26 21:01:26 +04:00
|
|
|
|
2020-05-25 22:33:24 +03:00
|
|
|
SyncRunFileLog::SyncRunFileLog() = default;
|
2014-03-26 21:01:26 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString SyncRunFileLog::dateTimeStr(const QDateTime &dt)
|
2014-03-26 21:01:26 +04:00
|
|
|
{
|
|
|
|
return dt.toString(Qt::ISODate);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString SyncRunFileLog::directionToStr(SyncFileItem::Direction dir)
|
2014-04-03 18:56:36 +04:00
|
|
|
{
|
|
|
|
QString re("N");
|
2017-05-17 11:55:42 +03:00
|
|
|
if (dir == SyncFileItem::Up) {
|
2014-04-03 18:56:36 +04:00
|
|
|
re = QLatin1String("Up");
|
2017-05-17 11:55:42 +03:00
|
|
|
} else if (dir == SyncFileItem::Down) {
|
2014-04-03 18:56:36 +04:00
|
|
|
re = QLatin1String("Down");
|
|
|
|
}
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString SyncRunFileLog::instructionToStr(csync_instructions_e inst)
|
2014-03-26 21:01:26 +04:00
|
|
|
{
|
|
|
|
QString re;
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
switch (inst) {
|
2014-03-26 21:01:26 +04:00
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
|
|
|
re = "INST_NONE";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_EVAL:
|
|
|
|
re = "INST_EVAL";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_REMOVE:
|
|
|
|
re = "INST_REMOVE";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_RENAME:
|
|
|
|
re = "INST_RENAME";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_EVAL_RENAME:
|
|
|
|
re = "INST_EVAL_RENAME";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_NEW:
|
|
|
|
re = "INST_NEW";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
|
|
|
re = "INST_CONFLICT";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
re = "INST_IGNORE";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_SYNC:
|
|
|
|
re = "INST_SYNC";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_STAT_ERROR:
|
|
|
|
re = "INST_STAT_ERR";
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_ERROR:
|
|
|
|
re = "INST_ERROR";
|
|
|
|
break;
|
2016-01-06 12:01:22 +03:00
|
|
|
case CSYNC_INSTRUCTION_TYPE_CHANGE:
|
|
|
|
re = "INST_TYPE_CHANGE";
|
|
|
|
break;
|
2016-08-15 15:17:51 +03:00
|
|
|
case CSYNC_INSTRUCTION_UPDATE_METADATA:
|
|
|
|
re = "INST_METADATA";
|
|
|
|
break;
|
2014-03-26 21:01:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-09 15:10:47 +03:00
|
|
|
void SyncRunFileLog::start(const QString &folderPath)
|
2014-03-26 21:01:26 +04:00
|
|
|
{
|
2018-05-24 17:02:52 +03:00
|
|
|
const qint64 logfileMaxSize = 10 * 1024 * 1024; // 10MiB
|
|
|
|
|
2018-04-30 17:09:04 +03:00
|
|
|
const QString logpath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
if(!QDir(logpath).exists()) {
|
|
|
|
QDir().mkdir(logpath);
|
2018-04-17 16:54:05 +03:00
|
|
|
}
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2018-04-30 17:09:04 +03:00
|
|
|
int length = folderPath.split(QLatin1String("/")).length();
|
|
|
|
QString filenameSingle = folderPath.split(QLatin1String("/")).at(length - 2);
|
|
|
|
QString filename = logpath + QLatin1String("/") + filenameSingle + QLatin1String("_sync.log");
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2018-04-30 17:09:04 +03:00
|
|
|
int depthIndex = 2;
|
|
|
|
while(QFile::exists(filename)) {
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2018-04-30 17:09:04 +03:00
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::ReadOnly| QIODevice::Text);
|
|
|
|
QTextStream in(&file);
|
|
|
|
QString line = in.readLine();
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2018-04-30 17:09:04 +03:00
|
|
|
if(QString::compare(folderPath,line,Qt::CaseSensitive)!=0) {
|
|
|
|
depthIndex++;
|
|
|
|
if(depthIndex <= length) {
|
|
|
|
filenameSingle = folderPath.split(QLatin1String("/")).at(length - depthIndex) + QString("_") ///
|
|
|
|
+ filenameSingle;
|
|
|
|
filename = logpath+ QLatin1String("/") + filenameSingle + QLatin1String("_sync.log");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
filenameSingle = filenameSingle + QLatin1String("_1");
|
|
|
|
filename = logpath + QLatin1String("/") + filenameSingle + QLatin1String("_sync.log");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else break;
|
2018-04-24 16:47:23 +03:00
|
|
|
}
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2014-03-28 13:13:35 +04:00
|
|
|
// When the file is too big, just rename it to an old name.
|
|
|
|
QFileInfo info(filename);
|
2014-04-02 18:18:09 +04:00
|
|
|
bool exists = info.exists();
|
|
|
|
if (exists && info.size() > logfileMaxSize) {
|
|
|
|
exists = false;
|
2014-03-28 13:13:35 +04:00
|
|
|
QString newFilename = filename + QLatin1String(".1");
|
|
|
|
QFile::remove(newFilename);
|
|
|
|
QFile::rename(filename, newFilename);
|
|
|
|
}
|
|
|
|
_file.reset(new QFile(filename));
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2014-03-26 21:01:26 +04:00
|
|
|
_file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
|
2017-05-17 11:55:42 +03:00
|
|
|
_out.setDevice(_file.data());
|
2018-05-24 17:02:52 +03:00
|
|
|
|
|
|
|
|
2014-04-02 18:18:09 +04:00
|
|
|
if (!exists) {
|
2018-11-23 12:33:25 +03:00
|
|
|
_out << folderPath << endl;
|
2014-04-02 18:18:09 +04:00
|
|
|
// We are creating a new file, add the note.
|
2014-04-03 18:56:36 +04:00
|
|
|
_out << "# timestamp | duration | file | instruction | dir | modtime | etag | "
|
2014-04-02 18:18:09 +04:00
|
|
|
"size | fileId | status | errorString | http result code | "
|
|
|
|
"other size | other modtime | other etag | other fileId | "
|
2017-05-17 11:55:42 +03:00
|
|
|
"other instruction"
|
|
|
|
<< endl;
|
2018-05-24 17:02:52 +03:00
|
|
|
|
2014-04-02 18:36:32 +04:00
|
|
|
FileSystem::setFileHidden(filename, true);
|
2014-04-02 18:18:09 +04:00
|
|
|
}
|
2018-05-24 17:02:52 +03:00
|
|
|
|
|
|
|
|
2016-06-09 15:28:15 +03:00
|
|
|
_totalDuration.start();
|
|
|
|
_lapDuration.start();
|
2017-09-25 12:49:11 +03:00
|
|
|
_out << "#=#=#=# Syncrun started " << dateTimeStr(QDateTime::currentDateTimeUtc()) << endl;
|
2014-03-26 21:01:26 +04:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
void SyncRunFileLog::logItem(const SyncFileItem &item)
|
2014-03-26 21:01:26 +04:00
|
|
|
{
|
|
|
|
// don't log the directory items that are in the list
|
2018-02-23 14:13:42 +03:00
|
|
|
if (item._direction == SyncFileItem::None
|
|
|
|
|| item._instruction == CSYNC_INSTRUCTION_IGNORE) {
|
2014-03-26 21:01:26 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-12-08 13:13:44 +03:00
|
|
|
QString ts = QString::fromLatin1(item._responseTimeStamp);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (ts.length() > 6) {
|
2020-05-26 21:13:51 +03:00
|
|
|
QRegExp rx(R"((\d\d:\d\d:\d\d))");
|
2017-05-17 11:55:42 +03:00
|
|
|
if (ts.contains(rx)) {
|
2014-04-03 18:56:36 +04:00
|
|
|
ts = rx.cap(0);
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 21:01:26 +04:00
|
|
|
|
2014-03-28 12:27:05 +04:00
|
|
|
const QChar L = QLatin1Char('|');
|
2014-04-03 18:56:36 +04:00
|
|
|
_out << ts << L;
|
2017-01-26 15:39:00 +03:00
|
|
|
_out << L;
|
2017-05-17 11:55:42 +03:00
|
|
|
if (item._instruction != CSYNC_INSTRUCTION_RENAME) {
|
2016-09-05 14:26:49 +03:00
|
|
|
_out << item._file << L;
|
|
|
|
} else {
|
|
|
|
_out << item._file << QLatin1String(" -> ") << item._renameTarget << L;
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
_out << instructionToStr(item._instruction) << L;
|
|
|
|
_out << directionToStr(item._direction) << L;
|
2017-01-25 16:53:10 +03:00
|
|
|
_out << QString::number(item._modtime) << L;
|
|
|
|
_out << item._etag << L;
|
|
|
|
_out << QString::number(item._size) << L;
|
|
|
|
_out << item._fileId << L;
|
2014-03-26 21:01:26 +04:00
|
|
|
_out << item._status << L;
|
|
|
|
_out << item._errorString << L;
|
|
|
|
_out << QString::number(item._httpErrorCode) << L;
|
2017-08-24 18:07:22 +03:00
|
|
|
_out << QString::number(item._previousSize) << L;
|
|
|
|
_out << QString::number(item._previousModtime) << L;
|
|
|
|
_out /* << other etag (removed) */ << L;
|
|
|
|
_out /* << other fileId (removed) */ << L;
|
|
|
|
_out /* << other instruction (removed) */ << L;
|
2014-03-26 21:01:26 +04:00
|
|
|
|
|
|
|
_out << endl;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
void SyncRunFileLog::logLap(const QString &name)
|
2016-06-09 15:28:15 +03:00
|
|
|
{
|
2017-09-25 12:49:11 +03:00
|
|
|
_out << "#=#=#=#=# " << name << " " << dateTimeStr(QDateTime::currentDateTimeUtc())
|
2016-06-09 15:28:15 +03:00
|
|
|
<< " (last step: " << _lapDuration.restart() << " msec"
|
|
|
|
<< ", total: " << _totalDuration.elapsed() << " msec)" << endl;
|
|
|
|
}
|
|
|
|
|
2016-06-09 15:10:47 +03:00
|
|
|
void SyncRunFileLog::finish()
|
2014-03-26 21:01:26 +04:00
|
|
|
{
|
2017-09-25 12:49:11 +03:00
|
|
|
_out << "#=#=#=# Syncrun finished " << dateTimeStr(QDateTime::currentDateTimeUtc())
|
2016-06-09 15:28:15 +03:00
|
|
|
<< " (last step: " << _lapDuration.elapsed() << " msec"
|
|
|
|
<< ", total: " << _totalDuration.elapsed() << " msec)" << endl;
|
2014-03-26 21:01:26 +04:00
|
|
|
_file->close();
|
|
|
|
}
|
|
|
|
}
|