source: branches/fscctrl_safety_limits/src/MessageImp.cc@ 18558

Last change on this file since 18558 was 17341, checked in by tbretz, 11 years ago
Implemented an output if between two messages, the date changes. This makes it easier to follow a long log file.
File size: 5.1 KB
Line 
1// **************************************************************************
2/** @class MessageImp
3
4@brief The base implementation of a distributed messaging system
5
6
7Overwriting the Write() member function allows to change the look and
8feel and also the target of the messages issued through a MessageImp
9
10**/
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
22using namespace std;
23
24// --------------------------------------------------------------------------
25//
26//! Stores a reference to the given ostream in fOut. This is the stream to
27//! which all messaged issued are redirected by default if Write() has
28//! not been overwritten doing something else
29//!
30//! The stored reference can be accessed by either
31//! operator()() or Out()
32//!
33//! Note, that you have to ensure the stream which is references doesn't
34//! go out of scope while in use by MessageImp or one of its derivatives.
35//!
36//! @param out
37//! ostream to which the output should be redirected
38//
39MessageImp::MessageImp(ostream &out) : fOut(out)
40{
41}
42
43// --------------------------------------------------------------------------
44//
45//! This is a special write function formatting a string when the
46//! state of a state machine has changed.
47//!
48//! If the state is <-1 nothing is done.
49//!
50//! Calls the virtual function IndicateStateChange
51//!
52//! @param time
53//! The time assigned to the message
54//!
55//! @param server
56//! The server name which is emitting the state change
57//!
58//! @param msg
59//! The message text
60//!
61//! @param state
62//! The new state of the system
63//
64void 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//
79//! The basic implementation of the output of a message to the output
80//! stream. This can overwritten by inheriting classes. The default is
81//! to redirect the message to the stream fOut. In addition colors
82//! from WindowLog are used depending on the severity. The color are ignored
83//! if the stream is not of type WindowLog.
84//!
85//! The default message has the form:
86//! ## 2011-02-22 11:13:32.000754 - Text message.
87//!
88//! while ## is a placeholder depending on the severity of the message, e.g.
89//!
90//! kMessage: default ->
91//! kInfo: green I>
92//! kWarn: yellow W>
93//! kError: red E>
94//! kAlarm red E>
95//! kFatal: red-blink !>
96//! kDebug: blue
97//! default: bold >>
98//!
99//! @param time
100//! The time assigned to the message
101//!
102//! @param txt
103//! The message text
104//!
105//! @param severity
106//! The severity of the message
107//
108int 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
133int 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//
148//! Calls Write with the current time the message text and the severity.
149//!
150//! @param txt
151//! The message text to be passed to Write
152//!
153//! @param severity
154//! The severity of the message to be passed to Write
155//
156int MessageImp::Update(const string &txt, int severity)
157{
158 Write(Time(), txt, severity);
159 return 0;
160}
161
162// --------------------------------------------------------------------------
163//
164//! Just a helper to format the message according to the user input.
165//! See the documentation of printf for details.
166//!
167//! @param severity
168//! The severity of the message to be passed to Write
169//!
170//! @param fmt
171//! Format string according to which the text is formatted
172//
173/*
174int 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*/
Note: See TracBrowser for help on using the repository browser.