2011-12-08 12:18:14 +02:00
#!/usr/bin/python
2007-12-21 23:49:39 +02:00
2008-08-13 13:03:04 +03:00
# LADITools - Linux Audio Desktop Integration Tools
2008-08-12 18:58:54 +03:00
# ladilog - A log viewer for your Linux Audio Desktop
2010-01-07 21:18:24 +02:00
# Copyright (C) 2007-2010, Marc-Olivier Barre <marco@marcochapeau.org>
# Copyright (C) 2007-2009, Nedko Arnaudov <nedko@arnaudov.name>
2007-12-21 23:49:39 +02:00
#
# 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/>.
2008-08-08 00:05:15 +03:00
import os
2008-04-25 22:00:55 +03:00
import sys
2008-08-09 16:45:48 +03:00
from subprocess import Popen, PIPE
import pty
2008-08-08 00:05:15 +03:00
from signal import SIGTERM
2008-08-09 16:45:48 +03:00
import termios
import tty
2011-08-05 11:36:34 +03:00
import gettext
2011-12-08 02:50:44 +02:00
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__)
2011-12-08 02:50:44 +02:00
2008-08-04 00:13:24 +03:00
try:
2008-08-06 00:19:38 +03:00
from gobject import timeout_add
import pygtk
pygtk.require ('2.0')
import gtk
import vte
2023-01-29 15:31:05 +02:00
except Exception as e:
2011-08-05 11:36:34 +03:00
error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_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))
2008-08-06 00:19:38 +03:00
error.run ()
exit (1)
2008-08-04 00:13:24 +03:00
2008-02-03 19:32:19 +02:00
# Default configuration
max_lines_default = 100
2008-08-21 05:18:25 +03:00
# 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
2008-08-21 05:18:25 +03:00
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
2008-02-03 19:32:19 +02:00
2011-08-05 11:36:34 +03:00
class ladilog(object):
2008-08-06 00:19:38 +03:00
def __init__ (self):
2008-08-07 00:28:44 +03:00
self.log_files = [
{
'name': 'JACK',
'config_name': 'jackdbus_log',
2010-01-06 17:51:59 +02:00
'config_default': os.sep.join([os.environ['HOME'], ".log", "jack", "jackdbus.log"])
2008-08-07 00:28:44 +03:00
},
{
2009-11-22 12:43:55 +02:00
'name': 'LADISH',
'config_name': 'ladish_log',
2010-01-06 17:51:59 +02:00
'config_default': os.sep.join([os.environ['HOME'], ".log", "ladish", "ladish.log"])
2008-08-07 00:28:44 +03:00
},
{
'name': 'A2J',
'config_name': 'a2j_log',
2010-01-06 17:51:59 +02:00
'config_default': os.sep.join([os.environ['HOME'], ".log", "a2j", "a2j.log"])
}
2008-08-07 00:28:44 +03:00
]
2008-02-03 19:32:19 +02:00
# Handle the configuration
2009-12-28 21:52:35 +02:00
self.global_config = laditools.config ()
self.param_dict = self.global_config.get_config_section ('ladilog')
2008-08-07 00:28:44 +03:00
2011-12-07 23:22:26 +02:00
for log in self.log_files:
2009-12-28 21:52:35 +02:00
if self.param_dict != None:
2011-12-07 23:22:26 +02:00
if log['config_name'] not in self.param_dict:
2009-12-28 21:52:35 +02:00
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']
2008-08-07 00:28:44 +03:00
2008-02-03 19:32:19 +02:00
if 'max_lines' not in self.param_dict:
2009-12-28 21:52:35 +02:00
self.param_dict['max_lines'] = max_lines_default
2008-08-07 00:28:44 +03:00
2010-01-06 18:55:45 +02:00
for log in self.log_files[:]:
2009-12-28 21:52:35 +02:00
log['logfile_path'] = self.param_dict[log['config_name']]
2008-09-06 23:00:52 +03:00
# skip logfiles that dont exist
if not os.access(log['logfile_path'], os.R_OK):
self.log_files.remove(log)
2023-01-29 15:31:05 +02:00
print(_("Skipping '%s' because it does not exist") % log['logfile_path'])
2009-11-22 12:43:55 +02:00
else:
2023-01-29 15:31:05 +02:00
print(_("Watching '%s'") % log['logfile_path'])
2008-08-07 00:28:44 +03:00
2009-12-28 21:52:35 +02:00
max_lines_text = self.param_dict['max_lines']
2008-08-04 00:13:24 +03:00
self.max_lines = int (max_lines_text)
2007-12-23 14:50:07 +02:00
# Load the glade file
2011-08-05 11:36:34 +03:00
builder = gtk.Builder()
builder.add_from_file(laditools.find_data_file("ladilog_ui.ui"))
2007-12-23 14:50:07 +02:00
# Get the ui ready for action
2008-08-07 00:43:20 +03:00
self.event_dict = {"on_ladilog_ui_destroy" : self.on_quit,
2007-12-23 14:50:07 +02:00
"on_close_button_clicked" : self.on_quit,
"on_clear_button_clicked" : self.on_clear_text,
2008-08-04 00:13:24 +03:00
"on_purge_button_clicked" : self.on_purge}
2011-08-05 11:36:34 +03:00
builder.connect_signals(self.event_dict)
2008-08-07 00:28:44 +03:00
2008-08-06 00:19:38 +03:00
# Create our terminal and display it
2008-08-07 00:28:44 +03:00
for log in self.log_files:
log['term'] = vte.Terminal ()
2008-08-09 16:45:48 +03:00
log["tab_label"] = gtk.Label (log["name"])
2008-08-07 00:28:44 +03:00
2011-08-05 11:36:34 +03:00
self.logview_notebook = builder.get_object ("ladilog_notebook")
2008-08-07 00:28:44 +03:00
for log in self.log_files:
log['term'].show ()
2009-11-22 12:58:44 +02:00
2008-08-06 00:19:38 +03:00
# Make it do something...
2008-08-07 00:28:44 +03:00
for log in self.log_files:
2008-08-08 00:05:15 +03:00
try:
2008-08-21 05:18:25 +03:00
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)
2008-08-09 16:45:48 +03:00
2008-08-08 00:05:15 +03:00
except ValueError:
2023-01-29 15:31:05 +02:00
print(_("You called Popen with invalid arguments... dumbass"))
2008-08-08 00:05:15 +03:00
except:
2023-01-29 15:31:05 +02:00
print(_("Unexpected error:"), sys.exc_info ()[0])
2008-08-04 00:13:24 +03:00
2009-11-22 12:58:44 +02:00
for log in self.log_files:
self.logview_notebook.append_page (log["term"])
self.logview_notebook.set_tab_label (log["term"], log["tab_label"])
2008-08-21 05:18:25 +03:00
self.auto_updater = timeout_add(250, self.update)
def update(self):
# Append latest output to the buffer
2008-08-08 00:05:15 +03:00
for log in self.log_files:
2008-08-21 05:18:25 +03:00
line = log['log_file'].readline()
while line:
log["term"].feed(line + '\r')
line = log['log_file'].readline()
log['log_file'].seek(log['log_file'].tell())
return True
def on_quit (self, data=None):
2008-08-04 00:13:24 +03:00
gtk.main_quit ()
def on_clear_text (self, data=None):
2008-08-09 16:45:48 +03:00
current_view = self.logview_notebook.get_current_page ()
2008-09-12 15:31:46 +03:00
self.log_files[current_view]["term"].feed ("\033[2J\033[;f")
2008-08-12 18:37:10 +03:00
2008-08-06 00:19:38 +03:00
def on_purge (self, data=None):
2008-08-09 16:45:48 +03:00
current_view = self.logview_notebook.get_current_page ()
2008-08-06 00:19:38 +03:00
# Opens the file in write anew mode thus clearing the file and close it right away
2008-08-07 00:28:44 +03:00
open (self.log_files[current_view]['logfile_path'], "w+")
2008-08-09 16:45:48 +03:00
self.log_files[current_view]["term"].feed ("\033[2J\033[;f")
2007-12-23 14:50:07 +02:00
2008-08-04 00:13:24 +03:00
def run (self):
gtk.main ()
2009-12-28 21:52:35 +02:00
self.global_config.set_config_section ("ladilog", self.param_dict)
2011-12-07 23:22:26 +02:00
self.global_config.save ()
2007-12-23 14:50:07 +02:00
return 0
2007-12-21 23:49:39 +02:00
2008-09-16 10:54:19 +03:00
try:
ladilog().run()
2023-01-29 15:31:05 +02:00
except Exception as e:
2011-08-05 11:36:34 +03:00
error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, _("Unexpected error\n\n") + repr(e))
2008-09-16 10:54:19 +03:00
error.run()
exit(1)