Changeset 95:fbfa8ebf0541 in rrlib_logging


Ignore:
Timestamp:
28.08.2012 14:54:59 (8 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Transplant:
-1:000000000000
Message:

Reenabled support for file output.

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • configuration.cpp

    r94 r95  
    6969// Const values 
    7070//---------------------------------------------------------------------- 
     71const char * const xml_attribute_max_level_values[static_cast<size_t>(tLogLevel::DIMENSION)] = { "user", "error", "warning", "debug", "debug_warning", "debug_verbose_1", "debug_verbose_2", "debug_verbose_3" }; 
     72const char * const xml_attribute_sink_values[eLOG_SINK_DIMENSION] = { "stdout", "stderr", "file", "combined_file" }; 
    7173 
    7274//---------------------------------------------------------------------- 
     
    136138{ 
    137139  const_cast<tConfiguration &>(tDomainRegistry::Instance().GetConfiguration(NULL, domain_name.c_str())).SetMaxMessageLevel(level); 
     140} 
     141 
     142//---------------------------------------------------------------------- 
     143// SetDomainSink 
     144//---------------------------------------------------------------------- 
     145void SetDomainSink(const std::string &domain_name, tLogSink sink_1, tLogSink sink_2, tLogSink sink_3, tLogSink sink_4) 
     146{ 
     147  const_cast<tConfiguration &>(tDomainRegistry::Instance().GetConfiguration(NULL, domain_name.c_str())).SetSinkMask(sink_1 | sink_2 | sink_3 | sink_4); 
    138148} 
    139149 
     
    231241  if (node.HasAttribute("max_level")) 
    232242  { 
    233     configuration.SetMaxMessageLevel(node.GetEnumAttribute<tLogLevel>("max_level")); 
    234   } 
    235  
    236 //  bool sinks_configured = false; 
    237 //  if (node.HasAttribute("sink")) 
    238 //  { 
    239 //    sinks_configured = true; 
    240 //    this->SetDomainSink(name, node.GetEnumAttribute<tLogSink>("sink", sink_names)); 
    241 //  } 
    242  
    243 //  int sink_mask = 0; 
    244 //  for (xml::tNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
    245 //  { 
    246 //    if (it->Name() == "sink") 
    247 //    { 
    248 //      if (sinks_configured) 
    249 //      { 
    250 //        std::cerr << "RRLib Logging: tDomainRegistry::AddConfigurationFromXMLNode >> Sink already configured in domain element!" << std::endl; 
    251 //        return false; 
    252 //      } 
    253 //      sink_mask |= 1 << it->GetEnumAttribute<tLogSink>("output", sink_names); 
    254 //    } 
    255 //  } 
    256 //  if (sink_mask != 0) 
    257 //  { 
    258 //    this->SetDomainSinkMask(name, sink_mask); 
    259 //  } 
     243    configuration.SetMaxMessageLevel(node.GetEnumAttribute<tLogLevel>("max_level", xml_attribute_max_level_values, xml_attribute_max_level_values + static_cast<size_t>(tLogLevel::DIMENSION))); 
     244  } 
     245 
     246  bool sinks_configured = false; 
     247  if (node.HasAttribute("sink")) 
     248  { 
     249    sinks_configured = true; 
     250    configuration.SetSinkMask(1 << node.GetEnumAttribute<tLogSink>("sink", xml_attribute_sink_values, xml_attribute_sink_values + eLOG_SINK_DIMENSION)); 
     251  } 
     252 
     253  int sink_mask = 0; 
     254  for (xml::tNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
     255  { 
     256    if (it->Name() == "sink") 
     257    { 
     258      if (sinks_configured) 
     259      { 
     260        RRLIB_LOG_PRINT(ERROR, "Sink already configured in domain element!"); 
     261        return false; 
     262      } 
     263      sink_mask |= 1 << it->GetEnumAttribute<tLogSink>("output", xml_attribute_sink_values, xml_attribute_sink_values + eLOG_SINK_DIMENSION); 
     264    } 
     265  } 
     266  if (sink_mask != 0) 
     267  { 
     268    configuration.SetSinkMask(sink_mask); 
     269  } 
    260270 
    261271  for (xml::tNode::const_iterator it = node.ChildrenBegin(); it != node.ChildrenEnd(); ++it) 
  • configuration.h

    r90 r95  
    7878void SetDomainMaxMessageLevel(const std::string &domain_name, tLogLevel level); 
    7979 
     80void SetDomainSink(const std::string &domain_name, tLogSink sink_1, tLogSink sink_2 = (tLogSink)0, tLogSink sink_3 = (tLogSink)0, tLogSink sink_4 = (tLogSink)0); 
     81 
    8082void PrintDomainConfigurations(); 
    8183 
  • configuration/tConfiguration.cpp

    r94 r95  
    7070const bool cDEFAULT_PRINTS_LOCATION = false;                   //!< Default prints location setting for reduced output mode 
    7171const tLogLevel cDEFAULT_MAX_LOG_LEVEL = tLogLevel::WARNING;   //!< Default max log level for reduced output mode 
    72 const int cDEFAULT_SINK_MASK = 1 << eLS_STDOUT;                //!< Default output stream mask 
     72const int cDEFAULT_SINK_MASK = 1 << eLOG_SINK_STDOUT;          //!< Default output stream mask 
    7373#else 
    7474const bool cDEFAULT_PRINTS_NAME = false;                       //!< Default prints name setting for normal output mode 
     
    7777const bool cDEFAULT_PRINTS_LOCATION = true;                    //!< Default prints location setting for normal output mode 
    7878const tLogLevel cDEFAULT_MAX_LOG_LEVEL = tLogLevel::DEBUG;     //!< Default max log level for normal output mode 
    79 const int cDEFAULT_SINK_MASK = 1 << eLS_STDOUT;                //!< Default output stream mask 
     79const int cDEFAULT_SINK_MASK = 1 << eLOG_SINK_STDOUT;          //!< Default output stream mask 
    8080#endif 
     81 
     82const int cLOG_SINK_COMBINED_FILE_CHILD_MASK = 1 << eLOG_SINK_DIMENSION; 
    8183 
    8284//---------------------------------------------------------------------- 
     
    9496    prints_level(parent ? parent->prints_level : cDEFAULT_PRINTS_LEVEL), 
    9597    prints_location(parent ? parent->prints_location : cDEFAULT_PRINTS_LOCATION), 
    96     max_message_level(parent ? parent->max_message_level : cDEFAULT_MAX_LOG_LEVEL)//, 
    97 //    sink_mask(cDEFAULT_SINK_MASK) 
     98    max_message_level(parent ? parent->max_message_level : cDEFAULT_MAX_LOG_LEVEL), 
     99    sink_mask(parent ? parent->sink_mask | cLOG_SINK_COMBINED_FILE_CHILD_MASK : cDEFAULT_SINK_MASK), 
     100    stream_buffer_ready(false) 
    98101{ 
    99102  assert(name.length() || !parent); 
    100   this->stream_buffer.AddStream(std::cout); 
    101103} 
    102104 
     
    106108tConfiguration::~tConfiguration() 
    107109{ 
     110  if (this->file_stream.is_open()) 
     111  { 
     112    this->file_stream.close(); 
     113  } 
     114 
    108115  for (auto it = this->children.begin(); it != this->children.end(); ++it) 
    109116  { 
     
    173180 
    174181//---------------------------------------------------------------------- 
     182// tConfiguration SetSinkMask 
     183//---------------------------------------------------------------------- 
     184void tConfiguration::SetSinkMask(int sink_mask) 
     185{ 
     186  this->sink_mask = sink_mask; 
     187  this->stream_buffer_ready = false; 
     188 
     189  sink_mask |= cLOG_SINK_COMBINED_FILE_CHILD_MASK; 
     190 
     191  for (auto it = this->children.begin(); it != this->children.end(); ++it) 
     192  { 
     193    (*it)->SetSinkMask(sink_mask); 
     194  } 
     195} 
     196 
     197//---------------------------------------------------------------------- 
    175198// tConfiguration GetConfigurationByName 
    176199//---------------------------------------------------------------------- 
     
    206229} 
    207230 
     231//---------------------------------------------------------------------- 
     232// tConfiguration LookupChild 
     233//---------------------------------------------------------------------- 
    208234const tConfiguration &tConfiguration::LookupChild(const char *name, size_t length) const 
    209235{ 
     
    256282 
    257283//---------------------------------------------------------------------- 
     284// tConfiguration PrepareStreamBuffer 
     285//---------------------------------------------------------------------- 
     286void tConfiguration::PrepareStreamBuffer() const 
     287{ 
     288  this->stream_buffer.Clear(); 
     289  if (this->sink_mask & (1 << eLOG_SINK_STDOUT)) 
     290  { 
     291    this->stream_buffer.AddStream(std::cout); 
     292  } 
     293  if (this->sink_mask & (1 << eLOG_SINK_STDERR)) 
     294  { 
     295    this->stream_buffer.AddStream(std::cerr); 
     296  } 
     297  if (this->sink_mask & (1 << eLOG_SINK_FILE)) 
     298  { 
     299    this->stream_buffer.AddStream(this->FileStream()); 
     300  } 
     301  if (this->sink_mask & (1 << eLOG_SINK_COMBINED_FILE)) 
     302  { 
     303    const tConfiguration *configuration = this; 
     304    while (configuration->parent && configuration->sink_mask & cLOG_SINK_COMBINED_FILE_CHILD_MASK) 
     305    { 
     306      configuration = configuration->parent; 
     307    } 
     308    this->stream_buffer.AddStream(configuration->FileStream()); 
     309  } 
     310 
     311  this->stream_buffer_ready = true; 
     312} 
     313 
     314//---------------------------------------------------------------------- 
     315// tConfiguration FileStream 
     316//---------------------------------------------------------------------- 
     317std::ofstream &tConfiguration::FileStream() const 
     318{ 
     319  if (!this->file_stream.is_open()) 
     320  { 
     321    this->OpenFileStream(); 
     322  } 
     323 
     324  return this->file_stream; 
     325} 
     326 
     327//---------------------------------------------------------------------- 
     328// tConfiguration OpenFileStream 
     329//---------------------------------------------------------------------- 
     330void tConfiguration::OpenFileStream() const 
     331{ 
     332  const std::string &file_name_prefix(tDomainRegistry::Instance().LogFilenamePrefix()); 
     333  if (file_name_prefix.length() == 0) 
     334  { 
     335    std::stringstream message; 
     336    message << "RRLib Logging >> Prefix for log filenames not set. Can not use eLOG_SINK_FILE or eLOG_SINK_COMBINED_FILE." << std::endl 
     337            << "                 Consider calling e.g. rrlib::logging::SetLogFilenamePrefix(basename(argv[0])) from main." << std::endl; 
     338    throw std::runtime_error(message.str()); 
     339  } 
     340 
     341  std::string fqdn = this->GetFullQualifiedName(); 
     342  std::string file_name(file_name_prefix + (fqdn != "." ? fqdn : "") + ".log"); 
     343  this->file_stream.open(file_name.c_str(), std::ios::out | std::ios::trunc); 
     344  if (!this->file_stream.is_open()) 
     345  { 
     346    std::stringstream message; 
     347    message << "RRLib Logging >> Could not open file `" << file_name << "'!" << std::endl; 
     348    throw std::runtime_error(message.str()); 
     349  } 
     350} 
     351 
     352//---------------------------------------------------------------------- 
    258353// End of namespace declaration 
    259354//---------------------------------------------------------------------- 
  • configuration/tConfiguration.h

    r88 r95  
    6666#include <string> 
    6767#include <list> 
     68#include <fstream> 
    6869 
    6970//---------------------------------------------------------------------- 
     
    9394enum tLogSink 
    9495{ 
    95   eLS_STDOUT,          //!< Messages are printed to stdout 
    96   eLS_STDERR,          //!< Messages are printed to stderr 
    97   eLS_FILE,            //!< Messages are printed to one file per domain 
    98   eLS_FILE_SUBTREE,    //!< Messages are collected in one file per subtree 
    99   eLS_DIMENSION        //!< Endmarker and dimension of eLogStream 
     96  eLOG_SINK_STDOUT,          //!< Messages are printed to stdout 
     97  eLOG_SINK_STDERR,          //!< Messages are printed to stderr 
     98  eLOG_SINK_FILE,            //!< Messages are printed to single files for each subdomain 
     99  eLOG_SINK_COMBINED_FILE,   //!< Messages are printed into one combined file 
     100  eLOG_SINK_DIMENSION        //!< Endmarker and dimension of tLogSink 
    100101}; 
    101102 
     
    138139  void SetPrintsLocation(bool value); 
    139140  void SetMaxMessageLevel(tLogLevel level); 
     141  void SetSinkMask(int sink_mask); 
    140142 
    141143  inline bool PrintsName() const 
     
    166168  inline tStreamBuffer &StreamBuffer() const 
    167169  { 
     170    if (!this->stream_buffer_ready) 
     171    { 
     172      this->PrepareStreamBuffer(); 
     173    } 
     174 
    168175    return this->stream_buffer; 
    169176  } 
     
    188195 
    189196  tLogLevel max_message_level; 
     197 
    190198  int sink_mask; 
    191  
     199  mutable bool stream_buffer_ready; 
     200  mutable std::ofstream file_stream; 
    192201  mutable tStreamBuffer stream_buffer; 
    193202 
     
    205214 
    206215  const tConfiguration &LookupChild(const char *name, size_t length) const; 
     216 
     217  void PrepareStreamBuffer() const; 
     218 
     219  std::ofstream &FileStream() const; 
     220 
     221  void OpenFileStream() const; 
    207222 
    208223}; 
  • log_levels.h

    r94 r95  
    6767  DEBUG_VERBOSE_2,  //!< Higher detail debug info (not available in release mode) - only relevant to developers in respective domain 
    6868  DEBUG_VERBOSE_3,  //!< Higher detail debug info (not available in release mode) - only relevant to developers in respective domain 
     69  DIMENSION         //!< Endmarker and dimension of tLogLevel 
    6970}; 
    7071 
  • test/test_messages.cpp

    r94 r95  
    133133//  rrlib::logging::SetDomainPrintsLocation(".", true); 
    134134//  rrlib::logging::SetDomainMaxMessageLevel(".", DEBUG_VERBOSE_3); 
    135 //  rrlib::logging::SetDomainSink(".", rrlib::logging::eLS_FILE); 
     135//  rrlib::logging::SetDomainSink(".", rrlib::logging::eLOG_SINK_FILE); 
    136136 
    137137  rrlib::logging::SetDomainMaxMessageLevel(".example", rrlib::logging::tLogLevel::DEBUG_VERBOSE_3); 
    138 //  rrlib::logging::SetDomainSink(".example", rrlib::logging::eLS_STDOUT, rrlib::logging::eLS_FILE_SUBTREE); 
     138//  rrlib::logging::SetDomainSink(".example", rrlib::logging::eLOG_SINK_STDOUT, rrlib::logging::eLOG_SINK_COMBINED_FILE); 
    139139 
    140140  RRLIB_LOG_PRINT(WARNING, "foo"); 
Note: See TracChangeset for help on using the changeset viewer.