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(std::ostream &out) : fOut(out)
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | // --------------------------------------------------------------------------
|
---|
42 | //
|
---|
43 | //! The basic implementation of the output of a message to the output
|
---|
44 | //! stream. This can overwritten by inheriting classes. The default is
|
---|
45 | //! to redirect the message to the stream fOut. In addition colors
|
---|
46 | //! from WindowLog are used depending on the severity. The color are ignored
|
---|
47 | //! if the stream is not of type WindowLog.
|
---|
48 | //!
|
---|
49 | //! The default message has the form:
|
---|
50 | //! ## 2011-02-22 11:13:32.000754 - Text message.
|
---|
51 | //!
|
---|
52 | //! while ## is a placeholder depending on the severity of the message, e.g.
|
---|
53 | //!
|
---|
54 | //! kMessage: default ->
|
---|
55 | //! kInfo: green I>
|
---|
56 | //! kWarn: yellow W>
|
---|
57 | //! kError: red E>
|
---|
58 | //! kFatal: red-blink !>
|
---|
59 | //! kDebug: blue
|
---|
60 | //! default: bold >>
|
---|
61 | //!
|
---|
62 | //! @param time
|
---|
63 | //! The time assigned to the message
|
---|
64 | //!
|
---|
65 | //! @param txt
|
---|
66 | //! The message text
|
---|
67 | //!
|
---|
68 | //! @param severity
|
---|
69 | //! The severity of the message
|
---|
70 | //
|
---|
71 | int MessageImp::Write(const Time &time, const char *txt, int severity)
|
---|
72 | {
|
---|
73 | switch (severity)
|
---|
74 | {
|
---|
75 | case kMessage: fOut << kDefault << " -> "; break;
|
---|
76 | case kInfo: fOut << kGreen << " I> "; break;
|
---|
77 | case kWarn: fOut << kYellow << " W> "; break;
|
---|
78 | case kError: fOut << kRed << " E> "; break;
|
---|
79 | case kFatal: fOut << kRed << kBlink << " !> "; break;
|
---|
80 | case kDebug: fOut << kBlue << " "; break;
|
---|
81 | default: fOut << kBold << " >> "; break;
|
---|
82 | }
|
---|
83 | fOut << time.GetAsStr() << " - " << txt << endl;
|
---|
84 |
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // --------------------------------------------------------------------------
|
---|
89 | //
|
---|
90 | //! Calls Write with the current time the message text and the severity.
|
---|
91 | //!
|
---|
92 | //! @param txt
|
---|
93 | //! The message text to be passed to Write
|
---|
94 | //!
|
---|
95 | //! @param severity
|
---|
96 | //! The severity of the message to be passed to Write
|
---|
97 | //
|
---|
98 | int MessageImp::Update(const char *txt, int severity)
|
---|
99 | {
|
---|
100 | Write(Time(), txt, severity);
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // --------------------------------------------------------------------------
|
---|
105 | //
|
---|
106 | //! Just a helper to format the message according to the user input.
|
---|
107 | //! See the documentation of printf for details.
|
---|
108 | //!
|
---|
109 | //! @param severity
|
---|
110 | //! The severity of the message to be passed to Write
|
---|
111 | //!
|
---|
112 | //! @param fmt
|
---|
113 | //! Format string according to which the text is formatted
|
---|
114 | //
|
---|
115 | int MessageImp::Update(int severity, const char *fmt, ...)
|
---|
116 | {
|
---|
117 | va_list ap;
|
---|
118 | va_start(ap, fmt);
|
---|
119 | string str = Format(fmt, ap);
|
---|
120 | va_end(ap);
|
---|
121 | return Update(str, severity);
|
---|
122 | }
|
---|