| 1 | #! /usr/bin/env python |
|---|
| 2 | # -*- coding: utf8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Itaka is free software; you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License as published by |
|---|
| 6 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | # any later version. |
|---|
| 8 | # |
|---|
| 9 | # Itaka is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with Itaka; if not, write to the Free Software |
|---|
| 16 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | # |
|---|
| 18 | # Copyright 2003-2007 Marc E. <santusmarc_at_gmail.com>. |
|---|
| 19 | # http://itaka.jardinpresente.com.ar |
|---|
| 20 | # |
|---|
| 21 | # $Id$ |
|---|
| 22 | |
|---|
| 23 | """ Itaka console output and logging engine """ |
|---|
| 24 | |
|---|
| 25 | class Console: |
|---|
| 26 | """ Console I/O handler organized by message type. Also handle GUI logging when passed an instance """ |
|---|
| 27 | |
|---|
| 28 | def __init__(self, itakaglobals): |
|---|
| 29 | """ Initiate console handler with configuration globals """ |
|---|
| 30 | self.itakaglobals = itakaglobals |
|---|
| 31 | if self.itakaglobals.output['normal']: print "[*] Itaka %s starting..." % (itakaglobals.version) |
|---|
| 32 | |
|---|
| 33 | def __del__(self): |
|---|
| 34 | """ Quitting message """ |
|---|
| 35 | if self.itakaglobals.output['normal']: print "[*] Itaka shutting down..." |
|---|
| 36 | |
|---|
| 37 | def msg(self, message, gui=False, eventsonly=True, icon=None): |
|---|
| 38 | """ Message handler. 'gui' is an instance of the 'Gui' class for logging purposes. 'eventsonly' is a boolean to spcecify if the log message will only go to the events log. 'icon' is gtk.STOCK_ICON string for the Gui event log """ |
|---|
| 39 | if self.itakaglobals.output['normal']: |
|---|
| 40 | print "[*] %s" % (message) |
|---|
| 41 | |
|---|
| 42 | # Twisted takes a dict with the first key being 'message', coupled with a str()'ed tuple'd message. |
|---|
| 43 | if gui: |
|---|
| 44 | gui.logger({'message': [str(message)]}, False, None, eventsonly, icon) |
|---|
| 45 | |
|---|
| 46 | def warn(self, caller, message, gui=False, eventsonly=False, icon=None): |
|---|
| 47 | """ Warning handler. 'caller' is a list or tuple specifying the class and method were the event ocurred. Arguments are inherited from msg() """ |
|---|
| 48 | self.array = ".".join(caller) |
|---|
| 49 | if self.itakaglobals.output['normal']: |
|---|
| 50 | print "[*] WARNING: %s: %s" % (self.array, message) |
|---|
| 51 | if gui: |
|---|
| 52 | gui.logger({'message': [str(message), str("WARNING: %s: %s" % (self.array, message))]}, True, 'warning', eventsonly, icon) |
|---|
| 53 | |
|---|
| 54 | def error(self, caller, message, gui=False, eventsonly=False, icon=None): |
|---|
| 55 | """ Error handler. 'caller' is a list or tuple specifying the class and method were the event ocurred. Arguments are inherited from msg() """ |
|---|
| 56 | self.array = ".".join(caller) |
|---|
| 57 | if not self.itakaglobals.output['quiet']: |
|---|
| 58 | print "[*] ERROR: %s: %s" % (self.array, message) |
|---|
| 59 | if gui: |
|---|
| 60 | gui.expander.set_expanded(True) |
|---|
| 61 | gui.expander.set_sensitive(True) |
|---|
| 62 | # Stop the server |
|---|
| 63 | if gui.server_listening: |
|---|
| 64 | gui.startstop(None, "stop", True) |
|---|
| 65 | |
|---|
| 66 | gui.logger({'message': [str(message), str("ERROR: %s: %s" % (self.array, message))]}, True, 'error', eventsonly, icon) |
|---|
| 67 | |
|---|
| 68 | def debug(self, caller, message, gui=False, eventsonly=False, icon=None): |
|---|
| 69 | """ Debug handler. 'caller' is a list or tuple specifying the class and method were the event ocurred. Arguments are inherited from msg() """ |
|---|
| 70 | self.array = ".".join(caller) |
|---|
| 71 | if self.itakaglobals.output['debug']: |
|---|
| 72 | print "[*] DEBUG: %s: %s" % (self.array, message) |
|---|
| 73 | if gui: |
|---|
| 74 | gui.logger({'message': [str(message), str("DEBUG: %s: %s" % (self.array, message))]}, True, 'debug', eventsonly, icon) |
|---|