Do not use printf() directly but use new wrapper function log_printf() instead.
[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
15 /*
16  * Logging functions
17  */
18
19 gint log_domain_printf(const char *domain, const gchar *format, ...)
20 {
21         va_list ap;
22         gchar buf[4096];
23         gint ret;
24
25         va_start(ap, format);
26         ret = vsnprintf(buf, sizeof(buf), format, ap);
27         va_end(ap);
28
29         puts(buf);
30
31         return ret;
32 }
33
34
35 /*
36  * Debugging only functions
37  */
38
39 #ifdef DEBUG
40
41 static gint debug_level = DEBUG_LEVEL_MIN;
42
43
44 gint get_debug_level(void)
45 {
46         return debug_level;
47 }
48
49 void set_debug_level(gint new_level)
50 {
51         debug_level = CLAMP(new_level, DEBUG_LEVEL_MIN, DEBUG_LEVEL_MAX);       
52 }
53
54 void debug_level_add(gint delta)
55 {
56         set_debug_level(debug_level + delta);
57 }
58
59 gint required_debug_level(gint level)
60 {
61         return (debug_level >= level);
62 }
63
64 static gint timeval_delta(struct timeval *result, struct timeval *x, struct timeval *y)
65 {
66         if (x->tv_usec < y->tv_usec)
67                 {
68                 gint nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
69                 y->tv_usec -= 1000000 * nsec;
70                 y->tv_sec += nsec;
71                 }
72
73         if (x->tv_usec - y->tv_usec > 1000000)
74                 {
75                 gint nsec = (x->tv_usec - y->tv_usec) / 1000000;
76                 y->tv_usec += 1000000 * nsec;
77                 y->tv_sec -= nsec;
78         }
79
80         result->tv_sec = x->tv_sec - y->tv_sec;
81         result->tv_usec = x->tv_usec - y->tv_usec;
82
83         return x->tv_sec < y->tv_sec;
84 }
85
86 const gchar *get_exec_time(void)
87 {
88         static gchar timestr[30];
89         static struct timeval start_tv = {0, 0};
90         static struct timeval previous = {0, 0};
91         static gint started = 0;
92
93         struct timeval tv = {0, 0};
94         static struct timeval delta = {0, 0};
95
96         gettimeofday(&tv, NULL);
97
98         if (start_tv.tv_sec == 0) start_tv = tv;
99
100         tv.tv_sec -= start_tv.tv_sec;
101         if (tv.tv_usec >= start_tv.tv_usec)
102                 tv.tv_usec -= start_tv.tv_usec;
103         else
104                 {
105                 tv.tv_usec += 1000000 - start_tv.tv_usec;
106                 tv.tv_sec -= 1;
107                 }
108
109         if (started) timeval_delta(&delta, &tv, &previous);
110
111         previous = tv;
112         started = 1;
113
114         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);
115
116         return timestr;
117 }
118
119 void init_exec_time(void)
120 {
121         get_exec_time();
122 }
123
124 #endif /* DEBUG */