Browse Source

Log to syslog when running in daemon mode.

pull/29/head
Zhang Yi 11 years ago
parent
commit
c6b3b99fe0
5 changed files with 38 additions and 10 deletions
  1. 1
      src/local.c
  2. 1
      src/redir.c
  3. 3
      src/server.c
  4. 4
      src/utils.c
  5. 39
      src/utils.h

1
src/local.c

@ -896,6 +896,7 @@ int main (int argc, char **argv)
if (pid_flags)
{
USE_SYSLOG(argv[0]);
demonize(pid_path);
}

1
src/redir.c

@ -749,6 +749,7 @@ int main (int argc, char **argv)
if (pid_flags)
{
USE_SYSLOG(argv[0]);
demonize(pid_path);
}

3
src/server.c

@ -988,6 +988,7 @@ int main (int argc, char **argv)
if (pid_flags)
{
USE_SYSLOG(argv[0]);
demonize(pid_path);
}
@ -1043,7 +1044,7 @@ int main (int argc, char **argv)
}
// Setup UDP
if (udprelay)
if (udprelay)
{
LOGD("udprelay enabled.");
udprelay_init(server_host[0], server_port, asyncns, m, iface);

4
src/utils.c

@ -14,6 +14,10 @@
#define INT_DIGITS 19 /* enough for 64 bit integer */
#ifdef HAS_SYSLOG
int use_syslog = 0;
#endif
#ifndef __MINGW32__
void ERROR(const char *s)
{

39
src/utils.h

@ -8,6 +8,7 @@
#include <android/log.h>
#define USE_SYSLOG(ident)
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "shadowsocks", __VA_ARGS__))
@ -20,6 +21,8 @@
#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
#define USE_SYSLOG(ident)
#define LOGD(format, ...) do {\
time_t now = time(NULL);\
char timestr[20];\
@ -36,23 +39,41 @@ while(0)
#else
#include <syslog.h>
#define HAS_SYSLOG
extern int use_syslog;
#define TIME_FORMAT "%F %T"
#define USE_SYSLOG(ident) do {\
use_syslog = 1;\
openlog((ident), LOG_CONS | LOG_PID, 0);}\
while(0)
#define LOGD(format, ...) do {\
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__);}\
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__);\
}}\
while(0)
#define LOGE(format, ...) do {\
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__);}\
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__);\
}}\
while(0)
#endif
#endif
/* _WIN32 */
#endif

Loading…
Cancel
Save