summaryrefslogtreecommitdiffstats
path: root/src/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..cd7c2d5
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,101 @@
1/*
2 * log.c
3 *
4 * Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
5 * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 or version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdarg.h>
29#include <time.h>
30#include <sys/time.h>
31#include <syslog.h>
32
33#include "log.h"
34#include "utils.h"
35
36unsigned int log_level = LL_WARNING;
37
38int log_syslog = 0;
39
40void log_enable_syslog()
41{
42 if (!log_syslog) {
43 openlog("usbmuxd", LOG_PID, 0);
44 log_syslog = 1;
45 }
46}
47
48void log_disable_syslog()
49{
50 if (log_syslog) {
51 closelog();
52 }
53}
54
55static int level_to_syslog_level(int level)
56{
57 int result = level + LOG_CRIT;
58 if (result > LOG_DEBUG) {
59 result = LOG_DEBUG;
60 }
61 return result;
62}
63
64void usbmuxd_log(enum loglevel level, const char *fmt, ...)
65{
66 va_list ap;
67 char *fs;
68
69 if(level > log_level)
70 return;
71
72 fs = malloc(20 + strlen(fmt));
73
74 if(log_syslog) {
75 sprintf(fs, "[%d] %s\n", level, fmt);
76 } else {
77 struct timeval ts;
78 struct tm tp_;
79 struct tm *tp;
80
81 gettimeofday(&ts, NULL);
82#ifdef HAVE_LOCALTIME_R
83 tp = localtime_r(&ts.tv_sec, &tp_);
84#else
85 tp = localtime(&ts.tv_sec);
86#endif
87
88 strftime(fs, 10, "[%H:%M:%S", tp);
89 sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt);
90 }
91
92 va_start(ap, fmt);
93 if (log_syslog) {
94 vsyslog(level_to_syslog_level(level), fs, ap);
95 } else {
96 vfprintf(stderr, fs, ap);
97 }
98 va_end(ap);
99
100 free(fs);
101}