laditools/bin/ladilog

202 lines
7.2 KiB
Plaintext
Raw Normal View History

2011-12-08 12:18:14 +02:00
#!/usr/bin/python
# LADITools - Linux Audio Desktop Integration Tools
# ladilog - A log viewer for your Linux Audio Desktop
# Copyright (C) 2007-2010, Marc-Olivier Barre <marco@marcochapeau.org>
# Copyright (C) 2007-2009, Nedko Arnaudov <nedko@arnaudov.name>
#
# 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
2011-08-05 11:36:34 +03:00
import gettext
try:
import imp
imp.find_module('laditools')
except ImportError:
# Running from the build tree?
sys.path.insert(0, os.path.join(sys.path[0], os.pardir))
import laditools
2011-12-08 18:07:27 +02:00
gettext.install(laditools.__name__)
try:
from gi.repository import Gtk, GObject, Vte
except Exception, e:
error = Gtk.MessageDialog (None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, _("You need to get you dependencies right before you run this program. Ask your package maintainer why this is happening to you\n%s") % repr(e))
error.run ()
exit (1)
timeout_add = GObject.timeout_add
# Default configuration
max_lines_default = 100
# Output the last <lines> lines
def read_last(lfile, lines):
chunk_size = lines * 60
lfile.seek(0, 2)
endpos = lfile.tell()
pos = endpos - chunk_size
if pos < 0:
pos = 0
backlog = ''
backlog_size = 0
lines += 1
while pos >= 0 and backlog_size <= lines:
lfile.seek(pos, 0)
s = lfile.read(chunk_size)
2010-07-25 02:42:56 +03:00
pos = pos - chunk_size
backlog_size += s.count("\n")
backlog = s + backlog
backlog = backlog.strip().split("\n")
if len(backlog) > lines:
backlog = backlog[-lines:]
lfile.seek(endpos, 0)
return backlog
2011-08-05 11:36:34 +03:00
class ladilog(object):
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': 'LADISH',
'config_name': 'ladish_log',
'config_default': os.sep.join([os.environ['HOME'], ".log", "ladish", "ladish.log"])
},
{
'name': 'A2J',
'config_name': 'a2j_log',
'config_default': os.sep.join([os.environ['HOME'], ".log", "a2j", "a2j.log"])
}
]
# Handle the configuration
self.global_config = laditools.config ()
self.param_dict = self.global_config.get_config_section ('ladilog')
2011-12-07 23:22:26 +02:00
for log in self.log_files:
if self.param_dict != None:
2011-12-07 23:22:26 +02:00
if log['config_name'] not in self.param_dict:
self.param_dict[log['config_name']] = log['config_default']
else:
2011-12-07 23:22:26 +02:00
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'] = self.param_dict[log['config_name']]
# skip logfiles that dont exist
if not os.access(log['logfile_path'], os.R_OK):
self.log_files.remove(log)
2011-08-05 11:36:34 +03:00
print _("Skipping '%s' because it does not exist") % log['logfile_path']
else:
2011-08-05 11:36:34 +03:00
print _("Watching '%s'") % log['logfile_path']
max_lines_text = self.param_dict['max_lines']
self.max_lines = int (max_lines_text)
# Load the glade file
builder = Gtk.Builder()
2011-08-05 11:36:34 +03:00
builder.add_from_file(laditools.find_data_file("ladilog_ui.ui"))
print laditools.find_data_file("ladilog_ui.ui")
# Get the ui ready for action
self.event_dict = {"on_ladilog_ui_destroy" : self.on_quit,
"on_ladilog_ui_delete" : self.on_delete,
"on_close_button_clicked" : self.on_quit,
"on_clear_button_clicked" : self.on_clear_text,
"on_purge_button_clicked" : self.on_purge}
2011-08-05 11:36:34 +03:00
builder.connect_signals(self.event_dict)
# Create our terminal and display it
for log in self.log_files:
log['term'] = Vte.Terminal.new ()
log['term'].set_scroll_on_output(True)
log["tab_label"] = Gtk.Label(label=log["name"])
2011-08-05 11:36:34 +03:00
self.logview_notebook = builder.get_object ("ladilog_notebook")
# Make it do something...
for log in self.log_files:
try:
log['log_file'] = open(log['logfile_path'], "rb")
lines = read_last(log['log_file'], self.max_lines)
for line in lines:
line = line.strip('\r\n') + '\r\n'
log["term"].feed(line, len(line))
except ValueError:
2011-08-05 11:36:34 +03:00
print _("You called Popen with invalid arguments... dumbass")
except:
2011-08-05 11:36:34 +03:00
print _("Unexpected error:"), sys.exc_info ()[0]
for log in self.log_files:
self.logview_notebook.append_page (log["term"], log["tab_label"])
# self.logview_notebook.set_tab_label (log["term"], log["tab_label"])
ui = builder.get_object("ladilog_ui")
ui.resize(800, 400)
ui.show_all()
self.auto_updater = timeout_add(250, self.update)
def update(self):
# Append latest output to the buffer
for log in self.log_files:
line = log['log_file'].readline()
while line:
log["term"].feed(line + '\r', len(line)+1)
line = log['log_file'].readline()
log['log_file'].seek(log['log_file'].tell())
return True
def on_delete(self, widget=None, data=None):
return False
def on_quit (self, data=None):
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", len("\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", len("\033[2J\033[;f"))
def run (self):
Gtk.main ()
self.global_config.set_config_section ("ladilog", self.param_dict)
2011-12-07 23:22:26 +02:00
self.global_config.save ()
return 0
try:
ladilog().run()
exit(0)
except Exception, e:
error = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, _("Unexpected error\n\n") + repr(e))
error.run()
exit(1)