Commit ac7526b5 authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

More work on Dbus #4 and logging

parent fb4c9fbe
......@@ -64,6 +64,8 @@ namespace FileSync
static const QString CONFIG_FILE_PATH = "%1/.kdeconnect-sync/%2/sync_config.json";
// Errors
static const QString DBUS_REGISTRATION_FAILED = "D-Bus registration failed for service %1";
static const QString NO_INSTANCE_AVAILABLE_ERROR = "No instance available, for the first time initialization please provide 'deviceId'!";
static const QString EXEC_ERROR = "Script execution failed, exit code %1";
static const QString INCONSISTENT_PATHS_ERROR = "Inconsistent paths settings, '%1' is a directory but '%2' is not!";
......
......@@ -22,7 +22,6 @@
#define KDECONNECT_CONFIG_H
#include <string>
#include "common.h"
#include <QJsonArray>
#include <QString>
#include <QLoggingCategory>
......@@ -33,6 +32,7 @@
#include <QtCore/QFileInfo>
#include <QtCore/QJsonDocument>
#include "common.h"
#include "singleton_pool.h"
namespace FileSync
......
......@@ -34,7 +34,7 @@ FileSync::Daemon::~Daemon()
void FileSync::Daemon::requestSync(const QString &deviceId,
const QStringList &syncList)
{
qCDebug(LOG_DAEMON) << DAEMON_SYNC_REQUEST.arg(deviceId, syncList.join("; "));
D(LOG_DAEMON, DAEMON_SYNC_REQUEST.arg(deviceId, syncList.join("; ")));
bool syncOngoing = false;
QMap<QString, Executor *>::iterator existingExecutor = m_executors.find(
deviceId);
......
......@@ -31,18 +31,13 @@ FileSync::Executor::Executor(const QString &deviceId,
: m_deviceId(deviceId), m_syncList(syncList),
m_executionResult(QString(), 0, 0, QStringList())
{
Log::instance(m_deviceId);
Index::instance(m_deviceId, INDEX_PATH.arg(
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
m_deviceId));
loadConfig();
}
FileSync::Executor::~Executor()
{
Log::destroyInstance();
Index::destroyInstance();
Log::destroyInstance(m_deviceId);
Index::destroyInstance(m_deviceId);
}
void FileSync::Executor::run()
......@@ -103,10 +98,12 @@ void FileSync::Executor::run()
// This will continue only after wake up signal
for (const QJsonValue &hash: m_hashList)
{
LOG_I(LOG_EXECUTOR, hash.toObject().value("filePath").toString() + " " + hash.toObject().value("hash").toString());
LOG_I(LOG_EXECUTOR, hash.toObject().value("filePath").toString() + " " +
hash.toObject().value("hash").toString());
}
SyncResult syncResult(determineAndPerformSyncOperation(*fileSyncList, m_hashList));
SyncResult syncResult(
determineAndPerformSyncOperation(*fileSyncList, m_hashList));
qDebug() << "Signal emitted ..." << endl;
Q_EMIT notifyDaemon(syncResult);
......@@ -122,7 +119,8 @@ void FileSync::Executor::run()
<< syncCompletedMsg.arg(m_executionResult.syncStatus());
}
FileSync::SyncResult FileSync::Executor::determineAndPerformSyncOperation(const QJsonArray &fileSyncList, const QJsonArray &fileHashList)
FileSync::SyncResult FileSync::Executor::determineAndPerformSyncOperation(
const QJsonArray &fileSyncList, const QJsonArray &fileHashList)
{
SyncResult syncResult(QString(), 0, 0, QStringList());
bool errorOccured = false;
......@@ -143,7 +141,7 @@ FileSync::SyncResult FileSync::Executor::determineAndPerformSyncOperation(const
{
errorOccured = true;
}
syncResult.setSyncedFilesCnt(syncResult.syncedFilesCnt()+1);
syncResult.setSyncedFilesCnt(syncResult.syncedFilesCnt() + 1);
}
if ((operation != NULL) && (postSyncScript != "") &&
((operation->getType() == SyncOperation::FROM_REMOTE) ||
......@@ -162,10 +160,10 @@ FileSync::SyncResult FileSync::Executor::determineAndPerformSyncOperation(const
if (errorOccured)
{
syncResult.setSyncErrorsCnt(syncResult.syncErrorsCnt()+1);
syncResult.setSyncErrorsCnt(syncResult.syncErrorsCnt() + 1);
}
}
return syncResult;
return syncResult;
}
......@@ -236,7 +234,8 @@ void FileSync::Executor::hashesReady(const QJsonArray &hashList)
}
void
FileSync::Executor::synchronizeEntry(QJsonArray *syncFileList, const QJsonObject &entry,
FileSync::Executor::synchronizeEntry(QJsonArray *syncFileList,
const QJsonObject &entry,
bool remotePathIsRelative)
{
QString localPath = entry.value("localPath").toString();
......@@ -345,58 +344,58 @@ bool FileSync::Executor::performSyncOperation(
if (operation.getType() == SyncOperation::UNKNOWN)
{
Log::instance().logError(
FileSync::SYNC_OPERATION_UNKNOWN.arg(localFilePath,
remoteFilePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::SYNC_OPERATION_UNKNOWN.arg(localFilePath,
remoteFilePath);
// Log::instance().logError(
// FileSync::SYNC_OPERATION_UNKNOWN.arg(localFilePath,
// remoteFilePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::SYNC_OPERATION_UNKNOWN.arg(localFilePath,
// remoteFilePath);
operationResult = false;
}
else if (operation.getType() == SyncOperation::NONE)
{
Log::instance().logInfo(
FileSync::NO_SYNC.arg(localFilePath, remoteFilePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::NO_SYNC.arg(localFilePath, remoteFilePath);
// Log::instance().logInfo(
// FileSync::NO_SYNC.arg(localFilePath, remoteFilePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::NO_SYNC.arg(localFilePath, remoteFilePath);
}
else if (operation.getType() == SyncOperation::CONFLICT)
{
Log::instance().logError(
FileSync::SYNC_OPERATION_CONFLICT.arg(localFilePath,
remoteFilePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::SYNC_OPERATION_CONFLICT.arg(localFilePath,
remoteFilePath);
// Log::instance().logError(
// FileSync::SYNC_OPERATION_CONFLICT.arg(localFilePath,
// remoteFilePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::SYNC_OPERATION_CONFLICT.arg(localFilePath,
// remoteFilePath);
operationResult = false;
}
else if (operation.getType() == SyncOperation::FROM_LOCAL)
{
Log::instance().logInfo(
FileSync::SYNC_OPERATION_FROM_LOCAL.arg(localFilePath,
remoteFilePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::SYNC_OPERATION_FROM_LOCAL.arg(localFilePath,
remoteFilePath);
// Log::instance().logInfo(
// FileSync::SYNC_OPERATION_FROM_LOCAL.arg(localFilePath,
// remoteFilePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::SYNC_OPERATION_FROM_LOCAL.arg(localFilePath,
// remoteFilePath);
if (!copyFile(localFilePath, remoteFilePath))
{
operationResult = false;
}
Index::instance().update(operation.getHashRecord());
Index::instance(m_deviceId).update(operation.getHashRecord());
}
else if (operation.getType() == SyncOperation::FROM_REMOTE)
{
Log::instance().logInfo(
FileSync::SYNC_OPERATION_FROM_REMOTE.arg(remoteFilePath,
localFilePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::SYNC_OPERATION_FROM_REMOTE.arg(remoteFilePath,
localFilePath);
// Log::instance().logInfo(
// FileSync::SYNC_OPERATION_FROM_REMOTE.arg(remoteFilePath,
// localFilePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::SYNC_OPERATION_FROM_REMOTE.arg(remoteFilePath,
// localFilePath);
if (!copyFile(remoteFilePath, localFilePath))
{
operationResult = false;
}
Index::instance().update(operation.getHashRecord());
Index::instance(m_deviceId).update(operation.getHashRecord());
}
return operationResult;
......@@ -436,11 +435,11 @@ bool FileSync::Executor::copyFile(QString sourceFilePath, QString destFilePath)
FileSync::MTIME_DATETIME_FORMAT), destFilePath);
QString stdout, stderr;
int exitCode = FileSync::Util::runCommand(command, stdout,
stderr);
stderr);
if (exitCode != 0)
{
Log::instance().logError(FileSync::EXEC_ERROR.arg(exitCode));
Log::instance().logError(stderr);
// Log::instance().logError(FileSync::EXEC_ERROR.arg(exitCode));
// Log::instance().logError(stderr);
copyResult = false;
}
......@@ -455,7 +454,7 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
QString localHash = Util::convertToHash(localPath);
QString remoteHash = Util::convertToHash(remotePath);
QString currentLocalRemotePairHash = localHash + remoteHash;
QString existingLocalRemotePairHash = Index::instance().search(
QString existingLocalRemotePairHash = Index::instance(m_deviceId).search(
currentLocalRemotePairHash);
QFileInfo localPathInfo = QFileInfo(localPath);
QFileInfo remotePathInfo = QFileInfo(remotePath);
......@@ -490,12 +489,12 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
// Both files / directories exist
if (existingLocalRemotePairHash == "")
{
Log::instance().logInfo(
FileSync::SYNC_DETERMINATION_BY_MTIME.arg(localPath,
remotePath));
qCDebug(LOG_EXECUTOR)
<< FileSync::SYNC_DETERMINATION_BY_MTIME.arg(localPath,
remotePath);
// Log::instance().logInfo(
// FileSync::SYNC_DETERMINATION_BY_MTIME.arg(localPath,
// remotePath));
// qCDebug(LOG_EXECUTOR)
// << FileSync::SYNC_DETERMINATION_BY_MTIME.arg(localPath,
// remotePath);
if (localPathInfo.lastModified().toMSecsSinceEpoch() ==
remotePathInfo.lastModified().toMSecsSinceEpoch())
{
......@@ -557,19 +556,19 @@ bool FileSync::Executor::executePostSyncScript(QString postSyncScript,
operationType),
localFilePath,
remoteFilePath);
Log::instance().logDebug("Executing command: " + command);
// Log::instance().logDebug("Executing command: " + command);
QString stdout, stderr;
int exitCode = FileSync::Util::runCommand(command, stdout,
stderr);
if (exitCode != 0)
{
Log::instance().logError(FileSync::EXEC_ERROR.arg(exitCode));
Log::instance().logError(stderr);
// Log::instance().logError(FileSync::EXEC_ERROR.arg(exitCode));
// Log::instance().logError(stderr);
executeResult = false;
}
else
{
Log::instance().logInfo(stdout);
// Log::instance().logInfo(stdout);
}
return executeResult;
......
......@@ -32,15 +32,14 @@
#include <QtCore/QJsonArray>
#include <QtCore/QCryptographicHash>
FileSync::Index *FileSync::Index::s_instance = NULL;
Q_LOGGING_CATEGORY(LOG_INDEX, "kdeconnect.filesyncapp.daemon.index");
FileSync::Index::Index(const QString &deviceId,
const QString &syncIndexFsPath)
FileSync::Index::Index(const QString &deviceId)
{
m_deviceId = deviceId;
m_syncIndexFsPath = syncIndexFsPath;
m_syncIndexFsPath = INDEX_PATH.arg(
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
m_deviceId);
createIfNotExisting(m_syncIndexFsPath, m_deviceId);
init(m_syncIndexFsPath, m_deviceId);
}
......@@ -54,7 +53,7 @@ void FileSync::Index::init(const QString &fsPath, const QString &deviceId)
{
qCDebug(LOG_INDEX)
<< FileSync::INDEX_READ_OPEN_ERROR.arg(indexFile.fileName(),
indexFile.errorString());
indexFile.errorString());
}
else
{
......@@ -65,7 +64,7 @@ void FileSync::Index::init(const QString &fsPath, const QString &deviceId)
void
FileSync::Index::createIfNotExisting(const QString &fsPath,
const QString &deviceId)
const QString &deviceId)
{
QFileInfo indexFileInfo(fsPath);
......@@ -77,7 +76,7 @@ FileSync::Index::createIfNotExisting(const QString &fsPath,
{
root.mkpath(indexFileInfo.dir().absolutePath());
qCDebug(LOG_INDEX) << "Creating dir "
<< indexFileInfo.dir().absolutePath();
<< indexFileInfo.dir().absolutePath();
}
writeDefaultData(fsPath);
......@@ -93,7 +92,7 @@ FileSync::Index::createIfNotExisting(const QString &fsPath,
}
void FileSync::Index::writeToFs(const QString &fsPath,
const QByteArray &data)
const QByteArray &data)
{
QFile indexFile;
indexFile.setFileName(fsPath);
......@@ -101,7 +100,7 @@ void FileSync::Index::writeToFs(const QString &fsPath,
{
qCDebug(LOG_INDEX)
<< FileSync::INDEX_WRITE_OPEN_ERROR.arg(indexFile.fileName(),
indexFile.errorString());
indexFile.errorString());
}
else
{
......@@ -121,40 +120,6 @@ void FileSync::Index::writeDefaultData(const QString &fsPath)
writeToFs(fsPath, defaultDoc.toJson());
}
FileSync::Index &FileSync::Index::instance()
{
if (s_instance == NULL)
{
throw std::logic_error(NO_INSTANCE_AVAILABLE_ERROR.toStdString());
}
return *s_instance;
}
FileSync::Index &FileSync::Index::instance(const QString &deviceId,
const QString &syncIndexFsPath)
{
if (s_instance == NULL)
{
qCDebug(LOG_INDEX)
<< "New sync index instance from path " << syncIndexFsPath
<< " will be created.";
s_instance = new FileSync::Index(deviceId, syncIndexFsPath);
}
return *s_instance;
}
void FileSync::Index::destroyInstance()
{
if (s_instance != NULL)
{
qCDebug(LOG_INDEX) << "Sync index instance for device "
<< s_instance->m_deviceId
<< " will be removed";
delete s_instance;
s_instance = NULL;
}
}
void FileSync::Index::update(QPair<QString, QString> record)
{
QJsonDocument defaultDoc;
......
......@@ -26,25 +26,20 @@
#include <QtCore/QPair>
#include <QJsonObject>
#include <QObject>
#include "sync_operation.h"
#include "singleton_pool.h"
namespace FileSync
{
/**
* TODO mutex access if more synchronizations will be requested
*/
class Index : QObject
class Index : public QObject, public SingletonPool<FileSync::Index>
{
Q_OBJECT
public:
Index(const QString &deviceId, const QString &syncIndexFsPath);
static Index &instance();
static Index &
instance(const QString &deviceId, const QString &syncIndexFsPath);
static void destroyInstance();
Index(const QString &deviceId);
QString search(const QString &localRemotePairHash);
......@@ -57,8 +52,6 @@ namespace FileSync
QString m_deviceId;
QString m_syncIndexFsPath;
static Index *s_instance;
void init(const QString &fsPath, const QString &deviceId);
void createIfNotExisting(const QString &fsPath,
......
......@@ -25,35 +25,6 @@
#include "log.h"
#include "common.h"
FileSync::Log *FileSync::Log::s_instance = NULL;
FileSync::Log &FileSync::Log::instance()
{
if (s_instance == NULL)
{
throw std::logic_error(NO_INSTANCE_AVAILABLE_ERROR.toStdString());
}
return *s_instance;
}
FileSync::Log &FileSync::Log::instance(const QString &deviceId)
{
if (s_instance == NULL)
{
s_instance = new FileSync::Log(deviceId, FileSync::Log::INFO);
}
return *s_instance;
}
void FileSync::Log::destroyInstance()
{
if (s_instance != NULL)
{
delete s_instance;
s_instance = NULL;
}
}
FileSync::Log::Log(const QString &deviceId,
FileSync::Log::LogLevel publicLogLevel)
{
......
......@@ -29,10 +29,12 @@
#include <QtCore/QObject>
#include <QtCore/QFile>
#include "singleton_pool.h"
namespace FileSync
{
class Log : QObject
class Log : public QObject, public SingletonPool<FileSync::Log>
{
Q_OBJECT
public:
......@@ -41,16 +43,10 @@ namespace FileSync
DEBUG, INFO, WARN, ERROR
};
Log(const QString &deviceId, LogLevel publicLogLevel);
Log(const QString &deviceId, LogLevel publicLogLevel = LogLevel::INFO);
~Log();
static Log &instance();
static Log &instance(const QString &deviceId);
static void destroyInstance();
void setPublicLogLevel(LogLevel publicLogLevel);
void logError(const QString &message);
......@@ -68,7 +64,6 @@ namespace FileSync
private:
void logMessage(const QString &message, LogLevel level);
static Log *s_instance;
QFile *m_logFile;
LogLevel m_publicLogLevel;
QString m_logFilePath;
......@@ -76,25 +71,44 @@ namespace FileSync
};
}
#define M(message) { \
return QString(message) \
}
#define D(logCategory, message) { \
qCDebug(logCategory) << message; \
}
#define I(logCategory, message) { \
qCInfo(logCategory) << message; \
}
#define W(logCategory, message) { \
qCWarning(logCategory) << qMessage; \
}
#define E(logCategory, message) { \
QString qMessage(message); \
qCCritical(logCategory) << qMessage; \
throw std::runtime_error(qMessage.toStdString()); \
}
#define LOG_D(logCategory, message) { \
Log::instance().logDebug(message); \
qCDebug(logCategory) << message; \
}
#define LOG_I(logCategory, message) { \
Log::instance().logInfo(message); \
qCInfo(logCategory) << message; \
}
#define LOG_W(logCategory, message) { \
QString qMessage(message); \
Log::instance().logWarn(qMessage); \
qCWarning(logCategory) << qMessage; \
}
#define LOG_E(logCategory, message) { \
QString qMessage(message); \
Log::instance().logError(qMessage); \
qCCritical(logCategory) << qMessage; \
throw std::runtime_error(qMessage.toStdString()); \
}
......
......@@ -23,13 +23,15 @@
#include <QDBusConnection>
#include <qdbuserror.h>
#include <daemon.h>
#include "config.h"
#include "daemon.h"
#include "common.h"
#include <filesyncapp/interfaces/filesyncapp_interface.h>
#include <filesyncapp/interfaces/sync_result.h>
#include <filesyncapp/interfaces/file_hash_array.h>
#include <filesyncapp/interfaces/json_types.h>
Q_LOGGING_CATEGORY(LOG_MAIN, "kdeconnect.filesyncapp.main");
int main(int argc, char *argv[])
{
FileSync::SyncResult::registerDbusType();
......@@ -37,7 +39,6 @@ int main(int argc, char *argv[])
FileSync::JsonTypes::registerQJsonObjectTypeAsDbusType();
FileSync::JsonTypes::registerQJsonArrayTypeAsDbusType();
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("KDE Connect File Synchronization");
QCoreApplication::setApplicationVersion("0.1.0");
......@@ -48,8 +49,7 @@ int main(int argc, char *argv[])
parser.process(app);
qDebug() << "Daemon starting ... " << endl;
I(LOG_MAIN, "Daemon starting ...");
FileSync::Daemon *daemon = new FileSync::Daemon();
FileSync::FileSyncDBusAdaptor *fileSyncDBusAdaptor = new FileSync::FileSyncDBusAdaptor(
daemon);
......@@ -57,9 +57,8 @@ int main(int argc, char *argv[])
if (!QDBusConnection::sessionBus().registerService(
"org.kde.kdeconnect.standalone.filesync"))
{
qDebug() << QString("D-Bus registration failed for service %1").arg(
QDBusConnection::sessionBus().lastError().message());
exit(1);
E(LOG_MAIN, FileSync::DBUS_REGISTRATION_FAILED.arg(
QDBusConnection::sessionBus().lastError().message()));
}
QString objPath("/daemon");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment