diff options
| author | 2018-04-09 16:57:38 +0200 | |
|---|---|---|
| committer | 2018-04-09 16:57:38 +0200 | |
| commit | e567b881bdce3e3e7b46d2a9d0d344b876257606 (patch) | |
| tree | b2c19da5b32374d72f5ddeaab52d4f8ad6baff5e | |
| parent | b888970f68fb16961a7cc3a526065fab7a5d96ca (diff) | |
| download | usbmuxd-e567b881bdce3e3e7b46d2a9d0d344b876257606.tar.gz usbmuxd-e567b881bdce3e3e7b46d2a9d0d344b876257606.tar.bz2 | |
log: Fix timestamps being printed incorrectly when running in foreground
Due to usage of wrong function (get_tick_count) the timestamps have
been printed incorrectly based on clock_gettime. This commit fixes it by
using gettimeofday correctly and also makes sure that this is thread-safe
by using localtime_r if available.
Furthermore, this commit will also have the effect that when logging
through syslog we don't determine the current time anymore because the
timestamp is not even used.
| -rw-r--r-- | src/log.c | 16 |
1 files changed, 11 insertions, 5 deletions
| @@ -65,20 +65,26 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...) | |||
| 65 | { | 65 | { |
| 66 | va_list ap; | 66 | va_list ap; |
| 67 | char *fs; | 67 | char *fs; |
| 68 | struct timeval ts; | ||
| 69 | struct tm *tp; | ||
| 70 | 68 | ||
| 71 | if(level > log_level) | 69 | if(level > log_level) |
| 72 | return; | 70 | return; |
| 73 | 71 | ||
| 74 | get_tick_count(&ts); | ||
| 75 | tp = localtime(&ts.tv_sec); | ||
| 76 | |||
| 77 | fs = malloc(20 + strlen(fmt)); | 72 | fs = malloc(20 + strlen(fmt)); |
| 78 | 73 | ||
| 79 | if(log_syslog) { | 74 | if(log_syslog) { |
| 80 | sprintf(fs, "[%d] %s\n", level, fmt); | 75 | sprintf(fs, "[%d] %s\n", level, fmt); |
| 81 | } else { | 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 | |||
| 82 | strftime(fs, 10, "[%H:%M:%S", tp); | 88 | strftime(fs, 10, "[%H:%M:%S", tp); |
| 83 | sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt); | 89 | sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt); |
| 84 | } | 90 | } |
