5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher/*
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher SSSD
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher sss_log.c
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher Authors:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher Copyright (C) 2010 Red Hat
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher This program is free software; you can redistribute it and/or modify
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher it under the terms of the GNU General Public License as published by
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher (at your option) any later version.
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher This program is distributed in the hope that it will be useful,
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher GNU General Public License for more details.
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher You should have received a copy of the GNU General Public License
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher*/
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher#include "util/util.h"
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#ifdef WITH_JOURNALD
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#include <systemd/sd-journal.h>
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#else /* WITH_JOURNALD */
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher#include <syslog.h>
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#endif /* WITH_JOURNALD */
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagherstatic int sss_to_syslog(int priority)
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher{
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher switch(priority) {
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_EMERG:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_EMERG;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_ALERT:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_ALERT;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_CRIT:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_CRIT;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_ERR:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_ERR;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_WARNING:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_WARNING;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_NOTICE:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_NOTICE;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_INFO:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_INFO;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher case SSS_LOG_DEBUG:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_DEBUG;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher default:
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher /* If we've been passed an invalid priority, it's
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher * best to assume it's an emergency.
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher */
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher return LOG_EMERG;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher }
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher}
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bosestatic void sss_log_internal(int priority, int facility, const char *format,
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose va_list ap);
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elleyvoid sss_log(int priority, const char *format, ...)
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley{
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley va_list ap;
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley va_start(ap, format);
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose sss_log_internal(priority, LOG_DAEMON, format, ap);
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley va_end(ap);
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley}
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elleyvoid sss_log_ext(int priority, int facility, const char *format, ...)
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek{
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek va_list ap;
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose va_start(ap, format);
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose sss_log_internal(priority, facility, format, ap);
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose va_end(ap);
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose}
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose#ifdef WITH_JOURNALD
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bosestatic void sss_log_internal(int priority, int facility, const char *format,
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose va_list ap)
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose{
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek int syslog_priority;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek int ret;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek char *message;
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek const char *domain;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek ret = vasprintf(&message, format, ap);
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek if (ret == -1) {
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek /* ENOMEM */
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek return;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek }
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek domain = getenv(SSS_DOM_ENV);
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek if (domain == NULL) {
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek domain = "";
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek }
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek syslog_priority = sss_to_syslog(priority);
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek sd_journal_send("MESSAGE=%s", message,
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek "SSSD_DOMAIN=%s", domain,
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek "PRIORITY=%i", syslog_priority,
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley "SYSLOG_FACILITY=%i", LOG_FAC(facility),
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek "SYSLOG_IDENTIFIER=%s", debug_prg_name,
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek NULL);
5a065092ef744540f57e3e6e72210fbe17a1e2f3Jakub Hrozek
5a065092ef744540f57e3e6e72210fbe17a1e2f3Jakub Hrozek free(message);
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek}
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#else /* WITH_JOURNALD */
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bosestatic void sss_log_internal(int priority, int facility, const char *format,
cbc005cd63523ca7d772c9f0c7ea9a4817c2c07eSumit Bose va_list ap)
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher{
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher int syslog_priority;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher syslog_priority = sss_to_syslog(priority);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
588f8fbe74e66cc015f185a5b798173d320a65b5Yassir Elley openlog(debug_prg_name, 0, facility);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher vsyslog(syslog_priority, format, ap);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher closelog();
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher}
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#endif /* WITH_JOURNALD */