From 2de1ef639ba3d32e4e2ca1931a609d896f6c14ef Mon Sep 17 00:00:00 2001 From: JohnnySun Date: Thu, 5 Mar 2015 21:32:05 +0800 Subject: [PATCH] Add isatty check when change STDERR_FILENO to a file --- src/local.c | 2 ++ src/server.c | 2 ++ src/tunnel.c | 2 ++ src/utils.c | 2 ++ src/utils.h | 67 +++++++++++++++++++++++++++++++++++----------------- 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/local.c b/src/local.c index c3859338..13d4748a 100644 --- a/src/local.c +++ b/src/local.c @@ -899,6 +899,8 @@ int main(int argc, char **argv) opterr = 0; + USE_TTY(); + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uv", long_options, &option_index)) != -1) { switch (c) { diff --git a/src/server.c b/src/server.c index 354cb402..ea3328e7 100644 --- a/src/server.c +++ b/src/server.c @@ -1093,6 +1093,8 @@ int main(int argc, char **argv) opterr = 0; + USE_TTY(); + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:c:i:d:a:uv", long_options, &option_index)) != -1) { switch (c) { diff --git a/src/tunnel.c b/src/tunnel.c index 536bd449..fb90bf67 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -652,6 +652,8 @@ int main(int argc, char **argv) opterr = 0; + USE_TTY(); + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uv")) != -1) { switch (c) { case 's': diff --git a/src/utils.c b/src/utils.c index 2398381a..7ef6f597 100644 --- a/src/utils.c +++ b/src/utils.c @@ -61,6 +61,8 @@ void ERROR(const char *s) } #endif +int use_tty = 1; + char *ss_itoa(int i) { /* Room for INT_DIGITS digits, - and '\0' */ diff --git a/src/utils.h b/src/utils.h index a9fbd08f..1bc072eb 100644 --- a/src/utils.h +++ b/src/utils.h @@ -24,6 +24,7 @@ #define _UTILS_H #include +#include #include #define PORTSTRLEN 16 @@ -33,6 +34,7 @@ #include +#define USE_TTY() #define USE_SYSLOG(ident) #define LOGI(...) \ ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", \ @@ -52,6 +54,8 @@ extern FILE * logfile; #define TIME_FORMAT "%Y-%m-%d %H:%M:%S" +#define USE_TTY() + #define USE_SYSLOG(ident) #define USE_LOGFILE(ident) \ @@ -91,6 +95,8 @@ extern FILE * logfile; #define TIME_FORMAT "%Y-%m-%d %H:%M:%S" +#define USE_TTY() + #define USE_SYSLOG(ident) #define LOGI(format, ...) \ @@ -115,6 +121,12 @@ extern FILE * logfile; #include +extern int use_tty; +#define USE_TTY() \ + do { \ + use_tty = isatty(STDERR_FILENO); \ + } while (0) \ + #define HAS_SYSLOG extern int use_syslog; @@ -126,30 +138,41 @@ extern int use_syslog; openlog((ident), LOG_CONS | LOG_PID, 0); } \ while (0) -#define LOGI(format, ...) \ - do { \ - if (use_syslog) { \ - syslog(LOG_INFO, format, ## __VA_ARGS__); \ - } else { \ - time_t now = time(NULL); \ - char timestr[20]; \ - strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ - fprintf(stderr, "\e[01;32m %s INFO: \e[0m" format "\n", timestr, \ - ## __VA_ARGS__); \ - } } \ +#define LOGI(format, ...) \ + do { \ + if (use_syslog) { \ + syslog(LOG_INFO, format, ## __VA_ARGS__); \ + } else { \ + time_t now = time(NULL); \ + char timestr[20]; \ + strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ + if(use_tty) { \ + fprintf(stderr, "\e[01;32m %s INFO: \e[0m" format "\n", timestr, \ + ## __VA_ARGS__); \ + } else { \ + fprintf(stderr, "%s INFO: " format "\n", timestr, \ + ## __VA_ARGS__); \ + } \ + } \ + } \ while (0) -#define LOGE(format, ...) \ - do { \ - if (use_syslog) { \ - syslog(LOG_ERR, format, ## __VA_ARGS__); \ - } else { \ - time_t now = time(NULL); \ - char timestr[20]; \ - strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ - fprintf(stderr, "\e[01;35m %s ERROR: \e[0m" format "\n", timestr, \ - ## __VA_ARGS__); \ - } } \ +#define LOGE(format, ...) \ + do { \ + if (use_syslog) { \ + syslog(LOG_ERR, format, ## __VA_ARGS__); \ + } else { \ + time_t now = time(NULL); \ + char timestr[20]; \ + strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ + if(use_tty) { \ + fprintf(stderr, "\e[01;35m %s ERROR: \e[0m" format "\n", timestr, \ + ## __VA_ARGS__); \ + } else { \ + fprintf(stderr, " %s ERROR: " format "\n", timestr, \ + ## __VA_ARGS__); \ + } \ + } } \ while (0) #endif