Skip to content

Commit 16665d5

Browse files
committed
Suppress internal sumo logging.
1 parent e2144e1 commit 16665d5

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

smarts/core/utils/sumo.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ def __init__(
8383
sumo_port: Optional[int],
8484
base_params: List[str],
8585
sumo_binary: str = "sumo", # Literal["sumo", "sumo-gui"]
86+
host: str = "localhost",
8687
):
8788
self._sumo_proc = None
8889
self._traci_conn = None
8990
self._sumo_port = None
9091
self._sumo_version: Tuple[int, ...] = tuple()
92+
self._host = host
93+
self._log = logging.Logger(self.__class__.__name__)
9194

9295
if sumo_port is None:
9396
sumo_port = networking.find_free_port()
@@ -98,7 +101,7 @@ def __init__(
98101
*base_params,
99102
]
100103

101-
logging.debug("Starting sumo process:\n\t %s", sumo_cmd)
104+
self._log.debug("Starting sumo process:\n\t %s", sumo_cmd)
102105
self._sumo_proc = subprocess.Popen(
103106
sumo_cmd,
104107
stdin=subprocess.PIPE,
@@ -124,22 +127,23 @@ def connect(
124127
"""Attempt a connection with the SUMO process."""
125128
traci_conn = None
126129
try:
127-
with suppress_output(stderr=not debug, stdout=False):
130+
with suppress_output(stderr=not debug, stdout=True):
128131
traci_conn = traci.connect(
129132
self._sumo_port,
133+
host=self._host,
130134
numRetries=max(0, int(20 * timeout)),
131135
proc=self._sumo_proc,
132136
waitBetweenRetries=0.05,
133137
) # SUMO must be ready within timeout seconds
134138
# We will retry since this is our first sumo command
135139
except traci.exceptions.FatalTraCIError:
136-
logging.debug("TraCI could not connect in time.")
140+
self._log.debug("TraCI could not connect in time.")
137141
raise
138142
except traci.exceptions.TraCIException:
139-
logging.error("SUMO process died.")
143+
self._log.warning("SUMO process died.")
140144
raise
141145
except ConnectionRefusedError:
142-
logging.error(
146+
self._log.warning(
143147
"Connection refused. Tried to connect to an unpaired TraCI client."
144148
)
145149
raise
@@ -155,8 +159,12 @@ def connect(
155159
) # e.g. "SUMO 1.11.0" -> (1, 11, 0)
156160
if self._sumo_version < minimum_sumo_version:
157161
raise OSError(f"SUMO version must be >= SUMO {minimum_sumo_version}")
158-
except traci.exceptions.FatalTraCIError as err:
159-
logging.debug("TraCI disconnected, process may have died.")
162+
except (traci.exceptions.FatalTraCIError, TypeError) as err:
163+
logging.error(
164+
"TraCI disconnected from '%s:%s', process may have died.",
165+
self._host,
166+
self._sumo_port,
167+
)
160168
# XXX: the error type is changed to TraCIException to make it consistent with the
161169
# process died case of `traci.connect`.
162170
raise traci.exceptions.TraCIException(err)
@@ -217,6 +225,7 @@ def __safe_close(conn):
217225
pass
218226

219227
if self._traci_conn:
228+
self._log.debug("Closing TraCI connection to %s", self._sumo_port)
220229
__safe_close(self._traci_conn)
221230

222231
if self._sumo_proc:

0 commit comments

Comments
 (0)