Oncaphillis::niftyLib

niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT > Class Template Reference

#include <io/log_filter.h>

Inheritance diagram for niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >:
Collaboration diagram for niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >:

Detailed Description

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
class niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >

Special purpose device nifty for outputting strings into a log file or syslog facility.

The device has to be associated with another device fulfilling the device preconditions.

Whenever a sequence of line-feed is encountered the method calls its own virtual protected method line_intro which simply puts out " * " as a line intro but can be overwritten to accomplish more sophisticated stuff, like printing out a state or the current time stamp.

We might do thinks like:

        using namespace niftyLib;
        // We subclass our own log_filter variant which
        // overwrites the virtual method line_intro

        template<class DeviceT>
        class log_filter : public IO::basic_log_filter<DeviceT,true> {

        private:

         typedef IO::basic_log_filter<DeviceT,true> super;

        public:

         // We include typedefs from our super class (which actually have been imported
         // from the associated device) in order to make it interact nicely with the
         // device_streambuf template class

         typedef typename super::char_type char_type;

         typedef typename super::traits_type traits_type;

         typedef typename super::device_t device_t;

         typedef typename super::device_ptr device_ptr;

         log_filter(device_ptr d) : super(d) {
         }

         virtual ~log_filter() {
         }

         // We just have to overwrite the line_inro method
         // which is expected to write data at the beginning of each line
         // and gets called by the write method (inherited from device::write)
         // whenever a valid new-line sequence has been encountered

         virtual std::streamsize line_intro() {
           return this->raw_write(" TEST -- ");
         }

        protected:

        log_filter() {
        };

        log_filter(const log_filter &) {
        }
       };

       int main() {

        try {
    
        // Create a file descriptor device referring to stdout
        IO::fd_device<char>              *fd = new IO::fd_device<char>(1);

        // Create a log_device pumping its chars to the fd_device
        log_filter<IO::fd_device<char> > *ld = new log_filter< IO::fd_device<char> >( fd );

        // Create a device_streambuf associated to the log_device
        IO::device_streambuf< log_filter < IO::fd_device<char> > > b(ld);
    
        // Finally create a ostream 
        std::ostream os(&b);
    
        for(int i=0;i<3;i++) {
         os << " hello world" << std::endl;
        }

        os << std::flush;
    
        // We do not have to delete the log_filter and the fd_device
        // since we told device_streambuf and log_filter to take 
        // onership. The will be deleted when b goes out of scope

        // delete lf;
        // delete fd;
    
        } catch(std::exception &ex) {
         std::cerr << "Ex:" << ex.what() << std::endl;
        }
       }

This should output

       
       TEST -- hello world
       TEST -- hello world
       TEST -- hello world

       

See also:
device, str_log_filter, time_log_filter

Public Types

typedef super::char_type char_type
 We inherit the char_type from our associated output device.
typedef super::traits_type traits_type
 We also inherit the traits_type from our associated output device.
typedef super::device_ptr device_ptr
 Handy typedef for the device we're operating on.

Public Member Functions

 basic_log_filter (device_ptr d, bool o=true)
 Standard creator.
virtual ~basic_log_filter ()
virtual std::streamsize write (const char_type *b, std::streamsize n)
 The simple write method overwrites the device::write method and scans the output for a valid new-line sequence.
virtual std::streamsize line_intro ()
 Method to generate the start of every new line.

Protected Member Functions

std::streamsize raw_write (const char_type *b, std::streamsize n)
 Helper method for line_intro.
std::streamsize raw_write (const char_type *b)
 Helper method for line_intro.
std::streamsize raw_write (const std::basic_string< char_type > &l)
 Helper method for line_intro.

Member Typedef Documentation

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
typedef super::char_type niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::char_type

We inherit the char_type from our associated output device.

This definition has to be here in order to make us work properly with a device_streambuf

Reimplemented from niftyLib::IO::filter< CharT, TraitsT, DeviceT >.

Reimplemented in niftyLib::IO::str_log_filter< CharT, TraitsT, DeviceT >, and niftyLib::IO::time_log_filter.

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
typedef super::traits_type niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::traits_type

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
typedef super::device_ptr niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::device_ptr


Constructor & Destructor Documentation

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::basic_log_filter ( device_ptr  d,
bool  o = true 
) [inline]

Standard creator.

We set the state to CRLF as if the last char sequence has been a valid NL sequence since the very first thing we have to output on a new buffer is the line_intro

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
virtual niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::~basic_log_filter (  )  [inline, virtual]


Member Function Documentation

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
virtual std::streamsize niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::write ( const char_type b,
std::streamsize  n 
) [inline, virtual]

The simple write method overwrites the device::write method and scans the output for a valid new-line sequence.

Whenever we encounter a line-feed we call line_intro before dumpung out any other chars.

See also:
line_intro

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
virtual std::streamsize niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::line_intro (  )  [inline, virtual]

Method to generate the start of every new line.

This one does nothing and is expected to get overwritten by subclasses doing more sophisticated stuff like dumping out the current date.

One might choose to build up a chane of methods by always calling the line_intro method of the super class. This way one can concatinate the output of various filters.

e.g:

          template<class D>
          class foo_log_filter : public log_filter<D> {
           ...
           ...
           std::streamsize line_intro() {
            std::streamsize n = super::line_intro();
            n+=this->raw_write("foo ");
            return n;
           }
          };

          template<class D>
          class bar_log_filter : public log_filter<D> {
           ...
           ...
           std::streamsize line_intro() {
            std::streamsize n = super::line_intro();
            n+=this->raw_write("bar ");
            return n;
           }
          };


          int main() {
           ...
           IO::device<char> * d = new bar_log_filter< foo_log_filter< IO::fd_device<char> > >( new foo_log_filter< IO::fd_device<char> >(new IO::fd_device<char>(1)));
           IO::device_streambuf< IO::device<char> > b(d);
    
           // Finally create a ostream 
           std::ostream os(&b);
    
           for(int i=0;i<3;i++) {
            os << " helloworld" << std::endl;
           }
           ...
           ...
          }

Which should dump out

	   foo bar hello world
	   foo bar hello world
	   foo bar hello world
	  

Of course this kind of cascading template orgy should be hidden nicely within some typedefs or container classes. It's just to tempting to be so ugly.

See also:
write

Reimplemented in niftyLib::IO::str_log_filter< CharT, TraitsT, DeviceT >, and niftyLib::IO::time_log_filter.

Referenced by niftyLib::IO::str_log_filter< CharT, TraitsT, DeviceT >::line_intro(), and niftyLib::IO::basic_log_filter< char >::write().

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
std::streamsize niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::raw_write ( const char_type b,
std::streamsize  n 
) [inline, protected]

Helper method for line_intro.

Raw write to the underyling device.

Returns:
number of char_type actually written to the underlyinig device.

Referenced by niftyLib::IO::str_log_filter< CharT, TraitsT, DeviceT >::line_intro(), niftyLib::IO::basic_log_filter< char >::raw_write(), and niftyLib::IO::basic_log_filter< char >::write().

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
std::streamsize niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::raw_write ( const char_type b  )  [inline, protected]

Helper method for line_intro.

Raw write to the underlying device.

template<class CharT, class TraitsT = std::char_traits<CharT>, class DeviceT = device<CharT,TraitsT>>
std::streamsize niftyLib::IO::basic_log_filter< CharT, TraitsT, DeviceT >::raw_write ( const std::basic_string< char_type > &  l  )  [inline, protected]

Helper method for line_intro.

Raw write to the underlying device.


The documentation for this class was generated from the following file:

Generated by  doxygen
© 2008; Dr. Sebastian Kloska ( Oncaphillis )
Powered by: [?]