purple: add support for latest appstreamcli
[siplcs.git] / src / telepathy / telepathy-debug.c
blob4b11cbd7372e95e68f16faf88306db0f5d06efbb
1 /**
2 * @file telepathy-debug.c
4 * pidgin-sipe
6 * Copyright (C) 2012-2019 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_UNSAFE_DEBUG=1] [SIPE_TIMING=1] [SIPE_LOGFILE="..."] \
31 * telepathy-sipe
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]
39 * SIPE_DEBUG=... :
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_UNSAFE_DEBUG=1 : enable unsafe debugging output, i.e. include the
45 * content of protocol messages which may reveal
46 * secret information, like passwords.
47 * [usually required to be able to debug issues]
49 * SIPE_TIMING=1 : enable time stamps
50 * [recommended for any usable log file]
52 * SIPE_LOGFILE="..." : redirect output to this file
53 * [prepend file name with "+" to enable append mode]
55 ******************************************************************************
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
62 #include <stdarg.h>
64 #include <glib.h>
65 #include <telepathy-glib/debug-sender.h>
66 #include <telepathy-glib/telepathy-glib.h>
68 #include "sipe-backend.h"
70 #include "telepathy-private.h"
72 #define SIPE_TELEPATHY_DEBUG 1
74 static TpDebugSender *debug;
75 static guint flags = 0;
76 static gboolean unsafe = FALSE;
78 void sipe_telepathy_debug_init(void)
80 static const GDebugKey keys[] = {
81 /* This simulates pidgin's --debug flag, i.e. we only see
82 * output from SIPE if this is set.
84 * @TODO: we could make this more finely grained, i.e.
85 * which levels should be visible
87 { "sipe", SIPE_TELEPATHY_DEBUG },
89 const gchar *env_flags = g_getenv("SIPE_DEBUG");
91 /* Telepathy debugger */
92 debug = tp_debug_sender_dup();
94 /* divert g_log_default_handler() output to a logfile */
95 tp_debug_divert_messages(g_getenv("SIPE_LOGFILE"));
97 /* sipe & telepathy-glib debugging flags */
98 if (env_flags) flags |= g_parse_debug_string(env_flags, keys, 1);
99 tp_debug_set_flags(env_flags);
101 /* enable unsafe debug output */
102 if (g_getenv("SIPE_UNSAFE_DEBUG"))
103 unsafe = TRUE;
105 /* add time stamps to debug output */
106 if (g_getenv("SIPE_TIMING"))
107 g_log_set_default_handler(tp_debug_timestamped_log_handler, NULL);
109 /* enable test mode */
110 if (g_getenv("SIPE_PERSIST"))
111 tp_debug_set_persistent(TRUE);
114 void sipe_telepathy_debug_finalize(void)
116 g_object_unref(debug);
119 static const GLogLevelFlags debug_level_mapping[] = {
120 G_LOG_LEVEL_DEBUG, /* SIPE_LOG_LEVEL_INFO */
121 G_LOG_LEVEL_WARNING, /* SIPE_LOG_LEVEL_WARNING */
122 G_LOG_LEVEL_CRITICAL, /* SIPE_LOG_LEVEL_ERROR */
123 G_LOG_LEVEL_DEBUG, /* SIPE_DEBUG_LEVEL_INFO */
124 G_LOG_LEVEL_WARNING, /* SIPE_DEBUG_LEVEL_WARNING */
125 G_LOG_LEVEL_CRITICAL, /* SIPE_DEBUG_LEVEL_ERROR */
128 void sipe_backend_debug_literal(sipe_debug_level level,
129 const gchar *msg)
131 if ((level < SIPE_DEBUG_LEVEL_LOWEST) || (flags & SIPE_TELEPATHY_DEBUG)) {
132 GLogLevelFlags g_level = debug_level_mapping[level];
133 g_log(SIPE_TELEPATHY_DOMAIN, g_level, "%s", msg);
134 tp_debug_sender_add_message(debug, NULL,
135 SIPE_TELEPATHY_DOMAIN,
136 g_level,
137 msg);
141 void sipe_backend_debug(sipe_debug_level level,
142 const gchar *format,
143 ...)
145 va_list ap;
147 va_start(ap, format);
148 if ((level < SIPE_DEBUG_LEVEL_LOWEST) || (flags & SIPE_TELEPATHY_DEBUG)) {
149 gchar *msg = g_strdup_vprintf(format, ap);
150 sipe_backend_debug_literal(level, msg);
151 g_free(msg);
153 va_end(ap);
156 gboolean sipe_backend_debug_enabled(void)
158 return((flags & SIPE_TELEPATHY_DEBUG) && unsafe);
162 Local Variables:
163 mode: c
164 c-file-style: "bsd"
165 indent-tabs-mode: t
166 tab-width: 8
167 End: