Using serial output for debug messages and console logs is very frequent tool for firmware and software developers. But when working on big systems and large data outputs it’s often not easy to find out needed data between lots of symbols.
Using color is a easy obtainable improvement, which can help to increase readability of logs by human eyes. This short article covers basic color codes and usage examples. Coloring is supported by many terminal programs, such as PuTTy, etc.
ANSI color definition
Foreground | Background | |||||
black | 30 | 40 | |
red | 31 | 41 |
green | 32 | 42 | ||||
yellow | 33 | 43 | ||||
blue | 34 | 44 | ||||
magenta | 35 | 45 | ||||
cyan | 36 | 46 | ||||
white | 37 | 47 |
\033[1m | Bold Text |
\033[4m | Underline Text |
---|---|
\033[5m | Blinking Text |
\033[7m | Inverse Text |
Foreground colors:
Code | ESC-string | Result |
\033[0;30;41m | ^[0;30;41m | Black on white |
\033[0;31m | ^[0;31m | Red foreground |
\033[0;32m | ^[0;32m | Green foreground |
\033[0;33m | ^[0;33m | Yellow foreground |
\033[0;34m | ^[0;34m | Blue foreground |
\033[0;35m | ^[0;35m | Magenta foreground |
\033[0;36m | ^[0;36m | Cyan foreground |
\033[0;37m | ^[0;37m | White foreground |
\033[0;39m | ^[0;39m | Default color |
Bright foreground colors:
Code | ESC-string | Result |
\033[1;30m | ^[1;30m | Bg Black on white |
\033[1;31m | ^[1;31m | Bg Red foreground |
\033[1;32m | ^[1;32m | Bg Green foreground |
\033[1;33m | ^[1;33m | Bg Yellow foreground |
\033[1;34m | ^[1;34m | Bg Blue foreground |
\033[1;35m | ^[1;35m | Bg Magenta foreground |
\033[1;36m | ^[1;36m | Bg Cyan foreground |
\033[1;37m | ^[1;37m | Bg White foreground |
\033[1;39m | ^[1;39m | Default color |
Background colors:
\033[0;40m | ^[0;40m | Black Background |
\033[0;41m | ^[0;41m | Red Background |
\033[0;42m | ^[0;42m | Green Background |
\033[0;43m | ^[0;43m | Yellow Background |
\033[0;44m | ^[0;44m | Blue Background |
\033[0;45m | ^[0;45m | Magenta Background |
\033[0;46m | ^[0;46m | Cyan Background |
\033[0;47m | ^[0;47m | White Background |
\033[0;49m | ^[0;49m | Default Background |
Reset color code: \033[m
Example usage for colors
void color_debug(void) { // Outputs black text on green background and returns back to normal printf("\033[0;30;41m Output black text on green background \r\n \033[0;39;49m"); };
Output:
Output black text on green background |
Projects like this are born from passion and a desire to share how things work. Education is the foundation of a healthy society - especially important in today's volatile world. xDevs began as a personal project notepad in Kherson, Ukraine back in 2008 and has grown with support of passionate readers just like you. There are no (and never will be) any ads, sponsors or shareholders behind xDevs.com, just a commitment to inspire and help learning. If you are in a position to help others like us, please consider supporting xDevs.com’s home-country Ukraine in its defense of freedom to speak, freedom to live in peace and freedom to choose their way. You can use official site to support Ukraine – United24 or Help99. Every cent counts.
Modified: Sept. 30, 2017, 12:22 p.m.