1 /* 2 * Copyright Andrey Semashev 2007 - 2015. 3 * Distributed under the Boost Software License, Version 1.0. 4 * (See accompanying file LICENSE_1_0.txt or copy at 5 * http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 /*! 8 * \file text_multifile_backend.cpp 9 * \author Andrey Semashev 10 * \date 09.06.2009 11 * 12 * \brief This header is the Boost.Log library implementation, see the library documentation 13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. 14 */ 15 16 #include <ios> 17 #include <boost/filesystem/path.hpp> 18 #include <boost/filesystem/fstream.hpp> 19 #include <boost/filesystem/operations.hpp> 20 #include <boost/log/sinks/text_multifile_backend.hpp> 21 #include <boost/log/detail/header.hpp> 22 23 namespace boost { 24 25 BOOST_LOG_OPEN_NAMESPACE 26 27 namespace sinks { 28 29 //! Sink implementation data 30 struct text_multifile_backend::implementation 31 { 32 //! File name composer 33 file_name_composer_type m_FileNameComposer; 34 //! Base path for absolute path composition 35 const filesystem::path m_BasePath; 36 //! File stream 37 filesystem::ofstream m_File; 38 implementationboost::sinks::text_multifile_backend::implementation39 implementation() : 40 m_BasePath(filesystem::current_path()) 41 { 42 } 43 44 //! Makes relative path absolute with respect to the base path make_absoluteboost::sinks::text_multifile_backend::implementation45 filesystem::path make_absolute(filesystem::path const& p) 46 { 47 return filesystem::absolute(p, m_BasePath); 48 } 49 }; 50 51 //! Default constructor text_multifile_backend()52BOOST_LOG_API text_multifile_backend::text_multifile_backend() : m_pImpl(new implementation()) 53 { 54 } 55 56 //! Destructor ~text_multifile_backend()57BOOST_LOG_API text_multifile_backend::~text_multifile_backend() 58 { 59 delete m_pImpl; 60 } 61 62 //! The method sets the file name composer set_file_name_composer_internal(file_name_composer_type const & composer)63BOOST_LOG_API void text_multifile_backend::set_file_name_composer_internal(file_name_composer_type const& composer) 64 { 65 m_pImpl->m_FileNameComposer = composer; 66 } 67 68 //! The method writes the message to the sink consume(record_view const & rec,string_type const & formatted_message)69BOOST_LOG_API void text_multifile_backend::consume(record_view const& rec, string_type const& formatted_message) 70 { 71 if (!m_pImpl->m_FileNameComposer.empty()) 72 { 73 filesystem::path file_name = m_pImpl->make_absolute(m_pImpl->m_FileNameComposer(rec)); 74 filesystem::create_directories(file_name.parent_path()); 75 m_pImpl->m_File.open(file_name, std::ios_base::out | std::ios_base::app); 76 if (m_pImpl->m_File.is_open()) 77 { 78 m_pImpl->m_File.write(formatted_message.data(), static_cast< std::streamsize >(formatted_message.size())); 79 m_pImpl->m_File.put(static_cast< string_type::value_type >('\n')); 80 m_pImpl->m_File.close(); 81 } 82 } 83 } 84 85 } // namespace sinks 86 87 BOOST_LOG_CLOSE_NAMESPACE // namespace log 88 89 } // namespace boost 90 91 #include <boost/log/detail/footer.hpp> 92