use ANSI color for _ERR_PRINTF, _DBG_PRINTF, and _MG_PRINTF

This commit is contained in:
VincentWei
2018-05-31 23:16:52 +08:00
parent a45ee0b3f4
commit 1cfd032718
2 changed files with 92 additions and 21 deletions
+88 -15
View File
@@ -1748,13 +1748,12 @@ struct tm {
#define fseek tp_fseek
#define feof tp_feof
#undef assert
#define _HAVE_ASSERT 1
#define assert(e) do { \
e; \
} while(0);
#define assert(e) do { \
e; \
} while (0)
#undef stdin
#undef stdout
@@ -1969,12 +1968,80 @@ int init_minigui_printf (int (*output_char) (int ch),
#endif /* _MGUSE_OWN_STDIO */
#ifdef __LINUX__
#define TCS_NONE(fp) fprintf (fp, "\e[0m")
#define TCS_BLACK(fp) fprintf (fp, "\e[0;30m")
#define TCS_BOLD_BLACK(fp) fprintf (fp, "\e[1;30m")
#define TCS_RED(fp) fprintf (fp, "\e[0;31m")
#define TCS_BOLD_RED(fp) fprintf (fp, "\e[1;31m")
#define TCS_GREEN(fp) fprintf (fp, "\e[0;32m")
#define TCS_BOLD_GREEN(fp) fprintf (fp, "\e[1;32m")
#define TCS_BROWN(fp) fprintf (fp, "\e[0;33m")
#define TCS_YELLOW(fp) fprintf (fp, "\e[1;33m")
#define TCS_BLUE(fp) fprintf (fp, "\e[0;34m")
#define TCS_BOLD_BLUE(fp) fprintf (fp, "\e[1;34m")
#define TCS_PURPLE(fp) fprintf (fp, "\e[0;35m")
#define TCS_BOLD_PURPLE(fp) fprintf (fp, "\e[1;35m")
#define TCS_CYAN(fp) fprintf (fp, "\e[0;36m")
#define TCS_BOLD_CYAN(fp) fprintf (fp, "\e[1;36m")
#define TCS_GRAY(fp) fprintf (fp, "\e[0;37m")
#define TCS_WHITE(fp) fprintf (fp, "\e[1;37m")
#define TCS_BOLD(fp) fprintf (fp, "\e[1m")
#define TCS_UNDERLINE(fp) fprintf (fp, "\e[4m")
#define TCS_BLINK(fp) fprintf (fp, "\e[5m")
#define TCS_REVERSE(fp) fprintf (fp, "\e[7m")
#define TCS_HIDE(fp) fprintf (fp, "\e[8m")
#define TCS_CLEAR(fp) fprintf (fp, "\e[2J")
#define TCS_CLRLINE(fp) fprintf (fp, "\e[1K\r")
#else
#define TCS_NONE(fp)
#define TCS_BLACK(fp)
#define TCS_BOLD_BLACK(fp)
#define TCS_RED(fp)
#define TCS_BOLD_RED(fp)
#define TCS_GREEN(fp)
#define TCS_BOLD_GREEN(fp)
#define TCS_BROWN(fp)
#define TCS_YELLOW(fp)
#define TCS_BLUE(fp)
#define TCS_BOLD_BLUE(fp)
#define TCS_PURPLE(fp)
#define TCS_BOLD_PURPLE(fp)
#define TCS_CYAN(fp)
#define TCS_BOLD_CYAN(fp)
#define TCS_GRAY(fp)
#define TCS_WHITE(fp)
#define TCS_BOLD(fp)
#define TCS_UNDERLINE(fp)
#define TCS_BLINK(fp)
#define TCS_REVERSE(fp)
#define TCS_HIDE(fp)
#define TCS_CLEAR(fp)
#define TCS_CLRLINE(fp)
#endif
#if defined(__GNUC__)
# define _ERR_PRINTF(fmt...) fprintf (stderr, fmt)
# define _ERR_PRINTF(fmt...) \
do { \
TCS_BOLD_RED (stderr); \
fprintf (stderr, fmt); \
TCS_NONE (stderr); \
} while (0)
# ifdef _DEBUG
# define _MG_PRINTF(fmt...) fprintf (stdout, fmt)
# define _MG_PRINTF(fmt...) \
do { \
TCS_GREEN (stdout); \
fprintf (stdout, fmt); \
TCS_NONE (stdout); \
} while (0)
# ifdef DEBUG
# define _DBG_PRINTF(fmt...) fprintf (stdout, fmt)
# define _DBG_PRINTF(fmt...) \
do { \
TCS_YELLOW (stderr); \
fprintf (stderr, fmt); \
TCS_NONE (stderr); \
} while (0)
# else
# define _DBG_PRINTF(fmt...)
# endif
@@ -1987,18 +2054,22 @@ int init_minigui_printf (int (*output_char) (int ch),
static inline void _ERR_PRINTF(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
va_start (ap, fmt);
TCS_BOLD_RED (stderr);
vfprintf (stderr, fmt, ap);
fprintf (stderr, "\n");
TCS_NONE (stderr);
va_end (ap);
}
static inline void _MG_PRINTF(const char* fmt, ...)
{
#ifdef DEBUG
va_list ap;
va_start (ap, fmt);
TCS_GREEN (stdout);
vfprintf (stdout, fmt, ap);
fprintf (stdout, "\n");
TCS_NONE (stdout);
va_end (ap);
#endif
}
@@ -2007,10 +2078,12 @@ static inline void _DBG_PRINTF(const char* fmt, ...)
{
#if defined(DEBUG) && defined(_DEBUG)
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
va_start (ap, fmt);
TCS_YELLOW (stderr);
vfprintf (stderr, fmt, ap);
fprintf (stderr, "\n");
TCS_NONE (stderr);
va_end (ap);
#endif
}
#endif /* !__GNUC__ */
+4 -6
View File
@@ -104,6 +104,7 @@ static inline void res_error(int type, const char* funcname, const char* strinfo
#ifdef WIN32
#else
#define ERR_RETV(ret, type, ...) do{ res_error((int)type, __FUNCTION__, __VA_ARGS__); return (ret); } while(0)
#define ERR_RET(type, ...) do{ res_error((int)type, __FUNCTION__, __VA_ARGS__); return ; } while(0)
@@ -380,8 +381,7 @@ BOOL InitializeResManager(int hash_table_size)
#endif
{
//step 2: get from MiniGUI.cfg
if(GetMgEtcValue("resinfo", "respath", szpath, sizeof(szpath)-1) == ETC_OK)
{
if(GetMgEtcValue("resinfo", "respath", szpath, sizeof(szpath)-1) == ETC_OK) {
cfg_res_path = strdup(szpath);
}
}
@@ -658,9 +658,7 @@ static char* get_res_file(const char* res_name, char* filename)
#ifndef __NOUNIX__
struct stat buf;
sprintf(filename,"%s/%s", res_paths[i], res_name);
if(stat(filename, &buf) == 0
&& !S_ISDIR(buf.st_mode)
&& (buf.st_mode&S_IRUSR))
if(stat(filename, &buf) == 0 && !S_ISDIR(buf.st_mode) && (buf.st_mode&S_IRUSR))
#else
sprintf(filename,"%s/%s", res_paths[i], res_name);
#ifndef __VXWORKS__
@@ -890,7 +888,7 @@ static void res_error(int type, const char* funcname, const char* strinfo, ... )
va_start (va, strinfo);
vsnprintf (strbuff, 255, strinfo, va);
fprintf (stderr, "SYSRES: (@%s)%s(ret %d): %s\n",
_ERR_PRINTF ("SYSRES: (@%s)%s(ret %d): %s\n",
funcname?funcname:"<>",
type>=1&&type<=RES_RET_INUSED?errinfo[type-1]:"",
type,