summaryrefslogtreecommitdiffstats
path: root/daemon/log.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-08-25 03:26:22 +0200
committerGravatar Nikias Bassen2009-08-25 03:26:22 +0200
commit4711a2b493f76561e9803bf7e8be77186f3e7798 (patch)
tree339684fe1b996e01047c3eb220a8e22e4491c5e2 /daemon/log.c
parentde30ca5d5c98a8cbee3d8748601519e2263b3e1d (diff)
downloadusbmuxd-4711a2b493f76561e9803bf7e8be77186f3e7798.tar.gz
usbmuxd-4711a2b493f76561e9803bf7e8be77186f3e7798.tar.bz2
Renamed directory 'usbmuxd' to more suitable 'daemon'.
Diffstat (limited to 'daemon/log.c')
-rw-r--r--daemon/log.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/daemon/log.c b/daemon/log.c
new file mode 100644
index 0000000..2ccb3cc
--- /dev/null
+++ b/daemon/log.c
@@ -0,0 +1,94 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
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
35int log_level = LL_WARNING;
36
37int log_syslog = 0;
38
39void log_enable_syslog()
40{
41 if (!log_syslog) {
42 openlog("usbmuxd", LOG_PID, 0);
43 log_syslog = 1;
44 }
45}
46
47void log_disable_syslog()
48{
49 if (log_syslog) {
50 closelog();
51 }
52}
53
54static int level_to_syslog_level(int level)
55{
56 int result = level + LOG_CRIT;
57 if (result > LOG_DEBUG) {
58 result = LOG_DEBUG;
59 }
60 return result;
61}
62
63void usbmuxd_log(enum loglevel level, const char *fmt, ...)
64{
65 va_list ap;
66 char *fs;
67 struct timeval ts;
68 struct tm *tp;
69
70 gettimeofday(&ts, NULL);
71 tp = localtime(&ts.tv_sec);
72
73 if(level > log_level)
74 return;
75
76 fs = malloc(20 + strlen(fmt));
77
78 if(log_syslog) {
79 sprintf(fs, "[%d] %s\n", level, fmt);
80 } else {
81 strftime(fs, 10, "[%H:%M:%S", tp);
82 sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt);
83 }
84
85 va_start(ap, fmt);
86 if (log_syslog) {
87 vsyslog(level_to_syslog_level(level), fs, ap);
88 } else {
89 vfprintf(stderr, fs, ap);
90 }
91 va_end(ap);
92
93 free(fs);
94}