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

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