Skip to content

Add --no-color so I can redirect the output of uart0 but to a file. #555

Closed

Description

@mofosyne

Tried

simavr -m atmega328p -f 16000000 --no-color ./example-avr.elf 2> testoutput.md

But got

�[32m## Fixed Linear Conversion (8-bit AVR)..
�[0m�[32m..
�[0m�[32m| ADC | Millivolt | ADC back | error |..
�[0m�[32m|-----|-----------|----------|-------|..
�[0m�[32m|   0 |         0 |        0 |     0 |..
�[0m�[32m|  16 |     65535 |        0 |   -16 |..
�[0m�[32m|  32 |         0 |        0 |   -32 |..
�[0m�[32m|  48 |     65535 |        0 |   -48 |..
�[0m�[32m|  64 |         0 |        0 |   -64 |..
�[0m�[32m|  80 |     65535 |        0 |   -80 |..
�[0m�[32m|  96 |         0 |        0 |   -96 |..
�[0m�[32m| 112 |     65535 |        0 |  -112 |..
�[0m�[32m| 128 |         0 |        0 |  -128 |..
�[0m�[32m| 144 |     65535 |        0 |   112 |..
�[0m�[32m| 160 |         0 |        0 |    96 |..
�[0m�[32m| 176 |     65535 |        0 |    80 |..
�[0m�[32m| 192 |         0 |        0 |    64 |..
�[0m�[32m| 208 |     65535 |        0 |    48 |..
�[0m�[32m| 224 |         0 |        0 |    32 |..
�[0m�[32m| 240 |     65535 |        0 |    16 |..
�[0m

Looks like simavr is adding terminal colors. Can you disable this via --no-color?

(Also would be nice to be able to redirect these uart output to file as well via -f uart0=output.txt or something similar)

Activity

  1. mofosyne commented on Sep 19, 2025

    @mofosyne
    Author

    FYI, having difficulties in implementing unit tests using simavr. I would prefer if the documentation has a section specifically on this application.

  2. mofosyne commented on Sep 19, 2025

    @mofosyne
    Author

    https://no-color.org/

    Consider using enviromental variable NO_COLOR as well

    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main(int argc, char *argv[])
    {
    	char *no_color = getenv("NO_COLOR");
    	bool color = true;
    
    	if (no_color != NULL && no_color[0] != '\0')
    		color = false;
    
    	/* do getopt(3) and/or config-file parsing to possibly turn color back on */
    	...
    }
  3. mofosyne commented on Sep 19, 2025

    @mofosyne
    Author

    For those trying to solve this issue, but don't want to wait on the devs.

    You can use sed -r "s/\x1B\[[0-9;]*[mK]//g" to strip out terminal colors then sed -r "s/\.\.$//" (Note you need to escape $ in bash so hence $$) to strip out the trailing dots

    simavr -m atmega328p -f 16000000 --no-color ./example-avr.elf 2>&1 >/dev/null | sed -r "s/\x1B\[[0-9;]*[mK]//g" | sed -r "s/\.\.$$//" > testoutput.md

    This will then give you

    ## Fixed Linear Conversion (8-bit AVR)
    
    | ADC | Millivolt | ADC back | error |
    |-----|-----------|----------|-------|
    |   0 |         0 |        0 |     0 |
    |  16 |     65535 |        0 |   -16 |
    |  32 |         0 |        0 |   -32 |
    |  48 |     65535 |        0 |   -48 |
    |  64 |         0 |        0 |   -64 |
    |  80 |     65535 |        0 |   -80 |
    |  96 |         0 |        0 |   -96 |
    | 112 |     65535 |        0 |  -112 |
    | 128 |         0 |        0 |  -128 |
    | 144 |     65535 |        0 |   112 |
    | 160 |         0 |        0 |    96 |
    | 176 |     65535 |        0 |    80 |
    | 192 |         0 |        0 |    64 |
    | 208 |     65535 |        0 |    48 |
    | 224 |         0 |        0 |    32 |
    | 240 |     65535 |        0 |    16 |
    
  4. edgar-bonet commented on Sep 19, 2025

    @edgar-bonet
    Contributor

    would be nice to be able to redirect these uart output to file as well via -f uart0=output.txt or something similar

    Indeed, and this has been asked before:

    Another common request is to connect the UART to a pty (pseudo-terminal):

    That would make it easy to redirect the output to a file:

    stty -F /tmp/simavr-uart raw
    cat /tmp/simavr-uart > testoutput.txt
  5. buserror commented on Sep 19, 2025

    @buserror
    Owner

    Lets not saddle poor old run_avr with too many system stuff, the pty support is present in simduino, we can either use that, or make another harness

  6. mofosyne commented on Sep 19, 2025

    @mofosyne
    Author

    Hmmm... that sounds like a lot of work just to get an output for testing purpose.
    I would at least hope to be able to direct uart0 to stdout and have a way to return an error code

    e.g.

    int main()
    {
        ... print to uart init code...
        printf("hello world");
        return -1; // 0=Pass -1=Fail
    }

    Where

    simavr -m atmega328p -f 16000000 --output uart0 ./example-avr.elf
    

    Would print 'hello world' to testoutput.md and return -1 which would trip CI with an error code

  7. edgar-bonet commented on Sep 19, 2025

    @edgar-bonet
    Contributor
    int main()
    {
        // ...
        return -1; // 0=Pass -1=Fail
    }

    Returning from main() will not end the simulation: the firmware will jump to exit(), which runs an infinite loop, and simavr will faithfully simulate the loop. This behavior can be changed by this pull request:

    Note that it should be possible to grab the value returned by main() (and passed to exit()) from r25:r24, and have run_avr return that. The above-mentioned pull request does not do that.

  8. gatk555 commented on Sep 20, 2025

    @gatk555
    Collaborator

    There is a compilation option, NO_COLOUR, to remove the coloured text. See Makefile.common.

  9. edgar-bonet commented on Oct 2, 2025

    @edgar-bonet
    Contributor

    As of v1.7-192-g70e9bec, now you can also use the NO_COLOR environment variable:

    NO_COLOR=1 run_avr -m atmega328p -f 16000000 example-avr.elf >testoutput.txt

    You may still want to sed out the trailing dots though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions