mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Map go2rtc log levels to Python log levels (#129894)
This commit is contained in:
parent
b76a94bd42
commit
e562b6f42b
@ -39,6 +39,16 @@ webrtc:
|
|||||||
ice_servers: []
|
ice_servers: []
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
_LOG_LEVEL_MAP = {
|
||||||
|
"TRC": logging.DEBUG,
|
||||||
|
"DBG": logging.DEBUG,
|
||||||
|
"INF": logging.DEBUG,
|
||||||
|
"WRN": logging.WARNING,
|
||||||
|
"ERR": logging.WARNING,
|
||||||
|
"FTL": logging.ERROR,
|
||||||
|
"PNC": logging.ERROR,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Go2RTCServerStartError(HomeAssistantError):
|
class Go2RTCServerStartError(HomeAssistantError):
|
||||||
"""Raised when server does not start."""
|
"""Raised when server does not start."""
|
||||||
@ -132,7 +142,10 @@ class Server:
|
|||||||
async for line in process.stdout:
|
async for line in process.stdout:
|
||||||
msg = line[:-1].decode().strip()
|
msg = line[:-1].decode().strip()
|
||||||
self._log_buffer.append(msg)
|
self._log_buffer.append(msg)
|
||||||
_LOGGER.debug(msg)
|
loglevel = logging.WARNING
|
||||||
|
if len(split_msg := msg.split(" ", 2)) == 3:
|
||||||
|
loglevel = _LOG_LEVEL_MAP.get(split_msg[1], loglevel)
|
||||||
|
_LOGGER.log(loglevel, msg)
|
||||||
if not self._startup_complete.is_set() and _SUCCESSFUL_BOOT_MESSAGE in msg:
|
if not self._startup_complete.is_set() and _SUCCESSFUL_BOOT_MESSAGE in msg:
|
||||||
self._startup_complete.set()
|
self._startup_complete.set()
|
||||||
|
|
||||||
|
@ -195,6 +195,75 @@ async def test_server_failed_to_start(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("server_stdout", "expected_loglevel"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
[
|
||||||
|
"09:00:03.466 TRC [api] register path path=/",
|
||||||
|
"09:00:03.466 DBG build vcs.time=2024-10-28T19:47:55Z version=go1.23.2",
|
||||||
|
"09:00:03.466 INF go2rtc platform=linux/amd64 revision=780f378 version=1.9.5",
|
||||||
|
"09:00:03.467 INF [api] listen addr=127.0.0.1:1984",
|
||||||
|
"09:00:03.466 WRN warning message",
|
||||||
|
'09:00:03.466 ERR [api] listen error="listen tcp 127.0.0.1:11984: bind: address already in use"',
|
||||||
|
"09:00:03.466 FTL fatal message",
|
||||||
|
"09:00:03.466 PNC panic message",
|
||||||
|
"exit with signal: interrupt", # Example of stderr write
|
||||||
|
],
|
||||||
|
[
|
||||||
|
logging.DEBUG,
|
||||||
|
logging.DEBUG,
|
||||||
|
logging.DEBUG,
|
||||||
|
logging.DEBUG,
|
||||||
|
logging.WARNING,
|
||||||
|
logging.WARNING,
|
||||||
|
logging.ERROR,
|
||||||
|
logging.ERROR,
|
||||||
|
logging.WARNING,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@patch("homeassistant.components.go2rtc.server._RESPAWN_COOLDOWN", 0)
|
||||||
|
async def test_log_level_mapping(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_create_subprocess: MagicMock,
|
||||||
|
server_stdout: list[str],
|
||||||
|
rest_client: AsyncMock,
|
||||||
|
server: Server,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
expected_loglevel: list[int],
|
||||||
|
) -> None:
|
||||||
|
"""Log level mapping."""
|
||||||
|
evt = asyncio.Event()
|
||||||
|
|
||||||
|
async def wait_event() -> None:
|
||||||
|
await evt.wait()
|
||||||
|
|
||||||
|
mock_create_subprocess.return_value.wait.side_effect = wait_event
|
||||||
|
|
||||||
|
await server.start()
|
||||||
|
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Verify go2rtc binary stdout was logged with default level
|
||||||
|
for i, entry in enumerate(server_stdout):
|
||||||
|
assert (
|
||||||
|
"homeassistant.components.go2rtc.server",
|
||||||
|
expected_loglevel[i],
|
||||||
|
entry,
|
||||||
|
) in caplog.record_tuples
|
||||||
|
|
||||||
|
evt.set()
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert_server_output_logged(server_stdout, caplog, logging.WARNING)
|
||||||
|
|
||||||
|
await server.stop()
|
||||||
|
|
||||||
|
|
||||||
@patch("homeassistant.components.go2rtc.server._RESPAWN_COOLDOWN", 0)
|
@patch("homeassistant.components.go2rtc.server._RESPAWN_COOLDOWN", 0)
|
||||||
async def test_server_restart_process_exit(
|
async def test_server_restart_process_exit(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user