Add LAN debugging and persistent logs
This commit is contained in:
1 parent
9cf29c11ab
commit
9153bc839e
4 files changed
+364
-16
No files matched your search
+45
-12
@@ -30,9 +30,25 @@ internal sealed class MainWindow : Window
|
||||
private Window? settingsWindow;
|
||||
private Window? outputWindow;
|
||||
private Window? newEmulatorWindow;
|
||||
private LanDebugPanel? lanDebug;
|
||||
private DispatcherTimer? statusTimer;
|
||||
private bool shuttingDown;
|
||||
private CancellationTokenSource? active;
|
||||
private static readonly string LogPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "emulator-manager.log");
|
||||
|
||||
private static void WriteLogFile(string line)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(LogPath) && new FileInfo(LogPath).Length > 5 * 1024 * 1024)
|
||||
File.Move(LogPath, LogPath + ".1", true);
|
||||
File.AppendAllText(LogPath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {line}{Environment.NewLine}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal MainWindow(bool smoke, Action<int> shutdown)
|
||||
{
|
||||
@@ -150,6 +166,7 @@ internal sealed class MainWindow : Window
|
||||
{
|
||||
allInstances = await ReadList(token, EmulatorCli.Instances, "-list", "-details");
|
||||
ApplyCategoryFilter();
|
||||
lanDebug?.Refresh();
|
||||
AppendOutput($"已读取 {allInstances.Length} 个模拟器实例。");
|
||||
}
|
||||
|
||||
@@ -166,6 +183,7 @@ internal sealed class MainWindow : Window
|
||||
return;
|
||||
allInstances = items;
|
||||
ApplyCategoryFilter();
|
||||
lanDebug?.Refresh();
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -201,23 +219,16 @@ internal sealed class MainWindow : Window
|
||||
if (!starting.Add((path, target)))
|
||||
throw new InvalidOperationException($"「{target}」已有启动进程。");
|
||||
string[] args = ["-start", target, .. extra];
|
||||
AppendOutput($"正在启动「{target}」。可刷新列表查看状态,也可直接停止实例。");
|
||||
_ = ObserveStart(path, target, args);
|
||||
}
|
||||
|
||||
private async Task ObserveStart(string path, string target, string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
await EmulatorCli.RunAsync(path, args, AppendOutput, CancellationToken.None);
|
||||
EmulatorCli.StartDetached(path, args, AppendOutput);
|
||||
AppendOutput($"「{target}」已独立启动,可刷新列表查看状态,也可直接停止实例。");
|
||||
_ = Task.Delay(TimeSpan.FromSeconds(5)).ContinueWith(_ => starting.Remove((path, target)));
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
AppendOutput($"「{target}」启动失败:{error.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
starting.Remove((path, target));
|
||||
throw new InvalidOperationException($"「{target}」启动失败:{error.Message}", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +241,7 @@ internal sealed class MainWindow : Window
|
||||
}
|
||||
else
|
||||
{
|
||||
StartInstance(instance.Name, []);
|
||||
StartInstance(instance.Name, ["-bootMode", "snapshot"]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,6 +302,7 @@ internal sealed class MainWindow : Window
|
||||
|
||||
private void AppendOutput(string line) => Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
WriteLogFile(line);
|
||||
if (output.ItemCount >= 1500)
|
||||
output.Items.RemoveAt(0);
|
||||
output.Items.Add(line);
|
||||
@@ -401,6 +413,27 @@ internal sealed class MainWindow : Window
|
||||
await Run(token, "-license", "accept");
|
||||
}
|
||||
})));
|
||||
lanDebug ??= new LanDebugPanel(AppendOutput, () => allInstances);
|
||||
settings.Children.Add(Caption("局域网调试"));
|
||||
settings.Children.Add(lanDebug.Content);
|
||||
settings.Children.Add(Caption("日志"));
|
||||
settings.Children.Add(new TextBlock
|
||||
{
|
||||
Text = LogPath, FontSize = 12, Opacity = 0.78, TextWrapping = TextWrapping.Wrap
|
||||
});
|
||||
var openLog = new Button { Content = "打开日志文件" };
|
||||
openLog.Click += (_, _) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(new ProcessStartInfo(LogPath) { UseShellExecute = true });
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
AppendOutput(error.Message);
|
||||
}
|
||||
};
|
||||
settings.Children.Add(openLog);
|
||||
var content = new Border
|
||||
{
|
||||
Margin = new Thickness(16),
|
||||
|
||||
Reference in new issue
Block a user