diff --git a/src/utils.h b/src/utils.h index 113cefd4..0a6bf5fd 100644 --- a/src/utils.h +++ b/src/utils.h @@ -98,8 +98,10 @@ extern FILE *logfile; time_t now = time(NULL); \ char timestr[20]; \ strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ - fprintf(stdout, " %s INFO: " format "\n", timestr, \ - ## __VA_ARGS__); \ + ss_color_info(); \ + fprintf(stdout, " %s INFO: ", timestr); \ + ss_color_reset(); \ + fprintf(stdout, format "\n", ## __VA_ARGS__); \ } \ while (0) @@ -108,8 +110,10 @@ extern FILE *logfile; time_t now = time(NULL); \ char timestr[20]; \ strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ - fprintf(stdout, " %s ERROR: " format "\n", timestr, \ - ## __VA_ARGS__); \ + ss_color_error(); \ + fprintf(stdout, " %s ERROR: ", timestr); \ + ss_color_reset(); \ + fprintf(stdout, format "\n", ## __VA_ARGS__); \ } \ while (0) @@ -196,7 +200,12 @@ extern int use_syslog; #undef ERROR #endif #define ERROR(s) ss_error(s) -void ss_error(const char *s); // Implemented in winsock.c + +// Implemented in winsock.c +void ss_error(const char *s); +void ss_color_info(void); +void ss_color_error(void); +void ss_color_reset(void); #else void ERROR(const char *s); #endif diff --git a/src/winsock.c b/src/winsock.c index 80c55956..8268be8b 100644 --- a/src/winsock.c +++ b/src/winsock.c @@ -33,6 +33,14 @@ #define STD_INPUT_HANDLE ((DWORD)-10) #endif +#ifndef ENABLE_EXTENDED_FLAGS +#define ENABLE_EXTENDED_FLAGS 0x0080 +#endif + +#ifndef STD_OUTPUT_HANDLE +#define STD_OUTPUT_HANDLE ((DWORD)-11) +#endif + static void disable_quick_edit(void) { @@ -40,7 +48,9 @@ disable_quick_edit(void) HANDLE console = GetStdHandle(STD_INPUT_HANDLE); // Get current console mode - if (console == NULL || !GetConsoleMode(console, &mode)) { + if (console == NULL || + console == INVALID_HANDLE_VALUE || + !GetConsoleMode(console, &mode)) { return; } @@ -99,6 +109,67 @@ ss_error(const char *s) } } +static BOOL +get_conattr(HANDLE console, WORD *out_attr) +{ + static BOOL done = FALSE; + static WORD saved_attr = 0; + if (!done) { + CONSOLE_SCREEN_BUFFER_INFO info; + if (GetConsoleScreenBufferInfo(console, &info)) { + saved_attr = info.wAttributes; + done = TRUE; + } + } + if (out_attr != NULL) { + *out_attr = saved_attr; + } + return done; +} + +static BOOL +set_concolor(WORD color, BOOL reset) +{ + static HANDLE console = NULL; + if (console == NULL) { + console = GetStdHandle(STD_OUTPUT_HANDLE); + if (console == NULL || + console == INVALID_HANDLE_VALUE) { + console = NULL; + return FALSE; + } + } + WORD attr; + if (!get_conattr(console, &attr)) { + return FALSE; + } + if (!reset) { + // Only override foreground color without changing background + attr &= ~(FOREGROUND_RED | FOREGROUND_GREEN | + FOREGROUND_BLUE | FOREGROUND_INTENSITY); + attr |= (color | FOREGROUND_INTENSITY); + } + return SetConsoleTextAttribute(console, attr); +} + +void +ss_color_info(void) +{ + set_concolor(FOREGROUND_GREEN, FALSE); +} + +void +ss_color_error(void) +{ + set_concolor(FOREGROUND_RED | FOREGROUND_BLUE, FALSE); +} + +void +ss_color_reset(void) +{ + set_concolor(0, TRUE); +} + #ifdef TCP_FASTOPEN_WINSOCK LPFN_CONNECTEX winsock_getconnectex(void)