1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * arch-tag: Implementation of simple Rhythmbox debugging interface
5 * Copyright (C) 2002 Jorn Baayen
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * NOTES: log domain hack stolen from nautilus
36 static void log_handler (const char *domain
,
41 static const char *debug_everything
= "everything";
42 static const char *debug_match
= NULL
;
44 /* Our own funky debugging function, should only be used when something
45 * is not going wrong, if something *is* wrong use g_warning.
48 rb_debug_real (const char *func
,
52 const char *format
, ...)
59 if (debug_match
== NULL
||
60 (debug_match
!= debug_everything
&&
61 (strstr (file
, debug_match
) == NULL
) &&
62 (strstr (func
, debug_match
) == NULL
)))
65 va_start (args
, format
);
67 g_vsnprintf (buffer
, 1024, format
, args
);
72 str_time
= g_new0 (char, 255);
73 strftime (str_time
, 254, "%H:%M:%S", localtime (&the_time
));
75 g_printerr (newline
? "(%s) [%p] [%s] %s:%d: %s\n" : "(%s) [%p] [%s] %s:%d: %s",
76 str_time
, g_thread_self (), func
, file
, line
, buffer
);
82 rb_debug_init (gboolean debug
)
84 rb_debug_init_match (debug
? debug_everything
: NULL
);
88 rb_debug_init_match (const char *match
)
92 /* This is a workaround for the fact that there is not way to
93 * make this useful debugging feature happen for ALL domains.
95 * What we did here is list all the ones we could think of that
96 * were interesting to us. It's OK to add more to the list.
98 static const char * const standard_log_domains
[] = {
140 if (debug_match
!= NULL
)
141 for (i
= 0; i
< G_N_ELEMENTS (standard_log_domains
); i
++)
142 g_log_set_handler (standard_log_domains
[i
], G_LOG_LEVEL_MASK
, log_handler
, NULL
);
144 rb_debug ("Debugging enabled");
147 /* Raise a SIGINT signal to get the attention of the debugger.
148 * When not running under the debugger, we don't want to stop,
149 * so we ignore the signal for just the moment that we raise it.
152 rb_debug_stop_in_debugger (void)
154 void (* saved_handler
) (int);
156 saved_handler
= signal (SIGINT
, SIG_IGN
);
158 signal (SIGINT
, saved_handler
);
161 /* Stop in the debugger after running the default log handler.
162 * This makes certain kinds of messages stop in the debugger
163 * without making them fatal (you can continue).
166 log_handler (const char *domain
,
167 GLogLevelFlags level
,
171 g_log_default_handler (domain
, level
, message
, data
);
172 if ((level
& (G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)) != 0)
174 rb_debug_stop_in_debugger ();
185 rb_profiler_new (const char *name
)
187 RBProfiler
*profiler
;
189 if (debug_match
== NULL
)
192 profiler
= g_new0 (RBProfiler
, 1);
193 profiler
->timer
= g_timer_new ();
194 profiler
->name
= g_strdup (name
);
196 g_timer_start (profiler
->timer
);
202 rb_profiler_dump (RBProfiler
*profiler
)
207 if (debug_match
== NULL
)
209 if (profiler
== NULL
)
212 seconds
= g_timer_elapsed (profiler
->timer
, &elapsed
);
214 rb_debug ("PROFILER %s %ld ms (%f s) elapsed", profiler
->name
,
215 elapsed
/ (G_USEC_PER_SEC
/ 1000), seconds
);
219 rb_profiler_reset (RBProfiler
*profiler
)
221 if (debug_match
== NULL
)
223 if (profiler
== NULL
)
226 g_timer_start (profiler
->timer
);
230 rb_profiler_free (RBProfiler
*profiler
)
232 if (debug_match
== NULL
)
234 if (profiler
== NULL
)
237 g_timer_destroy (profiler
->timer
);
238 g_free (profiler
->name
);
244 static int profile_indent
;
247 profile_add_indent (int indent
)
249 profile_indent
+= indent
;
250 if (profile_indent
< 0) {
251 g_error ("You screwed up your indentation");
256 _rb_profile_log (const char *func
,
266 profile_add_indent (indent
);
269 if (profile_indent
== 0) {
270 str
= g_strdup_printf ("MARK: [%s %s %d] %s %s", file
, func
, line
, msg1
? msg1
: "", msg2
? msg2
: "");
272 str
= g_strdup_printf ("MARK: %*c [%s %s %d] %s %s", profile_indent
- 1, ' ', file
, func
, line
, msg1
? msg1
: "", msg2
? msg2
: "");
280 profile_add_indent (indent
);