c5848de9dd75dec110ab6ea8d6b6a08d21a2a519
[geeqie.git] / src / debug.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 - 2010 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 #include "debug.h"
14
15 #include "logwindow.h"
16 #include "ui_fileops.h"
17
18 #include <glib/gprintf.h>
19
20 GMutex *debug_mutex;
21 /*
22  * Logging functions
23  */
24
25 gint log_domain_printf(const gchar *domain, const gchar *format, ...)
26 {
27         va_list ap;
28         gchar buf[4096];
29         gint ret;
30
31         va_start(ap, format);
32         ret = g_vsnprintf(buf, sizeof(buf), format, ap);
33         va_end(ap);
34
35         print_term(buf);
36         if (strcmp(domain, DOMAIN_INFO) == 0)
37                 log_window_append(buf, LOG_NORMAL);
38         else
39                 log_window_append(buf, LOG_MSG);
40
41         return ret;
42 }
43
44
45 /*
46  * Debugging only functions
47  */
48
49 #ifdef DEBUG
50
51 static gint debug_level = DEBUG_LEVEL_MIN;
52
53
54 gint get_debug_level(void)
55 {
56         return debug_level;
57 }
58
59 void set_debug_level(gint new_level)
60 {
61         debug_level = CLAMP(new_level, DEBUG_LEVEL_MIN, DEBUG_LEVEL_MAX);
62 }
63
64 void debug_level_add(gint delta)
65 {
66         set_debug_level(debug_level + delta);
67 }
68
69 gint required_debug_level(gint level)
70 {
71         return (debug_level >= level);
72 }
73
74 static gint timeval_delta(struct timeval *result, struct timeval *x, struct timeval *y)
75 {
76         if (x->tv_usec < y->tv_usec)
77                 {
78                 gint nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
79                 y->tv_usec -= 1000000 * nsec;
80                 y->tv_sec += nsec;
81                 }
82
83         if (x->tv_usec - y->tv_usec > 1000000)
84                 {
85                 gint nsec = (x->tv_usec - y->tv_usec) / 1000000;
86                 y->tv_usec += 1000000 * nsec;
87                 y->tv_sec -= nsec;
88         }
89
90         result->tv_sec = x->tv_sec - y->tv_sec;
91         result->tv_usec = x->tv_usec - y->tv_usec;
92
93         return x->tv_sec < y->tv_sec;
94 }
95
96 const gchar *get_exec_time(void)
97 {
98         static gchar timestr[30];
99         static struct timeval start_tv = {0, 0};
100         static struct timeval previous = {0, 0};
101         static gint started = 0;
102
103         struct timeval tv = {0, 0};
104         static struct timeval delta = {0, 0};
105
106         gettimeofday(&tv, NULL);
107
108         if (start_tv.tv_sec == 0) start_tv = tv;
109
110         tv.tv_sec -= start_tv.tv_sec;
111         if (tv.tv_usec >= start_tv.tv_usec)
112                 tv.tv_usec -= start_tv.tv_usec;
113         else
114                 {
115                 tv.tv_usec += 1000000 - start_tv.tv_usec;
116                 tv.tv_sec -= 1;
117                 }
118
119         if (started) timeval_delta(&delta, &tv, &previous);
120
121         previous = tv;
122         started = 1;
123
124         g_snprintf(timestr, sizeof(timestr), "%5d.%06d (+%05d.%06d)", (gint)tv.tv_sec, (gint)tv.tv_usec, (gint)delta.tv_sec, (gint)delta.tv_usec);
125
126         return timestr;
127 }
128
129 void init_exec_time(void)
130 {
131         get_exec_time();
132 }
133
134 #endif /* DEBUG */
135 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */