0
0
mirror of https://github.com/darkk/redsocks.git synced 2025-08-30 21:55:31 +00:00

Added logging to file.

Thanks to Przemyslaw Pawelczyk <przemoc@gmail.com>
This commit is contained in:
Leonid Evdokimov 2009-05-21 11:38:48 +07:00
parent b7cd855347
commit a37aa7a0e6
2 changed files with 31 additions and 4 deletions

33
log.c
View File

@ -28,15 +28,32 @@ static const char *lowmem = "<Can't print error, not enough memory>";
typedef void (*log_func)(const char *file, int line, const char *func, int priority, const char *message, const char *appendix);
static void stderr_msg(const char *file, int line, const char *func, int priority, const char *message, const char *appendix)
static void fprint_timestamp(
FILE* fd,
const char *file, int line, const char *func, int priority, const char *message, const char *appendix)
{
struct timeval tv = { };
gettimeofday(&tv, 0);
/* XXX: there is no error-checking, IMHO it's better to lose messages
* then to die and stop service */
if (appendix)
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s: %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message, appendix);
fprintf(fd, "%lu.%6.6lu %s:%u %s(...) %s: %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message, appendix);
else
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message);
fprintf(fd, "%lu.%6.6lu %s:%u %s(...) %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message);
}
static void stderr_msg(const char *file, int line, const char *func, int priority, const char *message, const char *appendix)
{
fprint_timestamp(stderr, file, line, func, priority, message, appendix);
}
static FILE *logfile = NULL;
static void logfile_msg(const char *file, int line, const char *func, int priority, const char *message, const char *appendix)
{
fprint_timestamp(logfile, file, line, func, priority, message, appendix);
fflush(logfile);
}
static void syslog_msg(const char *file, int line, const char *func, int priority, const char *message, const char *appendix)
@ -54,6 +71,7 @@ static log_func log_msg_next = NULL;
int log_preopen(const char *dst, bool log_debug, bool log_info)
{
const char *syslog_prefix = "syslog:";
const char *file_prefix = "file:";
if (strcmp(dst, "stderr") == 0) {
log_msg_next = stderr_msg;
}
@ -96,6 +114,15 @@ int log_preopen(const char *dst, bool log_debug, bool log_info)
log_msg_next = syslog_msg;
}
else if (strncmp(dst, file_prefix, strlen(file_prefix)) == 0) {
const char *filename = dst + strlen(file_prefix);
if ((logfile = fopen(filename, "a")) == NULL) {
log_error(LOG_ERR, "log_preopen(%s, ...): %s", dst, strerror(errno));
return -1;
}
log_msg_next = logfile_msg;
/* TODO: add log rotation */
}
else {
log_error(LOG_ERR, "log_preopen(%s, ...): unknown destination", dst);
return -1;

View File

@ -7,7 +7,7 @@ base {
/* possible `log' values are:
* stderr
* /path/to/file
* file:/path/to/file
* syslog:FACILITY facility is any of "daemon", "local0"..."local7"
*/
log = stderr;