diff --git a/src/libtracker-common/tracker-log.c b/src/libtracker-common/tracker-log.c index 057f27b..5f5c4ea 100644 --- a/src/libtracker-common/tracker-log.c +++ b/src/libtracker-common/tracker-log.c @@ -40,17 +40,21 @@ static FILE *fd; static gint verbosity; static guint log_handler_id; +static struct timeval last; +static struct tm *local_time; + static inline void log_output (const gchar *domain, GLogLevelFlags log_level, const gchar *message) { - time_t now; + struct timeval new; + struct timeval res; gchar time_str[64]; gchar *output; - struct tm *local_time; const gchar *log_level_str; static gsize size = 0; + static int max_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; g_return_if_fail (initialized == TRUE); g_return_if_fail (message != NULL && message[0] != '\0'); @@ -69,8 +73,74 @@ log_output (const gchar *domain, size = 0; } - now = time ((time_t *) NULL); - local_time = localtime (&now); + gettimeofday(&new, NULL); + + if( __builtin_expect(timercmp(&new, &last, <), 0)) { + timersub(&last, &new, &res); + local_time->tm_sec -= res.tv_sec; + while(local_time->tm_sec < 0) { + local_time->tm_min--; + local_time->tm_sec += 60; + } + + while(local_time->tm_min < 0) { + local_time->tm_hour--; + local_time->tm_min += 60; + } + + while(local_time->tm_hour < 0) { + local_time->tm_mday--; + local_time->tm_hour += 24; + } + + while(local_time->tm_mday < 1) { + local_time->tm_mon--; + if(local_time->tm_mon < 0) { + local_time->tm_year--; + local_time->tm_mon += 12; + } + local_time->tm_mday += max_days[local_time->tm_mon]; + } + + while(local_time->tm_mon < 0) { + local_time->tm_mon += 12; + local_time->tm_year--; + } + } + else { + timersub(&new, &last, &res); + local_time->tm_sec += res.tv_sec; + while(local_time->tm_sec > 59) { + local_time->tm_min++; + local_time->tm_sec -= 60; + } + + while(local_time->tm_min > 59) { + local_time->tm_min -= 60; + local_time->tm_hour++; + } + + while(local_time->tm_hour > 23) { + local_time->tm_hour -= 24; + local_time->tm_mday++; + } + + while(local_time->tm_mday > max_days[local_time->tm_mon]) { + local_time->tm_mday -= max_days[local_time->tm_mon]; + local_time->tm_mon++; + if(local_time->tm_mon > 11) { + local_time->tm_mon -= 12; + local_time->tm_year++; + } + } + + while(local_time->tm_mon > 11) { + local_time->tm_mon -= 12; + local_time->tm_year++; + } + } + last.tv_sec = new.tv_sec; + last.tv_usec = new.tv_usec; strftime (time_str, 64, "%d %b %Y, %H:%M:%S:", local_time); switch (log_level) { @@ -145,6 +215,7 @@ tracker_log_init (gint this_verbosity, gchar *basename; const gchar *env_verbosity; GLogLevelFlags hide_levels = 0; + time_t new; if (initialized) { return TRUE; @@ -223,6 +294,10 @@ tracker_log_init (gint this_verbosity, NULL); } + new = time((time_t *)NULL); + local_time = localtime(&new); + gettimeofday(&last, NULL); + /* Set log handler function for the rest */ g_log_set_default_handler (tracker_log_handler, NULL);