FACT++  1.0
MessageImp.cc
Go to the documentation of this file.
1 // **************************************************************************
11 // **************************************************************************
12 #include "MessageImp.h"
13 
14 #include <stdarg.h>
15 
16 #include <mutex>
17 
18 #include "tools.h"
19 #include "Time.h"
20 #include "WindowLog.h"
21 
22 using namespace std;
23 
24 // --------------------------------------------------------------------------
25 //
38 //
39 MessageImp::MessageImp(ostream &out) : fOut(out)
40 {
41 }
42 
43 // --------------------------------------------------------------------------
44 //
63 //
64 void MessageImp::StateChanged(const Time &time, const string &server, const string &msg, int state)
65 {
66  if (state<-1)
67  return;
68 
69  ostringstream out;
70  out << server << ": Changed state to " << state << " '" << msg << "' received.";
71 
72  Write(time, out.str(), MessageImp::kInfo);
73 
74  IndicateStateChange(time, server);
75 }
76 
77 // --------------------------------------------------------------------------
78 //
107 //
108 int MessageImp::WriteImp(const Time &time, const string &txt, int severity)
109 {
110  if (severity==kAlarm && txt.length()==0)
111  return 0;
112 
113  static mutex mtx;
114  const lock_guard<mutex> guard(mtx);
115 
116  switch (severity)
117  {
118  case kMessage: fOut << kDefault << " -> "; break;
119  case kComment: fOut << kDefault << " #> "; break;
120  case kInfo: fOut << kGreen << " I> "; break;
121  case kWarn: fOut << kYellow << " W> "; break;
122  case kError:
123  case kAlarm: fOut << kRed << " E> "; break;
124  case kFatal: fOut << kRed << kBlink << " !> "; break;
125  case kDebug: fOut << kBlue << " "; break;
126  default: fOut << kBold << " >> "; break;
127  }
128  fOut << time.GetAsStr("%H:%M:%S.%f") << " - " << txt << endl;
129 
130  return 0;
131 }
132 
133 int MessageImp::Write(const Time &time, const string &txt, int severity)
134 {
135  const uint32_t mjd = time.Mjd();
136 
137  if (fLastMjd != mjd)
138  WriteImp(time, "=================== "+time.GetAsStr("%Y-%m-%d")+" ["+to_string(mjd)+"] ==================");
139 
140  fLastMjd = mjd;
141 
142  WriteImp(time, txt, severity);
143  return 0;
144 }
145 
146 // --------------------------------------------------------------------------
147 //
155 //
156 int MessageImp::Update(const string &txt, int severity)
157 {
158  Write(Time(), txt, severity);
159  return 0;
160 }
161 
162 // --------------------------------------------------------------------------
163 //
172 //
173 /*
174 int MessageImp::Update(int severity, const char *fmt, ...)
175 {
176  va_list ap;
177  va_start(ap, fmt);
178  string str = Tools::Format(fmt, ap);
179  va_end(ap);
180  return Update(str, severity);
181 }
182 */
std::mutex mtx
Definition: EventBuilder.cc:95
A warning, things that somehow might result in unexpected or unwanted bahaviour.
Definition: MessageImp.h:18
Set color Green.
Definition: WindowLog.h:18
void StateChanged(const Time &time, const std::string &server, const std::string &msg, int state)
Definition: MessageImp.cc:64
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
Set color Yellow.
Definition: WindowLog.h:19
Set attribute Blink.
Definition: WindowLog.h:34
Set color Red.
Definition: WindowLog.h:17
Set default colors.
Definition: WindowLog.h:16
STL namespace.
int WriteImp(const Time &time, const std::string &txt, int qos=kMessage)
Mjd of last message.
Definition: MessageImp.cc:108
An info telling something which can be interesting to know.
Definition: MessageImp.h:17
Just a message, usually obsolete.
Definition: MessageImp.h:16
virtual int Write(const Time &time, const std::string &txt, int qos=kMessage)
Definition: MessageImp.cc:133
A message used for debugging only.
Definition: MessageImp.h:23
uint32_t fLastMjd
The ostream to which by default Write redirects its output.
Definition: MessageImp.h:28
void Mjd(double mjd)
Definition: Time.cc:145
std::ostream & fOut
Definition: MessageImp.h:27
Set color Blue.
Definition: WindowLog.h:20
int Update(const std::string &txt, int severity=kMessage)
Definition: MessageImp.cc:156
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
MessageImp(std::ostream &out=std::cout)
Definition: MessageImp.cc:39
Error, something unexpected happened, but can still be handled by the program.
Definition: MessageImp.h:19
A comment which is always printed.
Definition: MessageImp.h:22
Error, something unexpected happened, but needs user intervention (i.e. it needs a signal to the user...
Definition: MessageImp.h:20
virtual void IndicateStateChange(const Time &, const std::string &)
Definition: MessageImp.h:36
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
An error which cannot be handled at all happend, the only solution is program termination.
Definition: MessageImp.h:21
Set attribute Bold.
Definition: WindowLog.h:36