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_PrinterOStream.hxx>
18 #include <Message_PrinterSystemLog.hxx>
19 #include <Message_PrinterToReport.hxx>
20 #include <Message_Report.hxx>
21 #include <NCollection_Shared.hxx>
23 //==============================================================================
24 //function : printerType
26 //==============================================================================
27 static Standard_Boolean printerType (const TCollection_AsciiString& theTypeName,
28 Handle(Standard_Type)& theType)
30 if (theTypeName == "ostream")
32 theType = STANDARD_TYPE(Message_PrinterOStream);
35 else if (theTypeName == "systemlog")
37 theType = STANDARD_TYPE(Message_PrinterSystemLog);
40 else if (theTypeName == "report")
42 theType = STANDARD_TYPE(Message_PrinterToReport);
45 else if (theTypeName == "draw")
47 theType = STANDARD_TYPE(Draw_Printer);
51 return Standard_False;
54 //==============================================================================
55 //function : createPrinter
57 //==============================================================================
58 static Handle(Message_Printer) createPrinter (const Handle(Standard_Type)& theType, Draw_Interpretor& theDI)
60 const TCollection_AsciiString aTypeName (theType->Name());
61 if (aTypeName == STANDARD_TYPE(Message_PrinterOStream)->Name())
63 return new Message_PrinterOStream();
65 else if (aTypeName == STANDARD_TYPE(Message_PrinterSystemLog)->Name())
67 return new Message_PrinterSystemLog ("draw_messages");
69 else if (aTypeName == STANDARD_TYPE(Message_PrinterToReport)->Name())
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;
76 else if (aTypeName == STANDARD_TYPE(Draw_Printer)->Name())
78 return new Draw_Printer (theDI);
80 return Handle(Message_Printer)();
83 //==============================================================================
84 //function : SendMessage
86 //==============================================================================
87 static Standard_Integer SendMessage (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
91 theDI << "Error: wrong number of arguments";
95 const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
96 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
98 TCollection_AsciiString anArg (theArgVec[anArgIter]);
100 aMessenger->Send (anArg);
106 //==============================================================================
107 //function : PrintMessenger
109 //==============================================================================
110 static Standard_Integer PrintMessenger (Draw_Interpretor& theDI, Standard_Integer, const char**)
112 const Handle(Message_Messenger)& aMessenger = Message::DefaultMessenger();
114 Standard_SStream aSStream;
115 aMessenger->DumpJson (aSStream);
117 std::cout << aSStream.str() << std::endl;
122 //==============================================================================
123 //function : SetMessagePrinter
125 //==============================================================================
126 static Standard_Integer SetMessagePrinter (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
130 theDI << "Error: wrong number of arguments";
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)
139 TCollection_AsciiString anArg (theArgVec[anArgIter]);
141 if (anArg == "-state")
143 if (anArgIter + 1 < theArgNb
144 && Draw::ParseOnOff (theArgVec[anArgIter + 1], toAddPrinter))
149 else if (anArg == "-type"
150 && anArgIter + 1 < theArgNb)
152 TCollection_AsciiString aVal (theArgVec[++anArgIter]);
153 aPrinterTypes.Append (aVal);
157 theDI << "Syntax error: unknown argument '" << theArgVec[anArgIter] << "'";
162 for (NCollection_List<TCollection_AsciiString>::Iterator anIterator (aPrinterTypes); anIterator.More(); anIterator.Next())
164 Handle(Standard_Type) aPrinterType;
165 if (!printerType (anIterator.Value(), aPrinterType))
167 theDI << "Syntax error: unknown printer type '" << anIterator.Value() << "'";
173 Handle(Message_Printer) aPrinter = createPrinter (aPrinterType, theDI);
174 aMessenger->AddPrinter (aPrinter);
175 if (!Handle(Message_PrinterToReport)::DownCast(aPrinter).IsNull())
177 Message::DefaultReport (Standard_False)->UpdateActiveInMessenger();
182 aMessenger->RemovePrinters (aPrinterType);
188 //==============================================================================
189 //function : ClearReport
191 //==============================================================================
192 static Standard_Integer ClearReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char**)
196 theDI << "Error: wrong number of arguments";
200 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
201 if (aReport.IsNull())
203 theDI << "Error: report is no created";
211 //==============================================================================
212 //function : SetReportMetric
214 //==============================================================================
215 static Standard_Integer SetReportMetric(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
219 theDI << "Error: wrong number of arguments";
223 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_True);
224 if (aReport.IsNull())
229 aReport->ClearMetrics();
230 for (int i = 1; i < theArgNb; i++)
232 Standard_Integer aMetricId = Draw::Atoi (theArgVec[i]);
233 if (aMetricId < Message_MetricType_ThreadCPUUserTime || aMetricId > Message_MetricType_MemHeapUsage)
235 theDI << "Error: unrecognized message metric: " << aMetricId;
238 aReport->SetActiveMetric ((Message_MetricType)aMetricId, Standard_True);
243 //==============================================================================
244 //function : CollectMetricMessages
246 //==============================================================================
247 static Standard_Integer CollectMetricMessages(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
249 static Handle(NCollection_Shared<Message_Level>) MyLevel;
253 theDI << "Error: wrong number of arguments";
257 Standard_Boolean toActivate = Standard_False;
258 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
260 TCollection_AsciiString anArg (theArgVec[anArgIter]);
262 if (anArg == "-activate")
264 if (anArgIter + 1 < theArgNb
265 && Draw::ParseOnOff (theArgVec[anArgIter + 1], toActivate))
273 if (!MyLevel.IsNull())
275 theDI << "Error: collecting already activated";
278 MyLevel = new NCollection_Shared<Message_Level>("Level");
284 theDI << "Error: collecting was not activated";
293 //==============================================================================
294 //function : PrintReport
296 //==============================================================================
297 static Standard_Integer PrintReport(Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
301 theDI << "Error: wrong number of arguments";
305 const Handle(Message_Report)& aReport = Message::DefaultReport (Standard_False);
306 if (aReport.IsNull())
308 theDI << "Error: report is no created";
312 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
314 TCollection_AsciiString anArgCase (theArgVec[anArgIter]);
315 anArgCase.LowerCase();
316 if (anArgCase == "-messenger")
318 aReport->SendMessages (Message::DefaultMessenger());
320 else if (anArgCase == "-dump"
321 || anArgCase == "-print")
323 Standard_SStream aSStream;
324 aReport->Dump (aSStream);
327 else if (anArgCase == "-dumpjson")
329 Standard_SStream aSStream;
330 aReport->DumpJson (aSStream);
338 void Draw::MessageCommands(Draw_Interpretor& theCommands)
340 static Standard_Boolean Done = Standard_False;
342 Done = Standard_True;
344 const char* group = "DRAW Message Commands";
346 theCommands.Add("PrintMessenger",
348 "\n\t\t: Prints DumpJson information about messenger.",
349 __FILE__, PrintMessenger, group);
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);
358 theCommands.Add("SendMessage",
359 "SendMessage text [text ...]"
360 "\n Sends the text into the messenger.\n",
361 __FILE__, SendMessage, group);
363 theCommands.Add("ClearReport",
364 "Removes all alerts in default printer",
365 __FILE__, ClearReport, group);
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);
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);
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);