Browse Source

Merge pull request #276 from JohnnySun/master

Add isatty check when change STDERR_FILENO to a file
pull/278/merge
Max Lv 10 years ago
parent
commit
48b7492940
5 changed files with 53 additions and 22 deletions
  1. 2
      src/local.c
  2. 2
      src/server.c
  3. 2
      src/tunnel.c
  4. 2
      src/utils.c
  5. 67
      src/utils.h

2
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) {

2
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) {

2
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':

2
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' */

67
src/utils.h

@ -24,6 +24,7 @@
#define _UTILS_H
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#define PORTSTRLEN 16
@ -33,6 +34,7 @@
#include <android/log.h>
#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 <syslog.h>
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

Loading…
Cancel
Save