Boost logo

Boost-Commit :

From: john.groups_at_[hidden]
Date: 2007-10-17 10:01:57


Author: jtorjo
Date: 2007-10-17 10:01:56 EDT (Wed, 17 Oct 2007)
New Revision: 40118
URL: http://svn.boost.org/trac/boost/changeset/40118

Log:
[logging]
added shared_memory destination.
Added:
   sandbox/logging/boost/logging/format/destination/shared_memory.hpp (contents, props changed)
Text files modified:
   sandbox/logging/boost/logging/format/destination/convert_destination.hpp | 4 ++++
   sandbox/logging/boost/logging/format/optimize.hpp | 2 +-
   sandbox/logging/lib/logging/samples/vc8/loggingvc8/loggingvc8.vcproj | 4 ++++
   3 files changed, 9 insertions(+), 1 deletions(-)

Modified: sandbox/logging/boost/logging/format/destination/convert_destination.hpp
==============================================================================
--- sandbox/logging/boost/logging/format/destination/convert_destination.hpp (original)
+++ sandbox/logging/boost/logging/format/destination/convert_destination.hpp 2007-10-17 10:01:56 EDT (Wed, 17 Oct 2007)
@@ -47,6 +47,10 @@
 
     inline const char_type * do_convert(const char_type * c, const into<const char_type*> &) { return c; }
     inline const char_type * do_convert(const std::basic_string<char_type> & s, const into<const char_type* > &) { return s.c_str(); }
+
+ inline const std::basic_string<char_type> & do_convert(const std::basic_string<char_type> & s, const into< std::basic_string<char_type> > &) {
+ return s;
+ }
 }
 
 struct do_convert_destination {

Added: sandbox/logging/boost/logging/format/destination/shared_memory.hpp
==============================================================================
--- (empty file)
+++ sandbox/logging/boost/logging/format/destination/shared_memory.hpp 2007-10-17 10:01:56 EDT (Wed, 17 Oct 2007)
@@ -0,0 +1,120 @@
+// shared_memory.hpp
+
+// Boost Logging library
+//
+// Author: John Torjo, www.torjo.com
+//
+// Copyright (C) 2007 John Torjo (see www.torjo.com for email)
+//
+// 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)
+//
+// See http://www.boost.org for updates, documentation, and revision history.
+// See http://www.torjo.com/log2/ for more details
+
+
+#ifndef JT28092007_shared_memory_HPP_DEFINED
+#define JT28092007_shared_memory_HPP_DEFINED
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <boost/logging/detail/fwd.hpp>
+#include <boost/shmem/shmem_named_shared_object.hpp>
+
+namespace boost { namespace logging { namespace destination {
+
+ // FIXME not tested !!!
+
+namespace detail {
+ struct shared_memory_context {
+ shared_memory_context() : occupied_size(0), memory(0), mem_size(0) {
+ // note: we don't want to destroy this segment, since we want it to outlive our
+ // application. In the case of a problem, we need to have another program that will
+ // take a look at what we just logged.
+ }
+
+ // how much of the memory is occupied?
+ long * occupied_size;
+ // the shared memory
+ char_type * memory;
+ boost::shmem::named_shared_object segment;
+ std::size_t mem_size;
+
+ };
+}
+
+template<class convert_dest = do_convert_destination > struct shared_memory {
+
+ enum { just_in_case = 8192 };
+ basic_shared_memory_appender(const std::string & name, std::size_t mem_size = 2 * 1024 * 1024 * sizeof(char_type) ) : m_info(new info) {
+ non_const_context_base::context().mem_size = mem_size;
+
+ // this segment might have been previously created...
+ non_const_context_base::context().segment.open_or_create(name.c_str(), non_const_context_base::context().mem_size + just_in_case);
+
+ // the string
+ { typedef std::pair<char_type*, std::size_t> pair;
+ pair res = non_const_context_base::context().segment.find<char_type>("shared_log_object");
+ if ( !res.first)
+ // we're creating it right now
+ non_const_context_base::context().segment.construct<char_type>("shared_log_object")[mem_size](0);
+
+ res = non_const_context_base::context().segment.find<char_type>("shared_log_object");
+ BOOST_ASSERT( res.first); // should be created by now
+ non_const_context_base::context().memory = res.first;
+ }
+
+ // the occupied size
+ { typedef std::pair<long*, std::size_t> pair;
+ pair res = non_const_context_base::context().segment.find<long>("shared_occupied_size");
+ if ( !res.first)
+ // we're creating it right now
+ non_const_context_base::context().segment.construct<long>("shared_occupied_size")[1](0);
+
+ res = non_const_context_base::context().segment.find<long>("shared_occupied_size");
+ BOOST_ASSERT( res.first); // should be created by now
+ non_const_context_base::context().occupied_size = res.first;
+ }
+ }
+
+ template<class msg_type> void operator () (const msg_type& msg_arg) {
+ const string_type & msg = do_convert::do_convert(msg_arg, into<string_type>() );
+
+ bool can_fit = *(non_const_context_base::context().occupied_size) + msg.size() < non_const_context_base::context().mem_size;
+ if ( can_fit) {
+ std::copy(msg.begin(), msg.end(), non_const_context_base::context().memory + *non_const_context_base::context().occupied_size);
+ *non_const_context_base::context().occupied_size += (long)msg.size();
+ }
+ else {
+ // exceeds bounds
+ if ( msg.size() < non_const_context_base::context().mem_size) {
+ // move what was previously written, to the left, to make room
+ std::size_t keep = non_const_context_base::context().mem_size / 2;
+ if ( keep + msg.size() > non_const_context_base::context().mem_size) keep = non_const_context_base::context().mem_size - msg.size();
+ std::copy_backward(
+ non_const_context_base::context().memory + *non_const_context_base::context().occupied_size - keep,
+ non_const_context_base::context().memory + *non_const_context_base::context().occupied_size,
+ non_const_context_base::context().memory + keep);
+ std::copy( msg.begin(), msg.end(), non_const_context_base::context().memory + keep);
+ *non_const_context_base::context().occupied_size = (long)(keep + msg.size());
+ }
+ else {
+ // message too big
+ std::copy(msg.begin(), msg.begin() + non_const_context_base::context().mem_size, non_const_context_base::context().memory);
+ *non_const_context_base::context().occupied_size = (long)non_const_context_base::context().mem_size;
+ }
+ }
+ }
+
+
+};
+
+
+
+}}}
+
+#endif
+

Modified: sandbox/logging/boost/logging/format/optimize.hpp
==============================================================================
--- sandbox/logging/boost/logging/format/optimize.hpp (original)
+++ sandbox/logging/boost/logging/format/optimize.hpp 2007-10-17 10:01:56 EDT (Wed, 17 Oct 2007)
@@ -286,7 +286,7 @@
         */
         void restart() {
             // ******** whatever msg is in vector that has an id, it to be placed in coll.
- // FIXME
+ // FIXME also, need to work on the use_cache<> class from format_and_write
             m_full_msg_computed = false;
         }
 

Modified: sandbox/logging/lib/logging/samples/vc8/loggingvc8/loggingvc8.vcproj
==============================================================================
--- sandbox/logging/lib/logging/samples/vc8/loggingvc8/loggingvc8.vcproj (original)
+++ sandbox/logging/lib/logging/samples/vc8/loggingvc8/loggingvc8.vcproj 2007-10-17 10:01:56 EDT (Wed, 17 Oct 2007)
@@ -662,6 +662,10 @@
                                                 RelativePath="..\..\..\..\..\boost\logging\format\destination\rolling_file.hpp"
>
                                         </File>
+ <File
+ RelativePath="..\..\..\..\..\boost\logging\format\destination\shared_memory.hpp"
+ >
+ </File>
                                 </Filter>
                         </Filter>
                         <Filter


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk