#!/usr/bin/env python # # Copyright (c) 2000, by Arun Sharma # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. The name of the developer may NOT be used to endorse or promote products # derived from this software without specific prior written permission. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # $Id: $ import sys, string, fileinput from qt import QWidget, QPopupMenu, QSplitter, QSize from qt import SIGNAL, CTRL, Key_Q, Key_O, Key_S from kdecore import KApplication, KDNDDropZone from kdecore import kapp, DndURL, i18n, Icon from kdeui import KTMainWindow, KTreeList, KTreeListItem, KTabListBox from kdeui import KMsgBox from kfile import KFileDialog class KLogViewWidget(QWidget): def __init__(self, parent, filename, *args): apply(QWidget.__init__,(self, parent,) + args) tlist = self.tlist = KTabListBox(self, "Logs", 4) tlist.setColumn(0, "Date", 80) tlist.setColumn(1, "Time", 80) tlist.setColumn(2, "Source", 150) tlist.setColumn(3, "Message", 800) tlist.setSeparator('#') tlist.show() tlist.setMinimumSize(QSize(800, 400)) # Now populate the tablist for line in fileinput.input(filename): if (string.find(line, 'last message repeated') != -1): continue date = line[:6] time = line[8:16] host, source, message = string.split(line[16:], ' ', 2) # strip the colon source = source[:-1] tlist.insertItem(string.join((date, time, source, message), '#')) class KLogView(KTMainWindow): TOOLBAR_EXIT = 0 TOOLBAR_OPEN = 1 TOOLBAR_SAVE = 2 def __init__(self, *args): if len(sys.argv) > 1: filename = sys.argv[1] sys.argv = sys.argv[2:] else: # Default log file filename = "/var/log/messages" apply(KTMainWindow.__init__,(self,) + args) # The main widget. if filename: mainw = KLogViewWidget(self, filename) self.view = mainw self.setView(mainw) # Create the drop zone. self.dropZone = KDNDDropZone(self,DndURL) self.connect(self.dropZone,SIGNAL("dropAction(KDNDDropZone *)"),self.slotDropEvent); # Build the menus. self.fileMenu = QPopupMenu() self.fileMenu.insertItem(i18n("&Open"),self.open,CTRL+Key_O) self.fileMenu.insertItem(i18n("&Save"),self.save,CTRL+Key_S) self.fileMenu.insertItem(i18n("&Quit"),kapp.quit,CTRL+Key_Q) self.menuBar().insertItem(i18n("&File"),self.fileMenu) self.menuBar().insertSeparator() self.helpMenu = kapp.getHelpMenu(1,i18n( "KLogView --- A KDE based " + "Log viewer\n\n" + "(c) 1999 Arun Sharma\n" + "KLogView lets you " + "graphically view common log files")) self.menuBar().insertItem(i18n("&Help"),self.helpMenu) # Build the toolbar. self.toolBar().insertButton(Icon("fileopen.xpm"),KLogView.TOOLBAR_OPEN,1,i18n("Open")) self.toolBar().insertButton(Icon("filefloppy.xpm"),KLogView.TOOLBAR_SAVE,1,i18n("Save")) self.toolBar().insertButton(Icon("exit.xpm"),KLogView.TOOLBAR_EXIT,1,i18n("Exit")) self.connect(self.toolBar(),SIGNAL("clicked(int)"),self.slotToolbarClicked) self.enableStatusBar() def slotToolbarClicked(self,item): if item == KLogView.TOOLBAR_EXIT: kapp.quit() else: if item == KLogView.TOOLBAR_OPEN: self.open() else: if item == KLogView.TOOLBAR_SAVE: self.save() def slotDropEvent(self,zone): u = zone.getURLList()[0] print "Dropped", u def open(self): file = KFileDialog.getOpenFileName(".", "*.xml") if file: self.hide() mainw = KLogViewWidget(self, file) self.view = mainw self.setView(mainw) self.show() def save(self): KMsgBox.message(None, "KLogView: Unimplemented", "This feature is not implemented") app = KApplication(sys.argv,"klogviewer") widget = KLogView() widget.view.setMinimumSize(QSize(800, 450)) widget.setMinimumSize(QSize(800, 450)) widget.show() app.exec_loop()