Example #1
0
File: log.c Project: cutso/repmgr
bool
logger_init(t_configuration_options * opts, const char *ident)
{
	char	   *level = opts->loglevel;
	char	   *facility = opts->logfacility;

	int			l;
	int			f;

#ifdef HAVE_SYSLOG
	int			syslog_facility = DEFAULT_SYSLOG_FACILITY;
#endif

#ifdef REPMGR_DEBUG
	printf("Logger initialisation (Level: %s, Facility: %s)\n", level, facility);
#endif

	if (!ident)
	{
		ident = DEFAULT_IDENT;
	}

	if (level && *level)
	{
		l = detect_log_level(level);
#ifdef REPMGR_DEBUG
		printf("Assigned level for logger: %d\n", l);
#endif

		if (l >= 0)
			log_level = l;
		else
			stderr_log_warning(_("Invalid log level \"%s\" (available values: DEBUG, INFO, NOTICE, WARNING, ERR, ALERT, CRIT or EMERG)\n"), level);
	}

	if (facility && *facility)
	{

		f = detect_log_facility(facility);
#ifdef REPMGR_DEBUG
		printf("Assigned facility for logger: %d\n", f);
#endif

		if (f == 0)
		{
			/* No syslog requested, just stderr */
#ifdef REPMGR_DEBUG
			printf(_("Use stderr for logging\n"));
#endif
		}
		else if (f == -1)
		{
			stderr_log_warning(_("Cannot detect log facility %s (use any of LOCAL0, LOCAL1, ..., LOCAL7, USER or STDERR)\n"), facility);
		}
#ifdef HAVE_SYSLOG
		else
		{
			syslog_facility = f;
			log_type = REPMGR_SYSLOG;
		}
#endif
	}

#ifdef HAVE_SYSLOG

	if (log_type == REPMGR_SYSLOG)
	{
		setlogmask(LOG_UPTO(log_level));
		openlog(ident, LOG_CONS | LOG_PID | LOG_NDELAY, syslog_facility);

		stderr_log_notice(_("Setup syslog (level: %s, facility: %s)\n"), level, facility);
	}
#endif

	if (*opts->logfile)
	{
		FILE	   *fd;

		/* Check if we can write to the specified file before redirecting
		 * stderr - if freopen() fails, stderr output will vanish into
		 * the ether and the user won't know what's going on.
		 */

		fd = fopen(opts->logfile, "a");
		if (fd == NULL)
		{
			stderr_log_err(_("Unable to open specified logfile '%s' for writing: %s\n"), opts->logfile, strerror(errno));
			stderr_log_err(_("Terminating\n"));
			exit(ERR_BAD_CONFIG);
		}
		fclose(fd);

		stderr_log_notice(_("Redirecting logging output to '%s'\n"), opts->logfile);
		fd = freopen(opts->logfile, "a", stderr);

		/* It's possible freopen() may still fail due to e.g. a race condition;
		   as it's not feasible to restore stderr after a failed freopen(),
		   we'll write to stdout as a last resort.
		 */
		if (fd == NULL)
		{
			printf(_("Unable to open specified logfile %s for writing: %s\n"), opts->logfile, strerror(errno));
			printf(_("Terminating\n"));
			exit(ERR_BAD_CONFIG);
		}
	}

	return true;
}
Example #2
0
File: log.c Project: cynipe/repmgr
bool logger_init(t_configuration_options *opts, const char* ident, const char* level, const char* facility)
{

	int l;
	int f;

#ifdef HAVE_SYSLOG
	int syslog_facility = DEFAULT_SYSLOG_FACILITY;
#endif

#ifdef REPMGR_DEBUG
	printf("Logger initialisation (Level: %s, Facility: %s)\n", level, facility);
#endif

	if (!ident)
	{
		ident = DEFAULT_IDENT;
	}

	if (level && *level)
	{
		l = detect_log_level(level);
#ifdef REPMGR_DEBUG
		printf("Assigned level for logger: %d\n", l);
#endif

		if (l > 0)
			log_level = l;
		else
			stderr_log_warning(_("Cannot detect log level %s (use any of DEBUG, INFO, NOTICE, WARNING, ERR, ALERT, CRIT or EMERG)\n"), level);
	}

	if (facility && *facility)
	{

		f = detect_log_facility(facility);
#ifdef REPMGR_DEBUG
		printf("Assigned facility for logger: %d\n", f);
#endif

		if (f == 0)
		{
			/* No syslog requested, just stderr */
#ifdef REPMGR_DEBUG
			printf(_("Use stderr for logging\n"));
#endif
		}
		else if (f == -1)
		{
			stderr_log_warning(_("Cannot detect log facility %s (use any of LOCAL0, LOCAL1, ..., LOCAL7, USER or STDERR)\n"), facility);
		}
#ifdef HAVE_SYSLOG
		else
		{
			syslog_facility = f;
			log_type = REPMGR_SYSLOG;
		}
#endif
	}

#ifdef HAVE_SYSLOG

	if (log_type == REPMGR_SYSLOG)
	{
		setlogmask (LOG_UPTO (log_level));
		openlog (ident, LOG_CONS | LOG_PID | LOG_NDELAY, syslog_facility);

		stderr_log_notice(_("Setup syslog (level: %s, facility: %s)\n"), level, facility);
	}

#endif

	if (*opts->logfile)
	{
		freopen(opts->logfile, "a", stderr);
	}

	return true;

}