- implement BUTTERFLY_LOGFILE
[telepathy-butterfly.git] / telepathy-butterfly
blobb2bc1d5e0775c9ceba4ab09cff34c5e0f27d0c78
1 #!/usr/bin/python
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
21 import gobject
22 import dbus.glib
23 import signal
24 import os
25 import sys
27 import logging
29 logging.basicConfig(level=logging.DEBUG)
31 from butterfly import ButterflyConnectionManager
32 from butterfly.util.decorator import async
34 logger = logging.getLogger('Butterfly')
36 IDLE_TIMEOUT = 5000
37 PROCESS_NAME = 'telepathy-butterfly'
39 def debug_divert_messages(filename):
40 """debug_divert_messages:
41 @filename: A file to which to divert stdout and stderr or None to do
42 nothing.
44 Open the given file for writing and duplicate its file descriptor to
45 be used for stdout and stderr. This has the effect of closing the previous
46 stdout and stderr, and sending all messages that would have gone there
47 to the given file instead.
49 By default the file is truncated and hence overwritten each time the
50 process is executed.
51 If the filename is prefixed with '+' then the file is not truncated and
52 output is added at the end of the file.
53 Passing None to this function is guaranteed to have no effect. This is
54 so you can call it with the recommended usage
55 debug_divert_messages (os.getenv(MYAPP_LOGFILE))
56 and it won't do anything if the environment variable is not set. """
58 # TODO: this function should move to telepathy-python at some point
60 if filename is None:
61 return
63 try:
64 if filename.startswith('+'):
65 logfile = open(filename[1:], 'a')
66 else:
67 logfile = open(filename, 'w')
68 except IOError, e:
69 print "Can't open logfile '%s' : '%s'" %(filename, e)
70 return
72 sys.stdout = logfile
73 sys.stderr = logfile
75 if __name__ == '__main__':
76 debug_divert_messages(os.getenv('BUTTERFLY_LOGFILE'))
78 try: # change process name for killall
79 import ctypes
80 libc = ctypes.CDLL('libc.so.6')
81 libc.prctl(15, PROCESS_NAME, 0, 0, 0)
82 except Exception, e:
83 logger.warning('Unable to set processName: %s" % e')
85 @async
86 def quit():
87 manager.quit()
88 mainloop.quit()
90 if 'BUTTERFLY_PERSIST' not in os.environ:
91 def timeout_cb():
92 if len(manager._connections) == 0:
93 logger.info('No connection received - quitting')
94 quit()
95 return False
96 gobject.timeout_add(IDLE_TIMEOUT, timeout_cb)
97 shutdown_callback = quit
98 else:
99 shutdown_callback = None
101 signal.signal(signal.SIGTERM, lambda : quit)
103 manager = ButterflyConnectionManager(shutdown_callback)
104 mainloop = gobject.MainLoop(is_running=True)
106 while mainloop.is_running():
107 try:
108 mainloop.run()
109 except KeyboardInterrupt:
110 quit()