3 # telepathy-butterfly - an MSN connection manager for Telepathy
5 # Copyright (C) 2006-2007 Ali Sabil <ali.sabil@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 def debug_divert_messages(filename
):
30 """debug_divert_messages:
31 @filename: A file to which to divert stdout and stderr or None to do
34 Open the given file for writing and duplicate its file descriptor to
35 be used for stdout and stderr. This has the effect of closing the previous
36 stdout and stderr, and sending all messages that would have gone there
37 to the given file instead.
39 By default the file is truncated and hence overwritten each time the
41 If the filename is prefixed with '+' then the file is not truncated and
42 output is added at the end of the file.
43 Passing None to this function is guaranteed to have no effect. This is
44 so you can call it with the recommended usage
45 debug_divert_messages (os.getenv(MYAPP_LOGFILE))
46 and it won't do anything if the environment variable is not set. """
48 # TODO: this function should move to telepathy-python at some point
55 if filename
.startswith('+'):
56 logfile
= open(filename
[1:], 'a')
58 logfile
= open(filename
, 'w')
60 print "Can't open logfile '%s' : '%s'" %(filename
, e
)
66 debug_divert_messages(os
.getenv('BUTTERFLY_LOGFILE'))
68 logging
.basicConfig(level
=logging
.DEBUG
)
70 from butterfly
import ButterflyConnectionManager
71 from butterfly
.util
.decorator
import async
73 logger
= logging
.getLogger('Butterfly')
76 PROCESS_NAME
= 'telepathy-butterfly'
78 if __name__
== '__main__':
79 try: # change process name for killall
81 libc
= ctypes
.CDLL('libc.so.6')
82 libc
.prctl(15, PROCESS_NAME
, 0, 0, 0)
84 logger
.warning('Unable to set processName: %s" % e')
91 if 'BUTTERFLY_PERSIST' not in os
.environ
:
93 if len(manager
._connections
) == 0:
94 logger
.info('No connection received - quitting')
97 gobject
.timeout_add(IDLE_TIMEOUT
, timeout_cb
)
98 shutdown_callback
= quit
100 shutdown_callback
= None
102 signal
.signal(signal
.SIGTERM
, lambda : quit
)
104 manager
= ButterflyConnectionManager(shutdown_callback
)
105 mainloop
= gobject
.MainLoop(is_running
=True)
107 while mainloop
.is_running():
110 except KeyboardInterrupt: