Colored printf messages in Linux

If you have ever wanted to have colored printf statements for easier debugging - look no further

printf("%c[%d;%dmColor Me!%c[%dm\n",27,1,33,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);

Reference:
http://cc.byexamples.com/2007/01/20/print-color-string-without-ncurses/

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.