下面記錄了一個基於printf可以印出彩色訊息的function。
====================================================================
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define NONECOLOR "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"
//**********************************************************
// Usage Example:
// int num=7;
// COLOR_DBG("%s Lucky Number is %d", GREEN, num);
//
// Description:
// This message will show Green color.
//
// viod COLOR_DBG(const char* Format, const char* Color, const char* str, ...)
void COLOR_DBG(const char* Format, ...)
{
char str[255];
va_list ap;
va_start(ap,Format);
vsprintf(str,Format,ap);
sprintf(str,"%s%s",str,NONECOLOR);
printf("%s",str);
va_end(ap);
}
int main()
{
COLOR_DBG("%s Hello World!!!!!\n", YELLOW);
return 0;
}
=====================================================================
以下是使用gcc compiler後執行結果的截圖!