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