diff options
Diffstat (limited to 'LogThread.cpp')
-rw-r--r-- | LogThread.cpp | 197 |
1 files changed, 101 insertions, 96 deletions
diff --git a/LogThread.cpp b/LogThread.cpp index 4af7550..234ccbf 100644 --- a/LogThread.cpp +++ b/LogThread.cpp @@ -6,6 +6,7 @@ #include "LogThread.h" #include "ResourcesManager.h" #include "OptionEngine.h" +#include "cpp11addition.h" #ifdef Q_OS_WIN32 #ifndef NOMINMAX @@ -15,29 +16,29 @@ #endif #include <QMessageBox> -QString LogThread::text_header_copy=QStringLiteral("[Copy] "); -QString LogThread::text_header_move=QStringLiteral("[Move] "); -QString LogThread::text_header_skip=QStringLiteral("[Skip] "); -QString LogThread::text_header_stop=QStringLiteral("[Stop] "); -QString LogThread::text_header_error=QStringLiteral("[Error] "); -QString LogThread::text_header_MkPath=QStringLiteral("[MkPath] "); -QString LogThread::text_header_RmPath=QStringLiteral("[RmPath] "); +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] "; -QString LogThread::text_var_source=QStringLiteral("%source%"); -QString LogThread::text_var_size=QStringLiteral("%size%"); -QString LogThread::text_var_destination=QStringLiteral("%destination%"); -QString LogThread::text_var_path=QStringLiteral("%path%"); -QString LogThread::text_var_error=QStringLiteral("%error%"); -QString LogThread::text_var_mtime=QStringLiteral("%mtime%"); -QString LogThread::text_var_time=QStringLiteral("%time%"); -QString LogThread::text_var_timestring=QStringLiteral("%dd.MM.yyyy h:m:s%"); +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 -QString LogThread::text_var_computer=QStringLiteral("%computer%"); -QString LogThread::text_var_user=QStringLiteral("%user%"); +std::string LogThread::text_var_computer="%computer%"; +std::string LogThread::text_var_user="%user%"; #endif -QString LogThread::text_var_operation=QStringLiteral("%operation%"); -QString LogThread::text_var_rmPath=QStringLiteral("%rmPath%"); -QString LogThread::text_var_mkPath=QStringLiteral("%mkPath%"); +std::string LogThread::text_var_operation="%operation%"; +std::string LogThread::text_var_rmPath="%rmPath%"; +std::string LogThread::text_var_mkPath="%mkPath%"; LogThread::LogThread() { @@ -52,36 +53,36 @@ LogThread::LogThread() connect(this, &LogThread::newData, this,&LogThread::realDataWrite,Qt::QueuedConnection); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("transfer"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("transfer"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("error"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("error"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("folder"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("folder"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("sync"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("sync"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("transfer_format"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("transfer_format"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("error_format"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("error_format"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("folder_format"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("folder_format"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("sync"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("sync"))); - newOptionValue(QStringLiteral("Write_log"), QStringLiteral("enabled"), OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("enabled"))); + 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); + computer=QString::fromWCharArray(computerNameW,size-1).toStdString(); else - computer=QStringLiteral("Unknown computer"); + computer="Unknown computer"; delete computerNameW; WCHAR * userNameW=new WCHAR[size]; if(GetUserNameW(userNameW,&size)) - user=QString::fromWCharArray(userNameW,size-1); + user=QString::fromWCharArray(userNameW,size-1).toStdString(); else - user=QStringLiteral("Unknown user"); + user="Unknown user"; delete userNameW; #endif #ifdef Q_OS_WIN32 - lineReturn=QStringLiteral("\r\n"); + lineReturn="\r\n"; #else - lineReturn=QStringLiteral("\n"); + lineReturn="\n"; #endif } @@ -99,41 +100,41 @@ bool LogThread::logTransfer() const void LogThread::openLogs() { - if(OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("enabled")).toBool()==false) + 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,QString("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(OptionEngine::optionEngine->getOptionValue("Write_log","file").toString()); + 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,QString("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").toString()); + 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,QString("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,QStringLiteral("opened log: ")+OptionEngine::optionEngine->getOptionValue(QStringLiteral("Write_log"),QStringLiteral("file")).toString()); + 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.toUtf8()); + log.write(data.data(),data.size()); log.close(); } @@ -141,16 +142,17 @@ void LogThread::newTransferStart(const Ultracopier::ItemOfCopyList &item) { if(!logTransfer()) return; - QString text; + 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% - text=text.replace(LogThread::text_var_source,item.sourceFullPath); - text=text.replace(LogThread::text_var_size,QString::number(item.size)); - text=text.replace(LogThread::text_var_destination,item.destinationFullPath); + 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); } @@ -159,12 +161,13 @@ void LogThread::transferSkip(const Ultracopier::ItemOfCopyList &item) { if(!logTransfer()) return; - QString text=LogThread::text_header_skip+transfer_format+lineReturn; + std::string text=LogThread::text_header_skip+transfer_format+lineReturn; text=replaceBaseVar(text); //Variable is %source%, %size%, %destination% - text=text.replace(LogThread::text_var_source,item.sourceFullPath); - text=text.replace(LogThread::text_var_size,QString::number(item.size)); - text=text.replace(LogThread::text_var_destination,item.destinationFullPath); + 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); } @@ -172,26 +175,28 @@ void LogThread::newTransferStop(const Ultracopier::ItemOfCopyList &item) { if(!logTransfer()) return; - QString text=LogThread::text_header_stop+transfer_format+lineReturn; + std::string text=LogThread::text_header_stop+transfer_format+lineReturn; text=replaceBaseVar(text); //Variable is %source%, %size%, %destination% - text=text.replace(LogThread::text_var_source,item.sourceFullPath); - text=text.replace(LogThread::text_var_size,QString::number(item.size)); - text=text.replace(LogThread::text_var_destination,item.destinationFullPath); + 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 QString &path,const quint64 &size,const QDateTime &mtime,const QString &error) +void LogThread::error(const std::string &path,const uint64_t &size,const uint64_t &mtime,const std::string &error) { if(!log_enable_error) return; - QString text=LogThread::text_header_error+error_format+lineReturn; + std::string text=LogThread::text_header_error+error_format+lineReturn; text=replaceBaseVar(text); //Variable is %path%, %size%, %mtime%, %error% - text=text.replace(LogThread::text_var_path,path); - text=text.replace(LogThread::text_var_size,QString::number(size)); - text=text.replace(LogThread::text_var_mtime,mtime.toString(Qt::ISODate)); - text=text.replace(LogThread::text_var_error,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); } @@ -200,7 +205,7 @@ void LogThread::run() exec(); } -void LogThread::realDataWrite(const QString &text) +void LogThread::realDataWrite(const std::string &text) { #ifdef ULTRACOPIER_DEBUG if(!log.isOpen()) @@ -209,46 +214,46 @@ void LogThread::realDataWrite(const QString &text) return; } #endif // ULTRACOPIER_DEBUG - ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("start")); - if(log.write(text.toUtf8())==-1) + ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start"); + if(log.write(text.data(),text.size())==-1) { - ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("unable to write into transfer log: %1").arg(log.errorString())); + ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to write into transfer log: "+log.errorString().toStdString()); return; } if(sync) log.flush(); } -void LogThread::newOptionValue(const QString &group,const QString &name,const QVariant &value) +void LogThread::newOptionValue(const std::string &group,const std::string &name,const std::string &value) { - if(group!=QStringLiteral("Write_log")) + if(group!="Write_log") return; - if(name==QStringLiteral("transfer_format")) - transfer_format=value.toString(); - else if(name==QStringLiteral("error_format")) - error_format=value.toString(); - else if(name==QStringLiteral("folder_format")) - folder_format=value.toString(); - else if(name==QStringLiteral("sync")) + 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=value.toBool(); - ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QString("sync flag is set on: %1").arg(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==QStringLiteral("transfer")) - log_enable_transfer=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool(); - else if(name==QStringLiteral("error")) - log_enable_error=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool(); - else if(name==QStringLiteral("folder")) - log_enable_folder=OptionEngine::optionEngine->getOptionValue("Write_log","enabled").toBool() && value.toBool(); - if(name==QStringLiteral("enabled")) + 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=value.toBool(); + enabled=stringtobool(value); if(enabled) openLogs(); else @@ -256,36 +261,36 @@ void LogThread::newOptionValue(const QString &group,const QString &name,const QV } } -QString LogThread::replaceBaseVar(QString text) +std::string LogThread::replaceBaseVar(std::string text) { - text=text.replace(LogThread::text_var_time,QDateTime::currentDateTime().toString(LogThread::text_var_timestring)); + stringreplaceAll(text,LogThread::text_var_time,QDateTime::currentDateTime().toString(QString::fromStdString(LogThread::text_var_timestring)).toStdString()); #ifdef Q_OS_WIN32 - text=text.replace(LogThread::text_var_computer,computer); - text=text.replace(LogThread::text_var_user,user); + stringreplaceAll(text,LogThread::text_var_computer,computer); + stringreplaceAll(text,LogThread::text_var_user,user); #endif return text; } -void LogThread::rmPath(const QString &path) +void LogThread::rmPath(const std::string &path) { if(!logTransfer()) return; - QString text=LogThread::text_header_RmPath+folder_format+lineReturn; + std::string text=LogThread::text_header_RmPath+folder_format+lineReturn; text=replaceBaseVar(text); //Variable is %operation% %path% - text=text.replace(LogThread::text_var_path,path); - text=text.replace(LogThread::text_var_operation,LogThread::text_var_rmPath); + stringreplaceAll(text,LogThread::text_var_path,path); + stringreplaceAll(text,LogThread::text_var_operation,LogThread::text_var_rmPath); emit newData(text); } -void LogThread::mkPath(const QString &path) +void LogThread::mkPath(const std::string &path) { if(!logTransfer()) return; - QString text=LogThread::text_header_MkPath+folder_format+lineReturn; + std::string text=LogThread::text_header_MkPath+folder_format+lineReturn; text=replaceBaseVar(text); //Variable is %operation% %path% - text=text.replace(LogThread::text_var_path,path); - text=text.replace(LogThread::text_var_operation,LogThread::text_var_mkPath); + stringreplaceAll(text,LogThread::text_var_path,path); + stringreplaceAll(text,LogThread::text_var_operation,LogThread::text_var_mkPath); emit newData(text); } |