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

Last change on this file since 11014 was 10919, checked in by tbretz, 13 years ago
Added local mutices to protect Write calls from different threads - the current implementation is even overprotective
File size: 4.5 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//! kFatal: red-blink !>
95//! kDebug: blue
96//! default: bold >>
97//!
98//! @param time
99//! The time assigned to the message
100//!
101//! @param txt
102//! The message text
103//!
104//! @param severity
105//! The severity of the message
106//
107int MessageImp::Write(const Time &time, const string &txt, int severity)
108{
109 static mutex mtx;
110
111 mtx.lock();
112
113 switch (severity)
114 {
115 case kMessage: fOut << kDefault << " -> "; break;
116 case kInfo: fOut << kGreen << " I> "; break;
117 case kWarn: fOut << kYellow << " W> "; break;
118 case kError: fOut << kRed << " E> "; break;
119 case kFatal: fOut << kRed << kBlink << " !> "; break;
120 case kDebug: fOut << kBlue << " "; break;
121 default: fOut << kBold << " >> "; break;
122 }
123 fOut << time.GetAsStr() << " - " << txt << endl;
124
125 mtx.unlock();
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.