This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Changing unity printf to Segger RTT

I'm wondering if anyone uses unity.c for unit testing and managed to change the print function to segger rtt in order to read the output.

According to unity

By default, Unity prints its results to `stdout` as it runs. This works
perfectly fine in most situations where you are using a native compiler for
testing. It works on some simulators as well so long as they have `stdout`
routed back to the command line. There are times, however, where the simulator
will lack support for dumping results or you will want to route results
elsewhere for other reasons. In these cases, you should define the
`UNITY_OUTPUT_CHAR` macro. This macro accepts a single character at a time (as
an `int`, since this is the parameter type of the standard C `putchar` function
most commonly used). You may replace this with whatever function call you like.

    * Output*     - by default, Unity prints to standard out with putchar.  define UNITY_OUTPUT_CHAR(a) with a different function if desired
     
    /*-------------------------------------------------------
     * Output Method: stdout (DEFAULT)
     *-------------------------------------------------------*/
    #ifndef UNITY_OUTPUT_CHAR
    /* Default to using putchar, which is defined in stdio.h */
    #include <stdio.h>
    #define UNITY_OUTPUT_CHAR(a) (void)putchar(a)
    #else
      /* If defined as something else, make sure we declare it here so it's ready for use */
      #ifndef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION
    extern void UNITY_OUTPUT_CHAR(int);
      #endif
    #endif

I tried to define

#define UNITY_OUTPUT_CHAR(a) SEGGER_RTT_WriteString(0, a)

However i get the following

> $ make rm -rf _build echo  Makefile
> Makefile mkdir _build Compiling file:
> system_nrf51.c Compiling file: main.c
> C:/NXTSNS/nRF5_SDK_11.0.0_89a8197/examples/peripheral/blinky/main.c:31:53:
> error: expected declaration specifiers
> or '...' before numeric constant 
> #define UNITY_OUTPUT_CHAR(a) SEGGER_RTT_WriteString(0, a)
>                                                      ^
> C:/NXTSNS/Unittesting/unity/src/unity_internals.h:250:13:
> note: in expansion of macro
> 'UNITY_OUTPUT_CHAR'  extern void
> UNITY_OUTPUT_CHAR(int);
>              ^ Makefile:150: recipe for target '_build/main.o' failed
> make: *** [_build/main.o] Error 1

How should i define the macro such that i can send the output to segger or if there is a more suitable alternative what is it?

Related