0032806: Coding - get rid of unused headers [Contap to Extrema]
[occt.git] / src / Draw / Draw_MessageCommands.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <Draw.hxx>
15 #include <Draw_Printer.hxx>
16
17 #include <Message_PrinterOStream.hxx>
18 #include <Message_PrinterSystemLog.hxx>
19 #include <Message_PrinterToReport.hxx>
20 #include <Message_Report.hxx>
21 #include <NCollection_Shared.hxx>
22
23 //==============================================================================
24 //function : printerType
25 //purpose  :
26 //==============================================================================
27 static Standard_Boolean printerType (const TCollection_AsciiString& theTypeName,
28                                      Handle(Standard_Type)& theType)
29 {
30   if (theTypeName == "ostream")
31   {
32     theType = STANDARD_TYPE(Message_PrinterOStream);
33     return Standard_True;
34   }
35   else if (theTypeName == "systemlog")
36   {
37     theType = STANDARD_TYPE(Message_PrinterSystemLog);
38     return Standard_True;
39   }
40   else if (theTypeName == "report")
41   {
42     theType = STANDARD_TYPE(Message_PrinterToReport);
43     return Standard_True;
44   }
45   else if (theTypeName == "draw")
46   {
47     theType = STANDARD_TYPE(Draw_Printer);
48     return Standard_True;
49   }
50
51   return Standard_False;
52 }
53
54 //==============================================================================
55 //function : createPrinter
56 //purpose  :
57 //==============================================================================
58 static Handle(Message_Printer) createPrinter (const Handle(Standard_Type)& theType, Draw_Interpretor& theDI)
59 {
60   const TCollection_AsciiString aTypeName (theType->Name());
61   if (aTypeName == STANDARD_TYPE(Message_PrinterOStream)->Name())
62   {
63     return new Message_PrinterOStream();
64   }
65   else if (aTypeName == STANDARD_TYPE(Message_PrinterSystemLog)->Name())
66   {
67     return new Message_PrinterSystemLog ("draw_messages");
68   }
69   else if (aTypeName == STANDARD_TYPE(Message_PrinterToReport)->Name())
70   {
71     Handle(Message_PrinterToReport) aMessagePrinter = new Message_PrinterToReport();
72     const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_True);
73     aMessagePrinter->SetReport (aReport);
74     return aMessagePrinter;
75   }
76   else if (aTypeName == STANDARD_TYPE(Draw_Printer)->Name())
77   {
78     return new Draw_Printer (theDI);
79   }
80   return Handle(Message_Printer)();
81 }
82
83 //==============================================================================
84 //function : SendMessage
85 //purpose  :
86 //==============================================================================
87 static Standard_Integer SendMessage (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
88 {
89   if (theArgNb < 2)
90   {
91     theDI << "Error: wrong number of arguments";
92     return 1;
93   }
94
95   const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
96   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
97   {
98     TCollection_AsciiString anArg (theArgVec[anArgIter]);
99     anArg.LowerCase();
100     aMessenger->Send (anArg);
101   }
102
103   return 0;
104 }
105
106 //==============================================================================
107 //function : PrintMessenger
108 //purpose  :
109 //==============================================================================
110 static Standard_Integer PrintMessenger (Draw_Interpretor& theDI, Standard_Integer, const char**)
111 {
112   const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
113
114   Standard_SStream aSStream;
115   aMessenger->DumpJson (aSStream);
116   theDI << aSStream;
117   std::cout << aSStream.str() << std::endl;
118
119   return 0;
120 }
121
122 //==============================================================================
123 //function : SetMessagePrinter
124 //purpose  :
125 //==============================================================================
126 static Standard_Integer SetMessagePrinter (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
127 {
128   if (theArgNb < 2)
129   {
130     theDI << "Error: wrong number of arguments";
131     return 1;
132   }
133
134   Standard_Boolean toAddPrinter = Standard_True;
135   NCollection_List<TCollection_AsciiString> aPrinterTypes;
136   const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
137   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
138   {
139     TCollection_AsciiString anArg (theArgVec[anArgIter]);
140     anArg.LowerCase();
141     if (anArg == "-state")
142     {
143       if (anArgIter + 1 < theArgNb
144        && Draw::ParseOnOff (theArgVec[anArgIter + 1], toAddPrinter))
145       {
146         ++anArgIter;
147       }
148     }
149     else if (anArg == "-type"
150           && anArgIter + 1 < theArgNb)
151     {
152       TCollection_AsciiString aVal (theArgVec[++anArgIter]);
153       aPrinterTypes.Append (aVal);
154     }
155     else
156     {
157       theDI << "Syntax error: unknown argument '" << theArgVec[anArgIter] << "'";
158       return 1;
159     }
160   }
161
162   for (NCollection_List<TCollection_AsciiString>::Iterator anIterator (aPrinterTypes); anIterator.More(); anIterator.Next())
163   {
164     Handle(Standard_Type) aPrinterType;
165     if (!printerType (anIterator.Value(), aPrinterType))
166     {
167       theDI << "Syntax error: unknown printer type '" << anIterator.Value() << "'";
168       return 1;
169     }
170
171     if (toAddPrinter)
172     {
173       Handle(Message_Printer) aPrinter = createPrinter (aPrinterType, theDI);
174       aMessenger->AddPrinter (aPrinter);
175       if (!Handle(Message_PrinterToReport)::DownCast(aPrinter).IsNull())
176       {
177         Message::DefaultReport (Standard_False)->UpdateActiveInMessenger();
178       }
179     }
180     else
181     {
182       aMessenger->RemovePrinters (aPrinterType);
183     }
184   }
185   return 0;
186 }
187
188 //==============================================================================
189 //function : ClearReport
190 //purpose  :
191 //==============================================================================
192 static Standard_Integer ClearReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char**)
193 {
194   if (theArgNb < 1)
195   {
196     theDI << "Error: wrong number of arguments";
197     return 1;
198   }
199
200   const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
201   if (aReport.IsNull())
202   {
203     theDI << "Error: report is no created";
204     return 1;
205   }
206
207   aReport->Clear();
208   return 0;
209 }
210
211 //==============================================================================
212 //function : SetReportMetric
213 //purpose  :
214 //==============================================================================
215 static Standard_Integer SetReportMetric(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
216 {
217   if (theArgNb < 1)
218   {
219     theDI << "Error: wrong number of arguments";
220     return 1;
221   }
222
223   const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_True);
224   if (aReport.IsNull())
225   {
226     return 1;
227   }
228
229   aReport->ClearMetrics();
230   for (int i = 1; i < theArgNb; i++)
231   {
232     Standard_Integer aMetricId = Draw::Atoi (theArgVec[i]);
233     if (aMetricId < Message_MetricType_ThreadCPUUserTime || aMetricId > Message_MetricType_MemHeapUsage)
234     {
235       theDI << "Error: unrecognized message metric: " << aMetricId;
236       return 1;
237     }
238     aReport->SetActiveMetric ((Message_MetricType)aMetricId, Standard_True);
239   }
240   return 0;
241 }
242
243 //==============================================================================
244 //function : CollectMetricMessages
245 //purpose  :
246 //==============================================================================
247 static Standard_Integer CollectMetricMessages(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
248 {
249   static Handle(NCollection_Shared<Message_Level>) MyLevel;
250
251   if (theArgNb < 1)
252   {
253     theDI << "Error: wrong number of arguments";
254     return 1;
255   }
256
257   Standard_Boolean toActivate = Standard_False;
258   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
259   {
260     TCollection_AsciiString anArg (theArgVec[anArgIter]);
261     anArg.LowerCase();
262     if (anArg == "-activate")
263     {
264       if (anArgIter + 1 < theArgNb
265        && Draw::ParseOnOff (theArgVec[anArgIter + 1], toActivate))
266       {
267         ++anArgIter;
268       }
269     }
270   }
271   if (toActivate)
272   {
273     if (!MyLevel.IsNull())
274     {
275       theDI << "Error: collecting already activated";
276       return 1;
277     }
278     MyLevel = new NCollection_Shared<Message_Level>("Level");
279   }
280   else
281   {
282     if (!MyLevel)
283     {
284       theDI << "Error: collecting was not activated";
285       return 1;
286     }
287     MyLevel.Nullify();
288     MyLevel = 0;
289   }
290   return 0;
291 }
292
293 //==============================================================================
294 //function : PrintReport
295 //purpose  :
296 //==============================================================================
297 static Standard_Integer PrintReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
298 {
299   if (theArgNb < 1)
300   {
301     theDI << "Error: wrong number of arguments";
302     return 1;
303   }
304
305   const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
306   if (aReport.IsNull())
307   {
308     theDI << "Error: report is no created";
309     return 1;
310   }
311
312   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
313   {
314     TCollection_AsciiString anArgCase (theArgVec[anArgIter]);
315     anArgCase.LowerCase();
316     if (anArgCase == "-messenger")
317     {
318       aReport->SendMessages (Message::DefaultMessenger());
319     }
320     else if (anArgCase == "-dump"
321           || anArgCase == "-print")
322     {
323       Standard_SStream aSStream;
324       aReport->Dump (aSStream);
325       theDI << aSStream;
326     }
327     else if (anArgCase == "-dumpjson")
328     {
329       Standard_SStream aSStream;
330       aReport->DumpJson (aSStream);
331       theDI << aSStream;
332     }
333   }
334
335   return 0;
336 }
337
338 void Draw::MessageCommands(Draw_Interpretor& theCommands)
339 {
340   static Standard_Boolean Done = Standard_False;
341   if (Done) return;
342   Done = Standard_True;
343
344   const char* group = "DRAW Message Commands";
345
346   theCommands.Add("PrintMessenger",
347     "PrintMessenger"
348     "\n\t\t: Prints DumpJson information about messenger.",
349     __FILE__, PrintMessenger, group);
350
351   theCommands.Add("SetMessagePrinter",
352     "SetMessagePrinter [-type ostream|systemlog|report|draw] [-state {on|off}=on]"
353     "\n\t\t: Sets or removes the printer in messenger."
354     "\n\t\t: Option -type set type of printer. Printers are applied with And combination."
355     "\n\t\t: Option -state add or remove printer",
356     __FILE__, SetMessagePrinter, group);
357
358   theCommands.Add("SendMessage",
359     "SendMessage text [text ...]"
360     "\n Sends the text into the messenger.\n",
361     __FILE__, SendMessage, group);
362
363   theCommands.Add("ClearReport",
364     "Removes all alerts in default printer",
365     __FILE__, ClearReport, group);
366
367   theCommands.Add("SetReportMetric",
368     "SetReportMetric [metric ...] \n Activate report metrics, deactivate all if there are no parameters.\n"
369     "\n\t\t: metric is a value of Message_MetricType, e.g. 1 is Message_MetricType_UserTimeCPU" ,
370     __FILE__, SetReportMetric, group);
371
372   theCommands.Add("CollectMetricMessages",
373     "CollectMetricMessages [-activate {0|1}]"
374     "\n Start metric collection by 1, stop by 0. Result is placed in metric attributes of message report.\n",
375     __FILE__, CollectMetricMessages, group);
376   
377   theCommands.Add("PrintReport",
378     "PrintReport [-messenger] [-dump] [-dumpJson]"
379     "\n\t\t: Send report content to default messenger or stream"
380     "\n\t\t: Output options:"
381     "\n\t\t:   -messenger Prints the information about report into messenger."
382     "\n\t\t:   -dump      Prints Dump information about report."
383     "\n\t\t:   -dumpJson  Prints DumpJson information about report.",
384     __FILE__, PrintReport, group);
385 }