mirror of
https://github.com/RubyMetric/chsrc.git
synced 2025-01-22 12:49:56 +08:00
Add xy_log()
and xy_info()
xy_error()
This commit is contained in:
parent
9fd89d2911
commit
c7d8f88eb4
48
helper.h
48
helper.h
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
@ -43,6 +44,53 @@ xy_strjoin (const char* str1, const char* str2)
|
|||
}
|
||||
|
||||
|
||||
#define XY_INFO 1
|
||||
#define XY_SUCCESS 1<<1
|
||||
#define XY_WARN 1<<2
|
||||
#define XY_ERROR 1<<3
|
||||
|
||||
static void
|
||||
xy_log (int level, const char* str)
|
||||
{
|
||||
char* color_fmt_str = NULL;
|
||||
|
||||
bool to_stderr = false;
|
||||
|
||||
if (level & XY_INFO) {
|
||||
color_fmt_str = "\033[34m%s\033[0m"; // 蓝色
|
||||
}
|
||||
else if (level & XY_SUCCESS) {
|
||||
color_fmt_str = "\033[32m%s\033[0m"; // 绿色
|
||||
}
|
||||
else if (level & XY_WARN) {
|
||||
color_fmt_str = "\033[33m%s\033[0m\n"; // 黄色
|
||||
to_stderr = true;
|
||||
}
|
||||
else if (level & XY_ERROR) {
|
||||
color_fmt_str = "\033[31m%s\033[0m\n"; // 红色
|
||||
to_stderr = true;
|
||||
}
|
||||
else {
|
||||
//xy_assert ("CAN'T REACH!");
|
||||
}
|
||||
|
||||
// -2 把中间%s减掉,-1 把末尾nul减掉
|
||||
size_t len = sizeof(color_fmt_str) -2 -1;
|
||||
char* buf = malloc(strlen(str) + len + 1);
|
||||
|
||||
sprintf (buf, color_fmt_str, str);
|
||||
if (to_stderr) {
|
||||
fprintf(stderr, buf);
|
||||
} else {
|
||||
puts(buf);
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
||||
#define xy_info(str) xy_log (XY_INFO, str)
|
||||
#define xy_error(str) xy_log (XY_ERROR, str)
|
||||
|
||||
|
||||
static void
|
||||
xy_success (const char* str1)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user