Boost C++ Libraries: Ticket #12867: log library extensions https://svn.boost.org/trac10/ticket/12867 <p> <strong>Please forward this request to Mr. Andrey Semashev.</strong> </p> <p> Dear Andrey, </p> <p> in our company we have decided to use the boost log in our applications. We need 2 additional extensions for the file backend: </p> <ol><li> we want to delete the log files after specific amount of time </li><li> we want to make some logs un-deleteable, for example to change the log name in some circumstance, for example in case if the record with severity “fatal” has been written. </li></ol><p> For this I have added to your text_file_backend.hpp/ text_file_backend.cpp two additional predicates and some code for these predicates support. </p> <p> The time_based_deleting_predicate in my application is implemented as: </p> <p> </p> <pre class="wiki">m_sink-&gt;locked_backend()-&gt;set_file_collector(sinks::polytec_ext::file::make_collector( keywords::target = dir , keywords::max_size = maxSize , keywords::time_based_deleting = [=](std::time_t time) { if (deletingInterval.count() == 0) return false; std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t latest = std::chrono::system_clock::to_time_t(now) - deletingInterval.count(); return latest &gt; time; })); </pre><p> The record_based_renaming_predicate is implemented as: </p> <pre class="wiki"> m_sink-&gt;locked_backend()-&gt;set_record_based_renaming( [=](logging::record_view const&amp; rec, boost::filesystem::path&amp; path) { auto level = boost::log::extract&lt; severity_level &gt;("Severity", rec); if (level &amp;&amp; severity_level::fatal == level.get()) { auto fname = path.stem(); bool hasErrorSuffix = boost::algorithm::ends_with(fname.c_str(), L"_ERROR"); if (!hasErrorSuffix) { auto newFname = path.parent_path() / boost::filesystem::path(path.stem().wstring() + L"_ERROR" + path.extension().wstring()); path = newFname; return true; } } return false; }); </pre><p> If you are finding these features useful for other library users and merging (may be with some improvements accordingly to your vision of the library future) with the library code it will make me a pleasure :-) and we will remove our implementation and will use the library “as is”. The ZIP file with my hpp an cpp is attached to the request. I have used 1.57 vesrsion of boost. Thank you for your attention and best regards. Victor Grinenko. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12867 Trac 1.4.3 vgrinenko@… Fri, 24 Feb 2017 15:29:52 GMT attachment set https://svn.boost.org/trac10/ticket/12867 https://svn.boost.org/trac10/ticket/12867 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_log_ext.zip</span> </li> </ul> Ticket Andrey Semashev Fri, 24 Feb 2017 20:09:45 GMT <link>https://svn.boost.org/trac10/ticket/12867#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12867#comment:1</guid> <description> <p> Thanks for the proposal. </p> <p> The time_based_deleting feature looks interesting, although I think it would be easier to use if it was just a max age specification instead of a predicate. </p> <p> The record-based renaming seems too specific to me, and potentially complicated. It seems possible for the rename to actually move the file to another location, which would potentially require a copy operation. Also, in the current implementation I try to minimize the number of file operations because each of them is a potential point of failure. I don't think this feature is useful enough in general to warrant the risks. You can always have that kind of functionality by post-processing log files. </p> </description> <category>Ticket</category> </item> <item> <author>vgrinenko@…</author> <pubDate>Mon, 27 Feb 2017 08:36:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12867#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12867#comment:2</guid> <description> <blockquote class="citation"> <p> a max age specification instead of a predicate. </p> </blockquote> <p> I was trying to do similar to your implementation and more or less “generic”: max age or older than specific time or something else. </p> <blockquote class="citation"> <p> The record-based renaming seems too specific to me... </p> </blockquote> <p> I agree, probably it would be better provide “generic file manipulation” possibility instead of renaming and allow to do something with m_File in the predicate, for example copy, move, close and open new log dependently from the record content. In this case the predicate itself will be responsible for the implementation and robustness and risks, not a library. But it is just an idea. </p> <p> Thanks for the answer and good luck! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 16 Nov 2017 12:17:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12867#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12867#comment:3</guid> <description> <p> Hi Andrey, </p> <p> I have a problem with MFC applications, both shared and static. If logger is called from the std::thread there are no problems, but if from the concurrency::create_task - there are lot of memory leaks. Probably they are "fake" leaks, it happens if shared MFC unloaded too early, but the problem exist with a static MFC as well. Do you have any ideas? </p> <p> To reproduce the problem create an empty console application project with MFC, add code below and set paths to boost include and libs. </p> <p> Best regards, Victor. </p> <pre class="wiki">// ConsoleApplicationMFC.cpp : Defines the entry point for the console application. // #define _CRT_SECURE_NO_WARNINGS #define BOOST_SYSTEM_NO_DEPRECATED #define BOOST_LIB_DIAGNOSTIC #define CGAL_LIB_DIAGNOSTIC // From StdAfx.h -&gt; #include &lt;SDKDDKVer.h&gt; #include &lt;stdio.h&gt; #include &lt;tchar.h&gt; #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS // remove support for MFC controls in dialogs #ifndef VC_EXTRALEAN #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #endif #include &lt;afx.h&gt; #include &lt;afxwin.h&gt; // MFC core and standard components #include &lt;afxext.h&gt; // MFC extensions #ifndef _AFX_NO_OLE_SUPPORT #include &lt;afxdtctl.h&gt; // MFC support for Internet Explorer 4 Common Controls #endif #ifndef _AFX_NO_AFXCMN_SUPPORT #include &lt;afxcmn.h&gt; // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT #include &lt;iostream&gt; // &lt;- From StdAfx.h #include &lt;boost/log/core.hpp&gt; #include &lt;boost/log/expressions.hpp&gt; #include &lt;boost/log/attributes.hpp&gt; #include &lt;boost/log/utility/setup/common_attributes.hpp&gt; #include &lt;boost/log/sources/severity_channel_logger.hpp&gt; #include &lt;boost/log/trivial.hpp&gt; #include &lt;boost/log/support/date_time.hpp&gt; #include &lt;boost/log/utility/setup/console.hpp&gt; #include &lt;boost\log\sinks\text_file_backend.hpp&gt; #if !defined(BOOST_LOG_NO_THREADS) #include &lt;boost/thread/locks.hpp&gt; #include &lt;boost/thread/mutex.hpp&gt; #endif // !defined(BOOST_LOG_NO_THREADS) #include &lt;ppltasks.h&gt; #include &lt;thread&gt; #ifdef _DEBUG #define new DEBUG_NEW #endif // The one and only application object CWinApp theApp; using namespace std; namespace logging = boost::log; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; namespace keywords = boost::log::keywords; namespace expr = boost::log::expressions; BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string) int main() { int nRetCode = 0; HMODULE hModule = ::GetModuleHandle(nullptr); if (hModule != nullptr) { // initialize MFC and print and error on failure if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs wprintf(L"Fatal Error: MFC initialization failed\n"); nRetCode = 1; } else { // TODO: code your application's behavior here. typedef sinks::synchronous_sink&lt;sinks::text_file_backend&gt; file_sink; src::severity_channel_logger_mt&lt;boost::log::trivial::severity_level&gt; logger(keywords::channel = "L"); boost::shared_ptr&lt;file_sink&gt; sinkT(new file_sink(keywords::file_name = "logs\\TestLogger_%6N.log")); sinkT-&gt;set_formatter ( expr::stream &lt;&lt; " TimeStamp: " &lt;&lt; expr::format_date_time&lt; boost::posix_time::ptime &gt;("TimeStamp", "%Y-%m-%d %H:%M:%S") &lt;&lt; " ThreadID: " &lt;&lt; expr::attr&lt;logging::thread_id&gt;("ThreadID") &lt;&lt; " Message: " &lt;&lt; expr::smessage ); logging::core::get()-&gt;add_sink(sinkT); sinkT-&gt;set_filter(a_channel == "L"); logging::add_common_attributes(); BOOST_LOG(logger) &lt;&lt; L"A message from the MAIN thread"; std::thread testTthread([&amp;]() { BOOST_LOG(logger) &lt;&lt; L"A message from the std::thread"; }); testTthread.join(); auto task = concurrency::create_task([&amp;]() { // Produces memory leaks BOOST_LOG(logger) &lt;&lt; L"A message from the concurrency::create_task thread"; }); task.wait(); BOOST_LOG(logger) &lt;&lt; L"Is concurrency::create_task done: " &lt;&lt; task.is_done(); } } else { // TODO: change error code to suit your needs wprintf(L"Fatal Error: GetModuleHandle failed\n"); nRetCode = 1; } return nRetCode; } </pre> </description> <category>Ticket</category> </item> <item> <author>vgrinenko@…</author> <pubDate>Fri, 17 Nov 2017 12:17:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12867#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12867#comment:4</guid> <description> <p> Hi, it has nothing with MFC, sorry. Just this code is enough. </p> <p> Visual Studio 2015. </p> <p> For example: </p> <pre class="wiki">Detected memory leaks! Dumping objects -&gt; {887} normal block at 0x00B6A0F8, 128 bytes long. Data: &lt; TimeStamp: 2017&gt; 00 54 69 6D 65 53 74 61 6D 70 3A 20 32 30 31 37 ... </pre><pre class="wiki">// ConsoleApplication_wo_MFC.cpp : Defines the entry point for the console application. // #define _CRT_SECURE_NO_WARNINGS #define BOOST_SYSTEM_NO_DEPRECATED #define BOOST_LIB_DIAGNOSTIC #define CGAL_LIB_DIAGNOSTIC #include &lt;stdio.h&gt; #include &lt;tchar.h&gt; #include &lt;windows.h&gt; #include &lt;ppltasks.h&gt; #include &lt;thread&gt; #define _CRTDBG_MAP_ALLOC #include &lt;stdlib.h&gt; #include &lt;crtdbg.h&gt; #include &lt;boost/log/core.hpp&gt; #include &lt;boost/log/expressions.hpp&gt; #include &lt;boost/log/attributes.hpp&gt; #include &lt;boost/log/utility/setup/common_attributes.hpp&gt; #include &lt;boost/log/sources/severity_channel_logger.hpp&gt; #include &lt;boost/log/trivial.hpp&gt; #include &lt;boost/log/support/date_time.hpp&gt; #include &lt;boost/log/utility/setup/console.hpp&gt; #include &lt;boost\log\sinks\text_file_backend.hpp&gt; #include &lt;boost/locale/localization_backend.hpp&gt; #if !defined(BOOST_LOG_NO_THREADS) #include &lt;boost/thread/locks.hpp&gt; #include &lt;boost/thread/mutex.hpp&gt; #endif // !defined(BOOST_LOG_NO_THREADS) using namespace std; namespace logging = boost::log; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; namespace keywords = boost::log::keywords; namespace expr = boost::log::expressions; BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string) int main() { int nRetCode = 0; _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); typedef sinks::synchronous_sink&lt;sinks::text_file_backend&gt; file_sink; src::severity_channel_logger_mt&lt;boost::log::trivial::severity_level&gt; logger(keywords::channel = "L"); boost::shared_ptr&lt;file_sink&gt; sinkT(new file_sink(keywords::file_name = "logs\\TestLogger_%6N.log")); sinkT-&gt;set_formatter ( expr::stream &lt;&lt; " TimeStamp: " &lt;&lt; expr::format_date_time&lt; boost::posix_time::ptime &gt;("TimeStamp", "%Y-%m-%d %H:%M:%S") &lt;&lt; " ThreadID: " &lt;&lt; expr::attr&lt;logging::thread_id&gt;("ThreadID") &lt;&lt; " Message: " &lt;&lt; expr::smessage ); logging::core::get()-&gt;add_sink(sinkT); sinkT-&gt;set_filter(a_channel == "L"); logging::add_common_attributes(); BOOST_LOG(logger) &lt;&lt; L"A message from the MAIN thread"; std::thread testTthread([&amp;]() { BOOST_LOG(logger) &lt;&lt; L"A message from the std::thread"; }); testTthread.join(); auto task = concurrency::create_task([&amp;]() { // Produces memory leaks BOOST_LOG(logger) &lt;&lt; L"A message from the concurrency::create_task thread"; }); task.wait(); BOOST_LOG(logger) &lt;&lt; L"Is concurrency::create_task done: " &lt;&lt; task.is_done(); return nRetCode; } </pre> </description> <category>Ticket</category> </item> <item> <author>vgrinenko@…</author> <pubDate>Fri, 17 Nov 2017 12:34:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12867#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12867#comment:5</guid> <description> <p> Boost versions: 1.57 and 1.65.1 </p> </description> <category>Ticket</category> </item> </channel> </rss>