From 68c9139f94d2486a915c4cb3e050b9d9f997d747 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Calonne?= <aurel@nexedi.com>
Date: Tue, 7 Sep 2010 08:08:43 +0000
Subject: [PATCH] add methods to tail the event log file

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38162 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Tool/IntrospectionTool.py | 48 +++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Tool/IntrospectionTool.py b/product/ERP5/Tool/IntrospectionTool.py
index 098bc0ee40..c1eaee695d 100644
--- a/product/ERP5/Tool/IntrospectionTool.py
+++ b/product/ERP5/Tool/IntrospectionTool.py
@@ -172,7 +172,7 @@ class IntrospectionTool(LogMixin, BaseTool):
                                file_path='log/Z2.log', 
                                compressed=compressed) 
 
-  security.declareProtected(Permissions.ManagePortal, 'getAccessLog')
+  security.declareProtected(Permissions.ManagePortal, 'getEventLog')
   def getEventLog(self, compressed=1, REQUEST=None):
     """
       Get the Event Log.
@@ -186,6 +186,52 @@ class IntrospectionTool(LogMixin, BaseTool):
                                file_path='log/event.log',
                                compressed=compressed)
 
+
+  def _tailFile(self, file_name, line_number=10):
+    """
+    Do a 'tail -f -n line_number filename'
+    """
+    log_file = os.path.join(getConfiguration().instancehome, file_name)
+    if not os.path.exists(log_file):
+      raise IOError, 'The file: %s does not exist.' % log_file
+
+    char_per_line=75
+
+    tailed_file = open(log_file,'r')
+    while 1:
+      try:
+        tailed_file.seek(-1 * char_per_line * line_number, 2)
+      except IOError:
+        tailed_file.seek(0)
+      if tailed_file.tell() == 0:
+        at_start = 1
+      else:
+        at_start = 0
+
+      lines = tailed_file.read().split("\n")
+      if (len(lines) > (line_number + 1)) or at_start:
+        break
+      # The lines are bigger than we thought
+      char_per_line = char_per_line * 1.3 # Inc for retry
+
+    tailed_file.close()
+
+    if len(lines) > line_number:
+      start = len(lines) - line_number - 1
+    else:
+      start = 0
+
+    return "\n".join(lines[start:len(lines)])
+
+
+  security.declareProtected(Permissions.ManagePortal, 'tailEventLog')
+  def tailEventLog(self):
+    """
+    Tail the Event Log.
+    """
+    return self._tailFile('log/event.log', 50)
+
+
   security.declareProtected(Permissions.ManagePortal, 'getAccessLog')
   def getDataFs(self,  compressed=1, REQUEST=None):
     """
-- 
2.30.9