]> git.0d.be Git - empathy.git/blob - libempathy/empathy-debug.c
Updated.
[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 /* Set EMPATHY_DEBUG to a colon/comma/space separated list of domains, or "all"
38  * to get all debug output.
39  */
40
41 #include "empathy-debug.h"
42
43 static gchar    **debug_strv;
44 static gboolean   all_domains = FALSE;
45
46 static void
47 debug_init (void)
48 {
49         static gboolean inited = FALSE;
50
51         if (!inited) {
52                 const gchar *env;
53                 gint         i;
54
55                 env = g_getenv ("EMPATHY_DEBUG");
56
57                 if (env) {
58                         debug_strv = g_strsplit_set (env, ":, ", 0);
59                 } else {
60                         debug_strv = NULL;
61                 }
62
63                 for (i = 0; debug_strv && debug_strv[i]; i++) {
64                         if (strcmp ("all", debug_strv[i]) == 0) {
65                                 all_domains = TRUE;
66                         }
67                 }
68
69                 inited = TRUE;
70         }
71 }
72
73 void
74 empathy_debug_impl (const gchar *domain, const gchar *msg, ...)
75 {
76         gint i;
77
78         g_return_if_fail (domain != NULL);
79         g_return_if_fail (msg != NULL);
80
81         debug_init ();
82
83         for (i = 0; debug_strv && debug_strv[i]; i++) {
84                 if (all_domains || strcmp (domain, debug_strv[i]) == 0) {
85                         va_list args;
86
87                         g_print ("%s: ", domain);
88
89                         va_start (args, msg);
90                         g_vprintf (msg, args);
91                         va_end (args);
92
93                         g_print ("\n");
94                         break;
95                 }
96         }
97 }
98
99 void
100 empathy_debug_set_log_file_from_env (void)
101 {
102         const gchar *output_file;
103         gint         out;
104
105         output_file = g_getenv ("EMPATHY_LOGFILE");
106         if (output_file == NULL) {
107                 return;
108         }
109
110         out = g_open (output_file, O_WRONLY | O_CREAT, 0644);
111         if (out == -1) {
112                 g_warning ("Can't open logfile '%s': %s", output_file,
113                            g_strerror (errno));
114                 return;
115         }
116
117         if (dup2 (out, STDOUT_FILENO) == -1) {
118                 g_warning ("Error when duplicating stdout file descriptor: %s",
119                            g_strerror (errno));
120                 return;
121         }
122
123         if (dup2 (out, STDERR_FILENO) == -1) {
124                 g_warning ("Error when duplicating stderr file descriptor: %s",
125                            g_strerror (errno));
126                 return;
127         }
128 }
129