source: trunk/FACT++/src/MessageImp.cc@ 17205

Last change on this file since 17205 was 15080, checked in by tbretz, 12 years ago
If alarms are cleared sending an empty string, it is not necessary to produce any output on the console.
File size: 4.7 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::Write(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// --------------------------------------------------------------------------
134//
135//! Calls Write with the current time the message text and the severity.
136//!
137//! @param txt
138//! The message text to be passed to Write
139//!
140//! @param severity
141//! The severity of the message to be passed to Write
142//
143int MessageImp::Update(const string &txt, int severity)
144{
145 Write(Time(), txt, severity);
146 return 0;
147}
148
149// --------------------------------------------------------------------------
150//
151//! Just a helper to format the message according to the user input.
152//! See the documentation of printf for details.
153//!
154//! @param severity
155//! The severity of the message to be passed to Write
156//!
157//! @param fmt
158//! Format string according to which the text is formatted
159//
160/*
161int MessageImp::Update(int severity, const char *fmt, ...)
162{
163 va_list ap;
164 va_start(ap, fmt);
165 string str = Tools::Format(fmt, ap);
166 va_end(ap);
167 return Update(str, severity);
168}
169*/
Note: See TracBrowser for help on using the repository browser.