1 // Created on: 2017-06-26
2 // Created by: Andrey Betenev
3 // Copyright (c) 2017 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_Report_HeaderFile
17 #define _Message_Report_HeaderFile
18 
19 #include <Message_Gravity.hxx>
20 #include <Message_Level.hxx>
21 #include <Message_ListOfAlert.hxx>
22 #include <Message_MetricType.hxx>
23 #include <NCollection_IndexedMap.hxx>
24 #include <NCollection_Sequence.hxx>
25 #include <Standard_Mutex.hxx>
26 
27 class Message_CompositeAlerts;
28 class Message_Messenger;
29 
30 class Message_Report;
31 
32 DEFINE_STANDARD_HANDLE(Message_Report, Standard_Transient)
33 
34 //! Container for alert messages, sorted according to their gravity.
35 //!
36 //! For each gravity level, alerts are stored in simple list.
37 //! If alert being added can be merged with another alert of the same
38 //! type already in the list, it is merged and not added to the list.
39 //!
40 //! This class is intended to be used as follows:
41 //!
42 //! - In the process of execution, algorithm fills report by alert objects
43 //!   using methods AddAlert()
44 //!
45 //! - The result can be queried for presence of particular alert using
46 //!   methods HasAlert()
47 //!
48 //! - The reports produced by nested or sequentially executed algorithms
49 //!   can be collected in one using method Merge()
50 //!
51 //! - The report can be shown to the user either as plain text with method
52 //!   Dump() or in more advanced way, by iterating over lists returned by GetAlerts()
53 //!
54 //! - Report can be cleared by methods Clear() (usually after reporting)
55 //!
56 //! Message_PrinterToReport is a printer in Messenger to convert data sent to messenger into report
57 class Message_Report : public Standard_Transient
58 {
59 public:
60 
61   //! Empty constructor
62   Standard_EXPORT Message_Report ();
63 
64   //! Add alert with specified gravity.
65   //! This method is thread-safe, i.e. alerts can be added from parallel threads safely.
66   Standard_EXPORT void AddAlert (Message_Gravity theGravity, const Handle(Message_Alert)& theAlert);
67 
68   //! Returns list of collected alerts with specified gravity
69   Standard_EXPORT const Message_ListOfAlert& GetAlerts (Message_Gravity theGravity) const;
70 
71   //! Returns true if specific type of alert is recorded
72   Standard_EXPORT Standard_Boolean HasAlert (const Handle(Standard_Type)& theType);
73 
74   //! Returns true if specific type of alert is recorded with specified gravity
75   Standard_EXPORT Standard_Boolean HasAlert (const Handle(Standard_Type)& theType, Message_Gravity theGravity);
76 
77   //! Returns true if a report printer for the current report is registered in the messenger
78   //! @param theMessenger the messenger. If it's NULL, the default messenger is used
79   Standard_EXPORT Standard_Boolean IsActiveInMessenger (const Handle(Message_Messenger)& theMessenger = NULL) const;
80 
81   //! Creates an instance of Message_PrinterToReport with the current report and register it in messenger
82   //! @param toActivate if true, activated else deactivated
83   //! @param theMessenger the messenger. If it's NULL, the default messenger is used
84   Standard_EXPORT void ActivateInMessenger (const Standard_Boolean toActivate,
85                                             const Handle(Message_Messenger)& theMessenger = NULL);
86 
87   //! Updates internal flag IsActiveInMessenger.
88   //! It becomes true if messenger contains at least one instance of Message_PrinterToReport.
89   //! @param theMessenger the messenger. If it's NULL, the default messenger is used
90   Standard_EXPORT void UpdateActiveInMessenger (const Handle(Message_Messenger)& theMessenger = NULL);
91 
92   //! Add new level of alerts
93   //! @param theLevel a level
94   Standard_EXPORT void AddLevel (Message_Level* theLevel, const TCollection_AsciiString& theName);
95 
96   //! Remove level of alerts
97   Standard_EXPORT void RemoveLevel (Message_Level* theLevel);
98 
99   //! Clears all collected alerts
100   Standard_EXPORT void Clear ();
101 
102   //! Clears collected alerts with specified gravity
103   Standard_EXPORT void Clear (Message_Gravity theGravity);
104 
105   //! Clears collected alerts with specified type
106   Standard_EXPORT void Clear (const Handle(Standard_Type)& theType);
107 
108   //! Returns computed metrics when alerts are performed
ActiveMetrics() const109   const NCollection_IndexedMap<Message_MetricType>& ActiveMetrics() const { return myActiveMetrics; }
110 
111   //! Sets metrics to compute when alerts are performed
112   //! @param theMetrics container of metrics
113   Standard_EXPORT void SetActiveMetric (const Message_MetricType theMetricType, const Standard_Boolean theActivate);
114 
115   //! Removes all activated metrics
ClearMetrics()116   void ClearMetrics() { myActiveMetrics.Clear(); }
117 
118   //! Returns maximum number of collecting alerts. If the limit is achieved,
119   //! first alert is removed, the new alert is added in the container.
120   //! @return the limit value
Limit() const121   Standard_Integer Limit() const { return myLimit; }
122 
123   //! Sets maximum number of collecting alerts.
124   //! @param theLimit limit value
SetLimit(const Standard_Integer theLimit)125   void SetLimit(const Standard_Integer theLimit) { myLimit = theLimit; }
126 
127   //! Dumps all collected alerts to stream
128   Standard_EXPORT void Dump (Standard_OStream& theOS);
129 
130   //! Dumps collected alerts with specified gravity to stream
131   Standard_EXPORT void Dump (Standard_OStream& theOS, Message_Gravity theGravity);
132 
133   //! Sends all collected alerts to messenger.
134   Standard_EXPORT virtual void SendMessages (const Handle(Message_Messenger)& theMessenger);
135 
136   //! Dumps collected alerts with specified gravity to messenger.
137   //! Default implementation creates Message_Msg object with a message
138   //! key returned by alert, and sends it in the messenger.
139   Standard_EXPORT virtual void SendMessages (const Handle(Message_Messenger)& theMessenger,
140                                              Message_Gravity theGravity);
141 
142   //! Merges data from theOther report into this
143   Standard_EXPORT void Merge (const Handle(Message_Report)& theOther);
144 
145   //! Merges alerts with specified gravity from theOther report into this
146   Standard_EXPORT void Merge (const Handle(Message_Report)& theOther, Message_Gravity theGravity);
147 
148   //! Dumps the content of me into the stream
149   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
150 
151   // OCCT RTTI
152   DEFINE_STANDARD_RTTIEXT(Message_Report,Standard_Transient)
153 
154 protected:
155   //! Returns class provided hierarchy of alerts if created or create if the parameter is true
156   //! @param isCreate if composite alert has not been created for this alert, it should be created
157   //! @return instance or NULL
158   Standard_EXPORT const Handle(Message_CompositeAlerts)& compositeAlerts (const Standard_Boolean isCreate = Standard_False);
159 
160   //! Sends alerts to messenger
161   Standard_EXPORT void sendMessages (const Handle(Message_Messenger)& theMessenger, Message_Gravity theGravity,
162                                      const Handle(Message_CompositeAlerts)& theCompositeAlert);
163 
164   //! Dumps collected alerts with specified gravity to stream
165   Standard_EXPORT void dumpMessages (Standard_OStream& theOS, Message_Gravity theGravity,
166                                      const Handle(Message_CompositeAlerts)& theCompositeAlert);
167 
168 protected:
169   Standard_Mutex myMutex;
170 
171   Handle(Message_CompositeAlerts) myCompositAlerts; //!< container of alerts
172 
173   NCollection_Sequence<Message_Level*> myAlertLevels;   //!< container of active levels, new alerts are added below the latest level
174   NCollection_IndexedMap<Message_MetricType>  myActiveMetrics; //!< metrics to compute on alerts
175 
176   Standard_Integer myLimit; //!< Maximum number of collected alerts on the top level
177   Standard_Boolean myIsActiveInMessenger; //! state whether the report is activated in messenger
178 };
179 
180 #endif // _Message_Report_HeaderFile
181