1 /***************************************************************************
6 * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
7 * Copyright (C) 2006 Danny Kukawka, <danny.kukawka@web.de>
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 **************************************************************************/
43 * @defgroup HalDaemonLogging Logging system
45 * @brief Logging system for the HAL daemon
51 static const char *file
;
53 static const char *function
;
55 static int log_pid
= 0;
56 static int is_enabled
= 1;
57 static int syslog_enabled
= 0;
60 /** Disable all logging
69 /** Enable all logging
78 /** enable usage of syslog for logging
82 logger_enable_syslog (void)
87 /** disable usage of syslog for logging
91 logger_disable_syslog (void)
96 /** allow setup logger from a addon/prober via the env
102 if ((getenv ("HALD_VERBOSE")) != NULL
) {
109 if ((getenv ("HALD_USE_SYSLOG")) != NULL
)
115 /** Setup logging entry
117 * @param priority Logging priority, one of HAL_LOGPRI_*
118 * @param file Name of file where the log entry originated
119 * @param line Line number of file
120 * @param function Name of function
123 logger_setup (int _priority
, const char *_file
, int _line
, const char *_function
)
125 priority
= _priority
;
128 function
= _function
;
131 /** Emit logging entry
133 * @param format Message format string, printf style
134 * @param ... Parameters for message, printf style
137 logger_emit (const char *format
, ...)
145 struct tm
*tlocaltime
;
146 struct timezone tzone
;
147 static pid_t pid
= -1;
152 va_start (args
, format
);
153 vsnprintf (buf
, sizeof (buf
), format
, args
);
156 case HAL_LOGPRI_TRACE
:
159 case HAL_LOGPRI_DEBUG
:
162 case HAL_LOGPRI_INFO
:
165 case HAL_LOGPRI_WARNING
:
168 default: /* explicit fallthrough */
169 case HAL_LOGPRI_ERROR
:
174 gettimeofday (&tnow
, &tzone
);
175 tlocaltime
= localtime (&tnow
.tv_sec
);
176 strftime (tbuf
, sizeof (tbuf
), "%H:%M:%S", tlocaltime
);
181 snprintf (logmsg
, sizeof(logmsg
), "[%d]: %s.%03d %s %s:%d: %s\n", pid
, tbuf
, (int)(tnow
.tv_usec
/1000), pri
, file
, line
, buf
);
183 snprintf (logmsg
, sizeof(logmsg
), "%s.%03d %s %s:%d: %s\n", tbuf
, (int)(tnow
.tv_usec
/1000), pri
, file
, line
, buf
);
186 /** @todo Make programmatic interface to logging */
187 if (priority
!= HAL_LOGPRI_TRACE
&& !syslog_enabled
) {
188 fprintf (stderr
, "%s", logmsg
);
189 } else if (priority
!= HAL_LOGPRI_TRACE
&& syslog_enabled
) {
190 /* use syslog for debug/log messages if HAL started as daemon */
192 case HAL_LOGPRI_DEBUG
:
193 case HAL_LOGPRI_INFO
:
194 syslog(LOG_INFO
, "%s", logmsg
);
196 case HAL_LOGPRI_WARNING
:
197 syslog(LOG_WARNING
, "%s", logmsg
);
199 default: /* explicit fallthrough */
200 case HAL_LOGPRI_ERROR
:
201 syslog(LOG_ERR
, "%s", logmsg
);
210 logger_forward_debug (const char *format
, ...)
216 struct tm
*tlocaltime
;
217 struct timezone tzone
;
218 static pid_t pid
= -1;
226 va_start (args
, format
);
227 vsnprintf (buf
, sizeof (buf
), format
, args
);
229 gettimeofday (&tnow
, &tzone
);
230 tlocaltime
= localtime (&tnow
.tv_sec
);
231 strftime (tbuf
, sizeof (tbuf
), "%H:%M:%S", tlocaltime
);
234 syslog (LOG_INFO
, "%d: %s.%03d: %s", pid
, tbuf
, (int)(tnow
.tv_usec
/1000), buf
);
236 fprintf (stderr
, "%d: %s.%03d: %s", pid
, tbuf
, (int)(tnow
.tv_usec
/1000), buf
);