2011年5月16日 星期一

Print colored line in C


#include <stdio.h>
#include <stdarg.h>

#define DEBUG_GREEN "\033[35;32m"
#define DEBUG_LIGHT_BLUE "\033[01;34m"
#define DEBUG_RESTORE "\033[0m"

#define DBG(x,fmt, arg...) debug(x,fmt, ## arg)
extern void debug(const char *color,const char *format, ...)__attribute__((forma t(printf,2,3)));

void debug(const char *color,const char *format, ...)
{
char text[1024];
va_list ap;
//if (!debug_enabled)
// //return;

va_start(ap, format);
vsnprintf((char *)text,1024,format, ap);
va_end(ap);
if (text[strlen(text) - 1] == '\n')
{
text[strlen(text) - 1] = 0;
printf("%s%s\033[0m\n",color,text);
}
else
printf("%s%s\033[0m",color,text);
}

int main()
{
DBG(DEBUG_LIGHT_BLUE,"Himiro Test: %s\n",Hello World);
return 0;
}

沒有留言:

張貼留言