annotate src/debug.c @ 673:fbebf5cf4a55

Do not use printf() directly but use new wrapper function log_printf() instead.
author zas_
date Fri, 16 May 2008 12:16:49 +0000
parents 8268cbe682f1
children 6d6f042b8ca5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
507
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
1 /*
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
2 * Geeqie
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
3 * Copyright (C) 2008 The Geeqie Team
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
4 *
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
5 * Authors: Vladimir Nadvornik, Laurent Monin
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
6 *
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
7 * This software is released under the GNU General Public License (GNU GPL).
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
8 * Please read the included file COPYING for more information.
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
9 * This software comes with no warranty of any kind, use at your own risk!
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
10 */
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
11
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
12 #include "main.h"
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
13
673
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
14
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
15 /*
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
16 * Logging functions
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
17 */
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
18
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
19 gint log_domain_printf(const char *domain, const gchar *format, ...)
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
20 {
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
21 va_list ap;
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
22 gchar buf[4096];
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
23 gint ret;
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
24
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
25 va_start(ap, format);
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
26 ret = vsnprintf(buf, sizeof(buf), format, ap);
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
27 va_end(ap);
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
28
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
29 puts(buf);
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
30
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
31 return ret;
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
32 }
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
33
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
34
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
35 /*
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
36 * Debugging only functions
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
37 */
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
38
507
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
39 #ifdef DEBUG
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
40
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
41 static gint debug_level = DEBUG_LEVEL_MIN;
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
42
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
43
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
44 gint get_debug_level(void)
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
45 {
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
46 return debug_level;
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
47 }
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
48
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
49 void set_debug_level(gint new_level)
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
50 {
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
51 debug_level = CLAMP(new_level, DEBUG_LEVEL_MIN, DEBUG_LEVEL_MAX);
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
52 }
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
53
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
54 void debug_level_add(gint delta)
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
55 {
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
56 set_debug_level(debug_level + delta);
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
57 }
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
58
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
59 gint required_debug_level(gint level)
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
60 {
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
61 return (debug_level >= level);
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
62 }
135570a8bd96 Move debug macros from main.h to new debug.h.
zas_
parents:
diff changeset
63
509
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
64 static gint timeval_delta(struct timeval *result, struct timeval *x, struct timeval *y)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
65 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
66 if (x->tv_usec < y->tv_usec)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
67 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
68 gint nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
69 y->tv_usec -= 1000000 * nsec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
70 y->tv_sec += nsec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
71 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
72
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
73 if (x->tv_usec - y->tv_usec > 1000000)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
74 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
75 gint nsec = (x->tv_usec - y->tv_usec) / 1000000;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
76 y->tv_usec += 1000000 * nsec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
77 y->tv_sec -= nsec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
78 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
79
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
80 result->tv_sec = x->tv_sec - y->tv_sec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
81 result->tv_usec = x->tv_usec - y->tv_usec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
82
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
83 return x->tv_sec < y->tv_sec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
84 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
85
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
86 const gchar *get_exec_time(void)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
87 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
88 static gchar timestr[30];
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
89 static struct timeval start_tv = {0, 0};
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
90 static struct timeval previous = {0, 0};
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
91 static gint started = 0;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
92
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
93 struct timeval tv = {0, 0};
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
94 static struct timeval delta = {0, 0};
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
95
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
96 gettimeofday(&tv, NULL);
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
97
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
98 if (start_tv.tv_sec == 0) start_tv = tv;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
99
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
100 tv.tv_sec -= start_tv.tv_sec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
101 if (tv.tv_usec >= start_tv.tv_usec)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
102 tv.tv_usec -= start_tv.tv_usec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
103 else
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
104 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
105 tv.tv_usec += 1000000 - start_tv.tv_usec;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
106 tv.tv_sec -= 1;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
107 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
108
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
109 if (started) timeval_delta(&delta, &tv, &previous);
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
110
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
111 previous = tv;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
112 started = 1;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
113
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
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);
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
115
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
116 return timestr;
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
117 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
118
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
119 void init_exec_time(void)
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
120 {
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
121 get_exec_time();
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
122 }
b78a91d0779e Move get_exec_time() to debug.{c,h}.
zas_
parents: 507
diff changeset
123
673
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
124 #endif /* DEBUG */