summaryrefslogtreecommitdiff
path: root/src/mcd-debug.c
blob: b7266335d5664d03c1ea756f6866bbcdec0cb912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 8 -*- */
/*
 * This file is part of mission-control
 *
 * Copyright (C) 2007 Nokia Corporation. 
 *
 * Contact: Naba Kumar  <naba.kumar@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:mcd-debug
 * @title: Debugging
 * @short_description: Debugging utilities
 * @see_also: 
 * @stability: Unstable
 * @include: mcd-debug.h
 * 
 * FIXME
 */

#include <stdlib.h>
#include "mcd-debug.h"
#include "mcd-operation.h"

static gint mc_debug = 0;

void
mcd_debug_ref (gpointer obj, const gchar *filename, gint linenum)
{
    /* Function reference untouchable by macro processing */
    gpointer (*untouchable_ref) (gpointer object);
    
    untouchable_ref = g_object_ref;
    if (mc_debug >= 2)
	g_debug ("[%s:%d]: Referencing (%d) object %p of type %s",
		 filename, linenum, G_OBJECT (obj)->ref_count,
		 obj, G_OBJECT_TYPE_NAME(obj));
    untouchable_ref (obj);
}

void
mcd_debug_unref (gpointer obj, const gchar *filename, gint linenum)
{
    void (*untouchable_unref) (gpointer object);
    
    untouchable_unref = g_object_unref;
    if (mc_debug >= 2)
	g_debug ("[%s:%d]: Unreferencing (%d) object %p of type %s",
		 filename, linenum, G_OBJECT (obj)->ref_count, obj,
		 G_OBJECT_TYPE_NAME(obj));
    untouchable_unref (obj);
}

static void
mcd_debug_print_tree_real (gpointer object, gint level)
{
    GString *indent_str;
    gchar *indent = "    ";
    gint i;
    
    indent_str = g_string_new ("");
    
    for (i = 0; i < level; i++)
    {
	g_string_append (indent_str, indent);
    }
    
    g_debug ("%s%s (%p): %d", indent_str->str,
	     G_OBJECT_TYPE_NAME(object), object, G_OBJECT (object)->ref_count);
    
    if (MCD_IS_OPERATION (object))
    {
	const GList *missions = mcd_operation_get_missions (MCD_OPERATION (object));
	const GList *node = missions;
	while (node)
	{
	    mcd_debug_print_tree_real (node->data, level + 1);
	    node = g_list_next (node);
	}
    }
    g_string_free (indent_str, TRUE);
}

void
mcd_debug_print_tree (gpointer object)
{
    g_return_if_fail (MCD_IS_MISSION (object));

    if (mc_debug >= 2)
    {
	g_debug ("Object Hierarchy of object %p", object);
	g_debug ("[");
	mcd_debug_print_tree_real (object, 1);
	g_debug ("]");
    }
}

void mcd_debug_init ()
{
    gchar *mc_debug_str;

    mc_debug_str = getenv ("MC_DEBUG");
    if (mc_debug_str)
	mc_debug = atoi (mc_debug_str);
}

inline gint mcd_debug_get_level ()
{
    return mc_debug;
}