send all OpenCascade messages to a text file

David

Active CAD practitioner
I want to have all messages sent to a specific text file next to my app and not cerr or cout. I did not find any good explanation about messenger class, hence help were appreciated.
 

natalia

Moderator
Staff member
The Message_Messenger class is responsible for processing any output in OCCT .
The kind of result is defined by printers set in this messenger.
By default, the Message_PrinterOStream to 'cout' is set.

If output should go into a file, we should remove the current printer and add another one. For output into a file the same printer is responsible. However it should be created with another constructor:
C++:
Standard_EXPORT Message_PrinterOStream(const Standard_CString theFileName, const Standard_Boolean theDoAppend, const Message_Gravity theTraceLevel = Message_Info);
 

David

Active CAD practitioner
I did what you you recommended, @natalia.
I deleted OCCT messages to standard (i.e. std::cout)
Message::DefaultMessenger()->RemovePrinters(STANDARD_TYPE(Message_PrinterOStream));
and then tried to send all OCCT messages not to std::cout but to file "OCCTmessage.txt":
Message_PrinterOStream::Message_PrinterOStream("OCCTmessage.txt", Standard_False, Message_Trace);

I do not get the messages to cout and it created the "OCCTmessage.txt" file, but it is always empty (0 Byte). Any idea what I am doing wrong ?
 

natalia

Moderator
Staff member
Hi, David
The next code gives an output log file filled. It was checked on OCCT 7.6.0, Win32, x64, VStudio 2019:
C++:
#include <Message.hxx>
#include <Message_Messenger.hxx>
#include <Message_PrinterOStream.hxx>

#include <STEPCAFControl_Reader.hxx>

const std::string stepFileName = "D:/tmp/screw.step";
const std::string outputfileName = "D:/tmp/screw_log";

int main(int argc, char **argv)
{
  const Handle(Message_Messenger)& messenger = Message::DefaultMessenger();
  messenger->ChangePrinters().Clear();
  Handle(Message_PrinterOStream) printer = new Message_PrinterOStream(outputfileName.c_str(), Standard_False, Message_Gravity::Message_Trace);
  messenger->AddPrinter(printer);

  messenger->Send("start message");

  STEPCAFControl_Reader stepReader;
  const TCollection_AsciiString str(stepFileName.c_str());
  IFSelect_ReturnStatus status = stepReader.ReadFile(str.ToCString());

  if (status != IFSelect_RetDone) {
    std::cout << "could not read file: " << stepFileName.c_str() << std::endl;
    return 1;
  }

  std::cout << "success on file: " << stepFileName.c_str() << std::endl;
  messenger->Send("done message");

  return 0;
}

The result file contains:
Code:
start message
      ...    Step File Reading : 'D:/tmp/screw.step'      ...    STEP File   Read    ...
      ... Step File loaded  ...
   2480 records (entities,sub-lists,scopes), 7319 parameters      ... Parameters prepared ...
      ...   Objects analysed  ...
  STEP Loading done : 1239 Entities
done message
 
Top