]> git.0d.be Git - empathy.git/blob - libempathy/empathy-debug.c
Add support for apt:// URLS. Fixes bug #529049 (Gaƫtan Podevijn).
[empathy.git] / libempathy / empathy-debug.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Richard Hult <richard@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #include <glib.h>
34 #include <glib/gprintf.h>
35 #include <glib/gstdio.h>
36
37 #include <telepathy-glib/debug.h>
38
39 /* Set EMPATHY_DEBUG to a colon/comma/space separated list of domains, or "all"
40  * to get all debug output.
41  */
42
43 #include "empathy-debug.h"
44
45 static gchar    **debug_strv;
46 static gboolean   all_domains = FALSE;
47
48 static void
49 debug_init (void)
50 {
51         static gboolean inited = FALSE;
52
53         if (!inited) {
54                 const gchar *env;
55                 gint         i;
56
57                 env = g_getenv ("TELEPATHY_GLIB_DEBUG");
58                 tp_debug_set_flags (env);
59
60                 env = g_getenv ("EMPATHY_DEBUG");
61
62                 if (env) {
63                         debug_strv = g_strsplit_set (env, ":, ", 0);
64                 } else {
65                         debug_strv = NULL;
66                 }
67
68                 for (i = 0; debug_strv && debug_strv[i]; i++) {
69                         if (strcmp ("all", debug_strv[i]) == 0) {
70                                 all_domains = TRUE;
71                         }
72                 }
73
74                 inited = TRUE;
75         }
76 }
77
78 void
79 empathy_debug_impl (const gchar *domain, const gchar *msg, ...)
80 {
81         gint i;
82
83         g_return_if_fail (domain != NULL);
84         g_return_if_fail (msg != NULL);
85
86         debug_init ();
87
88         for (i = 0; debug_strv && debug_strv[i]; i++) {
89                 if (all_domains || strcmp (domain, debug_strv[i]) == 0) {
90                         va_list args;
91
92                         g_print ("%s: ", domain);
93
94                         va_start (args, msg);
95                         g_vprintf (msg, args);
96                         va_end (args);
97
98                         g_print ("\n");
99                         break;
100                 }
101         }
102 }
103
104 void
105 empathy_debug_set_log_file_from_env (void)
106 {
107         const gchar *output_file;
108         gint         out;
109
110         output_file = g_getenv ("EMPATHY_LOGFILE");
111         if (output_file == NULL) {
112                 return;
113         }
114
115         out = g_open (output_file, O_WRONLY | O_CREAT, 0644);
116         if (out == -1) {
117                 g_warning ("Can't open logfile '%s': %s", output_file,
118                            g_strerror (errno));
119                 return;
120         }
121
122         if (dup2 (out, STDOUT_FILENO) == -1) {
123                 g_warning ("Error when duplicating stdout file descriptor: %s",
124                            g_strerror (errno));
125                 return;
126         }
127
128         if (dup2 (out, STDERR_FILENO) == -1) {
129                 g_warning ("Error when duplicating stderr file descriptor: %s",
130                            g_strerror (errno));
131                 return;
132         }
133 }
134