feat: 设置新增服务器日志页(SFTP 读取 logs 目录,支持自动刷新与跟随底部)
This commit is contained in:
1 parent
71e76b1c2b
commit
eb2f9e9ea7
4 files changed
+416
No files matched your search
@@ -395,6 +395,16 @@ class S {
|
|||||||
'视频': 'Video',
|
'视频': 'Video',
|
||||||
'图片': 'Image',
|
'图片': 'Image',
|
||||||
'连接凭据已加密保存,仅存于本机': 'Credentials are encrypted and stored on this device only',
|
'连接凭据已加密保存,仅存于本机': 'Credentials are encrypted and stored on this device only',
|
||||||
|
'服务器日志': 'Server Logs',
|
||||||
|
'通过 SSH 查看服务器 logs 目录日志输出': 'View server logs directory output via SSH',
|
||||||
|
'请先在「服务器」页配置 SSH 连接': 'Configure an SSH connection on the Servers page first',
|
||||||
|
'选择服务器(需开启 SSH)': 'Select server (SSH required)',
|
||||||
|
'日志目录路径': 'Logs directory path',
|
||||||
|
'例如 /root/logs': 'e.g. /root/logs',
|
||||||
|
'自动刷新(每 5 秒)': 'Auto refresh (every 5s)',
|
||||||
|
'目录下没有日志文件': 'No log files in this directory',
|
||||||
|
'读取失败:{0}': 'Failed to read: {0}',
|
||||||
|
'自动刷新并跟随底部(每 5 秒)': 'Auto refresh & follow bottom (every 5s)',
|
||||||
};
|
};
|
||||||
|
|
||||||
static String t(String zh) => en ? (_en[zh] ?? zh) : zh;
|
static String t(String zh) => en ? (_en[zh] ?? zh) : zh;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:convert';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'package:dartssh2/dartssh2.dart';
|
import 'package:dartssh2/dartssh2.dart';
|
||||||
import 'dart:io' as io;
|
import 'dart:io' as io;
|
||||||
@@ -83,6 +84,31 @@ class SftpService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 读取远程文件文本内容(保留末尾 maxBytes 字节,适合大日志文件)。
|
||||||
|
Future<String> readString(
|
||||||
|
String remotePath, {
|
||||||
|
int maxBytes = 2 * 1024 * 1024,
|
||||||
|
}) async {
|
||||||
|
final sftp = _requireSftp();
|
||||||
|
final file = await sftp.open(remotePath, mode: SftpFileOpenMode.read);
|
||||||
|
try {
|
||||||
|
final buf = BytesBuilder();
|
||||||
|
await for (final chunk in file.read()) {
|
||||||
|
buf.add(chunk);
|
||||||
|
}
|
||||||
|
final bytes = buf.takeBytes();
|
||||||
|
if (bytes.length <= maxBytes) return utf8.decode(bytes, allowMalformed: true);
|
||||||
|
// 只保留末尾 maxBytes,并跳过半个字符
|
||||||
|
var start = bytes.length - maxBytes;
|
||||||
|
while (start < bytes.length && (bytes[start] & 0xC0) == 0x80) {
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
return utf8.decode(bytes.sublist(start), allowMalformed: true);
|
||||||
|
} finally {
|
||||||
|
await file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> delete(String remotePath) async {
|
Future<void> delete(String remotePath) async {
|
||||||
await _requireSftp().remove(remotePath);
|
await _requireSftp().remove(remotePath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,366 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import '../../core/services/sftp_service.dart';
|
||||||
|
import '../../core/l10n/strings.dart';
|
||||||
|
import '../../providers/server_provider.dart';
|
||||||
|
import '../dashboard/server_picker_bar.dart';
|
||||||
|
|
||||||
|
/// 服务器日志页(设置内入口):通过 SFTP 读取服务器 logs 目录,
|
||||||
|
/// 列出日志文件并查看内容(默认读取末尾 2MB,支持自动刷新)。
|
||||||
|
class ServerLogPage extends StatefulWidget {
|
||||||
|
const ServerLogPage({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<ServerLogPage> createState() => _ServerLogPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ServerLogPageState extends State<ServerLogPage> {
|
||||||
|
final _sftp = SftpService();
|
||||||
|
final _pathCtrl = TextEditingController();
|
||||||
|
Timer? _autoTimer;
|
||||||
|
|
||||||
|
bool _loading = false;
|
||||||
|
String? _error;
|
||||||
|
List<_LogFile> _files = [];
|
||||||
|
bool _autoRefresh = false;
|
||||||
|
|
||||||
|
ServerConfig? _server;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
final holder = context.read<SelectedServerHolder>();
|
||||||
|
_server = holder.server;
|
||||||
|
_pathCtrl.text = _defaultLogsDir(_server);
|
||||||
|
holder.addListener(_onHolderChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_autoTimer?.cancel();
|
||||||
|
context.read<SelectedServerHolder>().removeListener(_onHolderChanged);
|
||||||
|
_pathCtrl.dispose();
|
||||||
|
_sftp.disconnect();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onHolderChanged() {
|
||||||
|
if (!mounted) return;
|
||||||
|
final s = context.read<SelectedServerHolder>().server;
|
||||||
|
if (s?.id != _server?.id) {
|
||||||
|
setState(() {
|
||||||
|
_server = s;
|
||||||
|
_files = [];
|
||||||
|
_error = null;
|
||||||
|
_pathCtrl.text = _defaultLogsDir(s);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _defaultLogsDir(ServerConfig? s) {
|
||||||
|
if (s == null) return '';
|
||||||
|
final user = s.sshUser;
|
||||||
|
final home = user == 'root' ? '/root' : '/home/$user';
|
||||||
|
return '$home/logs';
|
||||||
|
}
|
||||||
|
|
||||||
|
String get _dir => _pathCtrl.text.trim().isEmpty
|
||||||
|
? _defaultLogsDir(_server)
|
||||||
|
: _pathCtrl.text.trim();
|
||||||
|
|
||||||
|
Future<void> _refresh() async {
|
||||||
|
final s = _server;
|
||||||
|
if (s == null || !s.hasSsh) {
|
||||||
|
setState(() => _error = S.t('请先在「服务器」页配置 SSH 连接'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_loading = true;
|
||||||
|
_error = null;
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await _sftp.connect(s);
|
||||||
|
final items = await _sftp.listDir(_dir);
|
||||||
|
final files = <_LogFile>[];
|
||||||
|
for (final it in items) {
|
||||||
|
if (it.attr.isDirectory) continue;
|
||||||
|
files.add(_LogFile(
|
||||||
|
name: it.filename,
|
||||||
|
size: it.attr.size ?? 0,
|
||||||
|
mtime: DateTime.fromMillisecondsSinceEpoch(
|
||||||
|
(it.attr.modifyTime ?? 0) * 1000),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
files.sort((a, b) => b.mtime.compareTo(a.mtime));
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() => _files = files);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() => _error = S.f('加载失败:{0}', [e.toString()]));
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _loading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _toggleAuto(bool v) {
|
||||||
|
setState(() => _autoRefresh = v);
|
||||||
|
_autoTimer?.cancel();
|
||||||
|
if (v) {
|
||||||
|
_autoTimer = Timer.periodic(const Duration(seconds: 5), (_) => _refresh());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _viewFile(_LogFile f) async {
|
||||||
|
final s = _server;
|
||||||
|
if (s == null || !s.hasSsh) return;
|
||||||
|
setState(() => _loading = true);
|
||||||
|
try {
|
||||||
|
await _sftp.connect(s);
|
||||||
|
final content = await _sftp.readString('$_dir/${f.name}');
|
||||||
|
if (!mounted) return;
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (_) => _LogViewerPage(
|
||||||
|
title: f.name,
|
||||||
|
content: content,
|
||||||
|
sftp: _sftp,
|
||||||
|
remotePath: '$_dir/${f.name}',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text(S.f('读取失败:{0}', [e.toString()]))),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _loading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _fmtSize(int b) {
|
||||||
|
if (b < 1024) return '$b B';
|
||||||
|
if (b < 1024 * 1024) return '${(b / 1024).toStringAsFixed(1)} KB';
|
||||||
|
return '${(b / 1024 / 1024).toStringAsFixed(1)} MB';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final servers = context.watch<ServerProvider>().servers;
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: T('服务器日志')),
|
||||||
|
body: Column(
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
|
||||||
|
child: DropdownButtonFormField<ServerConfig>(
|
||||||
|
value: _server != null && servers.any((s) => s.id == _server!.id)
|
||||||
|
? _server
|
||||||
|
: null,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: S.t('选择服务器(需开启 SSH)'),
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
items: servers
|
||||||
|
.map((s) => DropdownMenuItem(value: s, child: Text(s.name)))
|
||||||
|
.toList(),
|
||||||
|
onChanged: (s) {
|
||||||
|
setState(() {
|
||||||
|
_server = s;
|
||||||
|
_files = [];
|
||||||
|
_error = null;
|
||||||
|
_pathCtrl.text = _defaultLogsDir(s);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: TextField(
|
||||||
|
controller: _pathCtrl,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: S.t('日志目录路径'),
|
||||||
|
hintText: S.t('例如 /root/logs'),
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
IconButton.filled(
|
||||||
|
onPressed: _loading ? null : _refresh,
|
||||||
|
tooltip: S.t('刷新'),
|
||||||
|
icon: const Icon(Icons.refresh),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SwitchListTile(
|
||||||
|
title: T('自动刷新(每 5 秒)'),
|
||||||
|
value: _autoRefresh,
|
||||||
|
onChanged: _toggleAuto,
|
||||||
|
),
|
||||||
|
const Divider(height: 1),
|
||||||
|
Expanded(child: _buildList()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildList() {
|
||||||
|
if (_loading && _files.isEmpty) {
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
if (_error != null) {
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Text(_error!, textAlign: TextAlign.center),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
FilledButton(onPressed: _refresh, child: T('重试')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (_files.isEmpty) {
|
||||||
|
return Center(child: T('目录下没有日志文件'));
|
||||||
|
}
|
||||||
|
return RefreshIndicator(
|
||||||
|
onRefresh: _refresh,
|
||||||
|
child: ListView.builder(
|
||||||
|
physics: const AlwaysScrollableScrollPhysics(),
|
||||||
|
itemCount: _files.length,
|
||||||
|
itemBuilder: (_, i) {
|
||||||
|
final f = _files[i];
|
||||||
|
return ListTile(
|
||||||
|
leading: const Icon(Icons.description_outlined),
|
||||||
|
title: Text(f.name, overflow: TextOverflow.ellipsis),
|
||||||
|
subtitle: Text(
|
||||||
|
'${_fmtSize(f.size)} · ${f.mtime.toLocal()}',
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
|
trailing: const Icon(Icons.chevron_right),
|
||||||
|
onTap: () => _viewFile(f),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LogFile {
|
||||||
|
final String name;
|
||||||
|
final int size;
|
||||||
|
final DateTime mtime;
|
||||||
|
_LogFile({required this.name, required this.size, required this.mtime});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 单个日志文件内容查看页。
|
||||||
|
class _LogViewerPage extends StatefulWidget {
|
||||||
|
final String title;
|
||||||
|
final String content;
|
||||||
|
final SftpService sftp;
|
||||||
|
final String remotePath;
|
||||||
|
|
||||||
|
const _LogViewerPage({
|
||||||
|
required this.title,
|
||||||
|
required this.content,
|
||||||
|
required this.sftp,
|
||||||
|
required this.remotePath,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_LogViewerPage> createState() => _LogViewerPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LogViewerPageState extends State<_LogViewerPage> {
|
||||||
|
final _controller = ScrollController();
|
||||||
|
Timer? _autoTimer;
|
||||||
|
bool _autoRefresh = false;
|
||||||
|
bool _following = true;
|
||||||
|
late String _content;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_content = widget.content;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) => _jumpBottom());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_autoTimer?.cancel();
|
||||||
|
_controller.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _jumpBottom() {
|
||||||
|
if (_controller.hasClients) {
|
||||||
|
_controller.jumpTo(_controller.position.maxScrollExtent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _reload() async {
|
||||||
|
try {
|
||||||
|
final c = await widget.sftp.readString(widget.remotePath);
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() => _content = c);
|
||||||
|
if (_following) WidgetsBinding.instance.addPostFrameCallback((_) => _jumpBottom());
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _toggleAuto(bool v) {
|
||||||
|
setState(() => _autoRefresh = v);
|
||||||
|
_autoTimer?.cancel();
|
||||||
|
if (v) {
|
||||||
|
_autoTimer = Timer.periodic(const Duration(seconds: 5), (_) => _reload());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final lines = _content.split('\n');
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text(widget.title, overflow: TextOverflow.ellipsis),
|
||||||
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
tooltip: S.t('刷新'),
|
||||||
|
icon: const Icon(Icons.refresh),
|
||||||
|
onPressed: _reload,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
bottomNavigationBar: SwitchListTile(
|
||||||
|
title: T('自动刷新并跟随底部(每 5 秒)'),
|
||||||
|
value: _autoRefresh,
|
||||||
|
onChanged: _toggleAuto,
|
||||||
|
),
|
||||||
|
body: NotificationListener<ScrollNotification>(
|
||||||
|
onNotification: (n) {
|
||||||
|
if (n is UserScrollNotification) {
|
||||||
|
_following = n.metrics.pixels >=
|
||||||
|
n.metrics.maxScrollExtent - 24;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
controller: _controller,
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
child: SelectableText(
|
||||||
|
lines.join('\n'),
|
||||||
|
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import '../../core/widgets/background_widget.dart';
|
|||||||
import '../../providers/theme_provider.dart';
|
import '../../providers/theme_provider.dart';
|
||||||
import '../../providers/wallpaper_provider.dart';
|
import '../../providers/wallpaper_provider.dart';
|
||||||
import 'help_page.dart';
|
import 'help_page.dart';
|
||||||
|
import 'server_log_page.dart';
|
||||||
import 'download_settings_page.dart';
|
import 'download_settings_page.dart';
|
||||||
import '../../providers/language_provider.dart';
|
import '../../providers/language_provider.dart';
|
||||||
import '../../core/l10n/strings.dart';
|
import '../../core/l10n/strings.dart';
|
||||||
@@ -331,6 +332,19 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
),
|
),
|
||||||
const Divider(),
|
const Divider(),
|
||||||
|
|
||||||
|
// ---------- 服务器日志 ----------
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.receipt_long_outlined),
|
||||||
|
title: T('服务器日志'),
|
||||||
|
subtitle: T('通过 SSH 查看服务器 logs 目录日志输出'),
|
||||||
|
trailing: const Icon(Icons.chevron_right),
|
||||||
|
onTap: () => Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(builder: (_) => const ServerLogPage()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Divider(),
|
||||||
|
|
||||||
// ---------- 使用教程 ----------
|
// ---------- 使用教程 ----------
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.menu_book_outlined),
|
leading: const Icon(Icons.menu_book_outlined),
|
||||||
|
|||||||
Reference in new issue
Block a user