[cig-commits] r12944 - in cs/cigma/trunk/src: . contrib contrib/cxxtl contrib/cxxtl/example
luis at geodynamics.org
luis at geodynamics.org
Wed Sep 24 04:10:30 PDT 2008
Author: luis
Date: 2008-09-24 04:10:29 -0700 (Wed, 24 Sep 2008)
New Revision: 12944
Added:
cs/cigma/trunk/src/contrib/
cs/cigma/trunk/src/contrib/cxxtl/
cs/cigma/trunk/src/contrib/cxxtl/AUTHORS
cs/cigma/trunk/src/contrib/cxxtl/ChangeLog
cs/cigma/trunk/src/contrib/cxxtl/LICENSE_1_0.txt
cs/cigma/trunk/src/contrib/cxxtl/Makefile.add
cs/cigma/trunk/src/contrib/cxxtl/TODO
cs/cigma/trunk/src/contrib/cxxtl/example/
cs/cigma/trunk/src/contrib/cxxtl/example/Makefile
cs/cigma/trunk/src/contrib/cxxtl/example/foo.cpp
cs/cigma/trunk/src/contrib/cxxtl/example/main.cpp
cs/cigma/trunk/src/contrib/cxxtl/example/nullstream.hpp
cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.cpp
cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.hpp
cs/cigma/trunk/src/contrib/cxxtl/nullstream.hpp
cs/cigma/trunk/src/contrib/cxxtl/tri_logger.cpp
cs/cigma/trunk/src/contrib/cxxtl/tri_logger.hpp
cs/cigma/trunk/src/nullstream.hpp
cs/cigma/trunk/src/tri_logger.cpp
cs/cigma/trunk/src/tri_logger.hpp
Log:
Use CxxTL for logging
Added: cs/cigma/trunk/src/contrib/cxxtl/AUTHORS
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/AUTHORS (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/AUTHORS 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1 @@
+Seweryn Habdank-Wojewódzki <habdank (AT) gmail (DOT) com>
Added: cs/cigma/trunk/src/contrib/cxxtl/ChangeLog
===================================================================
Added: cs/cigma/trunk/src/contrib/cxxtl/LICENSE_1_0.txt
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/LICENSE_1_0.txt (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/LICENSE_1_0.txt 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,23 @@
+Boost Software License - Version 1.0 - August 17th, 2003
+
+Permission is hereby granted, free of charge, to any person or organization
+obtaining a copy of the software and accompanying documentation covered by
+this license (the "Software") to use, reproduce, display, distribute,
+execute, and transmit the Software, and to prepare derivative works of the
+Software, and to permit third-parties to whom the Software is furnished to
+do so, all subject to the following:
+
+The copyright notices in the Software and this entire statement, including
+the above license grant, this restriction and the following disclaimer,
+must be included in all copies of the Software, in whole or in part, and
+all derivative works of the Software, unless such copies or derivative
+works are solely in the form of machine-executable object code generated by
+a source language processor.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
Added: cs/cigma/trunk/src/contrib/cxxtl/Makefile.add
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/Makefile.add (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/Makefile.add 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,8 @@
+
+# C++ compile flags
+#CFLAGS+=-DFTLOG
+CFLAGS+=-DFTLOG="${PROG}"
+#CFLAGS+=-DETLOG
+#CFLAGS+=-DTLOG
+#CFLAGS+=-DCLEANTLOG
+
Added: cs/cigma/trunk/src/contrib/cxxtl/TODO
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/TODO (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/TODO 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,2 @@
+#SHW 18-02-2007: Extend functionality of the switching of the logger streams
+
Added: cs/cigma/trunk/src/contrib/cxxtl/example/Makefile
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/Makefile (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/Makefile 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,47 @@
+
+# Target binaries
+PROG=test
+
+# Compiler
+GCC=g++
+
+# C++ compile flags
+#CFLAGS+=-DFTLOG
+#CFLAGS+=-DFTLOG="${PROG}"
+CFLAGS+=-DETLOG
+#CFLAGS+=-DTLOG
+#CFLAGS+=-DCLEANTLOG
+
+CC=${GCC} -c ${INCLUDE} ${CFLAGS} ${GFLAGS}
+LINK=${GCC} ${GFLAGS} ${LFLAGS}
+
+.PHONY: program
+program: ${PROG}
+
+all: ${PROG}
+
+${PROG}: foo.o main.o tri_logger.o
+ @ echo "LINK ->" $@.debug
+ @ ${LINK} foo.o main.o tri_logger.o -o $@.debug
+ @ echo "STRIP ->" $@
+ @ strip -R .comment -R .note -R .note.ABI-tag -s $@.debug -o $@
+
+foo.o: foo.cpp
+ @ echo COMPILE $<
+ @ ${CC} $<
+
+main.o: main.cpp
+ @ echo COMPILE $<
+ @ ${CC} $<
+
+tri_logger.o: tri_logger.cpp
+ @ echo COMPILE $<
+ @ ${CC} $<
+
+clean:
+ @ echo CLEAN *.o
+ @ rm -f -v *.o
+ @ echo CLEAN ${PROG}
+ @ rm -f -v ${PROG}
+ @ rm -f -v ${PROG}.debug
+
Added: cs/cigma/trunk/src/contrib/cxxtl/example/foo.cpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/foo.cpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/foo.cpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,17 @@
+#include "tri_logger.hpp"
+
+struct Foo
+{
+ Foo()
+ {
+ TRI_LOG_STR ( "Creation of the foo object." );
+ }
+
+ ~Foo()
+ {
+ TRI_LOG_STR ( "Destruction of the foo object." );
+ }
+};
+
+Foo foo;
+
Added: cs/cigma/trunk/src/contrib/cxxtl/example/main.cpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/main.cpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/main.cpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,60 @@
+#include <string>
+#include <utility>
+#include <iostream>
+#include <sstream>
+#include <stdexcept>
+#include <cstdlib>
+
+#include "tri_logger.hpp"
+
+void finalize()
+{
+ TRI_LOG_STR("atexit()->finalize()->EXIT_SUCCESS");
+}
+
+template < typename T, typename U >
+std::ostream & operator<< ( std::ostream & os, std::pair < T, U > const & p )
+{
+ os << "( " << p.first << ", " << p.second << " )";
+ return os;
+}
+
+int main ()
+{
+ std::atexit(finalize);
+ double const a = 1.0;
+ std::string const str = "test";
+ std::pair < std::string, double > p ( "PI", 3.1415 );
+ std::pair < unsigned long, std::string > q ( 10, "EUR" );
+
+ TRI_LOG_STR ("Trivial logger in main().");
+ TRI_LOG (a);
+ TRI_LOG (str);
+ TRI_LOG (p);
+ if ( 1 ) TRI_LOG (q); else TRI_LOG(str);
+
+ TRI_LOG_OFF();
+ TRI_LOG_STR ("Trivial logger in main().");
+
+ TRI_LOG_ON();
+ TRI_LOG_STR("Test of the TRI_LOG_FN macro.");
+ TRI_LOG_FN (str);
+
+ //trivial_logger::tri_logger().set_out_stream ( std::cout );
+
+ //TRI_LOG (p);
+
+ //trivial_logger::tri_logger().set_out_stream ( std::cerr );
+
+ //TRI_LOG (q);
+
+ //std::auto_ptr<std::ostringstream> sstr_ptr ( new std::ostringstream );
+
+ //trivial_logger::tri_logger().set_out_stream ( sstr_ptr );
+
+ //TRI_LOG_STR ( "Logs in string stream" );
+
+ //std::cout << "String stream " << (dynamic_cast<std::ostringstream*>((trivial_logger::tri_logger().ostream_ptr())))->str() << std::endl;
+
+}
+
Added: cs/cigma/trunk/src/contrib/cxxtl/example/nullstream.hpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/nullstream.hpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/nullstream.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,58 @@
+//
+// Copyright (c) 2006 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+
+//
+// Copyright Maciej Sobczak, 2002
+//
+// Permission to copy, use, modify, sell and distribute this software
+// is granted provided this copyright notice appears in all copies.
+// This software is provided "as is" without express or implied
+// warranty, and with no claim as to its suitability for any purpose.
+//
+
+#ifndef NULLSTREAM_HPP_INCLUDED
+#define NULLSTREAM_HPP_INCLUDED 1
+
+#include <streambuf>
+#include <ostream>
+
+// null stream buffer class
+template <class charT, class traits = ::std::char_traits<charT> >
+class null_buffer : public ::std::basic_streambuf<charT, traits>
+{
+public:
+ typedef typename ::std::basic_streambuf<charT, traits>::int_type int_type;
+
+ null_buffer() {}
+
+private:
+ virtual int_type overflow(int_type c)
+ {
+ // just ignore the character
+ return traits::not_eof(c);
+ }
+};
+
+// generic null output stream class
+template <class charT, class traits = ::std::char_traits<charT> >
+class basic_null_stream
+ : private null_buffer<charT, traits>,
+ public ::std::basic_ostream<charT, traits>
+{
+public:
+ basic_null_stream()
+ : ::std::basic_ostream<charT, traits>(this)
+ {
+ }
+};
+
+// helper declarations for narrow and wide streams
+typedef basic_null_stream<char> null_stream;
+typedef basic_null_stream<wchar_t> null_wstream;
+
+#endif // NULLSTREAM_HPP_INCLUDED
Added: cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.cpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.cpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.cpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,288 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#include "tri_logger.hpp"
+#include <memory>
+
+#if !defined(CLEANTLOG)
+
+#if defined (FTLOG)
+#include <fstream>
+
+#else
+#include <iostream>
+#include "nullstream.hpp"
+#endif
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ struct tri_logger_impl
+ {
+ // true if logger is activated
+ static bool is_activated_;
+
+ // auto pointer helps manage resources;
+ static ::std::auto_ptr < ::std::ostream > outstream_helper_ptr_;
+
+ // pointer to the output stream of the logger
+ static ::std::ostream * outstream_;
+
+ //static NullStream null_stream_;
+
+ };
+
+ // activate logger by default
+ bool tri_logger_impl::is_activated_ = true;
+
+ }
+
+ std::auto_ptr<detail::tri_logger_impl>
+ tri_logger_t::tl_impl ( std::auto_ptr<detail::tri_logger_impl> ( new detail::tri_logger_impl ) );
+
+ tri_logger_t::tri_logger_t()
+ {}
+
+ tri_logger_t::~tri_logger_t()
+ {}
+
+ bool tri_logger_t::is_activated()
+ {
+ return tl_impl->is_activated_;
+ }
+
+ void tri_logger_t::activate ( bool const activate )
+ {
+ tl_impl->is_activated_ = activate;
+ }
+
+ ::std::ostream *& tri_logger_t::ostream_ptr()
+ {
+ return tl_impl->outstream_;
+ }
+
+ //void tri_logger_t::set_out_stream (
+ // ::std::auto_ptr<::std::ostream > ostream_ptr )
+ //{
+ //outstream_helper_ptr
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * & ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * const ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream & ostream )
+ //{
+ //outstream_helper_ptr_ = ::std::auto_ptr<::std::ostream>( new null_stream );
+ // outstream_ = &ostream;
+ //}
+
+ #if defined(TLOG)
+ // set auto pointer to the null stream
+ // reason: ::std::cout can not be created in runtime, so
+ // the auto pointer has nothing to do with its resources
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cout;
+
+ #elif defined (ETLOG)
+ // look to the TLOG comments
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cerr;
+
+ #elif defined (FTLOG)
+
+ #include <cctype>
+
+ #define XSTR(s) STR(s)
+ #define STR(s) #s
+
+ #define MAX(x,y) ( (x) < (y) ? (y) : (x) )
+
+ namespace detail
+ {
+ /**
+ * Function paste rhs C string to the lhs C string.
+ lhs should be long enough for that operation.
+ Additionally coping is stared from the point which
+ points lhs.
+ */
+ template < typename Char_type >
+ size_t const str_cat ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ return len;
+ }
+
+ /// Function calculates length of C string
+ /// It can be used with wide characters
+ template < typename Char_type >
+ size_t const str_len ( Char_type const * str )
+ {
+ size_t len = 0;
+ while ( *str != '\0' )
+ {
+ ++str;
+ ++len;
+ }
+ return len;
+ }
+
+ /**
+ * Function copy rhs C string in to the lhs.
+ * It do not check size of target C string
+ * It starts to copy from the beginning of the C string,
+ * but it begins put characters at the point where lhs points,
+ * so there can be a problem when lhs points on the end of lhs
+ * C string.
+ */
+ template < typename Char_type >
+ size_t const str_cpy ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ *lhs = '\0';
+ return len+1;
+ }
+
+ /**
+ * Function converts existing file name to the file name
+ * which has no non-printable signs and
+ * at the end is added extension.
+ * The space sign in file name is converted to the underscore.
+ * Lengths of C strings has to be proper.
+ */
+ template < typename Char_type >
+ size_t const
+ createfilename ( Char_type * result,
+ Char_type const * filename,
+ Char_type const * ext,
+ Char_type const * helper )
+ {
+ size_t len = 0;
+ if ( str_len ( filename ) > 1 )
+ {
+ while ( *filename != '\0' )
+ {
+ // check if characters have grapnical
+ // reprasentation
+ if ( 0 != ::std::isgraph ( *filename ) )
+ {
+ *result = *filename;
+ ++result;
+ ++len;
+ }
+ else
+ {
+ // convert space to underscore
+ if ( *filename == ' ' )
+ {
+ *result = '_';
+ ++result;
+ ++len;
+ }
+ }
+ ++filename;
+ }
+ // add extension
+ str_cat ( result, ext );
+ *result = '\0';
+ }
+ else
+ {
+ result = &result[0];
+ len = str_cpy ( result, helper );
+ }
+ return len;
+ }
+
+ template < typename T >
+ T const max ( T const x, T const y )
+ {
+ return ( x < y ? y : x );
+ }
+
+ } // namespace detail
+
+ typedef char char_type;
+
+ // convert definition of the TLOGFILE
+ // to the C string
+ char_type const tlogfilename[] = XSTR(FTLOG);
+
+ // extension C string
+ char_type const ext[] = ".log";
+
+ char_type const helper_name[] = "_logger.log";
+
+ // container for final file name
+ char_type filename[(MAX(sizeof(tlogfilename),sizeof(helper_name))+sizeof(ext))/sizeof(char_type)];
+ // create file name
+ size_t const len = detail::createfilename ( filename, tlogfilename, ext, helper_name );
+
+ #undef MAX
+ #undef STR
+ #undef XSTR
+
+ // new file is opened and its destruction is managed by auto_ptr
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream >
+ ( new ::std::ofstream ( filename ));
+ // set pointer output stream
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ // here is a place for user defined output stream and flag
+
+ // ...
+
+ #else
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ #endif
+
+ ::std::auto_ptr<tri_logger_t> detail::tri_logger_out_ptr ( new tri_logger_t() );
+
+ tri_logger_t& tri_logger()
+ {
+ //static tri_logger_t * ans = new tri_logger_t();
+ return *detail::tri_logger_out_ptr;
+ //return *ans;
+ }
+
+} // namespace trivial_logger
+
+#endif // !CLEANTLOG
+
Added: cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.hpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.hpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/example/tri_logger.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,148 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#ifndef TRI_LOGGER_HPP_INCLUDED
+#define TRI_LOGGER_HPP_INCLUDED 1
+
+#include <ostream>
+#include <memory>
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ class tri_logger_impl;
+ }
+
+ // main trivial logger class
+ class tri_logger_t
+ {
+ public:
+ tri_logger_t ();
+ ~tri_logger_t ();
+
+ /// return true if logger is activated
+ static bool is_activated();
+
+ /// activate and deactivate logger
+ static void activate ( bool const activated );
+
+ /// return reference to pointer to output stream
+ static ::std::ostream *& ostream_ptr();
+
+ // experimental methods crashes code if resource is released
+ // before finishing of work of logger
+
+ // static void set_out_stream ( ::std::ostream * const ostream );
+ // static void set_out_stream ( ::std::ostream & ostream );
+ // static void set_out_stream ( ::std::auto_ptr<::std::ostream > ostream_ptr );
+ // static void set_out_stream ( ::std::ostream * & ostream );
+ private:
+
+ static std::auto_ptr<detail::tri_logger_impl> tl_impl;
+
+ // in this design logger should be noncopyable
+ tri_logger_t ( tri_logger_t const & );
+ tri_logger_t & operator= ( tri_logger_t const & );
+
+ };
+
+ // important funtion which helps solves
+ // "static initialisation fiasco" problem
+ // see:
+ // 1. S. Habdank-Wojewodzki, "C++ Trivial Logger", Overload 77, Feb 2007, pp.19-23
+ // 2. http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13
+ // present solution is much better
+ extern tri_logger_t & tri_logger();
+ //extern ::std::auto_ptr < tri_logger_t > tri_logger();
+
+ namespace detail
+ {
+ //extern tri_logger_t * tri_logger_out_ptr;
+ extern ::std::auto_ptr<tri_logger_t> tri_logger_out_ptr;
+ }
+
+} // namespace trival_logger
+
+// macro prints variable name and its value to the logger stream
+#define TRI_LOG(name) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << #name \
+ << " = " << (name) << ::std::endl;} }while(false)
+
+// macro prints value of the variable to the logger stream
+// useful for printing constant strings
+#define TRI_LOG_STR(str) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << str \
+ << ::std::endl;} }while(false)
+
+// namespace for the trivial logger
+namespace trivial_logger
+{
+ // example how to create functions which operates on logger stream
+ // here are used templates for preparing function which is independent
+ // on the type, but this is not required
+ template < typename T1, typename T2, typename T3, typename T4 >
+ void put_debug_info ( tri_logger_t & log,
+ T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4 )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t1 << " (" << t2 << ") : ";
+ *(log.ostream_ptr()) << t3 << " = " << t4 << ::std::endl;
+ }
+ }
+
+ template < typename T >
+ void put_log_info ( tri_logger_t & log,
+ T const & t )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t << ::std::endl;
+ }
+ }
+} // namespace trivial_logger
+
+// macro shows how to write macros which using user-defined functions
+#define TRI_LOG_FN(name) \
+ ::trivial_logger::put_debug_info ( trivial_logger::tri_logger(),\
+ __FILE__, __LINE__, #name, (name) )
+
+// below is a place for user defined logger formating data
+
+// ...
+
+#define TRI_LOG_INFO(name) \
+ ::trivial_logger::put_log_info ( trivial_logger::tri_logger(), (name) )
+
+// macros for switching off and on logger
+#define TRI_LOG_ON() \
+ do { ::trivial_logger::tri_logger().activate ( true ); } while(false)
+#define TRI_LOG_OFF() \
+ do { ::trivial_logger::tri_logger().activate ( false ); } while(false)
+
+#if defined(CLEANTLOG)
+#undef TRI_LOG
+#undef TRI_LOG_ON
+#undef TRI_LOG_OFF
+#undef TRI_LOG_FN
+#undef TRI_LOG_STR
+#undef TRI_LOG_INFO
+#define TRI_LOG(name) do{}while(false)
+#define TRI_LOG_FN(name) do{}while(false)
+#define TRI_LOG_ON() do{}while(false)
+#define TRI_LOG_OFF() do{}while(false)
+#define TRI_LOG_STR(str) do{}while(false)
+#define TRI_LOG_INFO(str) do{}while(false)
+#endif
+
+#endif // TRI_LOGGER_HPP_INCLUDED
+
Added: cs/cigma/trunk/src/contrib/cxxtl/nullstream.hpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/nullstream.hpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/nullstream.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,58 @@
+//
+// Copyright (c) 2006 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+
+//
+// Copyright Maciej Sobczak, 2002
+//
+// Permission to copy, use, modify, sell and distribute this software
+// is granted provided this copyright notice appears in all copies.
+// This software is provided "as is" without express or implied
+// warranty, and with no claim as to its suitability for any purpose.
+//
+
+#ifndef NULLSTREAM_HPP_INCLUDED
+#define NULLSTREAM_HPP_INCLUDED 1
+
+#include <streambuf>
+#include <ostream>
+
+// null stream buffer class
+template <class charT, class traits = ::std::char_traits<charT> >
+class null_buffer : public ::std::basic_streambuf<charT, traits>
+{
+public:
+ typedef typename ::std::basic_streambuf<charT, traits>::int_type int_type;
+
+ null_buffer() {}
+
+private:
+ virtual int_type overflow(int_type c)
+ {
+ // just ignore the character
+ return traits::not_eof(c);
+ }
+};
+
+// generic null output stream class
+template <class charT, class traits = ::std::char_traits<charT> >
+class basic_null_stream
+ : private null_buffer<charT, traits>,
+ public ::std::basic_ostream<charT, traits>
+{
+public:
+ basic_null_stream()
+ : ::std::basic_ostream<charT, traits>(this)
+ {
+ }
+};
+
+// helper declarations for narrow and wide streams
+typedef basic_null_stream<char> null_stream;
+typedef basic_null_stream<wchar_t> null_wstream;
+
+#endif // NULLSTREAM_HPP_INCLUDED
Added: cs/cigma/trunk/src/contrib/cxxtl/tri_logger.cpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/tri_logger.cpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/tri_logger.cpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,289 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#include "tri_logger.hpp"
+#include <memory>
+
+#if !defined(CLEANTLOG)
+
+#if defined (FTLOG)
+#include <fstream>
+
+#else
+#include <iostream>
+#include "nullstream.hpp"
+#endif
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ struct tri_logger_impl
+ {
+ // true if logger is activated
+ static bool is_activated_;
+
+ // auto pointer helps manage resources;
+ static ::std::auto_ptr < ::std::ostream > outstream_helper_ptr_;
+
+ // pointer to the output stream of the logger
+ static ::std::ostream * outstream_;
+
+ //static NullStream null_stream_;
+
+ };
+
+ // activate logger by default
+ bool tri_logger_impl::is_activated_ = true;
+
+ }
+
+ std::auto_ptr<detail::tri_logger_impl>
+ tri_logger_t::tl_impl ( std::auto_ptr<detail::tri_logger_impl> ( new detail::tri_logger_impl ) );
+
+ tri_logger_t::tri_logger_t()
+ {}
+
+ tri_logger_t::~tri_logger_t()
+ {}
+
+ bool tri_logger_t::is_activated()
+ {
+ return tl_impl->is_activated_;
+ }
+
+ void tri_logger_t::activate ( bool const activate )
+ {
+ tl_impl->is_activated_ = activate;
+ }
+
+ ::std::ostream *& tri_logger_t::ostream_ptr()
+ {
+ return tl_impl->outstream_;
+ }
+
+ //void tri_logger_t::set_out_stream (
+ // ::std::auto_ptr<::std::ostream > ostream_ptr )
+ //{
+ //outstream_helper_ptr
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * & ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * const ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream & ostream )
+ //{
+ //outstream_helper_ptr_ = ::std::auto_ptr<::std::ostream>( new null_stream );
+ // outstream_ = &ostream;
+ //}
+
+ #if defined(TLOG)
+ // set auto pointer to the null stream
+ // reason: ::std::cout can not be created in runtime, so
+ // the auto pointer has nothing to do with its resources
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cout;
+
+ #elif defined (ETLOG)
+ // look to the TLOG comments
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cerr;
+
+ #elif defined (FTLOG)
+
+ #include <cctype>
+
+ #define XSTR(s) STR(s)
+ #define STR(s) #s
+
+ #define MAX(x,y) ( (x) < (y) ? (y) : (x) )
+
+ namespace detail
+ {
+ /**
+ * Function paste rhs C string to the lhs C string.
+ lhs should be long enough for that operation.
+ Additionally coping is stared from the point which
+ points lhs.
+ */
+ template < typename Char_type >
+ size_t const str_cat ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ return len;
+ }
+
+ /// Function calculates length of C string
+ /// It can be used with wide characters
+ template < typename Char_type >
+ size_t const str_len ( Char_type const * str )
+ {
+ size_t len = 0;
+ while ( *str != '\0' )
+ {
+ ++str;
+ ++len;
+ }
+ return len;
+ }
+
+ /**
+ * Function copy rhs C string in to the lhs.
+ * It do not check size of target C string
+ * It starts to copy from the beginning of the C string,
+ * but it begins put characters at the point where lhs points,
+ * so there can be a problem when lhs points on the end of lhs
+ * C string.
+ */
+ template < typename Char_type >
+ size_t const str_cpy ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ *lhs = '\0';
+ return len+1;
+ }
+
+ /**
+ * Function converts existing file name to the file name
+ * which has no non-printable signs and
+ * at the end is added extension.
+ * The space sign in file name is converted to the underscore.
+ * Lengths of C strings has to be proper.
+ */
+ template < typename Char_type >
+ size_t const
+ createfilename ( Char_type * result,
+ Char_type const * filename,
+ Char_type const * ext,
+ Char_type const * helper )
+ {
+ size_t len = 0;
+ if ( str_len ( filename ) > 1 )
+ {
+ while ( *filename != '\0' )
+ {
+ // check if characters have grapnical
+ // reprasentation
+ if ( 0 != ::std::isgraph ( *filename ) )
+ {
+ *result = *filename;
+ ++result;
+ ++len;
+ }
+ else
+ {
+ // convert space to underscore
+ if ( *filename == ' ' )
+ {
+ *result = '_';
+ ++result;
+ ++len;
+ }
+ }
+ ++filename;
+ }
+ // add extension
+ str_cat ( result, ext );
+ *result = '\0';
+ }
+ else
+ {
+ result = &result[0];
+ len = str_cpy ( result, helper );
+ }
+ return len;
+ }
+
+ template < typename T >
+ T const max ( T const x, T const y )
+ {
+ return ( x < y ? y : x );
+ }
+
+ } // namespace detail
+
+ typedef char char_type;
+
+ // convert definition of the TLOGFILE
+ // to the C string
+ char_type const tlogfilename[] = XSTR(FTLOG);
+
+ // extension C string
+ char_type const ext[] = ".log";
+
+ char_type const helper_name[] = "_logger.log";
+
+ // container for final file name
+ char_type filename[(MAX(sizeof(tlogfilename),sizeof(helper_name))+sizeof(ext))/sizeof(char_type)];
+ // create file name
+ size_t const len = detail::createfilename ( filename, tlogfilename, ext, helper_name );
+
+ #undef MAX
+ #undef STR
+ #undef XSTR
+
+ // new file is opened and its destruction is managed by auto_ptr
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream >
+ ( new ::std::ofstream ( filename ));
+ // set pointer output stream
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ // here is a place for user defined output stream and flag
+
+ // ...
+
+ #else
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ #endif
+
+ ::std::auto_ptr<tri_logger_t> detail::tri_logger_out_ptr ( new tri_logger_t() );
+
+ tri_logger_t& tri_logger()
+ {
+ //static tri_logger_t * ans = new tri_logger_t();
+ static ::std::auto_ptr<tri_logger_t> ans ( new tri_logger_t() );
+ return *ans;
+ //return *ans;
+ }
+
+} // namespace trivial_logger
+
+#endif // !CLEANTLOG
+
Added: cs/cigma/trunk/src/contrib/cxxtl/tri_logger.hpp
===================================================================
--- cs/cigma/trunk/src/contrib/cxxtl/tri_logger.hpp (rev 0)
+++ cs/cigma/trunk/src/contrib/cxxtl/tri_logger.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,148 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#ifndef TRI_LOGGER_HPP_INCLUDED
+#define TRI_LOGGER_HPP_INCLUDED 1
+
+#include <ostream>
+#include <memory>
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ class tri_logger_impl;
+ }
+
+ // main trivial logger class
+ class tri_logger_t
+ {
+ public:
+ tri_logger_t ();
+ ~tri_logger_t ();
+
+ /// return true if logger is activated
+ static bool is_activated();
+
+ /// activate and deactivate logger
+ static void activate ( bool const activated );
+
+ /// return reference to pointer to output stream
+ static ::std::ostream *& ostream_ptr();
+
+ // experimental methods crashes code if resource is released
+ // before finishing of work of logger
+
+ // static void set_out_stream ( ::std::ostream * const ostream );
+ // static void set_out_stream ( ::std::ostream & ostream );
+ // static void set_out_stream ( ::std::auto_ptr<::std::ostream > ostream_ptr );
+ // static void set_out_stream ( ::std::ostream * & ostream );
+ private:
+
+ static std::auto_ptr<detail::tri_logger_impl> tl_impl;
+
+ // in this design logger should be noncopyable
+ tri_logger_t ( tri_logger_t const & );
+ tri_logger_t & operator= ( tri_logger_t const & );
+
+ };
+
+ // important funtion which helps solves
+ // "static initialisation fiasco" problem
+ // see:
+ // 1. S. Habdank-Wojewodzki, "C++ Trivial Logger", Overload 77, Feb 2007, pp.19-23
+ // 2. http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13
+ // present solution is much better
+ extern tri_logger_t & tri_logger();
+ //extern ::std::auto_ptr < tri_logger_t > tri_logger();
+
+ namespace detail
+ {
+ //extern tri_logger_t * tri_logger_out_ptr;
+ extern ::std::auto_ptr<tri_logger_t> tri_logger_out_ptr;
+ }
+
+} // namespace trival_logger
+
+// macro prints variable name and its value to the logger stream
+#define TRI_LOG(name) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << #name \
+ << " = " << (name) << ::std::endl;} }while(false)
+
+// macro prints value of the variable to the logger stream
+// useful for printing constant strings
+#define TRI_LOG_STR(str) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << str \
+ << ::std::endl;} }while(false)
+
+// namespace for the trivial logger
+namespace trivial_logger
+{
+ // example how to create functions which operates on logger stream
+ // here are used templates for preparing function which is independent
+ // on the type, but this is not required
+ template < typename T1, typename T2, typename T3, typename T4 >
+ void put_debug_info ( tri_logger_t & log,
+ T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4 )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t1 << " (" << t2 << ") : ";
+ *(log.ostream_ptr()) << t3 << " = " << t4 << ::std::endl;
+ }
+ }
+
+ template < typename T >
+ void put_log_info ( tri_logger_t & log,
+ T const & t )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t << ::std::endl;
+ }
+ }
+} // namespace trivial_logger
+
+// macro shows how to write macros which using user-defined functions
+#define TRI_LOG_FN(name) \
+ ::trivial_logger::put_debug_info ( trivial_logger::tri_logger(),\
+ __FILE__, __LINE__, #name, (name) )
+
+// below is a place for user defined logger formating data
+
+// ...
+
+#define TRI_LOG_INFO(name) \
+ ::trivial_logger::put_log_info ( trivial_logger::tri_logger(), (name) )
+
+// macros for switching off and on logger
+#define TRI_LOG_ON() \
+ do { ::trivial_logger::tri_logger().activate ( true ); } while(false)
+#define TRI_LOG_OFF() \
+ do { ::trivial_logger::tri_logger().activate ( false ); } while(false)
+
+#if defined(CLEANTLOG)
+#undef TRI_LOG
+#undef TRI_LOG_ON
+#undef TRI_LOG_OFF
+#undef TRI_LOG_FN
+#undef TRI_LOG_STR
+#undef TRI_LOG_INFO
+#define TRI_LOG(name) do{}while(false)
+#define TRI_LOG_FN(name) do{}while(false)
+#define TRI_LOG_ON() do{}while(false)
+#define TRI_LOG_OFF() do{}while(false)
+#define TRI_LOG_STR(str) do{}while(false)
+#define TRI_LOG_INFO(str) do{}while(false)
+#endif
+
+#endif // TRI_LOGGER_HPP_INCLUDED
+
Added: cs/cigma/trunk/src/nullstream.hpp
===================================================================
--- cs/cigma/trunk/src/nullstream.hpp (rev 0)
+++ cs/cigma/trunk/src/nullstream.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,58 @@
+//
+// Copyright (c) 2006 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+
+//
+// Copyright Maciej Sobczak, 2002
+//
+// Permission to copy, use, modify, sell and distribute this software
+// is granted provided this copyright notice appears in all copies.
+// This software is provided "as is" without express or implied
+// warranty, and with no claim as to its suitability for any purpose.
+//
+
+#ifndef NULLSTREAM_HPP_INCLUDED
+#define NULLSTREAM_HPP_INCLUDED 1
+
+#include <streambuf>
+#include <ostream>
+
+// null stream buffer class
+template <class charT, class traits = ::std::char_traits<charT> >
+class null_buffer : public ::std::basic_streambuf<charT, traits>
+{
+public:
+ typedef typename ::std::basic_streambuf<charT, traits>::int_type int_type;
+
+ null_buffer() {}
+
+private:
+ virtual int_type overflow(int_type c)
+ {
+ // just ignore the character
+ return traits::not_eof(c);
+ }
+};
+
+// generic null output stream class
+template <class charT, class traits = ::std::char_traits<charT> >
+class basic_null_stream
+ : private null_buffer<charT, traits>,
+ public ::std::basic_ostream<charT, traits>
+{
+public:
+ basic_null_stream()
+ : ::std::basic_ostream<charT, traits>(this)
+ {
+ }
+};
+
+// helper declarations for narrow and wide streams
+typedef basic_null_stream<char> null_stream;
+typedef basic_null_stream<wchar_t> null_wstream;
+
+#endif // NULLSTREAM_HPP_INCLUDED
Added: cs/cigma/trunk/src/tri_logger.cpp
===================================================================
--- cs/cigma/trunk/src/tri_logger.cpp (rev 0)
+++ cs/cigma/trunk/src/tri_logger.cpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,289 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#include "tri_logger.hpp"
+#include <memory>
+
+#if !defined(CLEANTLOG)
+
+#if defined (FTLOG)
+#include <fstream>
+
+#else
+#include <iostream>
+#include "nullstream.hpp"
+#endif
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ struct tri_logger_impl
+ {
+ // true if logger is activated
+ static bool is_activated_;
+
+ // auto pointer helps manage resources;
+ static ::std::auto_ptr < ::std::ostream > outstream_helper_ptr_;
+
+ // pointer to the output stream of the logger
+ static ::std::ostream * outstream_;
+
+ //static NullStream null_stream_;
+
+ };
+
+ // activate logger by default
+ bool tri_logger_impl::is_activated_ = true;
+
+ }
+
+ std::auto_ptr<detail::tri_logger_impl>
+ tri_logger_t::tl_impl ( std::auto_ptr<detail::tri_logger_impl> ( new detail::tri_logger_impl ) );
+
+ tri_logger_t::tri_logger_t()
+ {}
+
+ tri_logger_t::~tri_logger_t()
+ {}
+
+ bool tri_logger_t::is_activated()
+ {
+ return tl_impl->is_activated_;
+ }
+
+ void tri_logger_t::activate ( bool const activate )
+ {
+ tl_impl->is_activated_ = activate;
+ }
+
+ ::std::ostream *& tri_logger_t::ostream_ptr()
+ {
+ return tl_impl->outstream_;
+ }
+
+ //void tri_logger_t::set_out_stream (
+ // ::std::auto_ptr<::std::ostream > ostream_ptr )
+ //{
+ //outstream_helper_ptr
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * & ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream * const ostream )
+ //{
+ // outstream_helper_ptr.release();
+ // outstream = ostream;
+ //}
+
+ //void tri_logger_t::set_out_stream ( ::std::ostream & ostream )
+ //{
+ //outstream_helper_ptr_ = ::std::auto_ptr<::std::ostream>( new null_stream );
+ // outstream_ = &ostream;
+ //}
+
+ #if defined(TLOG)
+ // set auto pointer to the null stream
+ // reason: ::std::cout can not be created in runtime, so
+ // the auto pointer has nothing to do with its resources
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cout;
+
+ #elif defined (ETLOG)
+ // look to the TLOG comments
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = &::std::cerr;
+
+ #elif defined (FTLOG)
+
+ #include <cctype>
+
+ #define XSTR(s) STR(s)
+ #define STR(s) #s
+
+ #define MAX(x,y) ( (x) < (y) ? (y) : (x) )
+
+ namespace detail
+ {
+ /**
+ * Function paste rhs C string to the lhs C string.
+ lhs should be long enough for that operation.
+ Additionally coping is stared from the point which
+ points lhs.
+ */
+ template < typename Char_type >
+ size_t const str_cat ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ return len;
+ }
+
+ /// Function calculates length of C string
+ /// It can be used with wide characters
+ template < typename Char_type >
+ size_t const str_len ( Char_type const * str )
+ {
+ size_t len = 0;
+ while ( *str != '\0' )
+ {
+ ++str;
+ ++len;
+ }
+ return len;
+ }
+
+ /**
+ * Function copy rhs C string in to the lhs.
+ * It do not check size of target C string
+ * It starts to copy from the beginning of the C string,
+ * but it begins put characters at the point where lhs points,
+ * so there can be a problem when lhs points on the end of lhs
+ * C string.
+ */
+ template < typename Char_type >
+ size_t const str_cpy ( Char_type *& lhs, Char_type const * rhs )
+ {
+ size_t len = 0;
+ while ( *rhs != '\0' )
+ {
+ *lhs = *rhs;
+ ++rhs;
+ ++lhs;
+ ++len;
+ }
+ *lhs = '\0';
+ return len+1;
+ }
+
+ /**
+ * Function converts existing file name to the file name
+ * which has no non-printable signs and
+ * at the end is added extension.
+ * The space sign in file name is converted to the underscore.
+ * Lengths of C strings has to be proper.
+ */
+ template < typename Char_type >
+ size_t const
+ createfilename ( Char_type * result,
+ Char_type const * filename,
+ Char_type const * ext,
+ Char_type const * helper )
+ {
+ size_t len = 0;
+ if ( str_len ( filename ) > 1 )
+ {
+ while ( *filename != '\0' )
+ {
+ // check if characters have grapnical
+ // reprasentation
+ if ( 0 != ::std::isgraph ( *filename ) )
+ {
+ *result = *filename;
+ ++result;
+ ++len;
+ }
+ else
+ {
+ // convert space to underscore
+ if ( *filename == ' ' )
+ {
+ *result = '_';
+ ++result;
+ ++len;
+ }
+ }
+ ++filename;
+ }
+ // add extension
+ str_cat ( result, ext );
+ *result = '\0';
+ }
+ else
+ {
+ result = &result[0];
+ len = str_cpy ( result, helper );
+ }
+ return len;
+ }
+
+ template < typename T >
+ T const max ( T const x, T const y )
+ {
+ return ( x < y ? y : x );
+ }
+
+ } // namespace detail
+
+ typedef char char_type;
+
+ // convert definition of the TLOGFILE
+ // to the C string
+ char_type const tlogfilename[] = XSTR(FTLOG);
+
+ // extension C string
+ char_type const ext[] = ".log";
+
+ char_type const helper_name[] = "_logger.log";
+
+ // container for final file name
+ char_type filename[(MAX(sizeof(tlogfilename),sizeof(helper_name))+sizeof(ext))/sizeof(char_type)];
+ // create file name
+ size_t const len = detail::createfilename ( filename, tlogfilename, ext, helper_name );
+
+ #undef MAX
+ #undef STR
+ #undef XSTR
+
+ // new file is opened and its destruction is managed by auto_ptr
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream >
+ ( new ::std::ofstream ( filename ));
+ // set pointer output stream
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ // here is a place for user defined output stream and flag
+
+ // ...
+
+ #else
+ ::std::auto_ptr < ::std::ostream >
+ detail::tri_logger_impl::outstream_helper_ptr_
+ = ::std::auto_ptr < ::std::ostream > ( new null_stream );
+ ::std::ostream * detail::tri_logger_impl::outstream_ = outstream_helper_ptr_.get();
+
+ #endif
+
+ ::std::auto_ptr<tri_logger_t> detail::tri_logger_out_ptr ( new tri_logger_t() );
+
+ tri_logger_t& tri_logger()
+ {
+ //static tri_logger_t * ans = new tri_logger_t();
+ static ::std::auto_ptr<tri_logger_t> ans ( new tri_logger_t() );
+ return *ans;
+ //return *ans;
+ }
+
+} // namespace trivial_logger
+
+#endif // !CLEANTLOG
+
Added: cs/cigma/trunk/src/tri_logger.hpp
===================================================================
--- cs/cigma/trunk/src/tri_logger.hpp (rev 0)
+++ cs/cigma/trunk/src/tri_logger.hpp 2008-09-24 11:10:29 UTC (rev 12944)
@@ -0,0 +1,148 @@
+//
+// Copyright (c) 2005 - 2007
+// Seweryn Habdank-Wojewodzki
+//
+// Distributed under the Boost Software License, Version 1.0.
+// ( copy at http://www.boost.org/LICENSE_1_0.txt )
+
+#ifndef TRI_LOGGER_HPP_INCLUDED
+#define TRI_LOGGER_HPP_INCLUDED 1
+
+#include <ostream>
+#include <memory>
+
+namespace trivial_logger
+{
+ namespace detail
+ {
+ class tri_logger_impl;
+ }
+
+ // main trivial logger class
+ class tri_logger_t
+ {
+ public:
+ tri_logger_t ();
+ ~tri_logger_t ();
+
+ /// return true if logger is activated
+ static bool is_activated();
+
+ /// activate and deactivate logger
+ static void activate ( bool const activated );
+
+ /// return reference to pointer to output stream
+ static ::std::ostream *& ostream_ptr();
+
+ // experimental methods crashes code if resource is released
+ // before finishing of work of logger
+
+ // static void set_out_stream ( ::std::ostream * const ostream );
+ // static void set_out_stream ( ::std::ostream & ostream );
+ // static void set_out_stream ( ::std::auto_ptr<::std::ostream > ostream_ptr );
+ // static void set_out_stream ( ::std::ostream * & ostream );
+ private:
+
+ static std::auto_ptr<detail::tri_logger_impl> tl_impl;
+
+ // in this design logger should be noncopyable
+ tri_logger_t ( tri_logger_t const & );
+ tri_logger_t & operator= ( tri_logger_t const & );
+
+ };
+
+ // important funtion which helps solves
+ // "static initialisation fiasco" problem
+ // see:
+ // 1. S. Habdank-Wojewodzki, "C++ Trivial Logger", Overload 77, Feb 2007, pp.19-23
+ // 2. http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13
+ // present solution is much better
+ extern tri_logger_t & tri_logger();
+ //extern ::std::auto_ptr < tri_logger_t > tri_logger();
+
+ namespace detail
+ {
+ //extern tri_logger_t * tri_logger_out_ptr;
+ extern ::std::auto_ptr<tri_logger_t> tri_logger_out_ptr;
+ }
+
+} // namespace trival_logger
+
+// macro prints variable name and its value to the logger stream
+#define TRI_LOG(name) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << #name \
+ << " = " << (name) << ::std::endl;} }while(false)
+
+// macro prints value of the variable to the logger stream
+// useful for printing constant strings
+#define TRI_LOG_STR(str) \
+ do { if ( trivial_logger::tri_logger().is_activated() ){\
+ *trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
+ << " [" << __LINE__ << "] : " << str \
+ << ::std::endl;} }while(false)
+
+// namespace for the trivial logger
+namespace trivial_logger
+{
+ // example how to create functions which operates on logger stream
+ // here are used templates for preparing function which is independent
+ // on the type, but this is not required
+ template < typename T1, typename T2, typename T3, typename T4 >
+ void put_debug_info ( tri_logger_t & log,
+ T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4 )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t1 << " (" << t2 << ") : ";
+ *(log.ostream_ptr()) << t3 << " = " << t4 << ::std::endl;
+ }
+ }
+
+ template < typename T >
+ void put_log_info ( tri_logger_t & log,
+ T const & t )
+ {
+ if ( log.is_activated() )
+ {
+ *(log.ostream_ptr()) << t << ::std::endl;
+ }
+ }
+} // namespace trivial_logger
+
+// macro shows how to write macros which using user-defined functions
+#define TRI_LOG_FN(name) \
+ ::trivial_logger::put_debug_info ( trivial_logger::tri_logger(),\
+ __FILE__, __LINE__, #name, (name) )
+
+// below is a place for user defined logger formating data
+
+// ...
+
+#define TRI_LOG_INFO(name) \
+ ::trivial_logger::put_log_info ( trivial_logger::tri_logger(), (name) )
+
+// macros for switching off and on logger
+#define TRI_LOG_ON() \
+ do { ::trivial_logger::tri_logger().activate ( true ); } while(false)
+#define TRI_LOG_OFF() \
+ do { ::trivial_logger::tri_logger().activate ( false ); } while(false)
+
+#if defined(CLEANTLOG)
+#undef TRI_LOG
+#undef TRI_LOG_ON
+#undef TRI_LOG_OFF
+#undef TRI_LOG_FN
+#undef TRI_LOG_STR
+#undef TRI_LOG_INFO
+#define TRI_LOG(name) do{}while(false)
+#define TRI_LOG_FN(name) do{}while(false)
+#define TRI_LOG_ON() do{}while(false)
+#define TRI_LOG_OFF() do{}while(false)
+#define TRI_LOG_STR(str) do{}while(false)
+#define TRI_LOG_INFO(str) do{}while(false)
+#endif
+
+#endif // TRI_LOGGER_HPP_INCLUDED
+
More information about the cig-commits
mailing list