Changeset 76:706ca58dc667 in rrlib_logging


Ignore:
Timestamp:
12.01.2012 13:11:04 (8 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Readded optional xml configuration

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • configuration.cpp

    r74 r76  
    4040#include "rrlib/logging/configuration/tDomainRegistry.h" 
    4141 
     42#include "rrlib/logging/messages.h" 
     43 
     44#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
     45#include "rrlib/xml2_wrapper/tXMLDocument.h" 
     46#endif 
     47 
    4248//---------------------------------------------------------------------- 
    4349// Debugging 
     
    7783 
    7884//---------------------------------------------------------------------- 
     85// SetPadPrefixColumns 
     86//---------------------------------------------------------------------- 
     87void SetPadPrefixColumns(bool value) 
     88{ 
     89  tDomainRegistry::Instance().SetPadPrefixColumns(value); 
     90} 
     91 
     92//---------------------------------------------------------------------- 
     93// SetPadMultiLineMessages 
     94//---------------------------------------------------------------------- 
     95void SetPadMultiLineMessages(bool value) 
     96{ 
     97  tDomainRegistry::Instance().SetPadMultiLineMessages(value); 
     98} 
     99 
     100//---------------------------------------------------------------------- 
    79101// SetDomainPrintsName 
    80102//---------------------------------------------------------------------- 
     
    116138} 
    117139 
     140//---------------------------------------------------------------------- 
     141// PrintDomainConfigurations 
     142//---------------------------------------------------------------------- 
     143void PrintDomainConfigurations() 
     144{ 
     145  struct RecursionHandler 
     146  { 
     147    static void Print(const tConfiguration &configuration) 
     148    { 
     149      std::cout << configuration.GetFullQualifiedName() << " (" << &configuration << ")" << std::endl; 
     150      for (auto it = configuration.Children().begin(); it != configuration.Children().end(); ++it) 
     151      { 
     152        Print(**it); 
     153      } 
     154    } 
     155  }; 
     156 
     157  RecursionHandler::Print(tDomainRegistry::Instance().GetConfiguration(NULL, ".")); 
     158} 
     159 
     160//---------------------------------------------------------------------- 
     161// ConfigureFromFile 
     162//---------------------------------------------------------------------- 
     163bool ConfigureFromFile(const std::string &file_name) 
     164{ 
     165 
     166#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
     167 
     168  xml2::tXMLDocument document(file_name); 
     169  return ConfigureFromXMLNode(document.RootNode()); 
     170 
     171#endif 
     172 
     173  RRLIB_LOG_PRINT(eLL_ERROR, "XML support not available due to missing rrlib_mca2_wrapper."); 
     174  return false; 
     175} 
     176 
     177#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
     178 
     179namespace 
     180{ 
     181 
     182//---------------------------------------------------------------------- 
     183// AddConfigurationFromXMLNode 
     184//---------------------------------------------------------------------- 
     185bool AddConfigurationFromXMLNode(const xml2::tXMLNode &node, const std::string &parent_name) 
     186{ 
     187  static const std::vector<std::string> level_names = { "error", "warning", "debug_warning", "debug", "debug_verbose_1", "debug_verbose_2", "debug_verbose_3" }; 
     188  static const std::vector<std::string> sink_names = { "stdout", "stderr", "file", "combined_file" }; 
     189 
     190  assert(node.Name() == "domain"); 
     191 
     192  const std::string node_name(node.GetStringAttribute("name")); 
     193  std::string name; 
     194  if (parent_name.length() == 0) 
     195  { 
     196    if (node_name[0] != '.') 
     197    { 
     198      RRLIB_LOG_PRINT(eLL_ERROR, "Trying to configure a log domain not below root domain"); 
     199      return false; 
     200    } 
     201  } 
     202  else 
     203  { 
     204    name += parent_name; 
     205    if (parent_name != ".") 
     206    { 
     207      name += "."; 
     208    } 
     209  } 
     210  name += node_name; 
     211 
     212  tConfiguration &configuration = const_cast<tConfiguration &>(GetConfiguration(NULL, name.c_str())); 
     213 
     214  if (node.HasAttribute("prints_name")) 
     215  { 
     216    configuration.SetPrintsName(node.GetBoolAttribute("prints_name")); 
     217  } 
     218 
     219  if (node.HasAttribute("prints_time")) 
     220  { 
     221    configuration.SetPrintsTime(node.GetBoolAttribute("prints_time")); 
     222  } 
     223 
     224  if (node.HasAttribute("prints_level")) 
     225  { 
     226    configuration.SetPrintsLevel(node.GetBoolAttribute("prints_level")); 
     227  } 
     228 
     229  if (node.HasAttribute("prints_location")) 
     230  { 
     231    configuration.SetPrintsLocation(node.GetBoolAttribute("prints_location")); 
     232  } 
     233 
     234  if (node.HasAttribute("max_level")) 
     235  { 
     236    configuration.SetMaxMessageLevel(static_cast<tLogLevel>(eLL_ERROR + node.GetEnumAttribute<tLogLevel>("max_level", level_names))); 
     237  } 
     238 
     239//  bool sinks_configured = false; 
     240//  if (node.HasAttribute("sink")) 
     241//  { 
     242//    sinks_configured = true; 
     243//    this->SetDomainSink(name, node.GetEnumAttribute<tLogSink>("sink", sink_names)); 
     244//  } 
     245 
     246//  int sink_mask = 0; 
     247//  for (xml2::tXMLNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
     248//  { 
     249//    if (it->Name() == "sink") 
     250//    { 
     251//      if (sinks_configured) 
     252//      { 
     253//        std::cerr << "RRLib Logging: tDomainRegistry::AddConfigurationFromXMLNode >> Sink already configured in domain element!" << std::endl; 
     254//        return false; 
     255//      } 
     256//      sink_mask |= 1 << it->GetEnumAttribute<tLogSink>("output", sink_names); 
     257//    } 
     258//  } 
     259//  if (sink_mask != 0) 
     260//  { 
     261//    this->SetDomainSinkMask(name, sink_mask); 
     262//  } 
     263 
     264  for (xml2::tXMLNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
     265  { 
     266    if (it->Name() == "domain") 
     267    { 
     268      if (!AddConfigurationFromXMLNode(*it, name)) 
     269      { 
     270        return false; 
     271      } 
     272    } 
     273  } 
     274 
     275  return true; 
     276} 
     277 
     278} 
     279 
     280//---------------------------------------------------------------------- 
     281// ConfigureFromXMLNode 
     282//---------------------------------------------------------------------- 
     283bool ConfigureFromXMLNode(const xml2::tXMLNode &node) 
     284{ 
     285  if (node.Name() != "rrlib_logging") 
     286  { 
     287    RRLIB_LOG_PRINT(eLL_ERROR, "Unexpected content (Not an rrlib_logging tree)"); 
     288    return false; 
     289  } 
     290 
     291  try 
     292  { 
     293    if (node.HasAttribute("pad_prefix_columns")) 
     294    { 
     295      SetPadPrefixColumns(node.GetBoolAttribute("pad_prefix_columns")); 
     296    } 
     297    if (node.HasAttribute("pad_multi_line_messages")) 
     298    { 
     299      SetPadMultiLineMessages(node.GetBoolAttribute("pad_multi_line_messages")); 
     300    } 
     301 
     302    for (xml2::tXMLNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
     303    { 
     304      if (it->Name() == "domain") 
     305      { 
     306        if (!AddConfigurationFromXMLNode(*it, "")) 
     307        { 
     308          return false; 
     309        } 
     310      } 
     311    } 
     312  } 
     313  catch (const xml2::tXML2WrapperException &exception) 
     314  { 
     315    RRLIB_LOG_PRINT(eLL_ERROR, exception); 
     316    return false; 
     317  } 
     318 
     319  return true; 
     320 
     321} 
     322 
     323#endif 
    118324 
    119325//---------------------------------------------------------------------- 
  • configuration.h

    r74 r76  
    6161//---------------------------------------------------------------------- 
    6262 
     63const char *GetLogDescription() 
     64{ 
     65  return "RRLib Logging"; 
     66} 
     67 
    6368void SetLogFilenamePrefix(const std::string &filename_prefix); 
     69 
     70void SetPadPrefixColumns(bool value); 
     71 
     72void SetPadMultiLineMessages(bool value); 
    6473 
    6574void SetDomainPrintsName(const std::string &domain_name, bool value); 
     
    7382void SetDomainMaxMessageLevel(const std::string &domain_name, tLogLevel level); 
    7483 
     84void PrintDomainConfigurations(); 
     85 
     86/*! Read domain configuration from a given XML file 
     87 * 
     88 * The overall configuration of the logging domains tends to be 
     89 * too complicated for a classical command line option interface. 
     90 * Therefore, it is possible to specify the configuration in form 
     91 * of an XML file following the DTD -//RRLIB//logging 
     92 * 
     93 * \param filename   The XML file to be read 
     94 * 
     95 * \returns Whether the configuration could be read and applied or not 
     96 */ 
     97bool ConfigureFromFile(const std::string &filename); 
     98 
     99#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
     100 
     101/*! Read domain configuration from a given XML node 
     102 * 
     103 * Instead of reading and parsing an XML file dedicated to configure 
     104 * logging domains this method can be used after externally parsing 
     105 * a document that contains an rrlib_logging node following the DTD 
     106 * -//RRLIB//logging 
     107 * 
     108 * \param node   The XML node containing the configuration 
     109 * 
     110 * \returns Whether the configuration could be applied or not 
     111 */ 
     112bool ConfigureFromXMLNode(const xml2::tXMLNode &node); 
     113 
     114#endif 
     115 
     116 
    75117//---------------------------------------------------------------------- 
    76118// End of namespace declaration 
  • configuration/tConfiguration.cpp

    r75 r76  
    9797//    sink_mask(cDEFAULT_SINK_MASK) 
    9898{ 
     99  assert(name.length() || !parent); 
    99100  this->stream_buffer.AddStream(std::cout); 
    100101} 
  • configuration/tConfiguration.h

    r74 r76  
    169169  } 
    170170 
     171  inline const std::list<tConfiguration *> &Children() const 
     172  { 
     173    return this->children; 
     174  } 
     175 
    171176//---------------------------------------------------------------------- 
    172177// Private fields and methods 
  • configuration/tDomainRegistry.cpp

    r75 r76  
    3939#include <sstream> 
    4040 
    41 #ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    42 #include "rrlib/xml2_wrapper/tXMLDocument.h" 
    43 #endif 
    44  
    4541//---------------------------------------------------------------------- 
    4642// Internal includes with "" 
     
    203199} 
    204200 
    205 ////---------------------------------------------------------------------- 
    206 //// tDomainRegistry ConfigureFromFile 
    207 ////---------------------------------------------------------------------- 
    208 //bool tDomainRegistry::ConfigureFromFile(const std::string &file_name) 
    209 //{ 
    210 //#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    211 //  try 
    212 //  { 
    213 //    tXMLDocument document(file_name); 
    214 //    return this->ConfigureFromXMLNode(document.GetRootNode()); 
    215 //  } 
    216 //  catch (const tXML2WrapperException &e) 
    217 //  { 
    218 //    std::cerr << "RRLib Logging: tDomainRegistry::ConfigureFromFile >> " << e.what() << std::endl; 
    219 //    return false; 
    220 //  } 
    221 //#else 
    222 //  std::cerr << "RRLib Logging: tDomainRegistry::ConfigureFromFile >> XML support not available due to missing rrlib_mca2_wrapper." << std::endl; 
    223 //  return false; 
    224 //#endif 
    225 //} 
    226  
    227  
    228  
    229 //#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    230 // 
    231 ////---------------------------------------------------------------------- 
    232 //// tDomainRegistry ConfigureFromXMLNode 
    233 ////---------------------------------------------------------------------- 
    234 //bool tDomainRegistry::ConfigureFromXMLNode(const tXMLNode &node) 
    235 //{ 
    236 //  if (node.GetName() != "rrlib_logging") 
    237 //  { 
    238 //    std::cerr << "RRLib Logging: tDomainRegistry::ConfigureFromXMLNode >> Unexpected content (Not an rrlib_logging tree)" << std::endl; 
    239 //    return false; 
    240 //  } 
    241 // 
    242 //  try 
    243 //  { 
    244 //    if (node.HasAttribute("pad_prefix_columns")) 
    245 //    { 
    246 //      this->pad_prefix_columns = node.GetBoolAttribute("pad_prefix_columns"); 
    247 //    } 
    248 //    if (node.HasAttribute("pad_multi_line_messages")) 
    249 //    { 
    250 //      this->pad_multi_line_messages = node.GetBoolAttribute("pad_multi_line_messages"); 
    251 //    } 
    252 // 
    253 //    for (tXMLNode::const_iterator it = node.GetChildrenBegin(); it != node.GetChildrenEnd(); ++it) 
    254 //    { 
    255 //      if (it->GetName() == "domain") 
    256 //      { 
    257 //        if (!this->AddConfigurationFromXMLNode(*it)) 
    258 //        { 
    259 //          return false; 
    260 //        } 
    261 //      } 
    262 //    } 
    263 //  } 
    264 //  catch (const tXML2WrapperException &e) 
    265 //  { 
    266 //    std::cerr << "RRLib Logging: tDomainRegistry::ConfigureFromXMLNode >> " << e.what() << std::endl; 
    267 //    return false; 
    268 //  } 
    269 // 
    270 //  return true; 
    271 //} 
    272 // 
    273 ////---------------------------------------------------------------------- 
    274 //// tDomainRegistry AddConfigurationFromXMLNode 
    275 ////---------------------------------------------------------------------- 
    276 //bool tDomainRegistry::AddConfigurationFromXMLNode(const tXMLNode &node, const std::string &parent_name) 
    277 //{ 
    278 //  static const char *level_names_init[eLL_DIMENSION - eLL_ERROR] = { "error", "warning", "debug_warning", "debug", "debug_verbose_1", "debug_verbose_2", "debug_verbose_3" }; 
    279 //  static const std::vector<std::string> level_names(level_names_init, level_names_init + eLL_DIMENSION - eLL_ERROR); 
    280 //  // FIXME: with c++0x this can be static const std::vector<std::string> level_names = { "error", "warning", "debug_warning", "debug", "debug_verbose_1", "debug_verbose_2", "debug_verbose_3" }; 
    281 // 
    282 //  static const char *sink_names_init[eLS_DIMENSION] = { "stdout", "stderr", "file", "combined_file" }; 
    283 //  static const std::vector<std::string> sink_names(sink_names_init, sink_names_init + eLS_DIMENSION); 
    284 //  // FIXME: with c++0x this can be static const std::vector<std::string> stream_names = { "stdout", "stderr", "file", "combined_file" }; 
    285 // 
    286 //  assert(node.GetName() == "domain"); 
    287 // 
    288 //  const std::string prefix((parent_name == "." ? "" : parent_name) + "."); 
    289 //  const std::string node_name(node.GetStringAttribute("name")); 
    290 //  const std::string name(prefix + (!parent_name.length() && node_name == "global" ? "" : node_name)); 
    291 // 
    292 //  if (node.HasAttribute("configures_sub_tree")) 
    293 //  { 
    294 //    this->SetDomainConfiguresSubTree(name, node.GetBoolAttribute("configures_sub_tree")); 
    295 //  } 
    296 // 
    297 //  if (node.HasAttribute("print_time")) 
    298 //  { 
    299 //    this->SetDomainPrintsTime(name, node.GetBoolAttribute("print_time")); 
    300 //  } 
    301 // 
    302 //  if (node.HasAttribute("print_name")) 
    303 //  { 
    304 //    this->SetDomainPrintsName(name, node.GetBoolAttribute("print_name")); 
    305 //  } 
    306 // 
    307 //  if (node.HasAttribute("print_level")) 
    308 //  { 
    309 //    this->SetDomainPrintsLevel(name, node.GetBoolAttribute("print_level")); 
    310 //  } 
    311 // 
    312 //  if (node.HasAttribute("print_location")) 
    313 //  { 
    314 //    this->SetDomainPrintsLocation(name, node.GetBoolAttribute("print_location")); 
    315 //  } 
    316 // 
    317 //  if (node.HasAttribute("max_level")) 
    318 //  { 
    319 //    this->SetDomainMaxMessageLevel(name, static_cast<tLogLevel>(eLL_ERROR + node.GetEnumAttribute<tLogLevel>("max_level", level_names))); 
    320 //  } 
    321 // 
    322 //  bool sinks_configured = false; 
    323 //  if (node.HasAttribute("sink")) 
    324 //  { 
    325 //    sinks_configured = true; 
    326 //    this->SetDomainSink(name, node.GetEnumAttribute<tLogSink>("sink", sink_names)); 
    327 //  } 
    328 // 
    329 //  int sink_mask = 0; 
    330 //  for (tXMLNode::const_iterator it = node.GetChildrenBegin(); it != node.GetChildrenEnd(); ++it) 
    331 //  { 
    332 //    if (it->GetName() == "sink") 
    333 //    { 
    334 //      if (sinks_configured) 
    335 //      { 
    336 //        std::cerr << "RRLib Logging: tDomainRegistry::AddConfigurationFromXMLNode >> Sink already configured in domain element!" << std::endl; 
    337 //        return false; 
    338 //      } 
    339 //      sink_mask |= 1 << it->GetEnumAttribute<tLogSink>("output", sink_names); 
    340 //    } 
    341 //  } 
    342 //  if (sink_mask != 0) 
    343 //  { 
    344 //    this->SetDomainSinkMask(name, sink_mask); 
    345 //  } 
    346 // 
    347 //  for (tXMLNode::const_iterator it = node.GetChildrenBegin(); it != node.GetChildrenEnd(); ++it) 
    348 //  { 
    349 //    if (it->GetName() == "domain") 
    350 //    { 
    351 //      if (!this->AddConfigurationFromXMLNode(*it, name)) 
    352 //      { 
    353 //        return false; 
    354 //      } 
    355 //    } 
    356 //  } 
    357 // 
    358 //  return true; 
    359 //} 
    360 // 
    361 //#endif 
    362201 
    363202//---------------------------------------------------------------------- 
  • configuration/tDomainRegistry.h

    r75 r76  
    197197  } 
    198198 
    199 //#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    200 //  /*! Read domain configuration from a given XML node 
    201 //   * 
    202 //   * Instead of reading and parsing an XML file dedicated to configure 
    203 //   * logging domains this method can be used after externally parsing 
    204 //   * a document that contains an rrlib_logging node following the DTD 
    205 //   * -//RRLIB//logging 
    206 //   * 
    207 //   * \param node   The XML node containing the configuration 
    208 //   * 
    209 //   * \returns Whether the configuration could be applied or not 
    210 //   */ 
    211 //  bool ConfigureFromXMLNode(const xml2::tXMLNode &node); 
    212 //#endif 
    213  
    214199//---------------------------------------------------------------------- 
    215200// Private fields and methods 
     
    228213  const tConfiguration &GetConfigurationByFilename(const char *filename) const; 
    229214 
    230 //#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    231 //  /*! Add a domain configuration from a given XML node 
    232 //   * 
    233 //   * This method configures a logging domain using the values specified in 
    234 //   * the given XML node. It also implements recursive configuration in case 
    235 //   * of nested nodes. 
    236 //   * 
    237 //   * \param node          The XML node that contains the configuration 
    238 //   * \param parent_name   For recursive calls the current domain name is build from parent_name and domain_name 
    239 //   * 
    240 //   * \returns Whether the domain was successfully configured or not 
    241 //   */ 
    242 //  bool AddConfigurationFromXMLNode(const xml2::tXMLNode &node, const std::string &parent_name = ""); 
    243 //#endif 
    244  
    245215}; 
    246216 
  • test/logging_config.xml

    r41 r76  
    22<!DOCTYPE rrlib_logging PUBLIC "-//RRLIB//DTD logging 1.0" "http://rrlib.org/xml/1.0/logging.dtd"> 
    33<rrlib_logging> 
    4   <domain name="global" configures_sub_tree="true" print_time="true" print_name="true" print_level="true" max_level="debug_verbose_3"> 
     4  <domain name="." prints_time="true" prints_name="true" prints_level="true" max_level="debug_verbose_3"> 
    55    <sink output="stdout" /> 
    66  </domain> 
  • test/test_messages.cpp

    r74 r76  
    116116{ 
    117117  rrlib::logging::default_log_description = basename(argv[0]); 
     118  rrlib::logging::SetLogFilenamePrefix(basename(argv[0])); 
    118119 
    119120#ifdef _LIB_RRLIB_XML2_WRAPPER_PRESENT_ 
    120   if (!tLogDomainRegistry::GetInstance()->ConfigureFromFile("logging_config.xml")) 
     121  if (!rrlib::logging::ConfigureFromFile("logging_config.xml")) 
    121122  { 
    122     std::cout << "Loading configuration failed!"; 
     123    RRLIB_LOG_PRINT(rrlib::logging::eLL_ERROR, "Loading configuration failed!"); 
    123124    return EXIT_FAILURE; 
    124125  } 
     126 
     127  rrlib::logging::PrintDomainConfigurations(); 
    125128#endif 
    126129 
    127   rrlib::logging::SetLogFilenamePrefix(basename(argv[0])); 
    128  
    129   rrlib::logging::SetDomainPrintsName(".", true); 
    130   rrlib::logging::SetDomainPrintsTime(".", true); 
    131   rrlib::logging::SetDomainPrintsLevel(".", true); 
    132   rrlib::logging::SetDomainPrintsLocation(".", true); 
    133   rrlib::logging::SetDomainMaxMessageLevel(".", rrlib::logging::eLL_DEBUG_VERBOSE_3); 
     130//  rrlib::logging::SetDomainPrintsName(".", true); 
     131//  rrlib::logging::SetDomainPrintsTime(".", true); 
     132//  rrlib::logging::SetDomainPrintsLevel(".", true); 
     133//  rrlib::logging::SetDomainPrintsLocation(".", true); 
     134//  rrlib::logging::SetDomainMaxMessageLevel(".", rrlib::logging::eLL_DEBUG_VERBOSE_3); 
    134135//  rrlib::logging::SetDomainSink(".", rrlib::logging::eLS_FILE); 
    135136 
     
    165166  RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG, "Function: ", main); 
    166167 
     168  rrlib::logging::PrintDomainConfigurations(); 
    167169  return EXIT_SUCCESS; 
    168170} 
Note: See TracChangeset for help on using the changeset viewer.