laditools/ladilog

140 lines
5.6 KiB
Python
Executable File

#!/usr/bin/env python
# pyjackctl - The python jackdbus controller suite
# jackctl_logview - A log viewer for the python JACK dbus suite
# Copyright (C) 2007-2008, Marc-Olivier Barre and Nedko Arnaudov.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from subprocess import Popen, PIPE
import pty
from signal import SIGTERM
import termios
import tty
try:
from gobject import timeout_add
import pygtk
pygtk.require ('2.0')
import gtk
import gtk.glade
import vte
import pyjackctl
except:
error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Wow dude...\n\nYou really need to get you dependencies right before you run this program. Ask your package maintainer why he didn't do his job properly")
error.run ()
exit (1)
# Default configuration
max_lines_default = 100
good_term = [11522, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15', '\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff', '\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']]
class ladilog (gtk.glade.XML):
def __init__ (self):
self.log_files = [
{
'name': 'JACK',
'config_name': 'jackdbus_log',
'config_default': os.sep.join([os.environ['HOME'], ".log", "jack", "jackdbus.log"]),
},
{
'name': 'LASH',
'config_name': 'lash_log',
'config_default': os.sep.join([os.environ['HOME'], ".log", "lash", "lash.log"]),
},
{
'name': 'A2J',
'config_name': 'a2j_log',
'config_default': os.sep.join([os.environ['HOME'], ".log", "a2j", "a2j.log"]),
},
]
# Handle the configuration
self.jacklog_config = pyjackctl.config ()
self.param_dict = self.jacklog_config.get_as_dict ('ladilog')
for log in self.log_files:
if log['config_name'] not in self.param_dict:
self.param_dict[log['config_name']] = log['config_default'], {}
if 'max_lines' not in self.param_dict:
self.param_dict['max_lines'] = max_lines_default, {}
for log in self.log_files:
log['logfile_path'], devnull = self.param_dict[log['config_name']]
max_lines_text, devnull = self.param_dict['max_lines']
self.max_lines = int (max_lines_text)
# Load the glade file
gtk.glade.XML.__init__ (self, "/usr/share/pyjackctl/data/ladilog_ui.glade")
# Get the ui ready for action
self.event_dict = {"on_ladilog_ui_destroy" : self.on_quit,
"on_close_button_clicked" : self.on_quit,
"on_clear_button_clicked" : self.on_clear_text,
"on_purge_button_clicked" : self.on_purge}
self.signal_autoconnect (self.event_dict)
# Create our terminal and display it
for log in self.log_files:
log['term'] = vte.Terminal ()
log["tab_label"] = gtk.Label (log["name"])
self.logview_notebook = self.get_widget ("ladilog_notebook")
for log in self.log_files:
self.logview_notebook.append_page (log["term"])
self.logview_notebook.set_tab_label (log["term"], log["tab_label"])
log['term'].show ()
# Make it do something...
self.tail_cmd = ["/usr/bin/tail", "-"+str (self.max_lines)+"f"]
for log in self.log_files:
try:
log["masterfd"], log["slavefd"] = pty.openpty ()
termios.tcsetattr (log["masterfd"], termios.TCSANOW, good_term)
termios.tcsetattr (log["slavefd"], termios.TCSANOW, good_term)
tty.setcbreak (log["masterfd"], termios.TCSANOW)
tty.setcbreak (log["slavefd"], termios.TCSANOW)
log["term"].set_pty (log["slavefd"])
log["tail_process"] = Popen (self.tail_cmd + [log['logfile_path']], stdout = log["masterfd"])
except ValueError:
print "You called Popen with invalid arguments... dumbass"
except:
print "Unexpected error:", sys.exc_info ()[0]
def on_quit (self, data=None):
for log in self.log_files:
os.kill (log["tail_process"].pid, SIGTERM)
log["tail_process"].poll ()
gtk.main_quit ()
def on_clear_text (self, data=None):
current_view = self.logview_notebook.get_current_page ()
self.log_files[current_view]["term"].feed ("\033[2J\033[;f")
def on_purge (self, data=None):
current_view = self.logview_notebook.get_current_page ()
# Opens the file in write anew mode thus clearing the file and close it right away
open (self.log_files[current_view]['logfile_path'], "w+")
self.log_files[current_view]["term"].feed ("\033[2J\033[;f")
def run (self):
gtk.main ()
self.jacklog_config.set_as_dict ("ladilog", self.param_dict)
return 0
ladilog ().run ()