d20495dea943f57d3c6b5faffdad4b21dd6a8d5e
[geeqie.git] / src / debug.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 The Geeqie Team
4  *
5  * Authors: Vladimir Nadvornik, Laurent Monin
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13
14 #ifdef DEBUG
15
16 static gint debug_level = DEBUG_LEVEL_MIN;
17
18
19 gint get_debug_level(void)
20 {
21         return debug_level;
22 }
23
24 void set_debug_level(gint new_level)
25 {
26         debug_level = CLAMP(new_level, DEBUG_LEVEL_MIN, DEBUG_LEVEL_MAX);       
27 }
28
29 void debug_level_add(gint delta)
30 {
31         set_debug_level(debug_level + delta);
32 }
33
34 gint required_debug_level(gint level)
35 {
36         return (debug_level >= level);
37 }
38
39 static gint timeval_delta(struct timeval *result, struct timeval *x, struct timeval *y)
40 {
41         if (x->tv_usec < y->tv_usec)
42                 {
43                 gint nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
44                 y->tv_usec -= 1000000 * nsec;
45                 y->tv_sec += nsec;
46                 }
47
48         if (x->tv_usec - y->tv_usec > 1000000)
49                 {
50                 gint nsec = (x->tv_usec - y->tv_usec) / 1000000;
51                 y->tv_usec += 1000000 * nsec;
52                 y->tv_sec -= nsec;
53         }
54
55         result->tv_sec = x->tv_sec - y->tv_sec;
56         result->tv_usec = x->tv_usec - y->tv_usec;
57
58         return x->tv_sec < y->tv_sec;
59 }
60
61 const gchar *get_exec_time(void)
62 {
63         static gchar timestr[30];
64         static struct timeval start_tv = {0, 0};
65         static struct timeval previous = {0, 0};
66         static gint started = 0;
67
68         struct timeval tv = {0, 0};
69         static struct timeval delta = {0, 0};
70
71         gettimeofday(&tv, NULL);
72
73         if (start_tv.tv_sec == 0) start_tv = tv;
74
75         tv.tv_sec -= start_tv.tv_sec;
76         if (tv.tv_usec >= start_tv.tv_usec)
77                 tv.tv_usec -= start_tv.tv_usec;
78         else
79                 {
80                 tv.tv_usec += 1000000 - start_tv.tv_usec;
81                 tv.tv_sec -= 1;
82                 }
83
84         if (started) timeval_delta(&delta, &tv, &previous);
85
86         previous = tv;
87         started = 1;
88
89         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);
90
91         return timestr;
92 }
93
94 void init_exec_time(void)
95 {
96         get_exec_time();
97 }
98
99 #endif