lestes::msg::logger Class Reference

#include <logger.hh>

Inheritance diagram for lestes::msg::logger:

lestes::std::object lestes::std::mem::keystone List of all members.

Public Types

typedef map< lstring, srp<
logger > > 
children_map_type
 Map that maps lstrings to loggers, used to store children of a logger.

Public Member Functions

ptr< loggerparent_get () const
lstring name_get () const
ptr< children_map_typechildren_get () const
ptr< logger_formatterformatter_get () const
void formatter_set (ptr< logger_formatter > x)
::std::ostream & operator<< (const ptr< logger_formatter > &)

Static Public Member Functions

static ptr< loggercreate (const lstring &a_name, const ptr< logger > &a_parent)
 Factory method, creates logger with given name. Also adds it to 'children' of given parent.
static ptr< loggerroot_instance ()
 Returns pointer to the root "grandparent" logger.
static bool init (const lstring &filename)
 Configures the logger tree by applying the settings found in given xml file.
static void finish ()
 Closes all files open during init().
::std::ostream & dump_skeleton (::std::ostream &)
 Traverses the logger tree starting at root_instance(), outputs xml configuration file with corresponding elements.

Protected Member Functions

 logger (const lstring &a_name, const ptr< logger > a_parent)
 Constructor run from the 'create' factory method.
void gc_mark ()
 Marks the keystone.

Private Types

typedef map< lstring, srp<
ostream_wrapper > > 
files_map_type

Private Member Functions

 logger ()
 This ctor is only used to construct the root logger.

Static Private Member Functions

static void subtree_dump (const ptr< logger > &,::std::ostream &)
 Dumps all children (including "transitive" ones:) as xml to given ostream; used internally by dump_skeleton method.

Private Attributes

bool logging
 Whether this logger is supposed to actually do something.
bool logging_changed
 True when logging field has already been changed.
srp< ostream_wrapper > ostr
 Pointer to a stream used for the actual logging.
const lstring name
 Name of this logger. This does not include names of parents.
const srp< loggerparent
 Pointer to parent logger, the root logger points to self; checked.
const srp< children_map_typechildren
srp< logger_formatterformatter
 the default formatter

Static Private Attributes

static ptr< loggerthe_root_instance = the_root_instance
 Hold pointer to the root logger instance.
static ptr< files_map_typefiles_map = files_map_type::create()
static ptr< ostream_wrapper > null_ostream
static ptr< ostream_wrapper > cerr_wrapper

Friends

class logger_configurator

Detailed Description

... One should only instantiate this class by hand when they know what they are doing.

Definition at line 140 of file logger.hh.


Member Typedef Documentation

typedef map< lstring, srp<logger> > lestes::msg::logger::children_map_type

Map that maps lstrings to loggers, used to store children of a logger.

Definition at line 144 of file logger.hh.

typedef map< lstring, srp<ostream_wrapper> > lestes::msg::logger::files_map_type [private]

Definition at line 186 of file logger.hh.


Constructor & Destructor Documentation

lestes::msg::logger::logger (  )  [private]

This ctor is only used to construct the root logger.

Definition at line 109 of file logger.cc.

Referenced by create(), and root_instance().

00110         : logging(false), logging_changed(false), ostr(NULL),
00111         parent(this), children( children_map_type::create() ),
00112         formatter( plain_formatter::instance() )
00113 {}

lestes::msg::logger::logger ( const lstring &  a_name,
const ptr< logger a_parent 
) [protected]

Constructor run from the 'create' factory method.

Definition at line 115 of file logger.cc.

References create(), lassert2, name, and parent.

00116         : logging(false), logging_changed(false), ostr(NULL), name(a_name),
00117         parent( checked(a_parent) ), children( children_map_type::create() ),
00118         formatter( plain_formatter::instance() )
00119 {
00120         bool not_inserted_yet = parent->children->insert(
00121                 *pair< lstring, srp<logger> >::create(name,this) ).second;
00122         lassert2( not_inserted_yet, "Trying to add a child logger with an already taken name." );
00123 }


Member Function Documentation

ptr< logger > lestes::msg::logger::create ( const lstring &  a_name,
const ptr< logger > &  a_parent 
) [static]

Factory method, creates logger with given name. Also adds it to 'children' of given parent.

Definition at line 64 of file logger.cc.

References logger().

Referenced by logger().

00065 {
00066         return new logger( a_name, a_parent );
00067 }

ptr< logger > lestes::msg::logger::root_instance (  )  [static]

Returns pointer to the root "grandparent" logger.

Definition at line 69 of file logger.cc.

References logger(), and the_root_instance.

Referenced by dump_skeleton(), and init().

00070 {
00071         if (!the_root_instance)
00072                 the_root_instance = new logger();
00073         return the_root_instance;
00074 }

bool lestes::msg::logger::init ( const lstring &  filename  )  [static]

Configures the logger tree by applying the settings found in given xml file.

Tries to parse filename of given name and apply settings in it to the logger tree. After returning true, should not be called again (violation is detected and not fatal). When not called at all, all loggers are off.

Returns:
false when file could not be read or parsed, when the root element is invalid, or when the root element did not contain the required attributes

true otherwise; this includes some non-fatal errors which are reported to the user

Definition at line 286 of file logger.cc.

References lestes::msg::attr2bool(), cast_c, cast_xml, cerr_wrapper, children, lestes::msg::logger_configurator::configure(), logging_changed, and root_instance().

Referenced by main().

00287 {
00288         if (root_instance()->logging_changed) {
00289                 ::std::cerr << "Trying to initialize loggers again, ignoring." << ::std::endl;
00290                 return false;
00291         }
00292 
00293         /*
00294          * this initializes the library and checks potential ABI mismatches
00295          * between the version it was compiled for and the actual shared
00296          * library used.
00297          */
00298         LIBXML_TEST_VERSION
00299 
00300         xmlDoc *doc = NULL;
00301         xmlNode *root_element = NULL;
00302         xmlChar *self_prop = NULL;
00303         xmlChar *children_prop = NULL;
00304         xmlChar *name_prop = NULL;
00305         xmlChar *file_prop = NULL;
00306 
00307         ptr<ostream_wrapper> stream;
00308 
00309         bool result = false;
00310         bool self;
00311         bool children;
00312 
00313         /* parse the file and get the DOM */
00314         doc = xmlParseFile( filename.c_str() );
00315         if (!doc) {
00316                 ::std::cerr << "Could not parse configuration file." << ::std::endl;
00317                 goto err_out;
00318         }
00319 
00320         root_element = xmlDocGetRootElement(doc);
00321         if (!root_element) {
00322                 ::std::cerr << "Root element missing." << ::std::endl;
00323                 goto err_out;
00324         }
00325         if (strcmp( cast_c(root_element->name), "logger" )) {
00326                 ::std::cerr << "Invalid root element '" << root_element->name <<
00327                         "'. Expected 'logger'." << ::std::endl;
00328                 goto err_out;
00329         }
00330 
00331         self_prop = xmlGetProp( root_element, cast_xml("self") );
00332         children_prop = xmlGetProp( root_element, cast_xml("children") );
00333 
00334         if (!self_prop || !children_prop) {
00335                 ::std::cerr << "Both 'self' and 'children' attributes have"
00336                         " to be set in the root logger element." << ::std::endl;
00337                 goto err_out;
00338         }
00339 
00340         name_prop = xmlGetProp( root_element, cast_xml("name") );
00341         if (name_prop)
00342                 ::std::cerr << "Name attribute ignored in the root logger element." << ::std::endl;
00343         file_prop = xmlGetProp( root_element, cast_xml("file") );
00344 
00345         self = attr2bool(self_prop);
00346         children = attr2bool(children_prop);
00347         // empty or non-existing 'file' attribute results in cerr being used
00348         if (file_prop && file_prop[0]) {
00349                 lstring fn(cast_c(file_prop));
00350                 stream = (*files_map)[fn] = ostream_wrapper::create(
00351                                 new ::std::ofstream(fn.c_str()), true );
00352         } else
00353                 stream = cerr_wrapper;
00354 
00355         root_instance()->logging = self;
00356         root_instance()->logging_changed = true;
00357         root_instance()->ostr = stream;
00358 
00359         if (root_element->children)
00360                 logger_configurator::configure( root_element->children, children,
00361                                                 root_instance(), stream );
00362 
00363         result = true;
00364 err_out:
00365         xmlFree( self_prop );
00366         xmlFree( children_prop );
00367         xmlFree( name_prop );
00368         xmlFree( file_prop );
00369         xmlFreeDoc( doc );
00370         xmlCleanupParser();
00371         return result;
00372 }

void lestes::msg::logger::finish (  )  [static]

Closes all files open during init().

Can be called without logger::init() being called previously.

Definition at line 377 of file logger.cc.

References files_map.

Referenced by lestes::std::lassert_fail(), and main().

00378 {
00379         for ( files_map_type::iterator it = files_map->begin(); it != files_map->end(); ++it )
00380                 it->second->release();  // this destructs the ofstream; the file is closed
00381 }

std::ostream & lestes::msg::logger::dump_skeleton ( ::std::ostream &   )  [static]

Traverses the logger tree starting at root_instance(), outputs xml configuration file with corresponding elements.

Definition at line 400 of file logger.cc.

References root_instance(), and subtree_dump().

Referenced by main().

00401 {
00402         os <<   "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
00403                 "<logger self=\"off\" children=\"off\""
00404                         " xmlns=\"http://lestes.jikos.cz/schemas/log-conf\">" << ::std::endl;
00405         
00406         subtree_dump( root_instance(), os );
00407         // the root element, unlike the other ones, is never shortened to <logger ... />
00408         os << "</logger>" << ::std::endl;
00409         return os;
00410 }

ptr< logger > lestes::msg::logger::parent_get (  )  const

Definition at line 76 of file logger.cc.

References parent.

00077 {
00078         return parent;
00079 }

lstring lestes::msg::logger::name_get (  )  const

Definition at line 80 of file logger.cc.

References name.

00081 {
00082         return name;
00083 }

ptr< logger::children_map_type > lestes::msg::logger::children_get (  )  const

Definition at line 84 of file logger.cc.

References children.

00085 {
00086         return children;
00087 }

ptr< logger_formatter > lestes::msg::logger::formatter_get (  )  const

Definition at line 89 of file logger.cc.

00090 {
00091         return formatter;
00092 }

void lestes::msg::logger::formatter_set ( ptr< logger_formatter x  ) 

Definition at line 93 of file logger.cc.

References lassert.

00094 {
00095         lassert(x);
00096         formatter = x;
00097 }

std::ostream & lestes::msg::logger::operator<< ( const ptr< logger_formatter > &   ) 

Definition at line 412 of file logger.cc.

References logging, null_ostream, and ostr.

00413 {
00414         return lf->format( this, *((logging ? *ostr : *null_ostream).stream_get()) );
00415 }

void lestes::msg::logger::subtree_dump ( const ptr< logger > &  ,
::std::ostream &   
) [static, private]

Dumps all children (including "transitive" ones:) as xml to given ostream; used internally by dump_skeleton method.

Definition at line 383 of file logger.cc.

Referenced by dump_skeleton().

00384 {
00385         children_map_type::const_iterator it = l->children->begin();
00386         children_map_type::const_iterator end_it = l->children->end();
00387 
00388         for ( ; it != end_it; ++it ) {
00389                 os << "<logger name=\"" << it->second->name << '"';
00390                 if (it->second->children->empty()) {
00391                         os << " />" << ::std::endl;
00392                 } else {
00393                         os << '>' << ::std::endl;
00394                         subtree_dump( it->second, os );
00395                         os << "</logger>" << ::std::endl;
00396                 }
00397         }
00398 }

void lestes::msg::logger::gc_mark ( void   )  [protected, virtual]

Marks the keystone.

Marks all directly reachable parts of the class. The method must be overriden for each inherited class. It should contain abc.gc_mark() for each field abc of the inherited class and call to gc_mark() of the direct ancestor of the class. Does nothing for keystone, only stops processing of ancestors.

Reimplemented from lestes::std::mem::keystone.

Definition at line 417 of file logger.cc.

References children, ostr, and parent.

00418 {
00419         ostr.gc_mark();
00420         parent.gc_mark();
00421         children.gc_mark();
00422         formatter.gc_mark();
00423         object::gc_mark();
00424 }


Friends And Related Function Documentation

friend class logger_configurator [friend]

Definition at line 141 of file logger.hh.


Member Data Documentation

bool lestes::msg::logger::logging [private]

Whether this logger is supposed to actually do something.

Definition at line 168 of file logger.hh.

Referenced by operator<<().

bool lestes::msg::logger::logging_changed [private]

True when logging field has already been changed.

Definition at line 170 of file logger.hh.

Referenced by init().

srp<ostream_wrapper> lestes::msg::logger::ostr [private]

Pointer to a stream used for the actual logging.

Definition at line 172 of file logger.hh.

Referenced by gc_mark(), and operator<<().

const lstring lestes::msg::logger::name [private]

Name of this logger. This does not include names of parents.

Definition at line 175 of file logger.hh.

Referenced by logger(), and name_get().

const srp<logger> lestes::msg::logger::parent [private]

Pointer to parent logger, the root logger points to self; checked.

Definition at line 177 of file logger.hh.

Referenced by gc_mark(), logger(), and parent_get().

const srp<children_map_type> lestes::msg::logger::children [private]

Definition at line 178 of file logger.hh.

Referenced by children_get(), gc_mark(), and init().

srp< logger_formatter > lestes::msg::logger::formatter [private]

the default formatter

Definition at line 181 of file logger.hh.

ptr< logger > lestes::msg::logger::the_root_instance = the_root_instance [static, private]

Hold pointer to the root logger instance.

Definition at line 184 of file logger.hh.

Referenced by root_instance().

ptr< logger::files_map_type > lestes::msg::logger::files_map = files_map_type::create() [static, private]

Definition at line 187 of file logger.hh.

Referenced by finish().

ptr< ostream_wrapper > lestes::msg::logger::null_ostream [static, private]

Initial value:

                ostream_wrapper::create( new ::std::ofstream(), true )

Definition at line 192 of file logger.hh.

Referenced by operator<<().

ptr< ostream_wrapper > lestes::msg::logger::cerr_wrapper [static, private]

Initial value:

                ostream_wrapper::create( &::std::cerr, false )

Definition at line 193 of file logger.hh.

Referenced by init().


The documentation for this class was generated from the following files:
Generated on Mon Feb 12 18:25:53 2007 for lestes by doxygen 1.5.1-20070107