0031037: Foundation Classes - add class Message_PrinterSystemLog for printing message...
[occt.git] / src / Message / Message_Messenger.hxx
1 // Created on: 2007-06-28
2 // Created by: OCC Team
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Message_Messenger_HeaderFile
17 #define _Message_Messenger_HeaderFile
18
19 #include <Message_Gravity.hxx>
20 #include <Message_SequenceOfPrinters.hxx>
21 #include <Standard_Transient.hxx>
22 #include <Standard_Boolean.hxx>
23 #include <Standard_Integer.hxx>
24 #include <Standard_Type.hxx>
25 #include <Standard_CString.hxx>
26 #include <TCollection_HAsciiString.hxx>
27 #include <TCollection_HExtendedString.hxx>
28
29 class Message_Printer;
30
31 // resolve name collisions with WinAPI headers
32 #ifdef AddPrinter
33   #undef AddPrinter
34 #endif
35
36 class Message_Messenger;
37 DEFINE_STANDARD_HANDLE(Message_Messenger, Standard_Transient)
38
39 //! Messenger is API class providing general-purpose interface for
40 //! libraries that may issue text messages without knowledge
41 //! of how these messages will be further processed.
42 //!
43 //! The messenger contains a sequence of "printers" which can be
44 //! customized by the application, and dispatches every received
45 //! message to all the printers.
46 //!
47 //! For convenience, a number of operators << are defined with left
48 //! argument being Handle(Message_Messenger); thus it can be used
49 //! with syntax similar to C++ streams.
50 //! Note that all these operators use trace level Warning.
51 class Message_Messenger : public Standard_Transient
52 {
53   DEFINE_STANDARD_RTTIEXT(Message_Messenger, Standard_Transient)
54 public:
55
56   //! Empty constructor; initializes by single printer directed to std::cout.
57   //! Note: the default messenger is not empty but directed to cout
58   //! in order to protect against possibility to forget defining printers.
59   //! If printing to cout is not needed, clear messenger by GetPrinters().Clear()
60   Standard_EXPORT Message_Messenger();
61   
62   //! Create messenger with single printer
63   Standard_EXPORT Message_Messenger(const Handle(Message_Printer)& thePrinter);
64   
65   //! Add a printer to the messenger.
66   //! The printer will be added only if it is not yet in the list.
67   //! Returns True if printer has been added.
68   Standard_EXPORT Standard_Boolean AddPrinter (const Handle(Message_Printer)& thePrinter);
69   
70   //! Removes specified printer from the messenger.
71   //! Returns True if this printer has been found in the list
72   //! and removed.
73   Standard_EXPORT Standard_Boolean RemovePrinter (const Handle(Message_Printer)& thePrinter);
74   
75   //! Removes printers of specified type (including derived classes)
76   //! from the messenger.
77   //! Returns number of removed printers.
78   Standard_EXPORT Standard_Integer RemovePrinters (const Handle(Standard_Type)& theType);
79   
80   //! Returns current sequence of printers
81   const Message_SequenceOfPrinters& Printers() const { return myPrinters; }
82
83   //! Returns sequence of printers
84   //! The sequence can be modified.
85   Message_SequenceOfPrinters& ChangePrinters() { return myPrinters; }
86
87   //! Dispatch a message to all the printers in the list.
88   //! Three versions of string representations are accepted for
89   //! convenience, by default all are converted to ExtendedString.
90   //! The parameter putEndl specifies whether the new line should
91   //! be started after this message (default) or not (may have
92   //! sense in some conditions).
93   Standard_EXPORT void Send (const Standard_CString theString, const Message_Gravity theGravity = Message_Warning, const Standard_Boolean putEndl = Standard_True) const;
94   
95   //! See above
96   Standard_EXPORT void Send (const TCollection_AsciiString& theString, const Message_Gravity theGravity = Message_Warning, const Standard_Boolean putEndl = Standard_True) const;
97   
98   //! See above
99   Standard_EXPORT void Send (const TCollection_ExtendedString& theString, const Message_Gravity theGravity = Message_Warning, const Standard_Boolean putEndl = Standard_True) const;
100
101 private:
102
103   Message_SequenceOfPrinters myPrinters;
104
105 };
106
107 // CString
108 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
109                                                     const Standard_CString theStr)
110 {
111   theMessenger->Send (theStr, Message_Info, Standard_False);
112   return theMessenger;
113 }
114
115 // AsciiString
116 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
117                                                     const TCollection_AsciiString& theStr)
118 {
119   theMessenger->Send (theStr, Message_Info, Standard_False);
120   return theMessenger;
121 }
122
123 // HAsciiString
124 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
125                                                     const Handle(TCollection_HAsciiString)& theStr)
126 {
127   theMessenger->Send (theStr->String(), Message_Info, Standard_False);
128   return theMessenger;
129 }
130
131 // ExtendedString
132 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
133                                                     const TCollection_ExtendedString& theStr)
134 {
135   theMessenger->Send (theStr, Message_Info, Standard_False);
136   return theMessenger;
137 }
138
139 // HExtendedString
140 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
141                                                     const Handle(TCollection_HExtendedString)& theStr)
142 {
143   theMessenger->Send (theStr->String(), Message_Info, Standard_False);
144   return theMessenger;
145 }
146
147 // Integer
148 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
149                                                     const Standard_Integer theVal)
150 {
151   TCollection_AsciiString aStr (theVal);
152   theMessenger->Send (aStr, Message_Info, Standard_False);
153   return theMessenger;
154 }
155
156 // Real
157 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
158                                                     const Standard_Real theVal)
159 {
160   TCollection_AsciiString aStr (theVal);
161   theMessenger->Send (aStr, Message_Info, Standard_False);
162   return theMessenger;
163 }
164
165 // Stream
166 inline const Handle(Message_Messenger)& operator<< (const Handle(Message_Messenger)& theMessenger,
167                                                     const Standard_SStream& theStream)
168 {
169   theMessenger->Send (theStream.str().c_str(), Message_Info, Standard_False);
170   return theMessenger;
171 }
172
173 // manipulators
174 inline const Handle(Message_Messenger)&
175        operator << (const Handle(Message_Messenger)& theMessenger,
176                     const Handle(Message_Messenger)& (*pman) (const Handle(Message_Messenger)&))
177 {
178   return pman (theMessenger);
179 }
180
181 // Message_EndLine
182 inline const Handle(Message_Messenger)& Message_EndLine (const Handle(Message_Messenger)& theMessenger)
183 {
184   theMessenger->Send ("", Message_Info, Standard_True);
185   return theMessenger;
186 }
187
188 #endif // _Message_Messenger_HeaderFile