sss_log.c revision 5a065092ef744540f57e3e6e72210fbe17a1e2f3
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
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#ifdef WITH_JOURNALD
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozekvoid sss_log(int priority, const char *format, ...)
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek{
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek va_list ap;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek int syslog_priority;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek int ret;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek char *message;
d6837823b0a002c9c8afb48caf3eeda5cb73f173Jakub Hrozek const char *domain;
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek va_start(ap, format);
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek ret = vasprintf(&message, format, ap);
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek va_end(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,
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek "SYSLOG_FACILITY=%i", LOG_FAC(LOG_DAEMON),
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
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallaghervoid sss_log(int priority, const char *format, ...)
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher{
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher va_list ap;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher int syslog_priority;
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher syslog_priority = sss_to_syslog(priority);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher openlog(debug_prg_name, 0, LOG_DAEMON);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher va_start(ap, format);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher vsyslog(syslog_priority, format, ap);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher va_end(ap);
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher closelog();
5c705137ea853613318a29f0cd760688ab4daef3Stephen Gallagher}
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek
77c0d1f6074059dafd2293f9c42ea0f9d60f8aadJakub Hrozek#endif /* WITH_JOURNALD */