diff --git a/src/orca/orca.py b/src/orca/orca.py
index ac300e4..c5f3d94 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -99,6 +99,8 @@ _userSettings = None
 #
 _commandLineSettings = {}
 
+debugSwitch = False # True if --debug is used in command line
+debugFileName = "" # file name of the debug file
 ########################################################################
 #                                                                      #
 # METHODS FOR HANDLING PRESENTATION MANAGERS                           #
@@ -991,6 +993,10 @@ def loadUserSettings(script=None, inputEvent=None):
     else:
         try:
             _userSettings = __import__("user-settings")
+            if debugSwitch:
+                debug.debugLevel = debug.LEVEL_ALL
+                debug.eventDebugLevel = debug.LEVEL_OFF
+                debug.debugFile = open(debugFileName, 'w', 0)
         except ImportError:
             debug.printException(debug.LEVEL_FINEST)
         except:
@@ -1560,6 +1566,8 @@ def main():
     bypassSetup     = False
     setupRequested  = False
     showGUI         = False
+    global debugSwitch
+    global debugFileName
 
     # We hack a little here because the shell script to start orca can
     # conflate all of command line arguments into one string, which is
@@ -1593,6 +1601,8 @@ def main():
              "text-setup",
              "no-setup",
              "list-apps",
+             "debug",
+             "debug-file=",
              "version"])
         for opt, val in opts:
             if opt in ("-u", "--user-prefs-dir"):
@@ -1650,6 +1660,12 @@ def main():
             if opt in ("-v", "--version"):
                 print "Orca %s" % platform.version
                 die(0)
+            if opt == "--debug":
+                debugSwitch = True
+                if debugFileName == "":
+                    debugFileName = time.strftime('debug-%Y-%m-%d-%H:%M:%S.out')
+            if opt == "--debug-file":
+                debugFileName = val.strip()
             if opt in ("-l", "--list-apps"):
                 apps = filter(lambda x: x is not None,
                               pyatspi.Registry.getDesktop(0))
