Sierra Toolkit  Version of the Day
LogControl.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
10 #define STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
11 
12 #include <ostream>
13 #include <sstream>
14 #include <string>
15 #include <map>
16 
17 #include <stk_util/util/string_case_compare.hpp>
18 
19 namespace stk_classic {
20 
26 {
31  virtual ~LogControlRule()
32  {}
33 
39  virtual LogControlRule *clone() const = 0;
40 
48  virtual bool next() = 0;
49 };
50 
51 
58 {
64  {}
65 
71  {}
72 
78  virtual LogControlRule *clone() const {
79  return new LogControlRuleAlways(*this);
80  }
81 
89  virtual bool next() {
90  return true;
91  }
92 };
93 
94 
95 struct LogControlRuleInterval : public LogControlRule
96 {
103  LogControlRuleInterval(int interval);
104 
109  virtual ~LogControlRuleInterval()
110  {}
111 
117  virtual LogControlRule *clone() const {
118  return new LogControlRuleInterval(*this);
119  }
120 
129  virtual bool next();
130 
131 private:
132  int m_interval;
133  int m_count;
134 };
135 
136 
137 class RuleMap
138 {
139 public:
140  typedef std::map<std::string, LogControlRule *, LessCase> Map;
141 
142  RuleMap()
143  : m_ruleMap()
144  {}
145 
146  ~RuleMap() {
147  for (Map::iterator it = m_ruleMap.begin(); it != m_ruleMap.end(); ++it)
148  delete (*it).second;
149  }
150 
151  void addLogControlRule(const std::string &rule_name, const LogControlRule &rule) {
152  Map::iterator it = m_ruleMap.find(rule_name);
153  if (it != m_ruleMap.end())
154  m_ruleMap.erase(it);
155 
156  m_ruleMap[rule_name] = rule.clone();
157  }
158 
159  LogControlRule *getLogControlRule(const std::string &rule_name) {
160  Map::iterator it = m_ruleMap.find(rule_name);
161 
162  if (it != m_ruleMap.end())
163  return (*it).second;
164 
165  else {
166  std::pair<Map::iterator, bool> result = m_ruleMap.insert(Map::value_type(rule_name, new LogControlRuleAlways));
167  return (*result.first).second;
168  }
169  }
170 
171 private:
172  Map m_ruleMap;
173 };
174 
175 
181 enum State {
182  ON,
184 };
185 
203 {
204 public:
214  LogControl(std::ostream &log_stream, const LogControlRule &rule);
215 
224  LogControl(std::ostream &log_stream,const std::string &rule_name);
225 
230  ~LogControl();
231 
237  void next();
238 
244  void fail();
245 
246 private:
247  LogControl * m_parent;
248  LogControlRule * m_rule;
249 
250  State m_state;
251 
252  std::ostream & m_logStream;
253  std::streambuf * m_logStreambuf;
254  std::ostringstream m_cacheStream;
255 
256  // Do not implement...
257  LogControl(const LogControl&);
258  LogControl & operator = (const LogControl&);
259 };
260 
261 } // namespace stk_classic
262 
263 #endif // STK_UTIL_ENVIRONMENT_LOGCONTROL_HPP
virtual bool next()=0
Member function next returns true if the log stream should write to the log file, and false if the lo...
virtual LogControlRule * clone() const =0
Member function clone creates a clone of the rule.
Output is to be written to the cache stream.
Definition: LogControl.hpp:183
Class LogControl provides a mechanism for reducing excessive output. The output is redirected to a ca...
Definition: LogControl.hpp:202
Interface LogControlRule describes the interface to a log control rule.
Definition: LogControl.hpp:25
virtual LogControlRule * clone() const
Member function clone creates a duplicate LogControlRuleAlways object.
Definition: LogControl.hpp:78
virtual bool next()
Member function next returns true to indicate that the log stream should write to the log file...
Definition: LogControl.hpp:89
Sierra Toolkit.
Output is to be written to the log stream.
Definition: LogControl.hpp:182
State
Enumeration State describes the current state of the caching for this controller. ...
Definition: LogControl.hpp:181
Class LogControlRuleAlways is a log control rule that always wants to write to the log file...
Definition: LogControl.hpp:57