[Pkg-mozext-commits] [gcontactsync] 64/88: Issue #25: Implement log rotation.

David Prévot taffit at moszumanska.debian.org
Thu Sep 18 20:52:30 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository gcontactsync.

commit e6bd9ea9f3a4963649eacbc99b32be3a5a9b9dc2
Author: Josh Geenen <joshgeenen at gmail.com>
Date:   Sun Jul 6 14:15:04 2014 -0500

    Issue #25: Implement log rotation.
---
 src/content/MessengerOverlay.js          | 45 ++++++++++++++++++++++++++++----
 src/content/Preferences.js               |  3 ++-
 src/defaults/preferences/gContactSync.js |  2 ++
 3 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/content/MessengerOverlay.js b/src/content/MessengerOverlay.js
index 485db09..a61942c 100644
--- a/src/content/MessengerOverlay.js
+++ b/src/content/MessengerOverlay.js
@@ -66,10 +66,9 @@ com.gContactSync.MessengerOverlay = {
   initialize: function MessengerOverlay_initialize() {
     // reset the needRestart pref
     com.gContactSync.Preferences.setSyncPref("needRestart", false);
-    // remove the old log file
-    if (com.gContactSync.FileIO.mLogFile && com.gContactSync.FileIO.mLogFile.exists()) {
-      com.gContactSync.FileIO.mLogFile.remove(false); // delete the old log file
-    }
+
+    // Perform log rotation
+    com.gContactSync.MessengerOverlay.rotateLog(Math.max(1, com.gContactSync.Preferences.mSyncPrefs.numLogsInRotation.value));
 
     // override SetBusyCursor to wrap it in a try/catch block as it and
     // this add-on do not get along...
@@ -85,8 +84,10 @@ com.gContactSync.MessengerOverlay = {
         "\n * Log location:     " + com.gContactSync.FileIO.mLogFile.path +
         "\n");
 
+    // Preferences.js is loaded before MessengerOverlay so the preferences are logged to the previous log file
+    // in the rotation (if any).  If verbose logging is enabled get the prefs again to log them.
     if (com.gContactSync.Preferences.mSyncPrefs.verboseLog.value) {
-      com.gContactSync.Preferences.getSyncPrefs();  // for logging
+      com.gContactSync.Preferences.getSyncPrefs();
     }
 
     var lastVersionMajor   = com.gContactSync.Preferences.mSyncPrefs.lastVersionMajor.value;
@@ -125,6 +126,40 @@ com.gContactSync.MessengerOverlay = {
     }
   },
   /**
+   * Rotates log files.
+   * @param numLogsInRotation The total number of logs in the rotation.  Must be at least 1.
+   */
+  rotateLog: function MessengerOverlay_rotateLog(numLogsInRotation) {
+    var file = com.gContactSync.FileIO.getProfileDirectory();
+    file.append(com.gContactSync.FileIO.fileNames.FOLDER_NAME);
+
+    // Remove the last file in the rotation
+    var lastName = this.getLogFileNameFromNumber(numLogsInRotation - 1);
+    file.append(lastName);
+    if (file.exists()) {file.remove(false);}
+
+    // Rename files
+    for (var i = numLogsInRotation - 2; i >= 0; --i) {
+      var name = this.getLogFileNameFromNumber(i);
+      file = file.parent;
+      file.append(name);
+      if (file.exists() && file.isFile()) {
+        file.moveTo(file.parent, lastName);
+      }
+      lastName = name;
+    }
+  },
+  /**
+   * Returns the name of the log file given the log index.
+   * @param i The log file number.
+   * @return {string} The name of the log file at the given index.
+   */
+  getLogFileNameFromNumber: function MessengerOverlay_getLogFileNameFromNumber(i) {
+    var fileName = com.gContactSync.FileIO.fileNames.LOG_FILE;
+    if (i) {fileName = fileName.replace(/log/, "log" + i);}
+    return fileName;
+  },
+  /**
    * Calls the original SetBusyCursor() function from mailCore.js wrapped in a
    * try/catch block.  For some unknown reason, gContactSync causes
    * SetBusyCursor to fail after a synchronization with an update from
diff --git a/src/content/Preferences.js b/src/content/Preferences.js
index a38491e..01e9705 100644
--- a/src/content/Preferences.js
+++ b/src/content/Preferences.js
@@ -210,7 +210,8 @@ com.gContactSync.Preferences = {
     v04RCUpgradeNeeded:       new com.gContactSync.Pref("v04RCUpgradeNeeded",       "bool", false),
     httpRequestTimeout:       new com.gContactSync.Pref("httpRequestTimeout",        "int", 0),
     httpRequestDelay:         new com.gContactSync.Pref("httpRequestDelay",          "int", 120),
-    numRelations:             new com.gContactSync.Pref("numRelations",              "int", 6)
+    numRelations:             new com.gContactSync.Pref("numRelations",              "int", 6),
+    numLogsInRotation:        new com.gContactSync.Pref("numLogsInRotation",         "int", 3)
   },
   /**
    * Gets a preference given its branch, name, and type
diff --git a/src/defaults/preferences/gContactSync.js b/src/defaults/preferences/gContactSync.js
index 9d8491b..162f50e 100644
--- a/src/defaults/preferences/gContactSync.js
+++ b/src/defaults/preferences/gContactSync.js
@@ -92,6 +92,8 @@ pref("extensions.gContactSync.httpRequestTimeout", 0);
 pref("extensions.gContactSync.httpRequestDelay", 120);
 // Number of relation/people fields to show
 pref("extensions.gContactSync.numRelations", 6);
+// Number of log files to store.  Must be >= 1.
+pref("extensions.gContactSync.numLogsInRotation", 3);
 // extended properties to sync
 pref("extensions.gContactSync.extended1", "Custom1");
 pref("extensions.gContactSync.extended2", "Custom2");

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/gcontactsync.git



More information about the Pkg-mozext-commits mailing list