From dfb3049a7cd9b6949372ea774bd71ddd047e235a Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sun, 22 Jun 2014 18:46:17 +0400 Subject: [PATCH] Extracted text_multifile_backend implementation to a separate file. --- build/Jamfile.v2 | 1 + src/text_file_backend.cpp | 62 ----------------------- src/text_multifile_backend.cpp | 91 ++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 62 deletions(-) create mode 100644 src/text_multifile_backend.cpp diff --git a/build/Jamfile.v2 b/build/Jamfile.v2 index 812146c..1cf80e7 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile.v2 @@ -203,6 +203,7 @@ local BOOST_LOG_COMMON_SRC = default_sink.cpp text_ostream_backend.cpp text_file_backend.cpp + text_multifile_backend.cpp syslog_backend.cpp thread_specific.cpp once_block.cpp diff --git a/src/text_file_backend.cpp b/src/text_file_backend.cpp index b9aa1ae..9c92738 100644 --- a/src/text_file_backend.cpp +++ b/src/text_file_backend.cpp @@ -54,7 +54,6 @@ #include #include #include -#include #if !defined(BOOST_LOG_NO_THREADS) #include @@ -1356,67 +1355,6 @@ BOOST_LOG_API uintmax_t text_file_backend::scan_for_files(file::scan_method meth } } - -//////////////////////////////////////////////////////////////////////////////// -// Multifile sink backend implementation -//////////////////////////////////////////////////////////////////////////////// -//! Sink implementation data -struct text_multifile_backend::implementation -{ - //! File name composer - file_name_composer_type m_FileNameComposer; - //! Base path for absolute path composition - const filesystem::path m_BasePath; - //! File stream - filesystem::ofstream m_File; - - implementation() : - m_BasePath(filesystem::current_path()) - { - } - - //! Makes relative path absolute with respect to the base path - filesystem::path make_absolute(filesystem::path const& p) - { - return filesystem::absolute(p, m_BasePath); - } -}; - -//! Default constructor -BOOST_LOG_API text_multifile_backend::text_multifile_backend() : m_pImpl(new implementation()) -{ -} - -//! Destructor -BOOST_LOG_API text_multifile_backend::~text_multifile_backend() -{ - delete m_pImpl; -} - -//! The method sets the file name composer -BOOST_LOG_API void text_multifile_backend::set_file_name_composer_internal(file_name_composer_type const& composer) -{ - m_pImpl->m_FileNameComposer = composer; -} - -//! The method writes the message to the sink -BOOST_LOG_API void text_multifile_backend::consume(record_view const& rec, string_type const& formatted_message) -{ - typedef file_char_traits< string_type::value_type > traits_t; - if (!m_pImpl->m_FileNameComposer.empty()) - { - filesystem::path file_name = m_pImpl->make_absolute(m_pImpl->m_FileNameComposer(rec)); - filesystem::create_directories(file_name.parent_path()); - m_pImpl->m_File.open(file_name, std::ios_base::out | std::ios_base::app); - if (m_pImpl->m_File.is_open()) - { - m_pImpl->m_File.write(formatted_message.data(), static_cast< std::streamsize >(formatted_message.size())); - m_pImpl->m_File.put(traits_t::newline); - m_pImpl->m_File.close(); - } - } -} - } // namespace sinks BOOST_LOG_CLOSE_NAMESPACE // namespace log diff --git a/src/text_multifile_backend.cpp b/src/text_multifile_backend.cpp new file mode 100644 index 0000000..16e0dad --- /dev/null +++ b/src/text_multifile_backend.cpp @@ -0,0 +1,91 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file text_multifile_backend.cpp + * \author Andrey Semashev + * \date 09.06.2009 + * + * \brief This header is the Boost.Log library implementation, see the library documentation + * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. + */ + +#include +#include +#include +#include +#include +#include + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace sinks { + +//! Sink implementation data +struct text_multifile_backend::implementation +{ + //! File name composer + file_name_composer_type m_FileNameComposer; + //! Base path for absolute path composition + const filesystem::path m_BasePath; + //! File stream + filesystem::ofstream m_File; + + implementation() : + m_BasePath(filesystem::current_path()) + { + } + + //! Makes relative path absolute with respect to the base path + filesystem::path make_absolute(filesystem::path const& p) + { + return filesystem::absolute(p, m_BasePath); + } +}; + +//! Default constructor +BOOST_LOG_API text_multifile_backend::text_multifile_backend() : m_pImpl(new implementation()) +{ +} + +//! Destructor +BOOST_LOG_API text_multifile_backend::~text_multifile_backend() +{ + delete m_pImpl; +} + +//! The method sets the file name composer +BOOST_LOG_API void text_multifile_backend::set_file_name_composer_internal(file_name_composer_type const& composer) +{ + m_pImpl->m_FileNameComposer = composer; +} + +//! The method writes the message to the sink +BOOST_LOG_API void text_multifile_backend::consume(record_view const& rec, string_type const& formatted_message) +{ + if (!m_pImpl->m_FileNameComposer.empty()) + { + filesystem::path file_name = m_pImpl->make_absolute(m_pImpl->m_FileNameComposer(rec)); + filesystem::create_directories(file_name.parent_path()); + m_pImpl->m_File.open(file_name, std::ios_base::out | std::ios_base::app); + if (m_pImpl->m_File.is_open()) + { + m_pImpl->m_File.write(formatted_message.data(), static_cast< std::streamsize >(formatted_message.size())); + m_pImpl->m_File.put(static_cast< string_type::value_type >('\n')); + m_pImpl->m_File.close(); + } + } +} + +} // namespace sinks + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include