Boost logo

Boost-Commit :

From: john.groups_at_[hidden]
Date: 2007-11-25 23:11:30


Author: jtorjo
Date: 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
New Revision: 41389
URL: http://svn.boost.org/trac/boost/changeset/41389

Log:
[logging]
v0.12.3, 26 nov 2007
- added scoped logs
Added:
   sandbox/logging/boost/logging/detail/raw_doc/scoped_logs.hpp (contents, props changed)
Text files modified:
   sandbox/logging/boost/logging/detail/raw_doc/changelog.hpp | 3 ++-
   sandbox/logging/boost/logging/detail/raw_doc/table_of_contents.hpp | 1 +
   sandbox/logging/boost/logging/detail/raw_doc/todo.hpp | 7 +++++++
   sandbox/logging/boost/logging/detail/scoped_log.hpp | 31 ++++++++++++++++++++++++-------
   sandbox/logging/lib/logging/internal/vc8/loggingvc8/loggingvc8.vcproj | 4 ++++
   sandbox/logging/lib/logging/internal/vc8/loggingvc8/test_now.cpp | 28 +++++++++++++++-------------
   6 files changed, 53 insertions(+), 21 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-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -1,7 +1,8 @@
 /**
 @page page_changelog Changelog
 
-_at_section changelog_cur_ver Current Version: v0.12.2, 20 nov 2007
+@section changelog_cur_ver Current Version: v0.12.3, 26 nov 2007
+- added scoped logs
 - removed #ifdef SINGLE_TEST from scenarios
 - added todo.html
 

Added: sandbox/logging/boost/logging/detail/raw_doc/scoped_logs.hpp
==============================================================================
--- (empty file)
+++ sandbox/logging/boost/logging/detail/raw_doc/scoped_logs.hpp 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -0,0 +1,134 @@
+namespace boost { namespace logging {
+
+/**
+@page scoped_logs Scoped Logs
+
+- @ref scoped_logs_whatis
+- @ref scoped_logs_equivalence
+- @ref scoped_logs_context
+- @ref scoped_logs_fast
+- @ref scoepd_logs_multiple
+
+
+@section scoped_logs_whatis Scoped Logs - what happens?
+
+The purpose of a "scoped log" is to log some information :
+- at the beginning of a given @c scope
+- at the end of the @c scope
+
+The information is logged like this:
+
+@code
+[prefix] start of [information]
+...
+...
+[prefix] end of [information]
+@endcode
+
+Example:
+
+@code
+[1] 05:29.51 [dbg] start of testing inout
+[2] 05:29.51 [dbg] end of testing inout
+@endcode
+
+
+@section scoped_logs_equivalence Equivalence in code
+
+To make it even clearer, using a scoped log:
+
+@code
+void func() {
+ BOOST_SCOPED_LOG_CTX(LDBG) << "func()" ;
+ // extra code
+}
+@endcode
+
+Is equivalent with:
+
+@code
+void func() {
+ LDBG << "start of func()" ;
+ // extra code
+ LDBG << " end of func()" ;
+}
+@endcode
+
+... of couse, using @c BOOST_SCOPED_LOG will have the right functionality even in the presence of exceptions.
+
+
+Note that I encountered a very big problem, when implementing scoped logs: I don't know how you @ref workflow_2a "gather your message", when using the logs.
+In other words, I don't know your Usage Syntax. So I had to make a few assumptions, as you'll see.
+
+
+
+@section scoped_logs_context The easy way - BOOST_SCOPED_LOG_CTX
+
+This allows you to simply log context in a straighforward manner, using the operator << ; context includes :
+- any variable in the local scope
+- any parameter passed to your function
+
+Example:
+
+@code
+#define LDBG BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, debug )
+...
+
+void func(int a, const char * str) {
+ BOOST_SCOPED_LOG_CTX(LDBG) << "func(" << a << ", str=" << str << ")";
+ ...
+}
+@endcode
+
+Things you should know:
+- You can only use @c BOOST_SCOPED_LOG_CTX, if your loggers use << to @ref workflow_2a "gather the message"
+- When using @c BOOST_SCOPED_LOG_CTX, you pass as parameter, one of the @ref macros_use "macros" you've already defined.
+- When you use this macro (BOOST_SCOPED_LOG_CTX), a temporary variable is created, which will hold the result
+ of gathering your context. In the above case, this variable will contain the contents of:
+ <tt> "func(" << a << ", str=" << str << ")"; </tt>
+
+
+
+
+@section scoped_logs_fast The fast way - BOOST_SCOPED_LOG
+
+The fast way makes no assumptions about your @ref workflow_2a "Usage Syntax". However, it's very limited in use:
+- You can only specify a string literal
+- If you use some operator (like "<<") when logging, you'll have to specify it as first argument
+- You cannot use any variable from your scope, nor any other variables (in fact, this is implied by the first item)
+
+Example:
+
+@code
+#define LDBG BOOST_LOG_USE_LOG_IF_LEVEL(g_l, g_log_filter, debug )
+...
+void func(int a, const char * str) {
+ BOOST_SCOPED_LOG(LDBG << , "testing inout" );
+ ...
+}
+@endcode
+
+It's fast, because:
+- It uses no extra temporary variable
+- It contactenates "start of " + message, and "end of " + message at <tt>compile time</tt>
+
+
+
+
+@section scoepd_logs_multiple Multiple scoped logs
+
+...are allowed. You can create a @c BOOST_SCOPED_LOG or @c BOOST_SCOPED_LOG_CTX at any time - within the body of a function, with the only limitation
+that you can't have 2 on the same line.
+
+Example:
+@code
+void func(int a, const char * str) {
+ BOOST_SCOPED_LOG_CTX(LDBG) << "func(" << a << ", str=" << str << ")";
+ int i = 0;
+ BOOST_SCOPED_LOG_CTX(LDBG) << "i =" << i;
+}
+@endcode
+
+*/
+
+}}

Modified: sandbox/logging/boost/logging/detail/raw_doc/table_of_contents.hpp
==============================================================================
--- sandbox/logging/boost/logging/detail/raw_doc/table_of_contents.hpp (original)
+++ sandbox/logging/boost/logging/detail/raw_doc/table_of_contents.hpp 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -32,6 +32,7 @@
 - @ref headers_to_include "Headers to #include"
 - @ref defining_your_logger_filter
 - @ref scenario::usage "Choose the best filter/logger class, based on your application's needs"
+- @ref scoped_logs
 - @ref tag "Using tags"
 - @ref thread_safety
 - @ref macros

Modified: sandbox/logging/boost/logging/detail/raw_doc/todo.hpp
==============================================================================
--- sandbox/logging/boost/logging/detail/raw_doc/todo.hpp (original)
+++ sandbox/logging/boost/logging/detail/raw_doc/todo.hpp 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -88,6 +88,13 @@
 - @c must_have each formatter - should have a "syntax" - like: idx("[%] ") - write [idx]<space>; idx("{%} ") - write {idx}<space>
   Or, have a formatter that can call other formatters, like format("[%1] [%2] {%3}"). Perhaps for starters, have a wrapper class
   which calls original formatter and then wrappers?
+ actually - have a formatter that has a map of other formatters (std::string -> formatter) - THEN you can string at runtime.
+ we should have the same for destinations as well TOTHINK how do we initialize them? like - a file destination - how can i specify the file name?
+ have a spacer formatter which writes before & after - some text.
+ have a % formatter taht can write more formatters at the same time %1 %2- message %3
+ have a named formatter that can write more formatters at the same time, but based on name - make it easy to use, and somehow the default should
+ encompass all existing formatters TOTHINK the problem is still setting params at the constructor of certain objects - like - time(%m:%s) etc.
+ we should make it as easy as possible; perhaps make it very easy for named formatter - TOTHINK
 
 - @c high new formatter - write a more "exact" time - up to the millisec or so; should use Boost.DateTime for higher-resolution timestamps.
 

Modified: sandbox/logging/boost/logging/detail/scoped_log.hpp
==============================================================================
--- sandbox/logging/boost/logging/detail/scoped_log.hpp (original)
+++ sandbox/logging/boost/logging/detail/scoped_log.hpp 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -22,6 +22,7 @@
 #endif
 
 #include <boost/logging/detail/fwd.hpp>
+#include <algorithm>
 
 namespace boost { namespace logging {
 
@@ -52,18 +53,34 @@
 #endif
 
 
+namespace detail {
+ template<class writer> struct scoped_writer {
+ scoped_writer(const writer & w) : m_w( w ) {}
+ template<class msg_type> void operator()(msg_type & msg) const {
+ m_w.gather_msg(msg);
+ }
+ private:
+ const writer &m_w;
+ };
+}
 
-
-
-#define BOOST_SCOPED_LOG_CTX(logger)
+#define BOOST_SCOPED_LOG_CTX_IMPL(logger_macro, operator_, class_name) \
 struct class_name { \
- class_name() { logger ( "start of " msg ) ;} \
- ~class_name() { logger ( " end of " msg ) ; } \
- boost::logging::logger<default_,
-} BOOST_LOG_CONCATENATE(log_, __LINE__);
+ typedef ::boost::logging::hold_string_type string_type; \
+ class_name() { } \
+ ~class_name() { logger_macro operator_ BOOST_LOG_STR(" end of ") operator_ m_str ; } \
+ void gather_msg(string_type & str) const { std::swap(m_str, str); logger_macro operator_ BOOST_LOG_STR("start of ") operator_ m_str ; } \
+ mutable string_type m_str; \
+} BOOST_LOG_CONCATENATE(log_, __LINE__); \
+::boost::logging::logger< ::boost::logging::gather::ostream_like::return_str< ::boost::logging::hold_string_type > , ::boost::logging::detail::scoped_writer< class_name > > \
+ BOOST_LOG_CONCATENATE(scoped_log_val, __LINE__) ( BOOST_LOG_CONCATENATE(log_, __LINE__) ); \
+ BOOST_LOG_CONCATENATE(scoped_log_val, __LINE__) .read_msg().gather().out()
 
 
 
+// note: to use BOOST_SCOPED_LOG_CTX, you need to #include <boost/logging/gather/ostream_like.hpp>
+// This is included by default, in #include <boost/logging/format_fwd.hpp>
+#define BOOST_SCOPED_LOG_CTX(logger) BOOST_SCOPED_LOG_CTX_IMPL(logger, << , BOOST_LOG_CONCATENATE(boost_scoped_log,__LINE__) )
 
 
 }}

Modified: sandbox/logging/lib/logging/internal/vc8/loggingvc8/loggingvc8.vcproj
==============================================================================
--- sandbox/logging/lib/logging/internal/vc8/loggingvc8/loggingvc8.vcproj (original)
+++ sandbox/logging/lib/logging/internal/vc8/loggingvc8/loggingvc8.vcproj 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -686,6 +686,10 @@
>
                         </File>
                         <File
+ RelativePath="..\..\..\..\..\boost\logging\detail\raw_doc\scoped_logs.hpp"
+ >
+ </File>
+ <File
                                 RelativePath="..\..\..\..\..\boost\logging\detail\raw_doc\starter_project.hpp"
>
                         </File>

Modified: sandbox/logging/lib/logging/internal/vc8/loggingvc8/test_now.cpp
==============================================================================
--- sandbox/logging/lib/logging/internal/vc8/loggingvc8/test_now.cpp (original)
+++ sandbox/logging/lib/logging/internal/vc8/loggingvc8/test_now.cpp 2007-11-25 23:11:26 EST (Sun, 25 Nov 2007)
@@ -13,6 +13,9 @@
 #include <boost/logging/writer/ts_write.hpp>
 #include <boost/logging/format/formatter/tags.hpp>
 
+#include <boost/logging/detail/scoped_log.hpp>
+
+
 using namespace boost::logging;
 
 // Step 3 : Specify your logging class(es)
@@ -50,9 +53,9 @@
   // 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::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_if_needed() ); //(2a)
   g_l->writer().add_formatter( formatter::append_newline() ); //(2b)
 
   // ... and where should it be written to
@@ -65,23 +68,21 @@
  */ g_l->writer().add_destination( destination::file("out.txt") );
 }
 
-#define BOOST_LOG_USEASINT2(x) #x
-#define BOOST_LOG_USEASINT(x) BOOST_LOG_USEASINT2(x)
-
-#define BOOST_SCOPED_LOG_CTX(logger)
 
-/*
-#define BOOST_LOG_HOLDER2(x) x, L ## x
-#define BOOST_LOG_HOLDER(x) BOOST_LOG_HOLDER2(x)
-#define BOOST_LOG_STRTEST(x) ( BOOST_LOG_HOLDER(x) )
-*/
 
 void test(int a, const char * str) {
 // BOOST_SCOPED_LOG(LDBG, "testing inout" << a << str );
+ std::string s;
     BOOST_SCOPED_LOG(LDBG << , "testing inout" );
- BOOST_SCOPED_LOG(LDBG << , "testing inout2" );
+// BOOST_SCOPED_LOG(LDBG << , "testing inout2" );
     int i = 1;
- LDBG << "this is so cool " << i++ << "\n";
+// LDBG << "this is so cool " << i++ << "\n";
+}
+
+
+void test2(int a, const char * str) {
+ BOOST_SCOPED_LOG_CTX(LDBG) << "test21-" << a << ", str=" << str;
+ BOOST_SCOPED_LOG_CTX(LDBG) << "test22-" << a << ", str=" << str;
 }
 
 void your_scenario_example() {
@@ -89,6 +90,7 @@
     init_logs();
 
     test(5, "cucu");
+ test2(5, "cucu2");
     // Step 8: use it...
     int i = 1;
     LDBG << "the end" << i++ << "\n";


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