Index: src/ch/MainWnd.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -244,8 +244,18 @@ CString strTasksDir = GetTasksDirectory(); TSQLiteSerializerFactoryPtr spSerializerFactory(new TSQLiteSerializerFactory(PathFromString(strTasksDir))); IFeedbackHandlerFactoryPtr spFeedbackFactory(new CFeedbackHandlerFactory); - TMultiLoggerConfigPtr spLoggerConfig = std::make_shared(); // #todo + chcore::TConfig& rConfig = GetConfig(); + TMultiLoggerConfigPtr spLoggerConfig = std::make_shared(); + spLoggerConfig->SetLogLevel(L"default", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"Filesystem", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"Filesystem-File", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"Task", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"ST-FastMove", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"ST-CopyMove", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"ST-Delete", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + spLoggerConfig->SetLogLevel(L"ST-ScanDirs", (boost::log::trivial::severity_level)GetPropValue(rConfig)); + try { m_spTasks.reset(new chcore::TTaskManager(spSerializerFactory, spFeedbackFactory, PathFromString(strTasksDir), spLoggerConfig)); Index: src/ch/ch.cpp =================================================================== diff -u -rffbc773697bd08220163bf1e4491a34376b0f61c -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/ch/ch.cpp (.../ch.cpp) (revision ffbc773697bd08220163bf1e4491a34376b0f61c) +++ src/ch/ch.cpp (.../ch.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -307,15 +307,16 @@ } // ================================= Logging ======================================== - m_logInitializer.Init(5, 1 * 1024 * 1024); // #todo - m_chEngine.Init(5, 1 * 1024 * 1024); + InitLoggers(strPath); - chcore::TMultiLoggerConfigPtr spLoggerConfig = std::make_shared(); // #todo + // logger config + chcore::TMultiLoggerConfigPtr spAppLoggerConfig = std::make_shared(); + spAppLoggerConfig->SetLogLevel(L"default", (boost::log::trivial::severity_level)GetPropValue(rCfg)); // initialize the global log file if it is requested by configuration file CString strLogPath = strPath + _T("\\ch.log"); - m_spLogFactory.reset(new chcore::TLoggerFactory(chcore::PathFromString(strLogPath), spLoggerConfig)); + m_spLogFactory.reset(new chcore::TLoggerFactory(chcore::PathFromString(strLogPath), spAppLoggerConfig)); m_spLog = m_spLogFactory->CreateLogger(L"App"); LOG_INFO(m_spLog) << _T("============================ Initializing Copy Handler ============================"); @@ -495,6 +496,20 @@ return true; } +void CCopyHandlerApp::InitLoggers(const CString& strBasePath) +{ + chcore::TConfig& rConfig = GetConfig(); + + int iMaxSize = GetPropValue(rConfig); + int iRotateCount = GetPropValue(rConfig); + + m_logInitializer.Init(chcore::PathFromString(strBasePath), iRotateCount, iMaxSize); + + chcore::TSmartPath pathTasks = chcore::PathFromString(strBasePath); + pathTasks += chcore::PathFromString(L"Tasks"); + m_chEngine.Init(pathTasks, iRotateCount, iMaxSize); +} + void CCopyHandlerApp::InitShellExtension() { // validate ch version against extension version Index: src/ch/ch.h =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/ch/ch.h (.../ch.h) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/ch/ch.h (.../ch.h) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -70,13 +70,15 @@ TCommandLineParser m_cmdLineParser; chcore::TCoreEngine m_chEngine; - TLoggerInitializer m_logInitializer; + chcore::TLoggerInitializer m_logInitializer; chcore::TLoggerFactoryPtr m_spLogFactory; chcore::TLoggerPtr m_spLog; CWnd *m_pMainWindow; DECLARE_MESSAGE_MAP() +private: + void InitLoggers(const CString& strBasePath); }; inline CCopyHandlerApp& GetApp() Index: src/common/TLogSink.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TLogSink.cpp (.../TLogSink.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TLogSink.cpp (.../TLogSink.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -32,10 +32,16 @@ if (m_handleFile != INVALID_HANDLE_VALUE) return m_handleFile; - m_handleFile = CreateFile(m_pathLog.ToString(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + m_handleFile = CreateFile(m_pathLog.ToString(), GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (m_handleFile == INVALID_HANDLE_VALUE) throw TFileException(eErr_CannotOpenFile, GetLastError(), m_pathLog, L"Cannot open log file", LOCATION); + LARGE_INTEGER liSeek = { 0 }; + + BOOL bRes = SetFilePointerEx(m_handleFile, liSeek, nullptr, SEEK_END); + if (!bRes) + throw TFileException(eErr_CannotOpenFile, GetLastError(), m_pathLog, L"Cannot seek to the end of log file", LOCATION); + return m_handleFile; } Index: src/common/TLoggerInitializer.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TLoggerInitializer.cpp (.../TLoggerInitializer.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TLoggerInitializer.cpp (.../TLoggerInitializer.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -41,79 +41,75 @@ using LogSink = sinks::asynchronous_sink; using LogSinkPtr = boost::shared_ptr; -struct TLoggerInitializer::InternalData +namespace chcore { - LogSinkPtr m_spSink; -}; + struct TLoggerInitializer::InternalData + { + LogSinkPtr m_spSink; + }; -TLoggerInitializer::TLoggerInitializer() : - m_spData(new InternalData) -{ -} + TLoggerInitializer::TLoggerInitializer() : + m_spData(new InternalData) + { + } -TLoggerInitializer::~TLoggerInitializer() -{ - Uninit(); -} + TLoggerInitializer::~TLoggerInitializer() + { + Uninit(); + } -void TLoggerInitializer::Init(unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) -{ - if(m_bWasInitialized) - return; + void TLoggerInitializer::Init(const TSmartPath& pathDirWithLogs, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) + { + if (m_bWasInitialized) + return; - boost::shared_ptr< logging::core > core = logging::core::get(); + boost::shared_ptr< logging::core > spCore = logging::core::get(); - logging::add_common_attributes(); + logging::add_common_attributes(); - // sink BACKEND - boost::shared_ptr backend = boost::make_shared(nullptr, uiMaxRotatedFiles, ullMaxLogSize); + // sink BACKEND + boost::shared_ptr spBackend = boost::make_shared(nullptr, uiMaxRotatedFiles, ullMaxLogSize); + spBackend->Init(pathDirWithLogs, uiMaxRotatedFiles, ullMaxLogSize); -/* - // Set up the file naming pattern - backend->set_file_name_composer - ( - sinks::file::as_file_name_composer(expr::stream << expr::attr< std::wstring >("LogPath")) - ); -*/ + // Sink FRONTEND + LogSinkPtr spSink(new LogSink(spBackend)); - // Sink FRONTEND - LogSinkPtr sink(new LogSink(backend)); + // Set the formatter + spSink->set_formatter + ( + expr::stream + << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") + << " [" << boost::log::trivial::severity << "] " + << expr::attr< std::wstring >("Channel") << ": " + << expr::wmessage + ); - // Set the formatter - sink->set_formatter - ( - expr::stream - << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") - << " [" << boost::log::trivial::severity << "] " - << expr::attr< std::wstring >("Channel") << ": " - << expr::wmessage - ); + std::locale loc = boost::locale::generator()("en_EN.UTF-8"); + spSink->imbue(loc); - std::locale loc = boost::locale::generator()("en_EN.UTF-8"); - sink->imbue(loc); + spCore->add_sink(spSink); - core->add_sink(sink); + m_spData->m_spSink = spSink; - m_spData->m_spSink = sink; + m_bWasInitialized = true; + } - m_bWasInitialized = true; -} + void TLoggerInitializer::Uninit() + { + if (!m_spData->m_spSink || !m_bWasInitialized) + return; -void TLoggerInitializer::Uninit() -{ - if (!m_spData->m_spSink || !m_bWasInitialized) - return; + boost::shared_ptr< logging::core > core = logging::core::get(); - boost::shared_ptr< logging::core > core = logging::core::get(); + // Remove the sink from the core, so that no records are passed to it + core->remove_sink(m_spData->m_spSink); - // Remove the sink from the core, so that no records are passed to it - core->remove_sink(m_spData->m_spSink); + // Break the feeding loop + m_spData->m_spSink->stop(); - // Break the feeding loop - m_spData->m_spSink->stop(); + // Flush all log records that may have left buffered + m_spData->m_spSink->flush(); - // Flush all log records that may have left buffered - m_spData->m_spSink->flush(); - - m_spData->m_spSink.reset(); + m_spData->m_spSink.reset(); + } } Index: src/common/TLoggerInitializer.h =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TLoggerInitializer.h (.../TLoggerInitializer.h) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TLoggerInitializer.h (.../TLoggerInitializer.h) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -19,19 +19,24 @@ #ifndef __TLOGGERINITIALIZER_H__ #define __TLOGGERINITIALIZER_H__ -class TLoggerInitializer +#include "..\libchcore\TPath.h" + +namespace chcore { -public: - TLoggerInitializer(); - ~TLoggerInitializer(); + class TLoggerInitializer + { + public: + TLoggerInitializer(); + ~TLoggerInitializer(); - void Init(unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize); - void Uninit(); + void Init(const TSmartPath& pathDirWithLogs, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize); + void Uninit(); -private: - struct InternalData; - std::unique_ptr m_spData; - bool m_bWasInitialized = false; -}; + private: + struct InternalData; + std::unique_ptr m_spData; + bool m_bWasInitialized = false; + }; +} #endif Index: src/common/TMultiFileBackend.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TMultiFileBackend.cpp (.../TMultiFileBackend.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TMultiFileBackend.cpp (.../TMultiFileBackend.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -58,9 +58,11 @@ if (hFile == INVALID_HANDLE_VALUE) return; - DWORD dwToWrite = boost::numeric_cast(formatted_message.length() * sizeof(wchar_t)); + string_type strFullMessage = formatted_message + "\n"; + + DWORD dwToWrite = boost::numeric_cast(strFullMessage.length()); DWORD dwWritten = 0; - WriteFile(hFile, formatted_message.c_str(), dwToWrite, &dwWritten, nullptr); + WriteFile(hFile, strFullMessage.c_str(), dwToWrite, &dwWritten, nullptr); } TSmartPath TMultiFileBackend::GetLogName(const boost::log::record_view &rec) Index: src/common/TMultiLoggerConfig.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TMultiLoggerConfig.cpp (.../TMultiLoggerConfig.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TMultiLoggerConfig.cpp (.../TMultiLoggerConfig.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -21,12 +21,23 @@ namespace chcore { - TLoggerLevelConfigPtr TMultiLoggerConfig::GetLoggerConfig(PCTSTR pszChannel) + TLoggerLevelConfigPtr TMultiLoggerConfig::GetLoggerConfig(PCTSTR pszChannel, bool bForceAdd) { auto iterConfig = m_mapConfigs.find(pszChannel); if (iterConfig == m_mapConfigs.end()) - iterConfig = m_mapConfigs.insert(std::make_pair(pszChannel, std::make_shared())).first; + { + if (bForceAdd) + iterConfig = m_mapConfigs.insert(std::make_pair(pszChannel, std::make_shared())).first; + else + return GetLoggerConfig(L"default", true); + } return iterConfig->second; } + + void TMultiLoggerConfig::SetLogLevel(PCTSTR pszChannel, boost::log::trivial::severity_level eLevel) + { + TLoggerLevelConfigPtr spLoggerConfig = GetLoggerConfig(pszChannel, true); + spLoggerConfig->SetMinSeverityLevel(eLevel); + } } Index: src/common/TMultiLoggerConfig.h =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/common/TMultiLoggerConfig.h (.../TMultiLoggerConfig.h) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/common/TMultiLoggerConfig.h (.../TMultiLoggerConfig.h) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -27,7 +27,8 @@ class TMultiLoggerConfig { public: - TLoggerLevelConfigPtr GetLoggerConfig(PCTSTR pszChannel); + TLoggerLevelConfigPtr GetLoggerConfig(PCTSTR pszChannel, bool bForceAdd = false); + void SetLogLevel(PCTSTR pszChannel, boost::log::trivial::severity_level eLevel); private: std::map m_mapConfigs; // channel, config Index: src/libchcore/TCoreEngine.cpp =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/libchcore/TCoreEngine.cpp (.../TCoreEngine.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/libchcore/TCoreEngine.cpp (.../TCoreEngine.cpp) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -31,9 +31,9 @@ } - void TCoreEngine::Init(unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) + void TCoreEngine::Init(const TSmartPath& pathLogs, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) { - m_loggerInitializer.Init(uiMaxRotatedFiles, ullMaxLogSize); + m_loggerInitializer.Init(pathLogs, uiMaxRotatedFiles, ullMaxLogSize); } void TCoreEngine::Uninitialize() Index: src/libchcore/TCoreEngine.h =================================================================== diff -u -r62d767936f1675e1db51174f53c91484fe691937 -rb8b97b70743c49fcc2aee0db4cb7118db5e26dab --- src/libchcore/TCoreEngine.h (.../TCoreEngine.h) (revision 62d767936f1675e1db51174f53c91484fe691937) +++ src/libchcore/TCoreEngine.h (.../TCoreEngine.h) (revision b8b97b70743c49fcc2aee0db4cb7118db5e26dab) @@ -30,7 +30,7 @@ TCoreEngine(); ~TCoreEngine(); - void Init(unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize); + void Init(const TSmartPath& pathLogs, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize); void Uninitialize(); private: