Boost logo

Boost-Commit :

From: john.groups_at_[hidden]
Date: 2007-11-16 02:02:20


Author: jtorjo
Date: 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
New Revision: 41139
URL: http://svn.boost.org/trac/boost/changeset/41139

Log:
[logging]
v0.11.15, 15 nov 2007
- solved bugs in append_newline_if_needed() when using tags
Text files modified:
   sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp | 5
   sandbox/logging/boost/logging/format/formatter/convert_format.hpp | 117 +++++++++++++++-----
   sandbox/logging/boost/logging/format/optimize.hpp | 40 +++++++
   sandbox/logging/lib/logging/samples/scenarios/using_tags.cpp | 3
   sandbox/logging/lib/logging/tests/do_not_use/testfast.cpp | 214 +++++++++++----------------------------
   5 files changed, 188 insertions(+), 191 deletions(-)

Modified: sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp
==============================================================================
--- sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp (original)
+++ sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
@@ -1,8 +1,9 @@
 /**
 @page page_changelog Changelog
 
-_at_section changelog_cur_ver Current Version: v0.11.14, 15 nov 2007
-- updated docs
+@section changelog_cur_ver Current Version: v0.11.15, 15 nov 2007
+- solved bugs in append_newline_if_needed() when using tags
+- updated docs - added page showing how to declare/define your logger/filter classes
 - updated macros documentation (still to do)
 - added @ref starter_project
 - solved bug when using append_newline_if_needed() - when not used with string

Modified: sandbox/logging/boost/logging/format/formatter/convert_format.hpp
==============================================================================
--- sandbox/logging/boost/logging/format/formatter/convert_format.hpp (original)
+++ sandbox/logging/boost/logging/format/formatter/convert_format.hpp 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
@@ -62,24 +62,38 @@
 namespace convert {
     typedef boost::logging::char_type char_type;
     typedef std::basic_string<char_type> string_type;
+ typedef const char_type* char_array;
 
- inline const string_type & get_underlying_string(const string_type & str) { return str; }
- template<class string> const string & get_underlying_string(const ::boost::logging::optimize::cache_string_one_str<string> & str) {
- return str;
- }
- template<class string> const string & get_underlying_string(const ::boost::logging::optimize::cache_string_several_str<string> & str) {
- return str;
- }
- template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> const string& get_underlying_string(const ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & str) {
- return str;
- }
+ template<class string> struct string_finder {
+ typedef string type;
+ typedef string original_type ;
+ static const type& get(const original_type & str) { return str; }
+ };
+ template<class string> struct string_finder< ::boost::logging::optimize::cache_string_one_str<string> > {
+ typedef string type;
+ typedef ::boost::logging::optimize::cache_string_one_str<string> original_type ;
+ static const type& get(const original_type & str) { return str; }
+ };
+ template<class string> struct string_finder< ::boost::logging::optimize::cache_string_several_str<string> > {
+ typedef string type;
+ typedef ::boost::logging::optimize::cache_string_several_str<string> original_type;
+ static const type& get(const original_type & str) { return str; }
+ };
+ template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10>
+ struct string_finder< ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> > {
+ typedef typename string_finder< string>::type type;
+ typedef ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> original_type;
+
+ // note: this needs 2 conversions - to string, and then to cache string
+ static const type& get(const original_type & str) { return (const string&)str; }
+ };
 
     /**
     Example : write_time
     */
     namespace prepend {
 
- inline void write(const char_type * src, string_type & dest ) {
+ inline void write(char_array src, string_type & dest ) {
             const char_type * end = src;
             for ( ; *end; ++end) {}
             dest.insert( dest.begin(), src, end);
@@ -98,14 +112,23 @@
             write(src, static_cast<string&>(dest) );
         }
 
+
+
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_one_str<string> & dest) {
+ dest.prepend_string(src);
+ }
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_several_str<string> & dest) {
+ dest.prepend_string(src);
+ }
+
+ template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> void write(char_array src, ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & dest) {
+ write(src, static_cast<string&>(dest) );
+ }
     }
 
     /**
     */
     namespace append {
- inline void write(const char_type * src, string_type & dest ) {
- dest += src;
- }
         inline void write(const string_type & src, string_type & dest) {
             dest += src;
         }
@@ -118,14 +141,26 @@
         template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> void write(const string_type & src, ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & dest) {
             write(src, static_cast<string&>(dest) );
         }
+
+
+ inline void write(char_array src, string_type & dest ) {
+ dest += src;
+ }
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_one_str<string> & dest) {
+ dest.append_string(src);
+ }
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_several_str<string> & dest) {
+ dest.append_string(src);
+ }
+ template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> void write(char_array src, ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & dest) {
+ write(src, static_cast<string&>(dest) );
+ }
+
     }
 
     /**
     */
     namespace modify {
- inline void write(const char_type * src, string_type & dest ) {
- dest = src;
- }
         inline void write(const string_type & src, string_type & dest) {
             dest = src;
         }
@@ -141,6 +176,21 @@
         template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> void write(const string_type & src, ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & dest) {
             write(src, static_cast<string&>(dest) );
         }
+
+
+
+ inline void write(char_array src, string_type & dest ) {
+ dest = src;
+ }
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_one_str<string> & dest) {
+ dest.set_string(src);
+ }
+ template<class string> void write(char_array src, boost::logging::optimize::cache_string_several_str<string> & dest) {
+ dest.set_string(src);
+ }
+ template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> void write(char_array src, ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & dest) {
+ write(src, static_cast<string&>(dest) );
+ }
     }
 }
 
@@ -149,12 +199,13 @@
     typedef std::basic_string<char_type> string_type;
 
     struct prepend {
- static inline const string_type & get_underlying_string(const string_type & str) { return convert::get_underlying_string(str); }
- template<class string> static inline const string & get_underlying_string(const ::boost::logging::optimize::cache_string_several_str<string> & str) {
- return convert::get_underlying_string(str);
+ template<class string>
+ static inline const typename convert::string_finder<string>::type & get_underlying_string(const string & str) {
+ return convert::string_finder<string>::get(str);
         }
- template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> static inline const string& get_underlying_string(const ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & str) {
- return convert::get_underlying_string(str);
+
+ template<class string> static void write(const char_type * src, string & dest) {
+ convert::prepend::write(src, dest);
         }
 
         template<class src_type, class string> static void write(const src_type & src, string & dest) {
@@ -166,12 +217,13 @@
     };
 
     struct append {
- static inline const string_type & get_underlying_string(const string_type & str) { return convert::get_underlying_string(str); }
- template<class string> static inline const string & get_underlying_string(const ::boost::logging::optimize::cache_string_several_str<string> & str) {
- return convert::get_underlying_string(str);
+ template<class string>
+ static inline const typename convert::string_finder<string>::type & get_underlying_string(const string & str) {
+ return convert::string_finder<string>::get(str);
         }
- template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> static inline const string& get_underlying_string(const ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & str) {
- return convert::get_underlying_string(str);
+
+ template<class string> static void write(const char_type * src, string & dest) {
+ convert::append::write(src, dest);
         }
 
         template<class src_type, class string> static void write(const src_type & src, string & dest) {
@@ -183,12 +235,13 @@
     };
 
     struct modify {
- static inline const string_type & get_underlying_string(const string_type & str) { return convert::get_underlying_string(str); }
- template<class string> static inline const string & get_underlying_string(const ::boost::logging::optimize::cache_string_several_str<string> & str) {
- return convert::get_underlying_string(str);
+ template<class string>
+ static inline const typename convert::string_finder<string>::type & get_underlying_string(const string & str) {
+ return convert::string_finder<string>::get(str);
         }
- template<class string, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10> static inline const string& get_underlying_string(const ::boost::logging::tag::holder<string,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & str ) {
- return convert::get_underlying_string(str);
+
+ template<class string> static void write(const char_type * src, string & dest) {
+ convert::modify::write(src, dest);
         }
 
         template<class src_type, class string> static void write(const src_type & src, string & dest) {

Modified: sandbox/logging/boost/logging/format/optimize.hpp
==============================================================================
--- sandbox/logging/boost/logging/format/optimize.hpp (original)
+++ sandbox/logging/boost/logging/format/optimize.hpp 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
@@ -26,6 +26,9 @@
 #include <map>
 #include <vector>
 
+#include <string.h>
+#include <wchar.h>
+
 namespace boost { namespace logging {
 
 /**
@@ -93,6 +96,43 @@
             m_grow_size = new_size;
         }
 
+ private:
+ static int str_len(const char* str) { return (int)strlen(str); }
+ static int str_len(const wchar_t* str) { return (int)wcslen(str); }
+ public:
+
+ void prepend_string(const char_type* str) {
+ int len = str_len(str);
+ if ( m_reserve_prepend < len) {
+ int new_reserve_prepend = len + m_grow_size ;
+ resize_string( new_reserve_prepend, m_reserve_append);
+ }
+
+ BOOST_ASSERT(m_reserve_prepend >= len );
+
+ int start_idx = m_reserve_prepend - len;
+ m_reserve_prepend -= len;
+
+ std::copy(str, str + len, m_str.begin() + start_idx);
+ m_full_msg_computed = false;
+ }
+ void append_string(const char_type* str) {
+ int len = str_len(str);
+ if ( m_reserve_append < len) {
+ int new_reserve_append = len + m_grow_size ;
+ resize_string( m_reserve_prepend, new_reserve_append);
+ }
+
+ BOOST_ASSERT(m_reserve_append >= len );
+
+ int start_idx = (int)m_str.size() - m_reserve_append;
+
+ std::copy(str, str + len, m_str.begin() + start_idx);
+ m_reserve_append -= len;
+ m_full_msg_computed = false;
+ }
+
+
 
         /**
             @brief pre-pends a string (inserts it at the beginning)

Modified: sandbox/logging/lib/logging/samples/scenarios/using_tags.cpp
==============================================================================
--- sandbox/logging/lib/logging/samples/scenarios/using_tags.cpp (original)
+++ sandbox/logging/lib/logging/samples/scenarios/using_tags.cpp 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
@@ -56,8 +56,7 @@
 BOOST_LOG_FORMAT_MSG( log_string )
 
 
-#include <boost/logging/format.hpp>
-#include <boost/logging/writer/ts_write.hpp>
+#include <boost/logging/format_ts.hpp>
 #include <boost/logging/format/formatter/tags.hpp>
 
 using namespace boost::logging;

Modified: sandbox/logging/lib/logging/tests/do_not_use/testfast.cpp
==============================================================================
--- sandbox/logging/lib/logging/tests/do_not_use/testfast.cpp (original)
+++ sandbox/logging/lib/logging/tests/do_not_use/testfast.cpp 2007-11-16 02:02:19 EST (Fri, 16 Nov 2007)
@@ -1,185 +1,89 @@
-// your_scenario.cpp
-//
-// A test of the Logging library with multiple levels, multiple logging classes (each writing to multiple destinations) and one filter.
-// The logger and filter are specified using the boost::logging::scenario namespace
-
-// 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
-
-
-/**
-_at_example your_scenario.cpp
-
-_at_copydoc your_scenario
-
-_at_page your_scenario your_scenario.cpp Example
-
-
-This usage:
-- You have several loggers
-- You have one filter, which can be turned on or off
-- You want to format the message before it's written
-- Each logger has several log destinations
-- The logger and filter are specified using the boost::logging::scenario namespace
- - the filter is always accurate (but slow)
- - the filter does not use levels
- - the logger favors speed (on a dedicated thread)
- - the logger is initialized once, when only one thread is running
-
-Optimizations:
-- use a cache string (from optimize namespace), in order to make formatting the message faster
-
-Logs:
-- Error messages go into err.txt file
- - formatting - prefix each message by time, index, and append enter
-- Info output goes to console, and a file called out.txt
- - formatting - prefix each message by time and append enter
-- Debug messages go to the debug output window, and the console
- - formatting - prefix each message by time, and append enter
-
-
-Here's how the output will look like:
-
-The debug output window:
-_at_code
-18:59.24 this is so cool 1
-18:59.24 this is so cool again 2
-_at_endcode
-
-
-The console:
-_at_code
-18:59.24 this is so cool 1
-18:59.24 this is so cool again 2
-18:59.24 hello, world
-18:59.24 good to be back ;) 4
-_at_endcode
-
-
-The out.txt file:
-_at_code
-18:59.24 hello, world
-18:59.24 good to be back ;) 4
-_at_endcode
-
-
-The err.txt file
-_at_code
-18:59.24 [1] first error 3
-18:59.24 [2] second error 5
-_at_endcode
-*/
-
-
 
 #define BOOST_LOG_COMPILE_FAST_OFF
 #include <boost/logging/format_fwd.hpp>
+#include <boost/logging/tags.hpp>
 
 // Step 1: Optimize : use a cache string, to make formatting the message faster
-BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
+namespace bl = boost::logging;
+typedef bl::tag::holder< bl::optimize::cache_string_one_str<>, bl::tag::file_line, bl::tag::thread_id, bl::tag::time> log_string; //(1a)
+//typedef bl::tag::holder< bl::hold_string_type, bl::tag::file_line, bl::tag::thread_id, bl::tag::time> log_string; //(1b)
+BOOST_LOG_FORMAT_MSG( log_string )
+
+#include <boost/logging/format.hpp>
+#include <boost/logging/writer/ts_write.hpp>
+#include <boost/logging/format/formatter/tags.hpp>
 
-#include <boost/logging/format_ts.hpp>
-#include <boost/thread/xtime.hpp>
 using namespace boost::logging;
 
 // Step 3 : Specify your logging class(es)
 using namespace boost::logging::scenario::usage;
-typedef use<
- // the filter is always accurate (but slow)
- filter_::change::always_accurate,
- // filter does not use levels
- filter_::level::no_levels,
- // the logger is initialized once, when only one thread is running
- logger_::change::single_thread
- // the logger favors speed (on a dedicated thread)
- > finder;
+typedef use<default_,filter_::level::use_levels> finder;
 
 // Step 4: declare which filters and loggers you'll use (usually in a header file)
-BOOST_DECLARE_LOG_FILTER(g_log_filter, finder::filter )
-BOOST_DECLARE_LOG(g_log_err, finder::logger )
-BOOST_DECLARE_LOG(g_log_app, finder::logger )
-BOOST_DECLARE_LOG(g_log_dbg, finder::logger )
+BOOST_DECLARE_LOG_FILTER(g_log_filter, finder::filter )
+BOOST_DECLARE_LOG(g_l, finder::logger)
 
 // Step 5: define the macros through which you'll log
-#define LDBG_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_dbg, g_log_filter->is_enabled() )
-#define LERR_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_err, g_log_filter->is_enabled() )
-#define LAPP_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_app, g_log_filter->is_enabled() )
+//#define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l, g_log_filter->is_enabled() ) .set_tag( BOOST_LOG_TAG_FILELINE)
+#define LDBG BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, debug ) \
+ .set_tag( BOOST_LOG_TAG_FILELINE) << "[dbg] "
+#define LINF BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, info ) \
+ .set_tag( BOOST_LOG_TAG_FILELINE) << "[inf] "
+#define LWRN BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, warning ) \
+ .set_tag( BOOST_LOG_TAG_FILELINE) << "[wrn] "
+#define LERR BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, error ) \
+ .set_tag( BOOST_LOG_TAG_FILELINE) << "[err] "
+#define LFAT BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, fatal ) \
+ .set_tag( BOOST_LOG_TAG_FILELINE) << "[fat] "
+
+void init_logs();
+
+
+
 
 // Step 6: Define the filters and loggers you'll use (usually in a source file)
-BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter )
-BOOST_DEFINE_LOG(g_log_err, finder::logger )
-BOOST_DEFINE_LOG(g_log_app, finder::logger )
-BOOST_DEFINE_LOG(g_log_dbg, finder::logger )
-
-void do_sleep(int ms) {
- using namespace boost;
- xtime next;
- xtime_get( &next, TIME_UTC);
- next.nsec += (ms % 1000) * 1000000;
-
- int nano_per_sec = 1000000000;
- next.sec += next.nsec / nano_per_sec;
- next.sec += ms / 1000;
- next.nsec %= nano_per_sec;
- thread::sleep( next);
-}
+BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter )
+BOOST_DEFINE_LOG(g_l, finder::logger)
 
+void init_logs() {
+ using namespace boost::logging;
+ // Add formatters and destinations
+ // That is, how the message is to be formatted...
+ g_l->writer().add_formatter( formatter::tag::time("$hh:$mm.$ss ") );
+ g_l->writer().add_formatter( formatter::tag::file_line() );
+ g_l->writer().add_formatter( formatter::idx() );
+ g_l->writer().add_formatter( formatter::append_newline_if_needed() ); //(2a)
+ g_l->writer().add_formatter( formatter::append_newline() ); //(2b)
+
+ // ... and where should it be written to
+ g_l->writer().add_destination( destination::cout() );
+ //g_l->writer().add_destination( destination::dbg_window() );
+/* g_l->writer().add_destination
+ ( destination::rolling_file("atne_logging",
+ destination::rolling_file_settings().max_size_bytes(1024 * 10) )
+ );
+ */ g_l->writer().add_destination( destination::file("out.txt") );
+}
 void your_scenario_example() {
 
- // Step 7: add formatters and destinations
- // That is, how the message is to be formatted and where should it be written to
-
- // Err log
- g_log_err->writer().add_formatter( formatter::idx() );
- g_log_err->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
- g_log_err->writer().add_formatter( formatter::append_newline() );
- g_log_err->writer().add_destination( destination::file("err.txt") );
-
- // App log
- g_log_app->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
- g_log_app->writer().add_formatter( formatter::append_newline_if_needed() );
-// g_log_app->writer().add_destination( destination::file("out.txt", destination::file_settings().initial_overwrite(true) ) );
- g_log_app->writer().add_destination( destination::file("out.txt") );
- g_log_app->writer().add_destination( destination::cout() );
-
- // Debug log
- g_log_dbg->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
- g_log_dbg->writer().add_formatter( formatter::append_newline_if_needed() );
- g_log_dbg->writer().add_destination( destination::dbg_window() );
- g_log_dbg->writer().add_destination( destination::cout() );
+ init_logs();
 
     // Step 8: use it...
     int i = 1;
- LDBG_ << "this is so cool " << i++ << "\n";
- LDBG_ << "this is so cool again " << i++;
- LERR_ << "first error " << i++;
+ LDBG << "this is so cool " << i++ << "\n";
+ LDBG << "this is so cool again " << i++;
+ LERR << "first error " << i++;
 
     std::string hello = "hello", world = "world";
- LAPP_ << hello << ", " << world;
-
- g_log_filter->set_enabled(false);
- LDBG_ << "this will not be written anywhere";
- LAPP_ << "this won't be written anywhere either";
- LERR_ << "this error is not logged " << i++;
+ LWRN << hello << ", " << world;
 
- g_log_filter->set_enabled(true);
- LAPP_ << "good to be back ;) " << i++;
- LERR_ << "second error " << i++;
+ LDBG << "this will not be written anywhere";
+ LWRN << "this won't be written anywhere either";
+ LERR << "this error is not logged " << i++;
 
- // Step 9 : Enjoy!
+ LWRN << "good to be back ;) " << i++;
+ LERR << "second error " << i++;
 
- // just so that we can see the output to the console as well (the messages are written no a different thread)...
- do_sleep(1000);
 }
 
 


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