FACT++  1.0
EventImp.cc
Go to the documentation of this file.
1 // **************************************************************************
108 // **************************************************************************
109 #include "EventImp.h"
110 
111 #include <sstream>
112 
113 #include "Time.h"
114 #include "WindowLog.h"
115 #include "Description.h"
116 
117 using namespace std;
118 
119 // --------------------------------------------------------------------------
120 //
123 //
124 EventImp::EventImp(const EventImp &cmd) : fAllowedStates(cmd.fAllowedStates),
125  fFunction(cmd.fFunction)
126 {
127 }
128 
129 // --------------------------------------------------------------------------
130 //
135 //
137 {
138  if (state>=0)
139  fAllowedStates.push_back(state);
140 }
141 
142 // --------------------------------------------------------------------------
143 //
150 //
152 {
153  stringstream stream(states);
154 
155  const bool sep = stream.str().find(',')==string::npos;
156 
157  string buffer;
158  while (getline(stream, buffer, sep ? ' ' : ','))
159  AddAllowedState(stoi(buffer));
160 }
161 
162 // --------------------------------------------------------------------------
163 //
173 //
174 bool EventImp::IsStateAllowed(int state) const
175 {
176  // States with negative values are internal states and are
177  // never allowed
178  // if (state<0)
179  // return false;
180 
181  // In case no allowed state is explicitly set
182  // all positive states are allowed
183  if (fAllowedStates.size()==0)
184  return true;
185 
186  return find(fAllowedStates.begin(), fAllowedStates.end(), state)!=fAllowedStates.end();
187 }
188 
189 // --------------------------------------------------------------------------
190 //
194 string EventImp::GetString() const
195 {
196  size_t s = GetSize()-1;
197  while (s>0 && GetText()[s]==0)
198  s--;
199 
200  return std::string(GetText(), s+1);
201 }
202 
203 // --------------------------------------------------------------------------
204 //
214 void EventImp::Print(ostream &out, bool strip) const
215 {
216  if (GetDescription().empty())
217  return;
218 
219  out << " -";
220 
221  const string str = GetName();
222  if (!str.empty())
223  out << kBold << str.substr(strip?str.find_first_of('/')+1:0) << kReset << "-";
224 
225  const string fmt = GetFormat();
226 
227  if (!str.empty() && !fmt.empty())
228  out << " ";
229 
230  if (!fmt.empty())
231  out << "[" << fmt << "]";
232 
233  vector<Description> v = Description::SplitDescription(GetDescription());
234 
235  if (!GetDescription().empty())
236  {
237  out << kBold;
238  for (vector<Description>::const_iterator j=v.begin()+1;
239  j!=v.end(); j++)
240  out << " <" << j->name << ">";
241  out << kReset;
242  }
243 
244  for (unsigned int i=0; i<fAllowedStates.size(); i++)
245  out << " " << fAllowedStates[i];
246 
247  const Time tm = GetTime();
248 
249  const bool t = tm!=Time::None && tm!=Time(1970,1,1);
250  const bool s = GetSize()>0;
251 
252  if (s || t)
253  out << "(";
254  if (t)
255  out << tm.GetAsStr("%H:%M:%S.%f");
256  if (s && t)
257  out << "/";
258  if (s)
259  out << "size=" << GetSize();
260  if (s || t)
261  out << ")";
262  out << endl;
263 
264  if (GetDescription().empty())
265  {
266  out << endl;
267  return;
268  }
269 
270  out << " " << v[0].comment << endl;
271 
272  for (vector<Description>::const_iterator j=v.begin()+1;
273  j!=v.end(); j++)
274  {
275  out << " ||" << kGreen << j->name;
276  if (!j->comment.empty())
277  out << kReset << ": " << kBlue << j->comment;
278  if (!j->unit.empty())
279  out << kYellow << " [" << j->unit << "]";
280  out << endl;
281  }
282  out << endl;
283 }
284 
285 // --------------------------------------------------------------------------
286 //
292 //
293 void EventImp::Print(bool strip) const
294 {
295  Print(cout, strip);
296 }
297 
298 string EventImp::GetTimeAsStr(const char *fmt) const
299 {
300  return GetTime().GetAsStr(fmt);
301 }
302 
303 uint64_t EventImp::GetJavaDate() const
304 {
305  return GetTime().JavaDate();
306 }
void AddAllowedStates(const std::string &states)
Definition: EventImp.cc:151
virtual std::string GetDescription() const
Definition: EventImp.h:27
uint64_t JavaDate() const
Definition: Time.h:111
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
const char * GetText() const
Definition: EventImp.h:88
EventImp()
Constructor. Stores the target state given.
Definition: EventImp.h:20
int i
Definition: db_dim_client.c:21
Set color Green.
Definition: WindowLog.h:18
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
Set color Yellow.
Definition: WindowLog.h:19
std::vector< int > fAllowedStates
Definition: EventImp.h:13
STL namespace.
std::string GetString() const
Definition: EventImp.cc:194
virtual void Print(std::ostream &out, bool strip=false) const
Definition: EventImp.cc:214
virtual Time GetTime() const
Definition: EventImp.h:57
virtual std::string GetFormat() const
Definition: EventImp.h:52
Set color Blue.
Definition: WindowLog.h:20
bool IsStateAllowed(int state) const
Definition: EventImp.cc:174
static std::vector< Description > SplitDescription(const std::string &buffer)
Definition: Description.cc:82
static const Time None
A none-time, this can be used as a simple representation of an invalid time.
Definition: Time.h:34
void AddAllowedState(int state)
Definition: EventImp.cc:136
uint64_t GetJavaDate() const
Definition: EventImp.cc:303
std::string GetTimeAsStr(const char *fmt) const
Definition: EventImp.cc:298
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
Warning states
Definition: smartfact.txt:92
virtual std::string GetName() const
Definition: EventImp.h:51
TT t
Definition: test_client.c:26
if(extraDns) new Dns
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
Reset all attributes.
Definition: WindowLog.h:29
Set attribute Bold.
Definition: WindowLog.h:36
virtual size_t GetSize() const
Definition: EventImp.h:55