Sierra Toolkit  Version of the Day
LogControl.cpp
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 #include <map>
10 
11 #include <stk_util/environment/LogControl.hpp>
12 
13 namespace stk_classic {
14 
15 namespace {
16 
17 typedef std::map<std::ostream *, LogControl *> OStreamLogControlMap;
18 
19 OStreamLogControlMap &
20 get_ostream_log_control_map()
21 {
22  static OStreamLogControlMap s_ostreamLogControlMap;
23 
24  return s_ostreamLogControlMap;
25 }
26 
27 } // namespace <unnamed>
28 
29 
30 LogControlRuleInterval::LogControlRuleInterval(
31  int interval)
32  : m_interval(interval),
33  m_count(-1)
34 {}
35 
36 
37 bool
38 LogControlRuleInterval::next()
39 {
40  ++m_count;
41 
42  if (m_count < 0) {
43  return false;
44  }
45 
46  else if (m_count == 0) {
47  return true;
48  }
49 
50  else {
51  return m_count%m_interval == 0 ? true : false;
52  }
53 }
54 
55 
57  std::ostream & log_ostream,
58  const LogControlRule & rule)
59  : m_parent(0),
60  m_rule(rule.clone()),
61  m_state(ON),
62  m_logStream(log_ostream),
63  m_logStreambuf(log_ostream.rdbuf()),
64  m_cacheStream()
65 {
66  OStreamLogControlMap &ostream_log_control_map = get_ostream_log_control_map();
67 
68  // Append this as tail of linked list of LogControl's sharing this ostream.
69  m_parent = ostream_log_control_map[&m_logStream];
70  ostream_log_control_map[&m_logStream] = this;
71 
72  // Make sure log stream buffer is that of the root's.
73  for (LogControl *parent = m_parent; parent != 0; parent = parent->m_parent)
74  m_logStreambuf = parent->m_logStream.rdbuf();
75 }
76 
77 
78 // LogControl::LogControl(
79 // std::ostream & log_ostream,
80 // const std::string & rule_name)
81 // : m_parent(0),
82 // m_rule(get_rule_map().getLogControlRule(rule_name)->clone()),
83 // m_state(ON),
84 // m_logStream(log_ostream),
85 // m_logStreambuf(log_ostream.rdbuf()),
86 // m_cacheStream()
87 // {
88 // OStreamLogControlMap &ostream_log_control_map = get_ostream_log_control_map();
89 
90 // // Append this as tail of linked list of LogControl's sharing this ostream.
91 // m_parent = ostream_log_control_map[&m_logStream];
92 // ostream_log_control_map[&m_logStream] = this;
93 // }
94 
95 
97 {
98  OStreamLogControlMap &ostream_log_control_map = get_ostream_log_control_map();
99 
100  // Reset tail pointer to this's parent.
101  ostream_log_control_map[&m_logStream] = m_parent;
102 
103  // Reset log stream to either the parent's cache or the original log stream buffer. And,
104  // concatenate cached text to the parent's cache or log stream.
105  if (!m_parent || m_parent->m_state == ON) { // Parent is writing
106  m_logStream.rdbuf(m_logStreambuf); // Set output stream back to real buffer
107  if (m_state == CACHE) // This is caching
108  m_logStream << m_cacheStream.str(); // Last cache is always written
109  }
110  else { // Parent is caching
111  m_logStream.rdbuf(m_parent->m_cacheStream.rdbuf()); // Set output to parent's cache
112  m_parent->m_cacheStream << m_cacheStream.str(); // Append our cache to parent's
113  }
114 
115  delete m_rule;
116 }
117 
118 
119 void
121 {
122  m_logStream.rdbuf(m_logStreambuf);
123  m_logStream << m_cacheStream.str();
124  m_cacheStream.str("");
125  m_state = ON;
126 }
127 
128 
129 void
131 {
132  m_cacheStream.str("");
133 
134  if (m_parent && m_parent->m_state == CACHE)
135  m_state = CACHE;
136  else
137  m_state = !m_rule || m_rule->next() ? ON : CACHE;
138 
139  if (m_state != CACHE) {
140  if (m_logStream.rdbuf() != m_logStreambuf) {
141  m_logStream.rdbuf(m_logStreambuf);
142  }
143  }
144  else {
145  if (m_logStream.rdbuf() != m_cacheStream.rdbuf())
146  m_logStream.rdbuf(m_cacheStream.rdbuf());
147  }
148 }
149 
150 
151 } // namespace stk_classic
virtual bool next()=0
Member function next returns true if the log stream should write to the log file, and false if the lo...
Output is to be written to the cache stream.
Definition: LogControl.hpp:183
LogControl(std::ostream &log_stream, const LogControlRule &rule)
Definition: LogControl.cpp:56
void fail()
Member function fail writes the cached output to the log stream due to an error.
Definition: LogControl.cpp:120
void next()
Member function next executes the rule and sets the log stream to write to the log file if true and t...
Definition: LogControl.cpp:130
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
Sierra Toolkit.
Output is to be written to the log stream.
Definition: LogControl.hpp:182