From 11d5e7c1002ec281c57b1181487e448b91e19b80 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 12 May 2015 13:50:02 -0400
Subject: [PATCH] Implemented memcache_debug also for session operations

---
 program/lib/Roundcube/rcube.php                  |   28 +++++++++
 CHANGELOG                                        |    2 
 program/lib/Roundcube/rcube_cache_shared.php     |   15 ----
 program/lib/Roundcube/rcube_session_memcache.php |   68 +++++++++++++++++-----
 program/lib/Roundcube/rcube_cache.php            |   15 ----
 5 files changed, 85 insertions(+), 43 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 85669f0..e1edae5 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,7 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
-- Implemented memcache_debug and apc_debug options for cache operations tracking
+- Implemented memcache_debug and apc_debug options
 - Installer: Remove system() function use (#1490139)
 - Password plugin: Added 'kpasswd' driver by Peter Allgeyer
 - Add initdb.sh to create database from initial.sql script with prefix support (#1490188)
diff --git a/program/lib/Roundcube/rcube.php b/program/lib/Roundcube/rcube.php
index cf6ebf9..ae5957e 100644
--- a/program/lib/Roundcube/rcube.php
+++ b/program/lib/Roundcube/rcube.php
@@ -37,6 +37,8 @@
     const REQUEST_ERROR_URL   = 1;
     const REQUEST_ERROR_TOKEN = 2;
 
+    const DEBUG_LINE_LENGTH = 4096;
+
     /**
      * Singleton instace of rcube
      *
@@ -1453,6 +1455,32 @@
 
 
     /**
+     * Write debug info to the log
+     *
+     * @param string Engine type - file name (memcache, apc)
+     * @param string Data string to log
+     * @param bool   Operation result
+     */
+    public static function debug($engine, $data, $result = null)
+    {
+        static $debug_counter;
+
+        $line = '[' . (++$debug_counter[$engine]) . '] ' . $data;
+
+        if (($len = strlen($line)) > self::DEBUG_LINE_LENGTH) {
+            $diff = $len - self::DEBUG_LINE_LENGTH;
+            $line = substr($line, 0, self::DEBUG_LINE_LENGTH) . "... [truncated $diff bytes]";
+        }
+
+        if ($result !== null) {
+            $line .= ' [' . ($result ? 'TRUE' : 'FALSE') . ']';
+        }
+
+        self::write_log($engine, $line);
+    }
+
+
+    /**
      * Returns current time (with microseconds).
      *
      * @return float Current time in seconds since the Unix
diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index c37f652..dffb600 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -48,8 +48,6 @@
     private $cache_sums    = array();
     private $max_packet    = -1;
 
-    const DEBUG_LINE_LENGTH = 4096;
-
 
     /**
      * Object constructor.
@@ -666,21 +664,12 @@
      */
     private function debug($type, $key, $data = null, $result = null)
     {
-        $line = '[' . (++$this->debug_count) . '] ' . strtoupper($type) . ' ' . $key;
+        $line = strtoupper($type) . ' ' . $key;
 
         if ($data !== null) {
             $line .= ' ' . ($this->packed ? $data : serialize($data));
-
-            if (($len = strlen($line)) > self::DEBUG_LINE_LENGTH) {
-                $diff = $len - self::DEBUG_LINE_LENGTH;
-                $line = substr($line, 0, self::DEBUG_LINE_LENGTH) . "... [truncated $diff bytes]";
-            }
         }
 
-        if ($result !== null) {
-            $line .= ' [' . ($result ? 'TRUE' : 'FALSE') . ']';
-        }
-
-        rcube::write_log($this->type, $line);
+        rcube::debug($this->type, $line, $result);
     }
 }
diff --git a/program/lib/Roundcube/rcube_cache_shared.php b/program/lib/Roundcube/rcube_cache_shared.php
index c95aedd..701f8d7 100644
--- a/program/lib/Roundcube/rcube_cache_shared.php
+++ b/program/lib/Roundcube/rcube_cache_shared.php
@@ -47,8 +47,6 @@
     private $cache_sums    = array();
     private $max_packet    = -1;
 
-    const DEBUG_LINE_LENGTH = 4096;
-
 
     /**
      * Object constructor.
@@ -655,21 +653,12 @@
      */
     private function debug($type, $key, $data = null, $result = null)
     {
-        $line = '[' . (++$this->debug_count) . '] ' . strtoupper($type) . ' ' . $key;
+        $line = strtoupper($type) . ' ' . $key;
 
         if ($data !== null) {
             $line .= ' ' . ($this->packed ? $data : serialize($data));
-
-            if (($len = strlen($line)) > self::DEBUG_LINE_LENGTH) {
-                $diff = $len - self::DEBUG_LINE_LENGTH;
-                $line = substr($line, 0, self::DEBUG_LINE_LENGTH) . "... [truncated $diff bytes]";
-            }
         }
 
-        if ($result !== null) {
-            $line .= ' [' . ($result ? 'TRUE' : 'FALSE') . ']';
-        }
-
-        rcube::write_log($this->type, $line);
+        rcube::debug($this->type, $line, $result);
     }
 }
diff --git a/program/lib/Roundcube/rcube_session_memcache.php b/program/lib/Roundcube/rcube_session_memcache.php
index 732d5fb..bbb7cb0 100644
--- a/program/lib/Roundcube/rcube_session_memcache.php
+++ b/program/lib/Roundcube/rcube_session_memcache.php
@@ -15,7 +15,7 @@
  +-----------------------------------------------------------------------+
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | Author: Aleksander Machniak <alec@alec.pl>                            |
- | Author: Cor Bosman <cor@roundcu.bet>                                   |
+ | Author: Cor Bosman <cor@roundcu.bet>                                  |
  +-----------------------------------------------------------------------+
 */
 
@@ -31,6 +31,7 @@
 class rcube_session_memcache extends rcube_session
 {
     private $memcache;
+    private $debug;
 
     /**
      * @param Object $config
@@ -40,12 +41,14 @@
         parent::__construct($config);
 
         $this->memcache = rcube::get_instance()->get_memcache();
+        $this->debug    = $config->get('memcache_debug');
 
         if (!$this->memcache) {
-            rcube::raise_error(array('code' => 604, 'type' => 'db',
-                                   'line' => __LINE__, 'file' => __FILE__,
-                                   'message' => "Failed to connect to memcached. Please check configuration"),
-                               true, true);
+            rcube::raise_error(array(
+                    'code' => 604, 'type' => 'db',
+                    'line' => __LINE__, 'file' => __FILE__,
+                    'message' => "Failed to connect to memcached. Please check configuration"),
+                true, true);
         }
 
         // register sessions handler
@@ -80,7 +83,11 @@
     {
         if ($key) {
             // #1488592: use 2nd argument
-            $this->memcache->delete($key, 0);
+            $result = $this->memcache->delete($key, 0);
+
+            if ($this->debug) {
+                $this->debug('delete', $key, null, $result);
+            }
         }
 
         return true;
@@ -101,32 +108,42 @@
             $this->ip      = $arr['ip'];
             $this->vars    = $arr['vars'];
             $this->key     = $key;
-
-            return !empty($this->vars) ? (string) $this->vars : '';
         }
 
-        return null;
+        if ($this->debug) {
+            $this->debug('get', $key, $value);
+        }
+
+        return $this->vars ?: '';
     }
 
     /**
-     * write data to memcache storage
+     * Write data to memcache storage
      *
      * @param $key
      * @param $vars
+     *
      * @return bool
      */
     public function write($key, $vars)
     {
-        return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars)),
-                                    MEMCACHE_COMPRESSED, $this->lifetime + 60);
+        $data   = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars));
+        $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
+
+        if ($this->debug) {
+            $this->debug('set', $key, $data, $result);
+        }
+
+        return $result;
     }
 
     /**
-     * update memcache session data
+     * Update memcache session data
      *
      * @param $key
      * @param $newvars
      * @param $oldvars
+     *
      * @return bool
      */
     public function update($key, $newvars, $oldvars)
@@ -134,11 +151,30 @@
         $ts = microtime(true);
 
         if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
-            return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)),
-                                        MEMCACHE_COMPRESSED, $this->lifetime + 60);
+            $data   = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
+            $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
+
+            if ($this->debug) {
+                $this->debug('set', $key, $data, $result);
+            }
+
+            return $result;
         }
 
         return true;
     }
 
-}
\ No newline at end of file
+    /**
+     * Write memcache debug info to the log
+     */
+    protected function debug($type, $key, $data = null, $result = null)
+    {
+        $line = strtoupper($type) . ' ' . $key;
+
+        if ($data !== null) {
+            $line .= ' ' . $data;
+        }
+
+        rcube::debug($this->type, $line, $result);
+    }
+}

--
Gitblit v1.9.1