Tracing text messages while running or debugging an application

It's often useful to trace text messages as a means of understanding what an RT application is doing. If you run or debug your application from within DevOps Model RealTime, messages printed to stdout will be routed to the Console view, while messages printed to stderr will be routed to the Debugger Console view (and printed there in red color). This is often convenient because it allows you to separate error messages from informational messages.

For performance reasons the stderr and stdout streams are not flushed after each printout. You need to explicitly flush them when you want the messages to appear. Here is an example of how it can be done:

// Using the Log service of the TargetRTS. It prints to stderr.
log.log("A message to be flushed");
log.commit();
// Using the standard std::cout C++ object. It prints to stdout.
std::cout << "Obtained data from sensor #" << sd.sensorId << std::flush;

If you don't have explicit code for flushing messages, the printouts will be hard to understand, because they will not appear until the application exits, or when the stream buffers are full.

Note that messages printed by the TargetRTS itself (such as when an unexpected event arrives) will be immediately flushed.

It is possible to change the buffering mode for the stdout and stderr streams so that all messages are immediately flushed. However, be aware that doing this will affect application performance. You can write this code in the Implementation Ending code snippet of your top capsule:

static int fix()
{
  setvbuf( stdout, 0, _IONBF, 0);
  setvbuf( stderr, 0, _IONBF, 0);
  fflush(stdout);
  fflush(stderr);
  return 0;
}

static int _fix = fix();

The code snippet requires inclusion of stdio.h, so don't forget to add it in the Implementation Preface code snippet of the same capsule.

#include <stdio.h>