Add logging facilities

This commit is contained in:
Théophile Bastian 2020-05-10 16:32:23 +02:00
parent fb246d39ec
commit 742c479dcd
2 changed files with 50 additions and 4 deletions

View file

@ -8,11 +8,21 @@ import tempfile
import patch import patch
from tmux import TmuxSession from tmux import TmuxSession
from vim_session import VimSession from vim_session import VimSession
import logging
logger = logging.getLogger(__name__)
def parse_args(): def parse_args():
""" Parse command-line arguments """ """ Parse command-line arguments """
parser = argparse.ArgumentParser(prog="patch2vimedit") parser = argparse.ArgumentParser(prog="patch2vimedit")
parser.add_argument(
"-g",
"--debug",
action="store_true",
help="Enable debug logging to /tmp/patch2log",
)
parser.add_argument( parser.add_argument(
"-C", "-C",
"--directory", "--directory",
@ -46,7 +56,19 @@ def configure_home(home):
tmux_conf.write("set -g status off\n") tmux_conf.write("set -g status off\n")
with vim_conf_path.open("w") as vim_conf: with vim_conf_path.open("w") as vim_conf:
vim_conf.write( vim_conf.write(
"syntax on\nset bg=dark\nset number\nset ts=4\nset sw=4\nset et\nset so=5\n" """
syntax on
set bg=dark
set number
set ts=4
set sw=4
set et
set so=5
set noautoindent
set nosmartindent
set nocindent
set indentexpr&
"""
) )
return tmux_conf_path, vim_conf_path return tmux_conf_path, vim_conf_path
@ -73,10 +95,21 @@ def apply_patchset(patchset):
tmux_session.session.kill_session() tmux_session.session.kill_session()
def configure_log(args):
if args.debug:
logging.basicConfig(filename="/tmp/patch2log", level=logging.DEBUG)
else:
logging.basicConfig(level=logging.CRITICAL)
logging.getLogger("libtmux").setLevel(logging.INFO)
def main(): def main():
""" Entry-point function """ """ Entry-point function """
args = parse_args() args = parse_args()
configure_log(args)
patchset = get_patch(args.patch) patchset = get_patch(args.patch)
if args.directory: if args.directory:

View file

@ -1,9 +1,11 @@
import subprocess
import multiprocessing import multiprocessing
import sys
import random import random
import libtmux
import time import time
import logging
import libtmux
logger = logging.getLogger(__name__)
class TmuxSession: class TmuxSession:
@ -15,10 +17,13 @@ class TmuxSession:
def __init__(self, session): def __init__(self, session):
if self.tmux_server is None: if self.tmux_server is None:
raise Exception("Server not initialized") raise Exception("Server not initialized")
self.session = session self.session = session
self.session_id = session.id self.session_id = session.id
self.keyboard_speed = 0.00001 self.keyboard_speed = 0.00001
self.sendkey_log_buffer = []
@classmethod @classmethod
def initialize_server(cls, socket_name=None, config_file=None): def initialize_server(cls, socket_name=None, config_file=None):
""" Initialize the tmux server """ """ Initialize the tmux server """
@ -67,6 +72,14 @@ class TmuxSession:
# Weirdly, `tmux send-keys 'blah;'` doesn't send the semicolon; and so # Weirdly, `tmux send-keys 'blah;'` doesn't send the semicolon; and so
# does `tmux send-keys ';'`. We must escape it with a backslash. # does `tmux send-keys ';'`. We must escape it with a backslash.
arg = arg[:-1] + r"\;" arg = arg[:-1] + r"\;"
if arg in ["escape", "enter"]:
logline = "".join(self.sendkey_log_buffer) + " " + arg
logger.debug(logline)
self.sendkey_log_buffer = []
else:
self.sendkey_log_buffer.append(arg)
self.session.attached_pane.send_keys( self.session.attached_pane.send_keys(
arg, suppress_history=False, enter=False arg, suppress_history=False, enter=False
) )