Public Member Functions | Protected Member Functions | Static Protected Attributes | Private Member Functions | Private Attributes

Message Class Reference

Abstract class which defines the interface for all types of message. More...

#include <keyvalue/sys/message/Message.h>

Inheritance diagram for Message:
Inheritance graph
[legend]

List of all members.

Public Member Functions

unsigned int getLevel () const
 Gets Message's level.
string getString () const
 Gets Message's text.
virtual const string & getPrefix () const =0
 Gets Message's prefix.
virtual logger::Colour getColour () const =0
 Gets Message's colour.
template<typename DataType >
Messageoperator& (const DataType &data)
 Appends data to the end of message.

Protected Member Functions

 Message (unsigned int level)
 Constructs a Message with a given level.
 Message (const Message &orig)

Static Protected Attributes

static const Traits_ traits_ []

Private Member Functions

Messageoperator= (const Message &orig)

Private Attributes

unsigned int level_
mutable::std::ostringstream buf_

Detailed Description

Abstract class which defines the interface for all types of message.

Each Message has a level that defines if it will be ignored or not by the Logger which receives it. (For more details, see Logger documentation).


Constructor & Destructor Documentation

Message ( unsigned int  level  )  [explicit, protected]

Constructs a Message with a given level.

Parameters:
level : Message's level.

Member Function Documentation

unsigned int getLevel (  )  const

Gets Message's level.

Returns:
The level.
string getString (  )  const

Gets Message's text.

Returns:
The text.
virtual const string& getPrefix (  )  const [pure virtual]

Gets Message's prefix.

Returns:
The prefix.

Implemented in MessageImpl< id >.

virtual logger::Colour getColour (  )  const [pure virtual]

Gets Message's colour.

Returns:
The colour.

Implemented in MessageImpl< id >.

Message & operator& ( const DataType &  data  ) 

Appends data to the end of message.

This template method makes use of std::ostream::operator<<() to append data to this Message. Hence, the type of data DataType must be such that std::ostream::operator<<(ostream&, const DataType&) is well defined.

A typical use of this method is

 Info info(1);  // Info is derived from Message
 size_t i;
 vector<double> x;
 // ...
 info & "x[" & i & "] = " & x[i];

Warning: For some reason, one cannot append std::endl to Message, despite the fact that a standard ostream can accept it!

Remark: It would be more natural using operator<< rather than operator&. However, the main use of operator& is related to exceptions (see exception::ExceptionImpl documentation). Indeed, there exist ExceptionImpl::operator&() which just redirects its call to this operator& here. In the past, KeyValue exceptions were derived from boost::exception which defines operator<< for other purposes. Therefore, operator& was preferred rather than operator<< to avoid conflict with boost. Be aware that, in the near future, if it is proven convenient, one might change operator& to operator<< again.

Parameters:
DataType : (template parameter) Type of data;
data : the data to be appended.