Skip to content

Commit

Permalink
Optimize log calls
Browse files Browse the repository at this point in the history
  • Loading branch information
delivrance committed Dec 26, 2022
1 parent d298c62 commit 01cd8bb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
14 changes: 7 additions & 7 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ async def authorize(self) -> User:
except BadRequest as e:
print(e.MESSAGE)
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)
raise
else:
self.password = None
Expand Down Expand Up @@ -684,11 +684,11 @@ def load_plugins(self):
try:
module = import_module(module_path)
except ImportError:
log.warning(f'[{self.name}] [LOAD] Ignoring non-existent module "{module_path}"')
log.warning('[%s] [LOAD] Ignoring non-existent module "%s"', self.name, module_path)
continue

if "__path__" in dir(module):
log.warning(f'[{self.name}] [LOAD] Ignoring namespace "{module_path}"')
log.warning('[%s] [LOAD] Ignoring namespace "%s"', self.name, module_path)
continue

if handlers is None:
Expand Down Expand Up @@ -719,11 +719,11 @@ def load_plugins(self):
try:
module = import_module(module_path)
except ImportError:
log.warning(f'[{self.name}] [UNLOAD] Ignoring non-existent module "{module_path}"')
log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path)
continue

if "__path__" in dir(module):
log.warning(f'[{self.name}] [UNLOAD] Ignoring namespace "{module_path}"')
log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path)
continue

if handlers is None:
Expand All @@ -750,7 +750,7 @@ def load_plugins(self):
log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format(
self.name, count, "s" if count > 1 else "", root))
else:
log.warning(f'[{self.name}] No plugin loaded from "{root}"')
log.warning('[%s] No plugin loaded from "%s"', self.name, root)

async def handle_download(self, packet):
file_id, directory, file_name, in_memory, file_size, progress, progress_args = packet
Expand Down Expand Up @@ -1012,7 +1012,7 @@ async def get_file(
except pyrogram.StopTransmission:
raise
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)

def guess_mime_type(self, filename: str) -> Optional[str]:
return self.mimetypes.guess_type(filename)[0]
Expand Down
10 changes: 5 additions & 5 deletions pyrogram/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async def start(self):
self.loop.create_task(self.handler_worker(self.locks_list[-1]))
)

log.info(f"Started {self.client.workers} HandlerTasks")
log.info("Started %s HandlerTasks", self.client.workers)

async def stop(self):
if not self.client.no_updates:
Expand All @@ -164,7 +164,7 @@ async def stop(self):
self.handler_worker_tasks.clear()
self.groups.clear()

log.info(f"Stopped {self.client.workers} HandlerTasks")
log.info("Stopped %s HandlerTasks", self.client.workers)

def add_handler(self, handler, group: int):
async def fn():
Expand Down Expand Up @@ -226,7 +226,7 @@ async def handler_worker(self, lock):
if await handler.check(self.client, parsed_update):
args = (parsed_update,)
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)
continue

elif isinstance(handler, RawUpdateHandler):
Expand All @@ -250,10 +250,10 @@ async def handler_worker(self, lock):
except pyrogram.ContinuePropagation:
continue
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)

break
except pyrogram.StopPropagation:
pass
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)
4 changes: 2 additions & 2 deletions pyrogram/methods/advanced/save_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async def worker(session):
try:
await session.invoke(data)
except Exception as e:
log.error(e)
log.exception(e)

part_size = 512 * 1024

Expand Down Expand Up @@ -201,7 +201,7 @@ async def worker(session):
except StopTransmission:
raise
except Exception as e:
log.error(e, exc_info=True)
log.exception(e)
else:
if is_big:
return raw.types.InputFileBig(
Expand Down
2 changes: 1 addition & 1 deletion pyrogram/methods/auth/terminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def terminate(

if self.takeout_id:
await self.invoke(raw.functions.account.FinishTakeoutSession())
log.warning(f"Takeout session {self.takeout_id} finished")
log.warning("Takeout session %s finished", self.takeout_id)

await self.storage.save()
await self.dispatcher.stop()
Expand Down
2 changes: 1 addition & 1 deletion pyrogram/methods/utilities/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def main():

if not await self.storage.is_bot() and self.takeout:
self.takeout_id = (await self.invoke(raw.functions.account.InitTakeoutSession())).id
log.warning(f"Takeout session {self.takeout_id} initiated")
log.warning("Takeout session %s initiated", self.takeout_id)

await self.invoke(raw.functions.updates.GetState())
except (Exception, KeyboardInterrupt):
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/parser/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def handle_endtag(self, tag):
line, offset = self.getpos()
offset += 1

log.debug(f"Unmatched closing tag </{tag}> at line {line}:{offset}")
log.debug("Unmatched closing tag </%s> at line %s:%s", tag, line, offset)
else:
if not self.tag_entities[tag]:
self.tag_entities.pop(tag)
Expand Down Expand Up @@ -131,7 +131,7 @@ async def parse(self, text: str):
for tag, entities in parser.tag_entities.items():
unclosed_tags.append(f"<{tag}> (x{len(entities)})")

log.warning(f"Unclosed tags: {', '.join(unclosed_tags)}")
log.warning("Unclosed tags: %s", ", ".join(unclosed_tags))

entities = []

Expand Down
24 changes: 12 additions & 12 deletions pyrogram/session/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,34 @@ async def create(self):
self.connection = Connection(self.dc_id, self.test_mode, self.ipv6, self.proxy)

try:
log.info(f"Start creating a new auth key on DC{self.dc_id}")
log.info("Start creating a new auth key on DC%s", self.dc_id)

await self.connection.connect()

# Step 1; Step 2
nonce = int.from_bytes(urandom(16), "little", signed=True)
log.debug(f"Send req_pq: {nonce}")
log.debug("Send req_pq: %s", nonce)
res_pq = await self.invoke(raw.functions.ReqPqMulti(nonce=nonce))
log.debug(f"Got ResPq: {res_pq.server_nonce}")
log.debug(f"Server public key fingerprints: {res_pq.server_public_key_fingerprints}")
log.debug("Got ResPq: %s", res_pq.server_nonce)
log.debug("Server public key fingerprints: %s", res_pq.server_public_key_fingerprints)

for i in res_pq.server_public_key_fingerprints:
if i in rsa.server_public_keys:
log.debug(f"Using fingerprint: {i}")
log.debug("Using fingerprint: %s", i)
public_key_fingerprint = i
break
else:
log.debug(f"Fingerprint unknown: {i}")
log.debug("Fingerprint unknown: %s", i)
else:
raise Exception("Public key not found")

# Step 3
pq = int.from_bytes(res_pq.pq, "big")
log.debug(f"Start PQ factorization: {pq}")
log.debug("Start PQ factorization: %s", pq)
start = time.time()
g = prime.decompose(pq)
p, q = sorted((g, pq // g)) # p < q
log.debug(f"Done PQ factorization ({round(time.time() - start, 3)}s): {p} {q}")
log.debug("Done PQ factorization (%ss): %s %s", round(time.time() - start, 3), p, q)

# Step 4
server_nonce = res_pq.server_nonce
Expand Down Expand Up @@ -168,7 +168,7 @@ async def create(self):
dh_prime = int.from_bytes(server_dh_inner_data.dh_prime, "big")
delta_time = server_dh_inner_data.server_time - time.time()

log.debug(f"Delta time: {round(delta_time, 3)}")
log.debug("Delta time: %s", round(delta_time, 3))

# Step 6
g = server_dh_inner_data.g
Expand Down Expand Up @@ -262,11 +262,11 @@ async def create(self):
# Step 9
server_salt = aes.xor(new_nonce[:8], server_nonce[:8])

log.debug(f"Server salt: {int.from_bytes(server_salt, 'little')}")
log.debug("Server salt: %s", int.from_bytes(server_salt, "little"))

log.info(f"Done auth key exchange: {set_client_dh_params_answer.__class__.__name__}")
log.info("Done auth key exchange: %s", set_client_dh_params_answer.__class__.__name__)
except Exception as e:
log.info(f"Retrying due to {type(e).__name__}: {e}")
log.info("Retrying due to %s: %s", type(e).__name__, e)

if retries_left:
retries_left -= 1
Expand Down
10 changes: 5 additions & 5 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3045,13 +3045,13 @@ async def copy(
RPCError: In case of a Telegram RPC error.
"""
if self.service:
log.warning(f"Service messages cannot be copied. "
f"chat_id: {self.chat.id}, message_id: {self.id}")
log.warning("Service messages cannot be copied. chat_id: %s, message_id: %s",
self.chat.id, self.id)
elif self.game and not await self._client.storage.is_bot():
log.warning(f"Users cannot send messages with Game media type. "
f"chat_id: {self.chat.id}, message_id: {self.id}")
log.warning("Users cannot send messages with Game media type. chat_id: %s, message_id: %s",
self.chat.id, self.id)
elif self.empty:
log.warning(f"Empty messages cannot be copied. ")
log.warning("Empty messages cannot be copied.")
elif self.text:
return await self._client.send_message(
chat_id,
Expand Down

0 comments on commit 01cd8bb

Please sign in to comment.