[PATCH] I've fixed bug #590.

Konrad Kleine (none) konrad at vaio.
Sun May 27 13:21:44 EDT 2007


The fix removes all non-shell logfiles from the
~/.sugar/default/logs directory (as usual) and keeps
the ten most recent shell logfile.

The backed up shell.log files carry their creation time as an
additional extension to their filename (e.g. shell.log.1180272197)

Konrad Kleine
---
 sugar/logger.py |   39 +++++++++++++++++++++++++++++++++++++--
 1 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/sugar/logger.py b/sugar/logger.py
old mode 100755
new mode 100644
index 2cfc730..670edaf
--- a/sugar/logger.py
+++ b/sugar/logger.py
@@ -34,7 +34,6 @@ formatter = logging.Formatter('%(name)s: %(message)s')
 class LogWriter:
     def __init__(self, module_id):
         self._module_id = module_id
-
         logs_dir = _get_logs_dir()
         log_path = os.path.join(logs_dir, module_id + '.log')
         self._log_file = open(log_path, 'w')
@@ -114,6 +113,42 @@ def start(module_id):
 
 def cleanup():
     logs_dir = _get_logs_dir()
+
+    """
+    Prevents the shell logfiles from being deleted (fixes bug #590).
+    Backup the current shell logfile with its ctime appended.
+    All other logfiles will be deleted!
+    """
     
+    shell_logfiles = []             # List of all shell logfiles
+
     for f in os.listdir(logs_dir):
-        os.remove(os.path.join(logs_dir, f))
+
+        filepath = os.path.join(logs_dir, f)
+
+        if f.startswith('shell.log'):
+            if f == 'shell.log':
+                ctime = os.stat(filepath).st_ctime
+                backup_name = filepath + '.' + str(ctime)
+                os.rename(filepath, backup_name)
+                shell_logfiles.append(backup_name)
+            else:
+                shell_logfiles.append(filepath)
+        else:
+            # Delete non-shell logfiles
+            os.remove(filepath)
+
+    """
+    Delete all shell logfiles except the 10 most recent.
+    """
+    
+    to_keep = 10                # How many files to keep in logs dir
+    
+    if len(shell_logfiles) > to_keep:
+
+        shell_logfiles.sort()
+        shell_logfiles.reverse()
+
+        for i in range(to_keep, len(shell_logfiles)):
+            os.remove(shell_logfiles[i])
+            
-- 
1.4.4.2


--------------050804010102090409080307--


More information about the Sugar mailing list