Search code examples
c++loggingconsoleopencascade

Preventing Opencascade to write to console


When using the OpenCascade C++ libaries, certain functions will print out some status or response to the console/terminal. For example STEPCAFControl_Writer::perform() will print out something like:

*******************************************************************
******        Statistics on Transfer (Write)                 ******

*******************************************************************
******        Transfer Mode = 0  I.E.  As Is       ******
******        Transferring Shape, ShapeType = 0                      ******
** WorkSession : Sending all data
 Step File Name : <file-name>  Write  Done

to the console. Is there a way to prevent this?


Solution

  • Normally OCCT does not print messages directly into console, instead it relies on Message_Messenger interface. Default implementation registers Message_PrinterOStream printer which puts all messages into console with colors assigned depending on a message gravity.

    So, there are several options in your case:

    • Suppress all OCCT messages by removing all printers within default messenger. This might be not a good idea as this will hide also error messages handful for failure analysis.
      Message::DefaultMessenger()->RemovePrinters (STANDARD_TYPE(Message_PrinterOStream))
    
    • Print only critical messages within default messenger by changing message gravity filter. Default filter is Message_Info, which means that all messages will be printed except Message_Trace designated for verbose or debug outputs.
      Message_Gravity aGravity = Message_Alarm;
      for (Message_SequenceOfPrinters::Iterator aPrinterIter (Message::DefaultMessenger()->Printers());
           aPrinterIter.More(); aPrinterIter.Next())
      {
        aPrinterIter.Value()->SetTraceLevel (aGravity);
      }
    
    • Remove Message_PrinterOStream and register your own printer implementing Message_Printer interface with desired behavior. Normally, GUI applications do not print messages to console and instead print them to dedicated GUI control basing on user preferences.
      class MyPrinter : public Message_Printer
      {
      protected:
        virtual void send (const TCollection_AsciiString& theString, const Message_Gravity theGravity) const override
        {
          std::cerr << "[GRAV: " << theGravity << "] " << theString << "\n";
        }
      };
    
    • When it comes to specific class STEPCAFControl_Writer, it should be noted that this class prints messages into Transfer_TransferProcess::Messenger(), which is set to a global Message::DefaultMessenger() messenger by default, but could be overridden by application in case if specific algorithm output should be suppressed / redirected. This would require diving into STEPCAFControl internal logic, as it doesn't look like an API easily accessible to application code... And it seems some messages will be print to Message::DefaultMessenger() anyway.
      STEPCAFControl_Writer aWriter;
      Handle(Message_Messenger) aMessenger = new Message_Messenger();
      aMessenger->ChangePrinters().Clear();
      aMessenger->AddPrinter (new MyPrinter());
      aWriter.ChangeWriter().WS()->TransferWriter()->FinderProcess()->SetMessenger (aMessenger);
      ...
    

    Below is a screenshot of Draw Harness showing messages with different gravity having different color (default behavior of Message_PrinterOStream since OCCT 7.5.0) - intense red for errors (Message_Alarm/Message_Fail), intense yellow for warnings (Message_ConsoleColor_Yellow), intense green for information (Message_ConsoleColor_Green), and thin yellow for verbose/debug messages (Message_ConsoleColor_Yellow). OCCT colored messages