summaryrefslogtreecommitdiff
path: root/src/gs-debug.c
blob: 52fbc1201c481ef5bea1b6859a8bb172bf2b7f8e (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2005 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gstdio.h>

#include "gs-debug.h"

static gboolean debugging = FALSE;
static FILE    *debug_out = NULL;

/* Based on rhythmbox/lib/rb-debug.c */
/* Our own funky debugging function, should only be used when something
 * is not going wrong, if something *is* wrong use g_warning.
 */
void
gs_debug_real (const char *func,
               const char *file,
               const int   line,
               const char *format, ...)
{
	va_list args;
	char    buffer [1025];
	char   *str_time;
	time_t  the_time;

	if (debugging == FALSE)
		return;

	va_start (args, format);

	g_vsnprintf (buffer, 1024, format, args);

	va_end (args);

	time (&the_time);
	str_time = g_new0 (char, 255);
	strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));

	fprintf ((debug_out ? debug_out : stderr),
	         "[%s] %s:%d (%s):\t %s\n",
	         func, file, line, str_time, buffer);

	if (debug_out)
		fflush (debug_out);

	g_free (str_time);
}

gboolean
gs_debug_enabled (void)
{
	return debugging;
}

void
gs_debug_init (gboolean debug,
               gboolean to_file)
{
	/* return if already initialized */
	if (debugging == TRUE)
	{
		return;
	}

	debugging = debug;

	if (debug && to_file)
	{
		const char path [50] = "mate_screensaver_debug_XXXXXX";
		int        fd;

		fd = g_file_open_tmp (path, NULL, NULL);

		if (fd >= 0)
		{
			debug_out = fdopen (fd, "a");
		}
	}

	gs_debug ("Debugging %s", (debug) ? "enabled" : "disabled");
}

void
gs_debug_shutdown (void)
{
	if (! debugging)
		return;

	gs_debug ("Shutting down debugging");

	debugging = FALSE;

	if (debug_out != NULL)
	{
		fclose (debug_out);
		debug_out = NULL;
	}
}

void
_gs_profile_log (const char *func,
                 const char *note,
                 const char *format,
                 ...)
{
	va_list args;
	char   *str;
	char   *formatted;

	if (format == NULL)
	{
		formatted = g_strdup ("");
	}
	else
	{
		va_start (args, format);
		formatted = g_strdup_vprintf (format, args);
		va_end (args);
	}

	if (func != NULL)
	{
		str = g_strdup_printf ("MARK: %s %s: %s %s", g_get_prgname(), func, note ? note : "", formatted);
	}
	else
	{
		str = g_strdup_printf ("MARK: %s: %s %s", g_get_prgname(), note ? note : "", formatted);
	}

	g_free (formatted);

	g_access (str, F_OK);
	g_free (str);
}