log to stdout if no logfile specified

some users want to run tinyproxy on an as-needed basis in a terminal,
without setting it up permanently to run as a daemon/service.
in such use case, it is very annoying that tinyproxy didn't have
an option to log to stdout, so the user has to keep a second terminal
open to `tail -f` the log.

additionally, this precluded usage with runit service supervisor,
which runs all services in foreground and creates logfiles from the
service's stdout/stderr.

since logging to stdout doesn't make sense when daemonized, now if
no logfile is specified and daemon mode activated, a warning is
printed to stderr once, and nothing is logged.
the original idea was to fail with an error message, though some users
might actually want to run tinyproxy as daemon and no logging at all.
This commit is contained in:
rofl0r 2017-11-04 19:40:42 +00:00 committed by Michael Adam
parent 64b29c5f4e
commit ccbbb81aa9
3 changed files with 21 additions and 5 deletions

View File

@ -90,7 +90,8 @@ StatFile "@pkgdatadir@/stats.html"
# LogFile: Allows you to specify the location where information should
# be logged to. If you would prefer to log to syslog, then disable this
# and enable the Syslog directive. These directives are mutually
# exclusive.
# exclusive. If neither Syslog nor LogFile are specified, output goes
# to stdout.
#
#LogFile "@localstatedir@/log/tinyproxy/tinyproxy.log"

View File

@ -70,7 +70,14 @@ static unsigned int logging_initialized = FALSE; /* boolean */
*/
int open_log_file (const char *log_file_name)
{
if (log_file_name == NULL) {
if(config.godaemon == FALSE)
log_file_fd = fileno(stdout);
else
log_file_fd = -1;
} else {
log_file_fd = create_file_safely (log_file_name, FALSE);
}
return log_file_fd;
}
@ -79,7 +86,7 @@ int open_log_file (const char *log_file_name)
*/
void close_log_file (void)
{
if (log_file_fd < 0) {
if (log_file_fd < 0 || log_file_fd == fileno(stdout)) {
return;
}
@ -108,6 +115,9 @@ void log_message (int level, const char *fmt, ...)
ssize_t ret;
if(!config.syslog && log_file_fd == -1)
return;
#ifdef NDEBUG
/*
* Figure out if we should write the message or not.

View File

@ -355,7 +355,7 @@ static void initialize_config_defaults (struct config_s *conf)
conf->errorpages = NULL;
conf->stathost = safestrdup (TINYPROXY_STATHOST);
conf->idletimeout = MAX_IDLE_TIME;
conf->logf_name = safestrdup (LOCALSTATEDIR "/log/tinyproxy/tinyproxy.log");
conf->logf_name = NULL;
conf->pidpath = NULL;
}
@ -415,8 +415,13 @@ main (int argc, char **argv)
anonymous_insert ("Content-Type");
}
if (config.godaemon == TRUE)
if (config.godaemon == TRUE) {
if (!config.syslog && config.logf_name == NULL)
fprintf(stderr, "WARNING: logging deactivated "
"(can't log to stdout when daemonized)\n");
makedaemon ();
}
if (set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR) {
fprintf (stderr, "%s: Could not set the \"SIGPIPE\" signal.\n",