Sierra Toolkit Version of the Day
WriterParser.cpp
00001 
00002 #include <sstream>
00003 #include <iomanip>
00004 #include <map>
00005 
00006 #include <stk_util/diag/WriterParser.hpp>
00007 #include <stk_util/diag/Trace.hpp>
00008 #include <stk_util/diag/Writer_fwd.hpp>
00009 #include <iostream>
00010 
00011 namespace stk {
00012 namespace diag {
00013 
00014 WriterParser::WriterParser()
00015   : OptionMaskParser()
00016 {
00017   mask("coverage", 0, "Collect and display traceable function usage coverage");
00018   mask("members", LOG_MEMBERS, "Display data structure members messages");
00019   mask("trace", LOG_TRACE, "Display execution trace");
00020   mask("trace-stats", LOG_TRACE_STATS, "Display execution time and memory usage during trace");
00021   mask("trace-down", LOG_TRACE_SUB_CALLS, "Display subsequent calls after tracing is enabled");
00022 }
00023 
00024 
00025 OptionMaskParser::Mask
00026 WriterParser::parse(
00027   const char *          mask_string) const
00028 {
00029   m_optionMask = LOG_MEMBERS;
00030   return OptionMaskParser::parse(mask_string);
00031 }
00032 
00033 
00034 void
00035 WriterParser::parseArg(
00036   const std::string &  name,
00037   const std::string &  arg) const
00038 {
00039   if (name == "trace") {
00040     m_optionMask |= LOG_TRACE;
00041     if (!arg.empty()) {
00042       std::string::const_iterator it0 = arg.begin();
00043       std::string::const_iterator it1;
00044       std::string::const_iterator it2;
00045       do {
00046         // Trim preceeding spaces
00047         while (it0 != arg.end() && *it0 == ' ')
00048           it0++;
00049 
00050         if (it0 == arg.end())
00051           break;
00052 
00053         int paren_count = 0;
00054         for (it1 = it0; it1 != arg.end(); ++it1) {
00055           if (*it1 == '(')
00056             ++paren_count;
00057           else if (*it1 == ')')
00058             --paren_count;
00059           else if (*it1 == ',' && paren_count == 0)
00060             break;
00061         }
00062 
00063 
00064         // Trim trailing spaces
00065         it2 = it1;
00066         while (it2 != it0 && *(it2 - 1) == ' ')
00067           --it2;
00068 
00069         std::string function(it0, it2);
00070 
00071         Trace::addTraceFunction(function);
00072 
00073         it0 = it1 + 1;
00074       } while (it1 != arg.end());
00075     }
00076     else
00077       m_optionMask |= LOG_TRACE_SUB_CALLS;
00078   }
00079 
00080   else {
00081     OptionMaskNameMap::const_iterator mask_entry = m_optionMaskNameMap.find(name.c_str());
00082 
00083     if (mask_entry != m_optionMaskNameMap.end())
00084       m_optionMask |= (*mask_entry).second.m_mask;
00085     else {
00086       Mask  mask_hex = 0;
00087       std::istringstream mask_hex_stream(name.c_str());
00088       if (mask_hex_stream >> std::resetiosflags(std::ios::basefield) >> mask_hex)
00089         m_optionMask |= mask_hex;
00090       else
00091         m_status = false;
00092     }
00093   }
00094 }
00095 
00096 
00097 OptionMaskParser::Mask
00098 OptionMaskParser::parse(
00099   const char *          mask) const
00100 {
00101   if (mask) {
00102     const std::string mask_string(mask);
00103 
00104     m_status = true;
00105 
00106     std::string::const_iterator it0 = mask_string.begin();
00107     std::string::const_iterator it1;
00108     std::string::const_iterator it2;
00109     std::string::const_iterator it3;
00110     do {
00111       // Trim preceeding spaces
00112       while (it0 != mask_string.end() && *it0 == ' ')
00113         it0++;
00114 
00115       if (it0 == mask_string.end())
00116         break;
00117 
00118       for (it1 = it0; it1 != mask_string.end(); ++it1) {
00119         if (*it1 == '(' || *it1 == ':' || *it1 == ',')
00120           break;
00121       }
00122 
00123       // Trim trailing spaces
00124       it2 = it1;
00125       while (it2 != it0 && *(it2 - 1) == ' ')
00126         --it2;
00127 
00128       std::string name(it0, it2);
00129 
00130       // Get argument list
00131       if (*it1 == '(') {
00132         it2 = it1 + 1;
00133 
00134         // Trim preceeding spaces
00135         while (it2 != mask_string.end() && *it2 == ' ')
00136           ++it2;
00137 
00138         int paren_count = 0;
00139 
00140         for (; it1 != mask_string.end(); ++it1) {
00141           if (*it1 == '(')
00142             ++paren_count;
00143           else if (*it1 == ')') {
00144             --paren_count;
00145             if (paren_count == 0)
00146               break;
00147           }
00148         }
00149         it3 = it1;
00150 
00151         // Trim trailing spaces
00152         while (it3 != it2 && *(it3 - 1) == ' ')
00153           --it3;
00154 
00155         // Find next argument start
00156         for (; it1 != mask_string.end(); ++it1)
00157           if (*it1 == ':' || *it1 == ',')
00158             break;
00159       }
00160       else
00161         it2 = it3 = it1;
00162 
00163       const std::string arg(it2, it3);
00164 
00165       parseArg(name, arg);
00166 
00167       it0 = it1 + 1;
00168     } while (it1 != mask_string.end());
00169   }
00170 
00171   return m_optionMask;
00172 }
00173 
00174 
00175 void
00176 OptionMaskParser::parseArg(
00177   const std::string &  name,
00178   const std::string &  arg) const
00179 {
00180   OptionMaskNameMap::const_iterator mask_entry = m_optionMaskNameMap.find(name);
00181 
00182   if (mask_entry != m_optionMaskNameMap.end()) m_optionMask |= (*mask_entry).second.m_mask;
00183   else {
00184     Mask  mask_hex = 0;
00185     std::istringstream mask_hex_stream(name.c_str());
00186     if (mask_hex_stream >> std::resetiosflags(std::ios::basefield) >> mask_hex)
00187       m_optionMask |= mask_hex;
00188     else
00189       m_status = false;
00190   }
00191 }
00192 
00193 
00194 std::ostream &
00195 OptionMaskParser::describe(
00196   std::ostream &    os) const
00197 {
00198   os << "Specify a comma separated list of:" << std::endl;
00199   for (OptionMaskNameMap::const_iterator it = m_optionMaskNameMap.begin(); it != m_optionMaskNameMap.end(); ++it)
00200     (*it).second.describe(os);
00201 
00202   return os;
00203 }
00204 
00205 
00206 std::ostream &
00207 OptionMaskName::describe(
00208   std::ostream &  os) const
00209 {
00210   return os << "  " << std::left << std::setw(20) << m_name << "\t" << m_description << std::endl;
00211 }
00212 
00213 } // namespace diag
00214 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends