Index: src/common/TMultiFileBackend.cpp =================================================================== diff -u -N -r205f3bfeed0082edb35430a9a0968699e5cc7fea -r62d767936f1675e1db51174f53c91484fe691937 --- src/common/TMultiFileBackend.cpp (.../TMultiFileBackend.cpp) (revision 205f3bfeed0082edb35430a9a0968699e5cc7fea) +++ src/common/TMultiFileBackend.cpp (.../TMultiFileBackend.cpp) (revision 62d767936f1675e1db51174f53c91484fe691937) @@ -18,17 +18,76 @@ // ============================================================================ #include "stdafx.h" #include "TMultiFileBackend.h" +#include "..\libchcore\TTimestampProviderTickCount.h" +#include +#include +#include +#include +#include +#include "..\libchcore\TStringArray.h" +#include "TLoggerLocationConfig.h" namespace chcore { - TMultiFileBackend::TMultiFileBackend() + TMultiFileBackend::TMultiFileBackend(ITimestampProviderPtr spTimestampProvider, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) : + m_spTimestampProvider(spTimestampProvider), + m_logRotator(uiMaxRotatedFiles, ullMaxLogSize) { + if (!m_spTimestampProvider) + m_spTimestampProvider.reset(new TTimestampProviderTickCount); + } + void TMultiFileBackend::Init(const TSmartPath& pathDirectory, unsigned int uiMaxRotatedFiles, unsigned long long ullMaxLogSize) + { + SetDirectory(pathDirectory); + m_logRotator.SetLimits(uiMaxRotatedFiles, ullMaxLogSize); } - void TMultiFileBackend::consume(boost::log::record_view const& /*rec*/, string_type const& /*formatted_message*/) + void TMultiFileBackend::consume(const boost::log::record_view& rec, const string_type& formatted_message) { + if (!m_bInitialized) + return; + TSmartPath pathLog = GetLogName(rec); + if (pathLog.IsEmpty()) + return; + + TLogSink& sinkData = m_mapLogs.GetSinkData(pathLog); + + HANDLE hFile = GetLogFile(pathLog, sinkData, formatted_message.length()); + if (hFile == INVALID_HANDLE_VALUE) + return; + + DWORD dwToWrite = boost::numeric_cast(formatted_message.length() * sizeof(wchar_t)); + DWORD dwWritten = 0; + WriteFile(hFile, formatted_message.c_str(), dwToWrite, &dwWritten, nullptr); } + TSmartPath TMultiFileBackend::GetLogName(const boost::log::record_view &rec) + { + auto attrLogPath = rec.attribute_values().find("LogPath"); + if (attrLogPath == rec.attribute_values().end()) + return TSmartPath(); + + boost::log::value_ref val = boost::log::extract(attrLogPath->second); + if (!val) + return TSmartPath(); + + return val.get()->GetLogPath(); + } + + HANDLE TMultiFileBackend::GetLogFile(const TSmartPath& pathLog, TLogSink& sinkData, size_t stRequiredSpace) + { + m_logRotator.RotateFile(pathLog, sinkData, stRequiredSpace); + + return sinkData.GetFileHandle(); + } + + void TMultiFileBackend::SetDirectory(const TSmartPath& pathDirectory) + { + m_mapLogs.Clear(); + m_logRotator.ScanForLogs(pathDirectory, m_mapLogs); + m_bInitialized = true; + } + }