1N/A/***************************************************************************
1N/A * CVSID: $Id$
1N/A *
1N/A * logger.c : Logging
1N/A *
1N/A * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
1N/A * Copyright (C) 2006 Danny Kukawka, <danny.kukawka@web.de>
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <stdarg.h>
1N/A#include <time.h>
1N/A#include <sys/time.h>
1N/A#include <syslog.h>
1N/A#include <unistd.h>
1N/A
1N/A#include "logger.h"
1N/A
1N/A/**
1N/A * @defgroup HalDaemonLogging Logging system
1N/A * @ingroup HalDaemon
1N/A * @brief Logging system for the HAL daemon
1N/A * @{
1N/A */
1N/A
1N/A
1N/Astatic int priority;
1N/Astatic const char *file;
1N/Astatic int line;
1N/Astatic const char *function;
1N/A
1N/Astatic int log_pid = 0;
1N/Astatic int is_enabled = 1;
1N/Astatic int syslog_enabled = 0;
1N/A
1N/A
1N/A/** Disable all logging
1N/A *
1N/A */
1N/Avoid
1N/Alogger_disable (void)
1N/A{
1N/A is_enabled = 0;
1N/A}
1N/A
1N/A/** Enable all logging
1N/A *
1N/A */
1N/Avoid
1N/Alogger_enable (void)
1N/A{
1N/A is_enabled = 1;
1N/A}
1N/A
1N/A/** enable usage of syslog for logging
1N/A *
1N/A */
1N/Avoid
1N/Alogger_enable_syslog (void)
1N/A{
1N/A syslog_enabled = 1;
1N/A}
1N/A
1N/A/** disable usage of syslog for logging
1N/A *
1N/A */
1N/Avoid
1N/Alogger_disable_syslog (void)
1N/A{
1N/A syslog_enabled = 0;
1N/A}
1N/A
1N/A/** allow setup logger from a addon/prober via the env
1N/A *
1N/A */
1N/Avoid
1N/Asetup_logger (void)
1N/A{
1N/A if ((getenv ("HALD_VERBOSE")) != NULL) {
1N/A is_enabled = 1;
1N/A log_pid = 1;
1N/A }
1N/A else
1N/A is_enabled = 0;
1N/A
1N/A if ((getenv ("HALD_USE_SYSLOG")) != NULL)
1N/A syslog_enabled = 1;
1N/A else
1N/A syslog_enabled = 0;
1N/A}
1N/A
1N/A/** Setup logging entry
1N/A *
1N/A * @param priority Logging priority, one of HAL_LOGPRI_*
1N/A * @param file Name of file where the log entry originated
1N/A * @param line Line number of file
1N/A * @param function Name of function
1N/A */
1N/Avoid
1N/Alogger_setup (int _priority, const char *_file, int _line, const char *_function)
1N/A{
1N/A priority = _priority;
1N/A file = _file;
1N/A line = _line;
1N/A function = _function;
1N/A}
1N/A
1N/A/** Emit logging entry
1N/A *
1N/A * @param format Message format string, printf style
1N/A * @param ... Parameters for message, printf style
1N/A */
1N/Avoid
1N/Alogger_emit (const char *format, ...)
1N/A{
1N/A va_list args;
1N/A char buf[512];
1N/A char *pri;
1N/A char tbuf[256];
1N/A char logmsg[1024];
1N/A struct timeval tnow;
1N/A struct tm *tlocaltime;
1N/A struct timezone tzone;
1N/A static pid_t pid = -1;
1N/A
1N/A if (!is_enabled)
1N/A return;
1N/A
1N/A va_start (args, format);
1N/A vsnprintf (buf, sizeof (buf), format, args);
1N/A
1N/A switch (priority) {
1N/A case HAL_LOGPRI_TRACE:
1N/A pri = "[T]";
1N/A break;
1N/A case HAL_LOGPRI_DEBUG:
1N/A pri = "[D]";
1N/A break;
1N/A case HAL_LOGPRI_INFO:
1N/A pri = "[I]";
1N/A break;
1N/A case HAL_LOGPRI_WARNING:
1N/A pri = "[W]";
1N/A break;
1N/A default: /* explicit fallthrough */
1N/A case HAL_LOGPRI_ERROR:
1N/A pri = "[E]";
1N/A break;
1N/A }
1N/A
1N/A gettimeofday (&tnow, &tzone);
1N/A tlocaltime = localtime (&tnow.tv_sec);
1N/A strftime (tbuf, sizeof (tbuf), "%H:%M:%S", tlocaltime);
1N/A
1N/A if (log_pid) {
1N/A if ((int) pid == -1)
1N/A pid = getpid ();
1N/A snprintf (logmsg, sizeof(logmsg), "[%d]: %s.%03d %s %s:%d: %s\n", pid, tbuf, (int)(tnow.tv_usec/1000), pri, file, line, buf);
1N/A } else {
1N/A snprintf (logmsg, sizeof(logmsg), "%s.%03d %s %s:%d: %s\n", tbuf, (int)(tnow.tv_usec/1000), pri, file, line, buf);
1N/A }
1N/A
1N/A /** @todo Make programmatic interface to logging */
1N/A if (priority != HAL_LOGPRI_TRACE && !syslog_enabled ) {
1N/A fprintf (stderr, "%s", logmsg );
1N/A } else if (priority != HAL_LOGPRI_TRACE && syslog_enabled ) {
1N/A /* use syslog for debug/log messages if HAL started as daemon */
1N/A switch (priority) {
1N/A case HAL_LOGPRI_DEBUG:
1N/A case HAL_LOGPRI_INFO:
1N/A syslog(LOG_INFO, "%s", logmsg );
1N/A break;
1N/A case HAL_LOGPRI_WARNING:
1N/A syslog(LOG_WARNING, "%s", logmsg );
1N/A break;
1N/A default: /* explicit fallthrough */
1N/A case HAL_LOGPRI_ERROR:
1N/A syslog(LOG_ERR, "%s", logmsg );
1N/A break;
1N/A }
1N/A }
1N/A
1N/A va_end (args);
1N/A}
1N/A
1N/Avoid
1N/Alogger_forward_debug (const char *format, ...)
1N/A{
1N/A va_list args;
1N/A char buf[512];
1N/A char tbuf[256];
1N/A struct timeval tnow;
1N/A struct tm *tlocaltime;
1N/A struct timezone tzone;
1N/A static pid_t pid = -1;
1N/A
1N/A if (!is_enabled)
1N/A return;
1N/A
1N/A if ((int) pid == -1)
1N/A pid = getpid ();
1N/A
1N/A va_start (args, format);
1N/A vsnprintf (buf, sizeof (buf), format, args);
1N/A
1N/A gettimeofday (&tnow, &tzone);
1N/A tlocaltime = localtime (&tnow.tv_sec);
1N/A strftime (tbuf, sizeof (tbuf), "%H:%M:%S", tlocaltime);
1N/A
1N/A if (syslog_enabled)
1N/A syslog (LOG_INFO, "%d: %s.%03d: %s", pid, tbuf, (int)(tnow.tv_usec/1000), buf);
1N/A else
1N/A fprintf (stderr, "%d: %s.%03d: %s", pid, tbuf, (int)(tnow.tv_usec/1000), buf);
1N/A
1N/A va_end (args);
1N/A}
1N/A
1N/A/** @} */