1 // Copyright (c) 2020 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <Draw_Printer.hxx>
17 #include <Message.hxx>
18 #include <Message_Messenger.hxx>
19 #include <Message_PrinterOStream.hxx>
20 #include <Message_PrinterSystemLog.hxx>
21 #include <Message_PrinterToReport.hxx>
22 #include <Message_Report.hxx>
23 #include <NCollection_Shared.hxx>
24 #include <Standard_Dump.hxx>
26 //==============================================================================
27 //function : printerType
29 //==============================================================================
30 static Standard_Boolean printerType (const TCollection_AsciiString& theTypeName,
31 Handle(Standard_Type)& theType)
33 if (theTypeName == "ostream")
35 theType = STANDARD_TYPE(Message_PrinterOStream);
38 else if (theTypeName == "systemlog")
40 theType = STANDARD_TYPE(Message_PrinterSystemLog);
43 else if (theTypeName == "report")
45 theType = STANDARD_TYPE(Message_PrinterToReport);
48 else if (theTypeName == "draw")
50 theType = STANDARD_TYPE(Draw_Printer);
54 return Standard_False;
57 //==============================================================================
58 //function : createPrinter
60 //==============================================================================
61 static Handle(Message_Printer) createPrinter (const Handle(Standard_Type)& theType, Draw_Interpretor& theDI)
63 const TCollection_AsciiString aTypeName (theType->Name());
64 if (aTypeName == STANDARD_TYPE(Message_PrinterOStream)->Name())
66 return new Message_PrinterOStream();
68 else if (aTypeName == STANDARD_TYPE(Message_PrinterSystemLog)->Name())
70 return new Message_PrinterSystemLog ("draw_messages");
72 else if (aTypeName == STANDARD_TYPE(Message_PrinterToReport)->Name())
74 Handle(Message_PrinterToReport) aMessagePrinter = new Message_PrinterToReport();
75 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_True);
76 aMessagePrinter->SetReport (aReport);
77 return aMessagePrinter;
79 else if (aTypeName == STANDARD_TYPE(Draw_Printer)->Name())
81 return new Draw_Printer (theDI);
83 return Handle(Message_Printer)();
86 //==============================================================================
87 //function : SendMessage
89 //==============================================================================
90 static Standard_Integer SendMessage (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
94 theDI << "Error: wrong number of arguments";
98 const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
99 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
101 TCollection_AsciiString anArg (theArgVec[anArgIter]);
103 aMessenger->Send (anArg);
109 //==============================================================================
110 //function : PrintMessenger
112 //==============================================================================
113 static Standard_Integer PrintMessenger (Draw_Interpretor& theDI, Standard_Integer, const char**)
115 const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
117 Standard_SStream aSStream;
118 aMessenger->DumpJson (aSStream);
120 std::cout << aSStream.str() << std::endl;
125 //==============================================================================
126 //function : SetMessagePrinter
128 //==============================================================================
129 static Standard_Integer SetMessagePrinter (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
133 theDI << "Error: wrong number of arguments";
137 Standard_Boolean toAddPrinter = Standard_True;
138 NCollection_List<TCollection_AsciiString> aPrinterTypes;
139 const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
140 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
142 TCollection_AsciiString anArg (theArgVec[anArgIter]);
144 if (anArg == "-state")
146 if (anArgIter + 1 < theArgNb
147 && Draw::ParseOnOff (theArgVec[anArgIter + 1], toAddPrinter))
152 else if (anArg == "-type"
153 && anArgIter + 1 < theArgNb)
155 TCollection_AsciiString aVal (theArgVec[++anArgIter]);
156 aPrinterTypes.Append (aVal);
160 theDI << "Syntax error: unknown argument '" << theArgVec[anArgIter] << "'";
165 for (NCollection_List<TCollection_AsciiString>::Iterator anIterator (aPrinterTypes); anIterator.More(); anIterator.Next())
167 Handle(Standard_Type) aPrinterType;
168 if (!printerType (anIterator.Value(), aPrinterType))
170 theDI << "Syntax error: unknown printer type '" << anIterator.Value() << "'";
176 Handle(Message_Printer) aPrinter = createPrinter (aPrinterType, theDI);
177 aMessenger->AddPrinter (aPrinter);
178 if (!Handle(Message_PrinterToReport)::DownCast(aPrinter).IsNull())
180 Message::DefaultReport (Standard_False)->UpdateActiveInMessenger();
185 aMessenger->RemovePrinters (aPrinterType);
191 //==============================================================================
192 //function : ClearReport
194 //==============================================================================
195 static Standard_Integer ClearReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char**)
199 theDI << "Error: wrong number of arguments";
203 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
204 if (aReport.IsNull())
206 theDI << "Error: report is no created";
214 //==============================================================================
215 //function : SetReportMetric
217 //==============================================================================
218 static Standard_Integer SetReportMetric(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
222 theDI << "Error: wrong number of arguments";
226 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_True);
227 if (aReport.IsNull())
232 aReport->ClearMetrics();
233 for (int i = 1; i < theArgNb; i++)
235 Standard_Integer aMetricId = Draw::Atoi (theArgVec[i]);
236 if (aMetricId < Message_MetricType_ThreadCPUUserTime || aMetricId > Message_MetricType_MemHeapUsage)
238 theDI << "Error: unrecognized message metric: " << aMetricId;
241 aReport->SetActiveMetric ((Message_MetricType)aMetricId, Standard_True);
246 //==============================================================================
247 //function : CollectMetricMessages
249 //==============================================================================
250 static Standard_Integer CollectMetricMessages(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
252 static Handle(NCollection_Shared<Message_Level>) MyLevel;
256 theDI << "Error: wrong number of arguments";
260 Standard_Boolean toActivate = Standard_False;
261 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
263 TCollection_AsciiString anArg (theArgVec[anArgIter]);
265 if (anArg == "-activate")
267 if (anArgIter + 1 < theArgNb
268 && Draw::ParseOnOff (theArgVec[anArgIter + 1], toActivate))
276 if (!MyLevel.IsNull())
278 theDI << "Error: collecting already activated";
281 MyLevel = new NCollection_Shared<Message_Level>("Level");
287 theDI << "Error: collecting was not activated";
296 //==============================================================================
297 //function : PrintReport
299 //==============================================================================
300 static Standard_Integer PrintReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
304 theDI << "Error: wrong number of arguments";
308 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
309 if (aReport.IsNull())
311 theDI << "Error: report is no created";
315 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
317 TCollection_AsciiString anArgCase (theArgVec[anArgIter]);
318 anArgCase.LowerCase();
319 if (anArgCase == "-messenger")
321 aReport->SendMessages (Message::DefaultMessenger());
323 else if (anArgCase == "-dump"
324 || anArgCase == "-print")
326 Standard_SStream aSStream;
327 aReport->Dump (aSStream);
330 else if (anArgCase == "-dumpjson")
332 Standard_SStream aSStream;
333 aReport->DumpJson (aSStream);
341 void Draw::MessageCommands(Draw_Interpretor& theCommands)
343 static Standard_Boolean Done = Standard_False;
345 Done = Standard_True;
347 const char* group = "DRAW Message Commands";
349 theCommands.Add("PrintMessenger",
351 "\n\t\t: Prints DumpJson information about messenger.",
352 __FILE__, PrintMessenger, group);
354 theCommands.Add("SetMessagePrinter",
355 "SetMessagePrinter [-type ostream|systemlog|report|draw] [-state {on|off}=on]"
356 "\n\t\t: Sets or removes the printer in messenger."
357 "\n\t\t: Option -type set type of printer. Printers are applied with And combination."
358 "\n\t\t: Option -state add or remove printer",
359 __FILE__, SetMessagePrinter, group);
361 theCommands.Add("SendMessage",
362 "SendMessage text [text ...]"
363 "\n Sends the text into the messenger.\n",
364 __FILE__, SendMessage, group);
366 theCommands.Add("ClearReport",
367 "Removes all alerts in default printer",
368 __FILE__, ClearReport, group);
370 theCommands.Add("SetReportMetric",
371 "SetReportMetric [metric ...] \n Activate report metrics, deactivate all if there are no parameters.\n"
372 "\n\t\t: metric is a value of Message_MetricType, e.g. 1 is Message_MetricType_UserTimeCPU" ,
373 __FILE__, SetReportMetric, group);
375 theCommands.Add("CollectMetricMessages",
376 "CollectMetricMessages [-activate {0|1}]"
377 "\n Start metric collection by 1, stop by 0. Result is placed in metric attributes of message report.\n",
378 __FILE__, CollectMetricMessages, group);
380 theCommands.Add("PrintReport",
381 "PrintReport [-messenger] [-dump] [-dumpJson]"
382 "\n\t\t: Send report content to default messenger or stream"
383 "\n\t\t: Output options:"
384 "\n\t\t: -messenger Prints the information about report into messenger."
385 "\n\t\t: -dump Prints Dump information about report."
386 "\n\t\t: -dumpJson Prints DumpJson information about report.",
387 __FILE__, PrintReport, group);