Skip to content

Commit 724a9c0

Browse files
committed
Use lazy formatting with logging
1 parent b0999b3 commit 724a9c0

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

rlbot/interface.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def send_bytes(self, data: bytes, data_type: SocketDataType):
8484
size = len(data)
8585
if size > MAX_SIZE_2_BYTES:
8686
self.logger.error(
87-
f"Couldn't send a {data_type} message because it was too big!"
87+
"Couldn't send a %s message because it was too big!", data_type
8888
)
8989
return
9090

@@ -141,8 +141,10 @@ def run_after_connect(
141141
self.on_connect_handlers.append(handler)
142142
try:
143143
self.connect_and_run(False, False, False, True, rlbot_server_port)
144-
except timeout:
145-
raise TimeoutError("Took too long to connect to the RLBot executable!")
144+
except timeout as e:
145+
raise TimeoutError(
146+
"Took too long to connect to the RLBot executable!"
147+
) from e
146148

147149
def connect_and_run(
148150
self,
@@ -170,7 +172,9 @@ def connect_and_run(
170172
self.socket.settimeout(None)
171173
self.is_connected = True
172174
self.logger.info(
173-
f"Socket manager connected to port {rlbot_server_port} from port {self.socket.getsockname()[1]}!"
175+
"Socket manager connected to port %s from port %s!",
176+
rlbot_server_port,
177+
self.socket.getsockname()[1],
174178
)
175179

176180
for handler in self.on_connect_handlers:
@@ -200,13 +204,16 @@ def handle_incoming_messages(self):
200204
self.handle_incoming_message(incoming_message)
201205
except flat.InvalidFlatbuffer as e:
202206
self.logger.error(
203-
f"Error while unpacking message of type {incoming_message.type.name} "
204-
f"({len(incoming_message.data)} bytes): {e}"
207+
"Error while unpacking message of type %s (%s bytes): %s",
208+
incoming_message.type.name,
209+
len(incoming_message.data),
210+
e,
205211
)
206212
except Exception as e:
207213
self.logger.warning(
208-
"Unexpected error while handling message of type "
209-
f"{incoming_message.type.name}: {e}"
214+
"Unexpected error while handling message of type %s: %s",
215+
incoming_message.type.name,
216+
e,
210217
)
211218
except:
212219
self.logger.error("Socket manager disconnected unexpectedly!")

rlbot/managers/bot.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535
self.logger.warning("RLBOT_SPAWN_IDS environment variable not set")
3636
else:
3737
self.spawn_id = int(spawn_id)
38-
self.logger.info(f"Spawn ID: {self.spawn_id}")
38+
self.logger.info("Spawn ID: %s", self.spawn_id)
3939

4040
self._game_interface = SocketRelay(logger=self.logger)
4141
self._game_interface.match_settings_handlers.append(self._handle_match_settings)
@@ -55,7 +55,7 @@ def _initialize_agent(self):
5555
self.initialize_agent()
5656
except Exception as e:
5757
self.logger.critical(
58-
f"Bot {self.name} failed to initialize due the following error: {e}"
58+
"Bot %s failed to initialize due the following error: %s", self.name, e
5959
)
6060
print_exc()
6161
exit()
@@ -131,7 +131,7 @@ def _handle_packet(self, packet: flat.GameTickPacket):
131131
try:
132132
controller = self.get_output(packet)
133133
except Exception as e:
134-
self.logger.error(f"Bot {self.name} returned an error to RLBot: {e}")
134+
self.logger.error("Bot %s returned an error to RLBot: %s", self.name, e)
135135
print_exc()
136136
return
137137

@@ -191,11 +191,9 @@ def initialize_agent(self):
191191
192192
NOTE: `self.index` is not set at this point, and should not be used. `self.team` and `self.name` _are_ set with correct information.
193193
"""
194-
pass
195194

196195
def retire(self):
197196
"""Called after the game ends"""
198-
pass
199197

200198
def get_output(self, game_tick_packet: flat.GameTickPacket) -> flat.ControllerState:
201199
"""

rlbot/managers/hivemind.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535
if spawn_ids is None:
3636
self._logger.warning("RLBOT_SPAWN_IDS environment variable not set")
3737
else:
38-
self._logger.info(f"Spawn ID: {spawn_ids}")
38+
self._logger.info("Spawn ID: %s", spawn_ids)
3939
self.spawn_ids = [int(id) for id in spawn_ids.split(",")]
4040

4141
self._game_interface = SocketRelay(logger=self._logger)
@@ -104,7 +104,7 @@ def _handle_packet(self, packet: flat.GameTickPacket):
104104
controller = self.get_outputs(packet)
105105
except Exception as e:
106106
self._logger.error(
107-
f"Hivemind (with {self.names}) returned an error to RLBot: {e}"
107+
"Hivemind (with %s) returned an error to RLBot: %s", self.names, e
108108
)
109109
print_exc()
110110
return
@@ -182,12 +182,12 @@ def initialize_agent(self):
182182
"""
183183
Called for all heaver initialization that needs to happen.
184184
Field info and match settings are fully loaded at this point, and won't return garbage data.
185+
186+
NOTE: `self.index` is not set at this point, and should not be used. `self.team` and `self.name` _are_ set with correct information.
185187
"""
186-
pass
187188

188189
def retire(self):
189190
"""Called after the game ends"""
190-
pass
191191

192192
def get_outputs(
193193
self, game_tick_packet: flat.GameTickPacket

rlbot/managers/match.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def ensure_server_started(self, print_version_info: bool = True):
3939
self.main_executable_name
4040
)
4141
if self.rlbot_server_process is not None:
42-
self.logger.info(f"Already have {self.main_executable_name} running!")
42+
self.logger.info("Already have %s running!", self.main_executable_name)
4343
return
4444

4545
if self.main_executable_path is None:
@@ -52,7 +52,9 @@ def ensure_server_started(self, print_version_info: bool = True):
5252
self.rlbot_server_process = psutil.Process(rlbot_server_process.pid)
5353

5454
self.logger.info(
55-
f"Started {self.main_executable_name} with process id {self.rlbot_server_process.pid}"
55+
"Started %s with process id %s",
56+
self.main_executable_name,
57+
self.rlbot_server_process.pid,
5658
)
5759

5860
def _packet_reporter(self, packet: flat.GameTickPacket):
@@ -112,20 +114,22 @@ def shut_down(self, ensure_shutdown=True):
112114

113115
if self.rlbot_server_process is not None:
114116
self.logger.info(
115-
f"Waiting for {self.main_executable_name} to shut down..."
117+
"Waiting for %s to shut down...", self.main_executable_name
116118
)
117119

118120
if ensure_shutdown:
119121
if i == 1:
120122
self.rlbot_server_process.terminate()
121123
elif i == 4 or i == 7:
122124
self.logger.warning(
123-
f"{self.main_executable_name} is not responding to terminate requests."
125+
"%s is not responding to terminate requests.",
126+
self.main_executable_name,
124127
)
125128
self.rlbot_server_process.terminate()
126129
elif i >= 10 and i % 3 == 1:
127130
self.logger.error(
128-
f"{self.main_executable_name} is not responding, forcefully killing."
131+
"%s is not responding, forcefully killing.",
132+
self.main_executable_name,
129133
)
130134
self.rlbot_server_process.kill()
131135

rlbot/managers/script.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,3 @@ def handle_packet(self, packet: flat.GameTickPacket):
9292

9393
def retire(self):
9494
"""Called after the game ends"""
95-
pass

rlbot/utils/gateway.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def launch(
7272

7373
port = find_open_server_port()
7474
args = str(path) + " " + str(port)
75-
DEFAULT_LOGGER.info(f"Launching RLBotServer with via {args}")
75+
DEFAULT_LOGGER.info("Launching RLBotServer with via %s", args)
7676

7777
return subprocess.Popen(args, shell=True, cwd=directory), port
7878

@@ -97,7 +97,9 @@ def find_server_process(
9797
return proc, port
9898
except Exception as e:
9999
logger.error(
100-
f"Failed to read the name of a process while hunting for {main_executable_name}: {e}"
100+
"Failed to read the name of a process while hunting for %s: %s",
101+
main_executable_name,
102+
e,
101103
)
102104

103105
return None, RLBOT_SERVER_PORT

rlbot/utils/os_detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class OS(IntEnum):
2121
MAIN_EXECUTABLE_NAME = ""
2222
CURRENT_OS = OS.UNKNOWN
2323

24-
get_logger("os_detector").warn(f"Unknown OS: {unknown_os}")
24+
get_logger("os_detector").warning("Unknown OS: %s", unknown_os)

0 commit comments

Comments
 (0)