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