Remove intermediate impl

This commit is contained in:
Aoran Zeng 2023-09-05 15:36:26 +08:00
parent cf20526f1c
commit db131f335e

54
xy.h
View File

@ -87,29 +87,34 @@ xy_malloc0 (size_t size)
}
#define XY_INFO 00001
#define XY_SUCCESS 00001<<1
#define XY_WARN 00001<<2
#define XY_ERROR 00001<<3
#define XY_LOG_INFO 00001
#define XY_LOG_SUCCESS 00001<<1
#define XY_LOG_WARN 00001<<2
#define XY_LOG_ERROR 00001<<3
#define xy_success(str) xy_log_ (XY_LOG_SUCCESS, str)
#define xy_info(str) xy_log_ (XY_LOG_INFO, str)
#define xy_warn(str) xy_log_ (XY_LOG_WARN, str)
#define xy_error(str) xy_log_ (XY_LOG_ERROR, str)
static void
xy_log (int level, const char* str)
xy_log_ (int level, const char* str)
{
char* color_fmt_str = NULL;
bool to_stderr = false;
if (level & XY_INFO) {
if (level & XY_LOG_INFO) {
color_fmt_str = "\033[34m%s\033[0m"; // 蓝色
}
else if (level & XY_SUCCESS) {
else if (level & XY_LOG_SUCCESS) {
color_fmt_str = "\033[32m%s\033[0m"; // 绿色
}
else if (level & XY_WARN) {
else if (level & XY_LOG_WARN) {
color_fmt_str = "\033[33m%s\033[0m\n"; // 黄色
to_stderr = true;
}
else if (level & XY_ERROR) {
else if (level & XY_LOG_ERROR) {
color_fmt_str = "\033[31m%s\033[0m\n"; // 红色
to_stderr = true;
}
@ -130,8 +135,6 @@ xy_log (int level, const char* str)
free(buf);
}
#define xy_info(str) xy_log (XY_INFO, str)
#define xy_error(str) xy_log (XY_ERROR, str)
/**
@ -164,35 +167,6 @@ xy_strch (const char* str, char src,const char* dest)
}
static void
xy_success (const char* str1)
{
char color_fmt_str[] = "\033[32m%s\033[0m";
// -2 把中间%s减掉,-1 把末尾nul减掉
size_t len = sizeof(color_fmt_str) -2 -1;
char* buf = malloc(strlen(str1) + len + 1);
sprintf (buf, color_fmt_str, str1);
puts(buf);
free(buf);
}
static void
xy_warn (const char* str1)
{
// 注意,我们这里相比于xy_suceess()多了一个换行符
char color_fmt_str[] = "\033[33m%s\033[0m\n";
// -2 把中间%s减掉,-1 把末尾nul减掉
size_t len = sizeof(color_fmt_str) -2 -1;
char* buf = malloc(strlen(str1) + len + 1);
sprintf (buf, color_fmt_str, str1);
fprintf(stderr, buf);
free(buf);
}
static char*
xy_2strjoin (const char* str1, const char* str2)
{