comparison src/debug.c @ 509:b78a91d0779e

Move get_exec_time() to debug.{c,h}.
author zas_
date Thu, 24 Apr 2008 09:43:23 +0000
parents 135570a8bd96
children 8268cbe682f1
comparison
equal deleted inserted replaced
508:011a6be611c8 509:b78a91d0779e
35 gint required_debug_level(gint level) 35 gint required_debug_level(gint level)
36 { 36 {
37 return (debug_level >= level); 37 return (debug_level >= level);
38 } 38 }
39 39
40 static gint timeval_delta(struct timeval *result, struct timeval *x, struct timeval *y)
41 {
42 if (x->tv_usec < y->tv_usec)
43 {
44 gint nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
45 y->tv_usec -= 1000000 * nsec;
46 y->tv_sec += nsec;
47 }
48
49 if (x->tv_usec - y->tv_usec > 1000000)
50 {
51 gint nsec = (x->tv_usec - y->tv_usec) / 1000000;
52 y->tv_usec += 1000000 * nsec;
53 y->tv_sec -= nsec;
54 }
55
56 result->tv_sec = x->tv_sec - y->tv_sec;
57 result->tv_usec = x->tv_usec - y->tv_usec;
58
59 return x->tv_sec < y->tv_sec;
60 }
61
62 const gchar *get_exec_time(void)
63 {
64 static gchar timestr[30];
65 static struct timeval start_tv = {0, 0};
66 static struct timeval previous = {0, 0};
67 static gint started = 0;
68
69 struct timeval tv = {0, 0};
70 static struct timeval delta = {0, 0};
71
72 gettimeofday(&tv, NULL);
73
74 if (start_tv.tv_sec == 0) start_tv = tv;
75
76 tv.tv_sec -= start_tv.tv_sec;
77 if (tv.tv_usec >= start_tv.tv_usec)
78 tv.tv_usec -= start_tv.tv_usec;
79 else
80 {
81 tv.tv_usec += 1000000 - start_tv.tv_usec;
82 tv.tv_sec -= 1;
83 }
84
85 if (started) timeval_delta(&delta, &tv, &previous);
86
87 previous = tv;
88 started = 1;
89
90 g_snprintf(timestr, sizeof(timestr), "%5d.%06d (+%05d.%06d)", (int)tv.tv_sec, (int)tv.tv_usec, (int)delta.tv_sec, (int)delta.tv_usec);
91
92 return timestr;
93 }
94
95 void init_exec_time(void)
96 {
97 get_exec_time();
98 }
99
40 #endif 100 #endif