Skip to content

Commit

Permalink
Reduce log verbosity. More verbose logs on demand.
Browse files Browse the repository at this point in the history
  • Loading branch information
subnut committed Mar 16, 2023
1 parent 79eb150 commit 325ac6f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SERVER_PORT: str = os.environ.get("GHOSTTEXT_SERVER_PORT", "4001")
FOCUSED_NVIM_ADDRESS = os.environ.get("NVIM_LISTEN_ADDRESS", None)
LOGGING_ENABLED: bool = False
VERBOSE_LOGGING: bool = bool(os.environ.get("NVIM_GHOST_VERBOSE_LOGGING"))
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED") is not None:
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED").isdigit():
LOGGING_ENABLED = bool(int(os.environ.get("NVIM_GHOST_LOGGING_ENABLED")))
Expand Down Expand Up @@ -58,6 +59,12 @@ def log(*args, **kwargs):
print(time.strftime("[%H:%M:%S]:"), *args, **kwargs)


if VERBOSE_LOGGING:
logv = log
else:
logv = lambda *args, **kwargs: None


def _port_occupied(port) -> bool:
"""
If port is occupied, returns True. Else returns False
Expand Down Expand Up @@ -265,7 +272,7 @@ def _respond(self, text):
class GhostWebSocket(WebSocket):
# New message received
def handle(self):
log(f"{self.address[1]} got", self.data)
logv(f"From {self.address[1]} got", self.data)

# Extract the data
data = json.loads(self.data)
Expand Down Expand Up @@ -366,7 +373,7 @@ def _neovim_listener(self):
self.loop_neovim_handle.run_loop(None, self._neovim_handler)

def _neovim_handler(self, *args):
log(f"nvim_event handle={self.handle_neovim_notifications} {args}")
logv(f"nvim_event handle={self.handle_neovim_notifications} {args}")
if not self.handle_neovim_notifications:
# Resume handling notifications, because this notification has been
# triggered by the buffer changes we have done above.
Expand Down Expand Up @@ -413,12 +420,16 @@ def _neovim_handler(self, *args):
message = {"text": text, "selections": [{"start": curpos, "end": curpos}]}
message = json.dumps(message)
self.send_message(message)
log(f"{self.address[1]} sent", message)
logv(f"To {self.address[1]} sent", message)

def _trigger_autocmds(self, url: str):
self.neovim_handle.command(f"doau nvim_ghost_user_autocommands User {url}")

def _do_close(self):
log(
"Closing websocket",
":".join([str(_) for _ in self.address]),
)
self.close()
log(
"Websocket",
Expand Down

0 comments on commit 325ac6f

Please sign in to comment.