2 * @file telepathy-debug.c
6 * Copyright (C) 2012 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ******************************************************************************
24 * How to collect debugging information
26 * Run the connection manager from the command line like this:
28 * $ G_MESSAGES_DEBUG="all" SIPE_PERSIST=1 \
29 * SIPE_DEBUG=[space separated keyword list \
30 * [SIPE_TIMING=1] [SIPE_LOGFILE="..."] \
33 * G_MESSAGES_DEBUG=all: make debug & informational messages visible
35 * SIPE_PERSISTS=1 : keep the CM running permanently,
36 * [otherwise the one installed in the system will
37 * be started automatically by D-Bus when needed]
40 * all - enable all sipe & telepathy-glib messages
41 * sipe - enable only sipe messages
42 * "sipe ..." - enable sipe and some telepathy-glib messages
44 * SIPE_TIMING=1 : enable time stamps
45 * [recommeded for any usable log file]
47 * SIPE_LOGFILE="..." : redirect output to this file
48 * [prepend file name with "+" to enable append mode]
50 ******************************************************************************
56 #include <telepathy-glib/debug-sender.h>
57 #include <telepathy-glib/telepathy-glib.h>
59 #include "sipe-backend.h"
61 #include "telepathy-private.h"
63 #define SIPE_TELEPATHY_DEBUG 1
65 static TpDebugSender
*debug
;
66 static guint flags
= 0;
68 void sipe_telepathy_debug_init(void)
70 static const GDebugKey
const keys
[] = {
71 /* This simulates pidgin's --debug flag, i.e. we only see
72 * output from SIPE if this is set.
74 * @TODO: we could make this more finely grained, i.e.
75 * which levels should be visible
77 { "sipe", SIPE_TELEPATHY_DEBUG
},
79 const gchar
*env_flags
= g_getenv("SIPE_DEBUG");
81 /* Telepathy debugger */
82 debug
= tp_debug_sender_dup();
84 /* divert g_log_default_handler() output to a logfile */
85 tp_debug_divert_messages(g_getenv("SIPE_LOGFILE"));
87 /* sipe & telepathy-glib debugging flags */
88 if (env_flags
) flags
|= g_parse_debug_string(env_flags
, keys
, 1);
89 tp_debug_set_flags(env_flags
);
91 /* add time stamps to debug output */
92 if (g_getenv("SIPE_TIMING"))
93 g_log_set_default_handler(tp_debug_timestamped_log_handler
, NULL
);
95 /* enable test mode */
96 if (g_getenv("SIPE_PERSIST"))
97 tp_debug_set_persistent(TRUE
);
100 void sipe_telepathy_debug_finalize(void)
102 g_object_unref(debug
);
105 static const GLogLevelFlags debug_level_mapping
[] = {
106 G_LOG_LEVEL_DEBUG
, /* SIPE_DEBUG_LEVEL_INFO */
107 G_LOG_LEVEL_WARNING
, /* SIPE_DEBUG_LEVEL_WARNING */
108 G_LOG_LEVEL_CRITICAL
, /* SIPE_DEBUG_LEVEL_ERROR */
109 G_LOG_LEVEL_ERROR
, /* SIPE_DEBUG_LEVEL_FATAL, this will abort! */
112 void sipe_backend_debug_literal(sipe_debug_level level
,
115 if (flags
& SIPE_TELEPATHY_DEBUG
) {
116 GLogLevelFlags g_level
= debug_level_mapping
[level
];
118 g_log(SIPE_TELEPATHY_DOMAIN
, g_level
, "%s", msg
);
119 g_get_current_time(&now
);
120 tp_debug_sender_add_message(debug
, &now
,
121 SIPE_TELEPATHY_DOMAIN
,
127 void sipe_backend_debug(sipe_debug_level level
,
133 va_start(ap
, format
);
134 if (flags
& SIPE_TELEPATHY_DEBUG
) {
135 GLogLevelFlags g_level
= debug_level_mapping
[level
];
137 g_logv(SIPE_TELEPATHY_DOMAIN
, g_level
, format
, ap
);
138 g_get_current_time(&now
);
139 tp_debug_sender_add_message_vprintf(debug
, &now
, NULL
,
140 SIPE_TELEPATHY_DOMAIN
,
147 gboolean
sipe_backend_debug_enabled(void)
149 return(flags
& SIPE_TELEPATHY_DEBUG
);