| 1 | // **************************************************************************
|
|---|
| 2 | /** @class MessageImp
|
|---|
| 3 |
|
|---|
| 4 | @brief The base implementation of a distributed messaging system
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | Overwriting the Write() member function allows to change the look and
|
|---|
| 8 | feel 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 "tools.h"
|
|---|
| 17 | #include "Time.h"
|
|---|
| 18 | #include "WindowLog.h"
|
|---|
| 19 |
|
|---|
| 20 | using namespace std;
|
|---|
| 21 |
|
|---|
| 22 | // --------------------------------------------------------------------------
|
|---|
| 23 | //
|
|---|
| 24 | //! Stores a reference to the given ostream in fOut. This is the stream to
|
|---|
| 25 | //! which all messaged issued are redirected by default if Write() has
|
|---|
| 26 | //! not been overwritten doing something else
|
|---|
| 27 | //!
|
|---|
| 28 | //! The stored reference can be accessed by either
|
|---|
| 29 | //! operator()() or Out()
|
|---|
| 30 | //!
|
|---|
| 31 | //! Note, that you have to ensure the stream which is references doesn't
|
|---|
| 32 | //! go out of scope while in use by MessageImp or one of its derivatives.
|
|---|
| 33 | //!
|
|---|
| 34 | //! @param out
|
|---|
| 35 | //! ostream to which the output should be redirected
|
|---|
| 36 | //
|
|---|
| 37 | MessageImp::MessageImp(ostream &out) : fOut(out)
|
|---|
| 38 | {
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | // --------------------------------------------------------------------------
|
|---|
| 42 | //
|
|---|
| 43 | //! This is a special write function formatting a string when the
|
|---|
| 44 | //! state of a state machine has changed.
|
|---|
| 45 | //!
|
|---|
| 46 | //! If the state is <-1 nothing is done.
|
|---|
| 47 | //!
|
|---|
| 48 | //! Calls the virtual function IndicateStateChange
|
|---|
| 49 | //!
|
|---|
| 50 | //! @param time
|
|---|
| 51 | //! The time assigned to the message
|
|---|
| 52 | //!
|
|---|
| 53 | //! @param server
|
|---|
| 54 | //! The server name which is emitting the state change
|
|---|
| 55 | //!
|
|---|
| 56 | //! @param msg
|
|---|
| 57 | //! The message text
|
|---|
| 58 | //!
|
|---|
| 59 | //! @param state
|
|---|
| 60 | //! The new state of the system
|
|---|
| 61 | //
|
|---|
| 62 | void MessageImp::StateChanged(const Time &time, const string &server, const string &msg, int state)
|
|---|
| 63 | {
|
|---|
| 64 | if (state<-1)
|
|---|
| 65 | return;
|
|---|
| 66 |
|
|---|
| 67 | ostringstream out;
|
|---|
| 68 | out << server << ": Changed state to " << state << " '" << msg << "' received.";
|
|---|
| 69 |
|
|---|
| 70 | Write(time, out.str(), MessageImp::kInfo);
|
|---|
| 71 |
|
|---|
| 72 | IndicateStateChange(time, server);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // --------------------------------------------------------------------------
|
|---|
| 76 | //
|
|---|
| 77 | //! The basic implementation of the output of a message to the output
|
|---|
| 78 | //! stream. This can overwritten by inheriting classes. The default is
|
|---|
| 79 | //! to redirect the message to the stream fOut. In addition colors
|
|---|
| 80 | //! from WindowLog are used depending on the severity. The color are ignored
|
|---|
| 81 | //! if the stream is not of type WindowLog.
|
|---|
| 82 | //!
|
|---|
| 83 | //! The default message has the form:
|
|---|
| 84 | //! ## 2011-02-22 11:13:32.000754 - Text message.
|
|---|
| 85 | //!
|
|---|
| 86 | //! while ## is a placeholder depending on the severity of the message, e.g.
|
|---|
| 87 | //!
|
|---|
| 88 | //! kMessage: default ->
|
|---|
| 89 | //! kInfo: green I>
|
|---|
| 90 | //! kWarn: yellow W>
|
|---|
| 91 | //! kError: red E>
|
|---|
| 92 | //! kFatal: red-blink !>
|
|---|
| 93 | //! kDebug: blue
|
|---|
| 94 | //! default: bold >>
|
|---|
| 95 | //!
|
|---|
| 96 | //! @param time
|
|---|
| 97 | //! The time assigned to the message
|
|---|
| 98 | //!
|
|---|
| 99 | //! @param txt
|
|---|
| 100 | //! The message text
|
|---|
| 101 | //!
|
|---|
| 102 | //! @param severity
|
|---|
| 103 | //! The severity of the message
|
|---|
| 104 | //
|
|---|
| 105 | int MessageImp::Write(const Time &time, const string &txt, int severity)
|
|---|
| 106 | {
|
|---|
| 107 | switch (severity)
|
|---|
| 108 | {
|
|---|
| 109 | case kMessage: fOut << kDefault << " -> "; break;
|
|---|
| 110 | case kInfo: fOut << kGreen << " I> "; break;
|
|---|
| 111 | case kWarn: fOut << kYellow << " W> "; break;
|
|---|
| 112 | case kError: fOut << kRed << " E> "; break;
|
|---|
| 113 | case kFatal: fOut << kRed << kBlink << " !> "; break;
|
|---|
| 114 | case kDebug: fOut << kBlue << " "; break;
|
|---|
| 115 | default: fOut << kBold << " >> "; break;
|
|---|
| 116 | }
|
|---|
| 117 | fOut << time.GetAsStr() << " - " << txt << endl;
|
|---|
| 118 |
|
|---|
| 119 | return 0;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // --------------------------------------------------------------------------
|
|---|
| 123 | //
|
|---|
| 124 | //! Calls Write with the current time the message text and the severity.
|
|---|
| 125 | //!
|
|---|
| 126 | //! @param txt
|
|---|
| 127 | //! The message text to be passed to Write
|
|---|
| 128 | //!
|
|---|
| 129 | //! @param severity
|
|---|
| 130 | //! The severity of the message to be passed to Write
|
|---|
| 131 | //
|
|---|
| 132 | int MessageImp::Update(const string &txt, int severity)
|
|---|
| 133 | {
|
|---|
| 134 | Write(Time(), txt, severity);
|
|---|
| 135 | return 0;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | // --------------------------------------------------------------------------
|
|---|
| 139 | //
|
|---|
| 140 | //! Just a helper to format the message according to the user input.
|
|---|
| 141 | //! See the documentation of printf for details.
|
|---|
| 142 | //!
|
|---|
| 143 | //! @param severity
|
|---|
| 144 | //! The severity of the message to be passed to Write
|
|---|
| 145 | //!
|
|---|
| 146 | //! @param fmt
|
|---|
| 147 | //! Format string according to which the text is formatted
|
|---|
| 148 | //
|
|---|
| 149 | /*
|
|---|
| 150 | int MessageImp::Update(int severity, const char *fmt, ...)
|
|---|
| 151 | {
|
|---|
| 152 | va_list ap;
|
|---|
| 153 | va_start(ap, fmt);
|
|---|
| 154 | string str = Tools::Format(fmt, ap);
|
|---|
| 155 | va_end(ap);
|
|---|
| 156 | return Update(str, severity);
|
|---|
| 157 | }
|
|---|
| 158 | */
|
|---|