       cmd = [command, uri].concat args
     else
       cmd = [command].concat args
+    debug "Running ghc-modi command #{cmd}"
     process.stdin.write cmd.join(' ').replace(/\r|\r?\n/g,' ') + '\n'
 
     if text?
+      debug "Unloading file text from ghc-modi"
       process.stdin.write "unload #{uri}\n"
 
   killProcess: =>
     return unless @processMap?
+    debug "Killing all ghc-modi processes"
     atom.project.getDirectories().forEach (dir) =>
       @killProcessForDir dir
 
   killProcessForDir: (dir) =>
     return unless @processMap?
+    debug "Killing ghc-modi process for #{dir.getPath()}"
     clearTimeout @processMap.get(dir)?.timer
     @processMap.get(dir)?.process.stdin?.end?()
     @processMap.get(dir)?.process.kill?()

{BufferedProcess,Emitter,CompositeDisposable} = require('atom')
CP = require('child_process')

module.exports =
class GhcModiProcessBase

  spawnProcess: (rootDir,options)=>
    return unless @processMap?
    return unless atom.config.get('haskell-ghc-mod.enableGhcModi')
    timer = setTimeout (=>
      debug "Killing ghc-modi for #{rootDir.getPath()} due to inactivity"
      @killProcessForDir rootDir), 60*60*1000
    proc = @processMap.get(rootDir)
    debug "Checking for ghc-modi in #{rootDir.getPath()}"
    if proc?
      debug "Found running ghc-modi instance for #{rootDir.getPath()}"
      clearTimeout proc.timer
      proc.timer = timer
      return proc.process
    debug "Spawning new ghc-modi instance for #{rootDir.getPath()} with
          #{"options.#{k} = #{v}" for k,v of options}"
    modiPath = atom.config.get('haskell-ghc-mod.ghcModiPath')
    proc = CP.spawn(modiPath,[],options)
    proc.on 'stderr', (data) ->
      console.error 'Ghc-modi says:',data
    proc.on 'exit', (code) =>
      debug "ghc-modi for #{rootDir.getPath()} ended with #{code}"
      @processMap.delete(rootDir)
      @spawnProcess(rootDir,options) if code!=0
    @processMap.set rootDir,
