LogControl.hpp

00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
00010 #define STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
00011 
00012 #include <ostream>
00013 #include <sstream>
00014 #include <string>
00015 #include <map>
00016 
00017 #include <stk_util/util/string_case_compare.hpp>
00018 
00019 namespace stk {
00020 
00025 struct LogControlRule
00026 {
00031   virtual ~LogControlRule()
00032   {}
00033   
00039   virtual LogControlRule *clone() const = 0;
00040   
00048   virtual bool next() = 0;
00049 };
00050 
00051 
00057 struct LogControlRuleAlways : public LogControlRule
00058 {
00063   virtual ~LogControlRuleAlways()
00064   {}
00065   
00070   LogControlRuleAlways()
00071   {}
00072 
00078   virtual LogControlRule *clone() const {
00079     return new LogControlRuleAlways(*this);
00080   }
00081 
00089   virtual bool next() {
00090     return true;
00091   }
00092 };
00093 
00094 
00095 struct LogControlRuleInterval : public LogControlRule
00096 {
00103   LogControlRuleInterval(int interval);
00104 
00109   virtual ~LogControlRuleInterval()
00110   {}
00111   
00117   virtual LogControlRule *clone() const {
00118     return new LogControlRuleInterval(*this);
00119   }
00120 
00129   virtual bool next();
00130   
00131 private:
00132   int           m_interval;
00133   int           m_count;
00134 };
00135 
00136 
00137 class RuleMap
00138 {
00139 public:
00140   typedef std::map<std::string, LogControlRule *, LessCase> Map;
00141 
00142   RuleMap()
00143     : m_ruleMap()
00144   {}
00145 
00146   ~RuleMap() {
00147     for (Map::iterator it = m_ruleMap.begin(); it != m_ruleMap.end(); ++it)
00148       delete (*it).second;
00149   }
00150   
00151   void addLogControlRule(const std::string &rule_name, const LogControlRule &rule) {
00152     Map::iterator it = m_ruleMap.find(rule_name);
00153     if (it != m_ruleMap.end())
00154       m_ruleMap.erase(it);
00155     
00156     m_ruleMap[rule_name] = rule.clone();
00157   }
00158 
00159   LogControlRule *getLogControlRule(const std::string &rule_name) {
00160     Map::iterator it = m_ruleMap.find(rule_name);
00161 
00162     if (it != m_ruleMap.end())
00163       return (*it).second;
00164 
00165     else {
00166       std::pair<Map::iterator, bool> result = m_ruleMap.insert(Map::value_type(rule_name, new LogControlRuleAlways));
00167       return (*result.first).second;
00168     }
00169   }
00170 
00171 private:
00172   Map           m_ruleMap;
00173 };
00174 
00175 
00181 enum State {
00182   ON,           
00183   CACHE         
00184 };
00185 
00202 class LogControl
00203 {
00204 public:
00214   LogControl(std::ostream &log_stream, const LogControlRule &rule);
00215 
00224   LogControl(std::ostream &log_stream,const std::string &rule_name);
00225   
00230   ~LogControl();
00231 
00237   void next();
00238 
00244   void fail();
00245 
00246 private:
00247   LogControl *          m_parent;               
00248   LogControlRule *      m_rule;                 
00249 
00250   State                 m_state;                
00251 
00252   std::ostream &        m_logStream;            
00253   std::streambuf *      m_logStreambuf;         
00254   std::ostringstream    m_cacheStream;          
00255 
00256   // Do not implement...
00257   LogControl(const LogControl&);
00258   LogControl & operator = (const LogControl&);
00259 };
00260 
00261 } // namespace stk
00262 
00263 #endif //  STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
Generated on Wed Apr 13 10:05:48 2011 for Sierra Toolkit by  doxygen 1.6.3