We are doing some testing directly on the embedded device and are printing "custom formatted" test results over USART or SWO using std::cout.
Our embedded device uses a custom definition of _write which redirects all the prints to USART or SWO:
//======================================================================================================================
//! @brief Redirects the underlying write call (called by printf for instance) to write to the UART or SWO interface
//======================================================================================================================
int _write(int file, char* ptr, int len)
{
for (std::uint16_t idx = 0; idx < len; idx++)
{
// SWO
if (g_kEnableSWO)
{
ITM_SendChar(*ptr);
}
// UART
if (nullptr != g_pUart)
{
// Here we call a custom USART driver function that transmits a single character.
}
ptr++;
}
return len;
}
Now, this worked great for us and everything that we printed with std::cout was also seen on USART or SWO.
We are now migrating from the "custom formated" test results to the Unity (link) which also has prints of its own. "Unity formated" test results were not printed over USART or SWO until we provided three custom definition that enable Unity to compile and print on embedded:
//======================================================================================================================
//! @brief This extern "C" block is needed by Unity framework which fails to compile if these definitions are missing.
//======================================================================================================================
extern "C"
{
// Unity executes this before each test.
void setUp(void)
{
}
// Unity executes this after each test.
void tearDown(void)
{
}
// Unity needs this definition to be able to print anything.
void unityPrintChar(int character)
{
putchar(character);
}
}
Unity works and now we have an output which is a mix of our "custom formated" test results and "Unity formatted" test results. At this moment I could remove all the std::cout statements from the code and be done with, but we decided that it is good to keep the prints for debugging and enable them using some sort of a define.
I tried implementing this define like shown below together with replacing all the std::cout with MY_COUT.
//======================================================================================================================
// Disable/enable std::cout() prints.
//======================================================================================================================
#ifdef ENABLE_STD_COUT
#define MY_COUT std::cout
#else
class NullBuffer : public std::streambuf {
public:
int overflow(int c) override {
return traits_type::not_eof(c); // Do nothing
}
};
class NullStream : public std::ostream {
public:
NullStream() : std::ostream(&nullBuffer) {}
private:
NullBuffer nullBuffer;
};
#define MY_COUT NullStream()
#endif
However, this changes nothing... All the prints are still printed. Any ideas why?