cryptonas/plugins/logs/logs.py

94 lines
2.4 KiB
Python
Raw Normal View History

#
# Copyright 2006 sense.lab e.V.
#
# This file is part of the CryptoBox.
#
# The CryptoBox 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 2 of the License, or
# (at your option) any later version.
#
# The CryptoBox 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 the CryptoBox; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""The logs feature of the CryptoBox.
"""
__revision__ = "$Id"
import cryptobox.plugins.base
2006-11-06 17:05:00 +01:00
import os
class logs(cryptobox.plugins.base.CryptoBoxPlugin):
"""The logs feature of the CryptoBox.
"""
2006-11-06 17:05:00 +01:00
plugin_capabilities = [ "system" ]
plugin_visibility = [ "preferences" ]
request_auth = False
2006-11-06 17:05:00 +01:00
rank = 90
def do_action(self, lines=50, size=3000, pattern=None):
"""Show the latest part of the log file.
"""
import re
## filter input
try:
lines = int(lines)
if lines <= 0:
raise(ValueError)
except ValueError:
lines = 50
try:
size = int(size)
if size <= 0:
raise(ValueError)
except ValueError:
size = 3000
if not pattern is None:
pattern = str(pattern)
if re.search(u'\W', pattern):
pattern = None
self.hdf[self.hdf_prefix + "Content"] = self.__get_log_content(
lines, size, pattern)
self.hdf[self.hdf_prefix + "StyleSheetFile"] = os.path.abspath(os.path.join(
self.plugin_dir, "logs.css"))
2006-11-06 17:05:00 +01:00
return "show_log"
def get_status(self):
"""The current status includes the log configuration details.
"""
2006-11-06 17:05:00 +01:00
return "%s:%s:%s" % (
self.cbox.prefs["Log"]["Level"],
self.cbox.prefs["Log"]["Destination"],
self.cbox.prefs["Log"]["Details"])
def __get_log_content(self, lines, max_size, pattern):
"""Filter, sort and shorten the log content.
"""
if pattern:
content = []
current_length = 0
for line in self.cbox.get_log_data():
if line.find(pattern) != -1:
content.append(line)
current_length += len(line)
if lines and len(content) >= lines:
break
if max_size and current_length >= max_size:
break
else:
content = self.cbox.get_log_data(lines, max_size)
return "<br/>".join(content)
2006-11-06 17:05:00 +01:00