[gedit-plugins] Add debug messages to git plugin



commit 6250425f707bcca5be1786b557eb69076a5a7de9
Author: Garrett Regier <garrettregier gmail com>
Date:   Sun Jun 29 05:24:12 2014 -0700

    Add debug messages to git plugin

 plugins/git/Makefile.am              |    1 +
 plugins/git/git/debug.py             |   73 ++++++++++++++++++++++++++++++++++
 plugins/git/git/windowactivatable.py |   11 +++++
 plugins/git/git/workerthread.py      |    4 ++
 4 files changed, 89 insertions(+), 0 deletions(-)
---
diff --git a/plugins/git/Makefile.am b/plugins/git/Makefile.am
index 94d1eb8..6ba229e 100644
--- a/plugins/git/Makefile.am
+++ b/plugins/git/Makefile.am
@@ -2,6 +2,7 @@ if ENABLE_GIT
 plugins_gitdir = $(plugindir)/git
 plugins_git_PYTHON =                           \
        plugins/git/git/__init__.py             \
+       plugins/git/git/debug.py                \
        plugins/git/git/diffrenderer.py         \
        plugins/git/git/viewactivatable.py      \
        plugins/git/git/windowactivatable.py    \
diff --git a/plugins/git/git/debug.py b/plugins/git/git/debug.py
new file mode 100644
index 0000000..e36c4d2
--- /dev/null
+++ b/plugins/git/git/debug.py
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+ 
+#  Copyright (C) 2014 - Garrett Regier
+#
+#  This program 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.
+#
+#  This program 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 this program; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330,
+#  Boston, MA 02111-1307, USA.
+ 
+import inspect
+import io
+import os
+import sys
+import traceback
+ 
+ 
+_DEBUG = os.getenv('GEDIT_DEBUG_GIT_PLUGIN') is not None
+ 
+ 
+def debug(msg, *, frames=1, print_stack=False, limit=None):
+    """Mimicks Gedit's gedit_debug_message() output, but only prints
+       when the GEDIT_DEBUG_GIT_PLUGIN enviroment variable exists.
+    """
+    if not _DEBUG:
+        return
+ 
+    current_frame = inspect.currentframe()
+    calling_frame = current_frame
+ 
+    try:
+        for i in range(frames):
+            calling_frame = calling_frame.f_back
+ 
+        info = inspect.getframeinfo(calling_frame)
+ 
+        path = min(info.filename.replace(x, '') for x in sys.path)
+        if path[0] == os.path.sep:
+            path = path[1:]
+ 
+        full_message = io.StringIO()
+        full_message.writelines((info.filename, ':', str(info.lineno),
+                                 ' (', info.function, ') ', msg, '\n'))
+ 
+        if print_stack:
+            full_message.write('Stack (most recent call last):\n')
+            traceback.print_stack(calling_frame,
+                                  file=full_message, limit=limit)
+ 
+        if full_message.getvalue()[-1] != '\n':
+            full_message.write('\n')
+ 
+        # Always write the message in a single call to avoid to
+        # avoid having the message get split when threads call it
+        sys.stderr.write(full_message.getvalue())
+ 
+        full_message.close()
+ 
+    finally:
+        # Avoid leaking
+        del calling_frame
+        del current_frame
+ 
+# ex:ts=4:et:
diff --git a/plugins/git/git/windowactivatable.py b/plugins/git/git/windowactivatable.py
index 9d96076..13a951e 100644
--- a/plugins/git/git/windowactivatable.py
+++ b/plugins/git/git/windowactivatable.py
@@ -21,11 +21,22 @@ from gi.repository import GLib, GObject, Gio, Gedit, Ggit
 
 import weakref
 
+from .debug import debug
 from .workerthread import WorkerThread
 
 
 class GitStatusThread(WorkerThread):
     def push(self, repo, location):
+        if repo is None:
+            debug('Invalid repository', print_stack=True)
+            return
+ 
+        workdir = repo.get_workdir()
+        if workdir.get_relative_path(location) is None:
+            debug('Invalid location "%s" for workdir "%s"' %
+                  (location.get_uri(), workdir.get_uri()), print_stack=True)
+            return
+
         super().push(repo, location)
 
     def handle_task(self, repo, location):
diff --git a/plugins/git/git/workerthread.py b/plugins/git/git/workerthread.py
index 07cbea2..b490d33 100644
--- a/plugins/git/git/workerthread.py
+++ b/plugins/git/git/workerthread.py
@@ -25,6 +25,8 @@ import queue
 import threading
 import traceback
 
+from .debug import debug
+
 
 class WorkerThread(threading.Thread):
     __metaclass__ = abc.ABCMeta
@@ -115,6 +117,7 @@ class WorkerThread(threading.Thread):
             if not self.__has_idle.is_set():
                 self.__has_idle.set()
 
+                debug('%s<%s>: idle added' % (type(self).__name__, self.name))
                 GLib.source_set_name_by_id(GLib.idle_add(self.__in_idle),
                                            '[gedit] %s result callback idle' %
                                            (self.__class__.__name__))
@@ -137,6 +140,7 @@ class WorkerThread(threading.Thread):
             # Only remove the idle when there are no more items,
             # some could have been added after the IndexError was raised
             if len(self.__results) == 0:
+                debug('%s<%s>: idle done' % (type(self).__name__, self.name))
                 return GLib.SOURCE_REMOVE
 
         return GLib.SOURCE_CONTINUE


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]