i3-companion: reduce CPU usage by avoiding f-string for logger.debug

This commit is contained in:
Vincent Bernat 2021-07-16 08:22:59 +02:00
parent bf08c327a3
commit 4d76aedb61

View file

@ -144,7 +144,7 @@ def retry(max_retries):
retries = max_retries
while True:
try:
logger.debug(f"execute {fn} (remaining tries: {retries})")
logger.debug("execute %s (remaining tries: %s)", fn, retries)
return await fn(*args, **kwargs)
except Exception as e:
if retries > 0:
@ -177,7 +177,7 @@ def debounce(sleep, *, unless=None):
await asyncio.wait_for(
workers[fn].urgent.wait(), timeout=sleep
)
logger.debug(f"urgent work received for {fn}")
logger.debug("urgent work received for %s", fn)
except asyncio.TimeoutError:
pass
args, kwargs = workers[fn].queue
@ -185,11 +185,11 @@ def debounce(sleep, *, unless=None):
workers[fn].urgent.clear()
# Execute the work
logger.debug(f"execute work for {fn}")
logger.debug("execute work for %s", fn)
try:
await fn(*args, **kwargs)
except Exception as e:
logger.debug(f"while running {fn}, worker got %s", e)
logger.debug("while running %s, worker got %s", fn, e)
workers[fn] = None
raise
@ -198,21 +198,21 @@ def debounce(sleep, *, unless=None):
break
# No more work
logger.debug(f"no more work for {fn}")
logger.debug("no more work for %s", fn)
workers[fn] = None
@functools.wraps(fn)
async def wrapper(*args, **kwargs):
if workers[fn] is None:
logger.debug(f"create new worker for {fn}")
logger.debug("create new worker for %s", fn)
workers[fn] = types.SimpleNamespace()
workers[fn].task = asyncio.create_task(worker())
workers[fn].urgent = asyncio.Event()
workers[fn].queue = (args, kwargs)
else:
logger.debug(f"enqueue new work for {fn}")
logger.debug("enqueue new work for %s", fn)
if unless is not None and unless(*args, **kwargs):
logger.debug(f"wake up now for {fn}")
logger.debug("wake up now for %s", fn)
workers[fn].urgent.set()
return await workers[fn].task
@ -282,7 +282,7 @@ async def workspace_rename(i3, event):
continue
for k, v in application_icons.items():
if re.match(rf"^{k}\b", name, re.IGNORECASE):
logger.debug(f"in {attr}, found '{name}', matching {k}")
logger.debug("in %s, found '%s', matching %s", attr, name, k)
return v
return application_icons_nomatch
@ -298,7 +298,7 @@ async def workspace_rename(i3, event):
icons -= application_icons_alone
new_name = f"{workspace.num}:{'|'.join(icons)}".rstrip(":")
if workspace.name != new_name:
logger.debug(f"rename workspace {workspace.num}")
logger.debug("rename workspace %s", workspace.num)
command = f'rename workspace "{workspace.name}" to "{new_name}"'
commands.append(command)
await i3.command(";".join(commands))
@ -356,7 +356,7 @@ async def worksplace_exclusive(i3, event):
# Can the new window just intrude?
if can_intrude(w):
logger.debug(f"window {w.name} can intrude")
logger.debug("window %s can intrude", w.name)
return
# Get the workspace. From an event, w.workspace() is None, so
@ -377,7 +377,7 @@ async def worksplace_exclusive(i3, event):
}
exclusives = ids.intersection(exclusive_apps)
if not exclusives:
logger.debug("no exclusive app, {w.name} can go there")
logger.debug("no exclusive app, %s can go there", w.name)
return
# Create a new workspace and move the window here
@ -426,7 +426,7 @@ async def quake_console(i3, event):
"scratchpad show,"
f"move absolute position {posx}px {posy}px"
)
logger.debug(f"QuakeConsole: {command}")
logger.debug("QuakeConsole: %s", command)
await i3.command(command)
@ -761,7 +761,7 @@ async def dunst_status_check(i3, event):
async def network_manager_notifications(i3, event, path, state, reason):
"""Display notifications related to Network Manager state."""
ofnm = "org.freedesktop.NetworkManager"
logger.debug(f"from {path} state: {state}, reason: {reason}")
logger.debug("from %s state: %s, reason: %s", path, state, reason)
if state not in {NM_ACTIVE_CONNECTION_STATE_ACTIVATED}:
# Deactivated state does not contain enough information,
# unless we maintain state.
@ -899,7 +899,12 @@ async def main(options):
for fn, events in on.functions.items():
for event in events:
if isinstance(event, I3Event):
i3.on(event, fn)
def wrapping(fn, event):
async def wrapped(i3, event):
logger.debug("received i3 event %s for %s", event, fn)
return await fn(i3, event)
return wrapped
i3.on(event, wrapping(fn, event))
# React to some bindings
async def binding_event(i3, event):
@ -918,6 +923,7 @@ async def main(options):
for fn, events in on.functions.items():
for e in events:
if isinstance(e, CommandEvent) and e.name == kind:
logger.debug("received command event %s for %s", event, fn)
await fn(i3, cmd)
i3.on(I3Event.BINDING, binding_event)
@ -937,7 +943,10 @@ async def main(options):
)
async def wrapped(path, args):
if event.onlyif is not None and not event.onlyif(args):
logger.debug("received DBus event for %s but not interested",
fn)
return
logger.debug("received DBus event %s for %s", event, fn)
return await fn(i3, event, path, *args)
return wrapped