One-KVM/kvmd/apps/kvmd/logreader.py
mofeng-git 187c713424 refactor: 完善代码质量检查和修复系统
主要改进:
- 添加 make tox-local 本地代码质量检查支持
- 创建 check-code.sh 脚本支持独立工具执行
- 修复 51+ flake8 代码风格问题(未使用导入、行尾空格、注释格式等)
- 解决 pylint 变量命名和日志格式问题
- 重构 make_image 方法解决 too-many-statements 警告
- 添加类型注解和修复方法签名不匹配问题
- 统一代码风格规范(引号使用、空格格式等)

工具配置:
- 更新 tox.ini 支持 Python 3.10 本地环境
- 添加缺失的核心依赖包定义
- 完善 Makefile 构建系统集成
2025-08-20 19:25:57 +08:00

86 lines
3.8 KiB
Python

# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <mdevaev@gmail.com> #
# Copyright (C) 2023-2025 SilentWind <mofeng654321@hotmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import re
import asyncio
import time
from typing import AsyncGenerator
from xmlrpc.client import ServerProxy
us_systemd_journal = True
try:
import systemd.journal
except ImportError:
us_systemd_journal = False
# =====
class LogReader:
async def poll_log(self, seek: int, follow: bool) -> AsyncGenerator[dict, None]:
if us_systemd_journal:
reader = systemd.journal.Reader() # type: ignore
reader.this_boot()
# XXX: Из-за смены ID машины в bootconfig это не работает при первой загрузке.
# reader.this_machine()
reader.log_level(systemd.journal.LOG_DEBUG) # type: ignore
services = set(
service
for service in systemd.journal.Reader().query_unique("_SYSTEMD_UNIT") # type: ignore
if re.match(r"kvmd(-\w+)*\.service", service)
).union(["kvmd.service"])
for service in services:
reader.add_match(_SYSTEMD_UNIT=service)
if seek > 0:
reader.seek_realtime(float(time.time() - seek))
for entry in reader:
yield self.__entry_to_record(entry)
while follow:
entry = reader.get_next()
if entry:
yield self.__entry_to_record(entry)
else:
await asyncio.sleep(1)
else:
import supervisor.xmlrpc # pylint: disable=import-outside-toplevel
server_transport = supervisor.xmlrpc.SupervisorTransport(None, None, serverurl="unix:///tmp/supervisor.sock")
server = ServerProxy("http://127.0.0.1", transport=server_transport)
log_entries = server.supervisor.readLog(0, 0)
yield {
"dt": int(time.time()),
"service": "kvmd.service",
"msg": str(log_entries).rstrip()
}
def __entry_to_record(self, entry: dict) -> dict[str, dict]:
return {
"dt": entry["__REALTIME_TIMESTAMP"],
"service": entry["_SYSTEMD_UNIT"],
"msg": entry["MESSAGE"].rstrip(),
}