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

Last change on this file since 14814 was 14544, checked in by tbretz, 12 years ago
Use a lock guard instead of lock/unlock
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 static mutex mtx;
111 const lock_guard<mutex> guard(mtx);
112
113 switch (severity)
114 {
115 case kMessage: fOut << kDefault << " -> "; break;
116 case kComment: fOut << kDefault << " #> "; break;
117 case kInfo: fOut << kGreen << " I> "; break;
118 case kWarn: fOut << kYellow << " W> "; break;
119 case kError:
120 case kAlarm: fOut << kRed << " E> "; break;
121 case kFatal: fOut << kRed << kBlink << " !> "; break;
122 case kDebug: fOut << kBlue << " "; break;
123 default: fOut << kBold << " >> "; break;
124 }
125 fOut << time.GetAsStr("%H:%M:%S.%f") << " - " << txt << endl;
126
127 return 0;
128}
129
130// --------------------------------------------------------------------------
131//
132//! Calls Write with the current time the message text and the severity.
133//!
134//! @param txt
135//! The message text to be passed to Write
136//!
137//! @param severity
138//! The severity of the message to be passed to Write
139//
140int MessageImp::Update(const string &txt, int severity)
141{
142 Write(Time(), txt, severity);
143 return 0;
144}
145
146// --------------------------------------------------------------------------
147//
148//! Just a helper to format the message according to the user input.
149//! See the documentation of printf for details.
150//!
151//! @param severity
152//! The severity of the message to be passed to Write
153//!
154//! @param fmt
155//! Format string according to which the text is formatted
156//
157/*
158int MessageImp::Update(int severity, const char *fmt, ...)
159{
160 va_list ap;
161 va_start(ap, fmt);
162 string str = Tools::Format(fmt, ap);
163 va_end(ap);
164 return Update(str, severity);
165}
166*/
Note: See TracBrowser for help on using the repository browser.