diff options
Diffstat (limited to 'daemon/log.c')
| -rw-r--r-- | daemon/log.c | 94 |
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 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, 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 | |||
| 35 | int log_level = LL_WARNING; | ||
| 36 | |||
| 37 | int log_syslog = 0; | ||
| 38 | |||
| 39 | void log_enable_syslog() | ||
| 40 | { | ||
| 41 | if (!log_syslog) { | ||
| 42 | openlog("usbmuxd", LOG_PID, 0); | ||
| 43 | log_syslog = 1; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | void log_disable_syslog() | ||
| 48 | { | ||
| 49 | if (log_syslog) { | ||
| 50 | closelog(); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | static 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 | |||
| 63 | void 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 | } | ||
