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
,
51 const char *format
, ...)
58 if (debug_match
== NULL
||
59 (debug_match
!= debug_everything
&&
60 (strstr (file
, debug_match
) == NULL
) &&
61 (strstr (func
, debug_match
) == NULL
)))
64 va_start (args
, format
);
66 g_vsnprintf (buffer
, 1024, format
, args
);
71 str_time
= g_new0 (char, 255);
72 strftime (str_time
, 254, "%H:%M:%S", localtime (&the_time
));
74 g_printerr ("(%s) [%p] [%s] %s:%d: %s\n", str_time
, g_thread_self (),
75 func
, file
, line
, buffer
);
81 rb_debug_init (gboolean debug
)
83 rb_debug_init_match (debug
? debug_everything
: NULL
);
87 rb_debug_init_match (const char *match
)
91 /* This is a workaround for the fact that there is not way to
92 * make this useful debugging feature happen for ALL domains.
94 * What we did here is list all the ones we could think of that
95 * were interesting to us. It's OK to add more to the list.
97 static const char * const standard_log_domains
[] = {
139 if (debug_match
!= NULL
)
140 for (i
= 0; i
< G_N_ELEMENTS (standard_log_domains
); i
++)
141 g_log_set_handler (standard_log_domains
[i
], G_LOG_LEVEL_MASK
, log_handler
, NULL
);
143 rb_debug ("Debugging enabled");
146 /* Raise a SIGINT signal to get the attention of the debugger.
147 * When not running under the debugger, we don't want to stop,
148 * so we ignore the signal for just the moment that we raise it.
151 rb_debug_stop_in_debugger (void)
153 void (* saved_handler
) (int);
155 saved_handler
= signal (SIGINT
, SIG_IGN
);
157 signal (SIGINT
, saved_handler
);
160 /* Stop in the debugger after running the default log handler.
161 * This makes certain kinds of messages stop in the debugger
162 * without making them fatal (you can continue).
165 log_handler (const char *domain
,
166 GLogLevelFlags level
,
170 g_log_default_handler (domain
, level
, message
, data
);
171 if ((level
& (G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_WARNING
)) != 0)
173 rb_debug_stop_in_debugger ();
184 rb_profiler_new (const char *name
)
186 RBProfiler
*profiler
;
188 if (debug_match
== FALSE
)
191 profiler
= g_new0 (RBProfiler
, 1);
192 profiler
->timer
= g_timer_new ();
193 profiler
->name
= g_strdup (name
);
195 g_timer_start (profiler
->timer
);
201 rb_profiler_dump (RBProfiler
*profiler
)
206 if (debug_match
== NULL
)
208 if (profiler
== NULL
)
211 seconds
= g_timer_elapsed (profiler
->timer
, &elapsed
);
213 rb_debug ("PROFILER %s %ld ms (%f s) elapsed", profiler
->name
,
214 elapsed
/ (G_USEC_PER_SEC
/ 1000), seconds
);
218 rb_profiler_reset (RBProfiler
*profiler
)
220 if (debug_match
== NULL
)
222 if (profiler
== NULL
)
225 g_timer_start (profiler
->timer
);
229 rb_profiler_free (RBProfiler
*profiler
)
231 if (debug_match
== NULL
)
233 if (profiler
== NULL
)
236 g_timer_destroy (profiler
->timer
);
237 g_free (profiler
->name
);
243 static int profile_indent
;
246 profile_add_indent (int indent
)
248 profile_indent
+= indent
;
249 if (profile_indent
< 0) {
250 g_error ("You screwed up your indentation");
255 _rb_profile_log (const char *func
,
265 profile_add_indent (indent
);
268 if (profile_indent
== 0) {
269 str
= g_strdup_printf ("MARK: [%s %s %d] %s %s", file
, func
, line
, msg1
? msg1
: "", msg2
? msg2
: "");
271 str
= g_strdup_printf ("MARK: %*c [%s %s %d] %s %s", profile_indent
- 1, ' ', file
, func
, line
, msg1
? msg1
: "", msg2
? msg2
: "");
279 profile_add_indent (indent
);