Monday 9 March 2015

Colored printf messages in Linux

If you have ever wanted to have colored printf statements for easier debugging - look no further
Ex.You can add bellow printf statement in your code with replace "PASS" word with your ans see the MAGIC.
printf("%c[%d;%dm PASS%c[%dm\n",27,1,32,27,0);

Starting with the parameter - 27, this initiates the coloring. The rest of the parameters are (attribute);(fore color);(background color).

Text attributes
0 All attributes off
1 Bold on
4 Underscore (on monochrome display adapter only)
5 Blink on
7 Reverse video on
8 Concealed on
Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
More code:
printf("%c[%d;%dmTH_FINACK || TH_RST || TH_FIN%c[%dm\n",27,1,31,27,0);
printf("%c[%d;%dmTH_PSH || TH_ACK%c[%dm\n",27,1,33,27,0);
printf("%c[%d;%dmTH_SYN%c[%dm\n",27,1,32,27,0);

No comments:

Post a Comment