lestes::msg::logger_configurator Class Reference

A friend of the logger class, see comments for its 'configure' method. More...

List of all members.

Static Private Member Functions

static void configure (xmlNode *node, bool inherited, const ptr< logger > &parent_logger, const ptr< ostream_wrapper > &parent_stream)
 Configures children of given logger according to given xml node chain.

Friends

class logger


Detailed Description

A friend of the logger class, see comments for its 'configure' method.

Definition at line 141 of file logger.cc.


Member Function Documentation

void lestes::msg::logger_configurator::configure ( xmlNode *  node,
bool  inherited,
const ptr< logger > &  parent_logger,
const ptr< ostream_wrapper > &  parent_stream 
) [static, private]

Configures children of given logger according to given xml node chain.

This method is not part of the logger class to avoid dependency on libxml headers. However, as it touches logger's private fields so it has to be in a friend class. Friend method would not help, as that would require parameter types to be known in the header (where the logger class is declared).

Definition at line 156 of file logger.cc.

References lestes::msg::attr2bool(), cast_c, cast_xml, lestes::msg::formatter_factory::create_formatter(), lassert, and lestes::msg::logger2fullname().

Referenced by lestes::msg::logger::init().

00159 {
00160         lassert( node );
00161         lassert( parent_stream );
00162         
00163         // "full path" to the parent logger; used in error output
00164         lstring path = logger2fullname(parent_logger);
00165         if (path != "/")
00166                 path += "/";
00167 
00168         xmlChar *self_prop = NULL;
00169         xmlChar *children_prop = NULL;
00170         xmlChar *name_prop = NULL;
00171         xmlChar *file_prop = NULL;
00172         xmlChar * formatter_prop = NULL;
00173         xmlChar * formatter_param_prop = NULL;
00174 
00175         for ( ; node; node = node->next ) {
00176 
00177                 // skip text and other irrelevant nodes
00178                 if (node->type != XML_ELEMENT_NODE)
00179                         continue;
00180 
00181                 if (strcmp( cast_c(node->name), "logger" )) {
00182                         ::std::cerr << "Ignoring unrecognized element '" <<
00183                                 path << node->name << "'." << ::std::endl;
00184                         continue;
00185                 }
00186 
00187                 // cleanup after previous iteration; freeing NULLs in the first one will not hurt
00188                 xmlFree( self_prop );
00189                 xmlFree( children_prop );
00190                 xmlFree( name_prop );
00191                 xmlFree( file_prop );
00192                 xmlFree( formatter_prop );
00193                 xmlFree( formatter_param_prop );
00194                 name_prop = xmlGetProp( node, cast_xml("name") );
00195                 self_prop = xmlGetProp( node, cast_xml("self") );
00196                 children_prop = xmlGetProp( node, cast_xml("children") );
00197                 file_prop = xmlGetProp( node, cast_xml("file") );
00198                 formatter_prop = xmlGetProp( node, cast_xml("formatter") );
00199                 formatter_param_prop = xmlGetProp( node, cast_xml("parameter") );
00200 
00201                 if (!name_prop) {
00202                         ::std::cerr << "Name attribute missing in element under '" <<
00203                                 path << "'." << ::std::endl;
00204                         continue;
00205                 }
00206                 lstring name_attr(cast_c(name_prop));
00207 
00208                 bool self;
00209                 if (!self_prop)
00210                         self = inherited;
00211                 else
00212                         self = attr2bool(self_prop);
00213 
00214                 bool children;
00215                 if (!children_prop)
00216                         children = inherited;
00217                 else
00218                         children = attr2bool(children_prop);
00219 
00220                 if (parent_logger->children->find(name_attr)
00221                                 == parent_logger->children->end()) {
00222                         ::std::cerr << "Trying to configure non-existing logger '" <<
00223                                 path << name_attr << "'." << ::std::endl;
00224                         continue;
00225                 }
00226 
00227                 ptr<ostream_wrapper> stream;
00228                 if (file_prop && file_prop[0]) {
00229                         lstring fn(cast_c(file_prop));
00230                         if (logger::files_map->find(fn) == logger::files_map->end()) {
00231                                 stream = (*logger::files_map)[fn] =
00232                                         ostream_wrapper::create(
00233                                                 new ::std::ofstream(fn.c_str()), true );
00234                         } else
00235                                 stream = (*logger::files_map)[fn];
00236                 } else if (file_prop) {
00237                         // empty value, use cerr
00238                         stream = logger::cerr_wrapper;
00239                 } else {
00240                         // attribute missing, use stream from parent
00241                         stream = parent_stream;
00242                 }
00243 
00244                 const srp<logger> & this_logger = (*(parent_logger->children))[name_attr];
00245                 if (this_logger->logging_changed) {
00246                         ::std::cerr << "Trying to configure logger '" <<
00247                                 path << name_attr <<
00248                                 "' for the second time, ignoring." << ::std::endl;
00249                         continue;
00250                 }
00251                 this_logger->logging = self;
00252                 this_logger->logging_changed = true;
00253                 this_logger->ostr = stream;
00254 
00255                 this_logger->formatter = formatter_factory::create_formatter(
00256                                 formatter_prop ? cast_c(formatter_prop) : "",
00257                                 formatter_param_prop ? cast_c(formatter_param_prop) : "");
00258 
00259                 // call self recursively
00260                 if (node->children)
00261                         configure( node->children, children, this_logger, stream );
00262         }
00263         
00264         // check that all the child loggers were configured
00265         ptr<logger::children_map_type> ch_map = parent_logger->children;
00266         for (logger::children_map_type::const_iterator it = ch_map->begin();
00267                         it != ch_map->end(); ++it )
00268                 if (!(it->second->logging_changed))
00269                         ::std::cerr << "Logger '" << path << it->second->name <<
00270                                 "' not configured." << ::std::endl;
00271 
00272         xmlFree( self_prop );
00273         xmlFree( children_prop );
00274         xmlFree( name_prop );
00275         xmlFree( file_prop );
00276 }


Friends And Related Function Documentation

friend class logger [friend]

Definition at line 143 of file logger.cc.


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