Some Examples

We have a few examples which can also be found in the example directory. At the core is a fictitious ``detectorcode'' function which produces messages with various profiles and also through printf. The idea is that this function is compiled once and then linked with different applications, which have established different custom streambufs to filter or customize the output. The ``detectorcode.cc'' file looks like this:
 

#include <iostream.h>
#include <stdio.h>
#include <msg_control.h>

void detectorcode()
{
  msg_control *mvd_info = new msg_control(MSG_TYPE_CODEDEBUG, 
					     MSG_SOURCE_MVD, 
					     MSG_SEV_INFORMATIONAL);
 
  msg_control *mvd_warning = new msg_control(MSG_TYPE_CODEDEBUG, 
					     MSG_SOURCE_MVD, 
					     MSG_SEV_WARNING);
 
  msg_control *mvd_error = new msg_control(MSG_TYPE_CODEDEBUG,
					     MSG_SOURCE_MVD, 
					     MSG_SEV_ERROR);

  msg_control *tof_error = new msg_control(MSG_TYPE_CODEDEBUG,
					     MSG_SOURCE_TOF, 
					     MSG_SEV_ERROR);
 
  cout <<  *mvd_info    << " this is a info message from MVD (1)"       << endl;
  cout <<  *mvd_warning << " this is a warning from MVD (2)"            << endl;
  cout <<  *mvd_error   << " this is an error from MVD (3)"             << endl;
  cout <<  *mvd_info    << " this is another info message from MVD (4)" << endl;
  cout <<  *tof_error   << " this is an error message from TOF (5)"     << endl;
  cout <<                  " this is an unspecified message (6)"        << endl;
  printf                  (" this is a message through printf (7)\n");

}

This can be linked with an application wihich does not have any specialized message handling environment (``x1.cc''):

 
#include <iostream.h>

void detectorcode();

int main()
{
  detectorcode();
}

which produces
 
> ./x1
 this is a info messsge from MVD (1)
 this is a warning from MVD (2)
 this is an error from MVD (3)
 this is another info message from MVD (4)
 this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
>

The next example (x2.cc) defines a filter_msg_buffer, but does not filter anything.

 
#include <filter_msg_buffer.h>

void detectorcode();

filter_msg_buffer filter(256);

int main()
{
  detectorcode();
}

So the output looks exactly as before. In the next example, x3.cc, we actually activate the filter and filter out 2 profiles (which match the profiles of messages number 1, 2 and 4 above):
 
//@Include: x3.cc
 
> ./x3
 this is an error from MVD (3)
 this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
>

In the next example (x4.cc) we first switch all messages off, and then enable error messages from TOF again. Note that this global switch does not switch off the unspecified messages (that came without profile):

 

#include <filter_msg_buffer.h>
#include <msg_control.h>

filter_msg_buffer filter(256);

void detectorcode();

int main()
{
   filter.all_off();

   filter.set(MSG_TYPE_CODEDEBUG,  MSG_SOURCE_TOF, 
   		  MSG_SEV_ERROR,  ON);

  detectorcode();
}

 
> ./x4 
 this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
> 

Finally, we show in example 5 (x5.cc) that we can dynamically, at run-time, establish different custom streambuf's, delete them again, and establish another one. We call the detectorcode() function three times, once with the already well-known filter_msg_buffer, then without (we delete the object, so it re-establishes the original streambuf cout came with), and then we establish a different custom one, a ``date_filter_msg_buffer'' which in addition to filtering, also prepends a time tag to messages with a profile.

 
#include <date_filter_msg_buffer.h>
#include <msg_control.h>



void detectorcode();

int main()
{

  filter_msg_buffer *filter;
  filter = new filter_msg_buffer(256);
  
   filter->all_off();

   filter->set(MSG_TYPE_CODEDEBUG,  MSG_SOURCE_TOF, 
   		  MSG_SEV_ERROR,  ON);

  detectorcode();
	cout << " -------------------" << endl;

  delete filter;

  detectorcode();
	cout << " -------------------" << endl;

  filter = new date_filter_msg_buffer(256);

  detectorcode();
	cout << " -------------------" << endl;
}

 
> ./x5
 this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
 -------------------
 this is a info message from MVD (1)
 this is a warning from MVD (2)
 this is an error from MVD (3)
 this is another info message from MVD (4)
 this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
 -------------------
Wed Apr 21 18:18:31 1999:  this is a info message from MVD (1)
Wed Apr 21 18:18:31 1999:  this is a warning from MVD (2)
Wed Apr 21 18:18:31 1999:  this is an error from MVD (3)
Wed Apr 21 18:18:31 1999:  this is another info message from MVD (4)
Wed Apr 21 18:18:31 1999:  this is an error message from TOF (5)
 this is an unspecified message (6)
 this is a message through printf (7)
 -------------------
>

The first output section is what we already had in example 4. The second section is like the very first example, without any custom streambuf; the messages are just coming through the normal standard output.

The last output section may look odd, but there is logic to it. We do the standard filtering (that is, none at all, all messages go through). However, the data_filter_message_buffer prepends a date/time tag to all output lines except the ``unspecified'' ones (it would clobber help texts and even the command prompt of interactive applications).

Note that the dividing lines (``-----'') are also unspecified messages.

This shows that this message system is a very versatile tool to tailor the message handling to your needs.

Alphabetic index Hierarchy of classes



This page was generated with the help of DOC++.