Sync desktop daily logs and fix emulator ADB upstream connections

This commit is contained in:
tongtong committed 2026-11-13 11:57:07 +08:00
1 parent 87e1fff59b
commit 0d269f3ded
15 files changed
+139 -12

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
using System.Text;
namespace Ads.Desktop;
public sealed class DailyLog
{
readonly string folder; readonly object gate = new(); DateTime cleanedDate;
public string Folder => folder;
public DailyLog(string path) => folder = path;
public void Write(string message)
{
lock (gate)
{
try
{
Directory.CreateDirectory(folder); var now = DateTime.Now; var name = $"ads-{now:yyyy-MM-dd}";
if (cleanedDate != now.Date)
{
foreach (var file in Directory.EnumerateFiles(folder, "ads-*.log"))
{
var text = Path.GetFileName(file);
if (System.Text.RegularExpressions.Regex.IsMatch(text, @"^ads-\d{4}-\d{2}-\d{2}(\.[1-4])?\.log$") && DateTime.TryParseExact(text.Substring(4, 10), "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out var date) && date < now.Date.AddDays(-13)) File.Delete(file);
}
cleanedDate = now.Date;
}
var path = Path.Combine(folder, name + ".log");
if (File.Exists(path) && new FileInfo(path).Length >= 5 * 1024 * 1024)
{
var oldest = Path.Combine(folder, name + ".4.log"); if (File.Exists(oldest)) File.Delete(oldest);
for (var i = 3; i >= 1; i--) { var old = Path.Combine(folder, $"{name}.{i}.log"); if (File.Exists(old)) File.Move(old, Path.Combine(folder, $"{name}.{i + 1}.log")); }
File.Move(path, Path.Combine(folder, name + ".1.log"));
}
if (message.Length > 16384) message = message[..16384] + " [truncated]";
File.AppendAllText(path, $"{now:O} {message}\n", Encoding.UTF8);
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
}
}
+8 -3
View File
@@ -8,8 +8,9 @@ using CommunityToolkit.Mvvm.Input;
namespace Ads.Desktop;
public sealed record DeviceItem(string Id, bool Busy, bool IsLocal, bool Online = true, string LocalSummary = "")
public sealed record DeviceItem(string Id, bool Busy, bool IsLocal, bool Online = true, string LocalSummary = "", string ComputerName="", string AgentId="")
{
public string ComputerLabel => "电脑:"+(ComputerName.Length==0?"未上报电脑名":ComputerName)+(AgentId.Length==0?"":" · "+AgentId);
public string Description => IsLocal ? "本机 · " + LocalSummary : !Online ? "远程暂时离线" : Busy ? "在线 · 其他客户端使用中" : "在线 · 可连接";
}
@@ -143,9 +144,13 @@ public partial class MainViewModel : ObservableObject, IDisposable
return text;
}
private DailyLog? dailyLog;
public string LogFolder => Path.Combine(folder,"logs");
[RelayCommand] private void OpenLogFolder(){try{Directory.CreateDirectory(LogFolder);Process.Start(new ProcessStartInfo(LogFolder){UseShellExecute=true});}catch(Exception error){Report(error);}}
private void Log(string message)
{
var line = $"{DateTime.Now:HH:mm:ss} {Redact(message)}";
var safe=Redact(message);(dailyLog ??= new DailyLog(LogFolder)).Write(safe);
var line = $"{DateTime.Now:HH:mm:ss} {safe}";
void Append() { if (disposed) return; Logs.Add(line); while (Logs.Count > 1000) Logs.RemoveAt(0); }
if (Dispatcher.UIThread.CheckAccess()) Append(); else Dispatcher.UIThread.Post(Append);
}
@@ -202,7 +207,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
Devices.Clear();
foreach (var row in rows) {
var connection = sessions.GetValueOrDefault(row.Id);
Devices.Add(new DeviceItem(row.Id, row.Busy || connection?.HasConnection == true, connection?.HasConnection == true, true, connection?.Status ?? ""));
Devices.Add(new DeviceItem(row.Id, row.Busy || connection?.HasConnection == true, connection?.HasConnection == true, true, connection?.Status ?? "",row.ComputerName,row.AgentId));
}
foreach (var local in Connections.Where(c => rows.All(r => r.Id != c.DeviceId)))
Devices.Add(new DeviceItem(local.DeviceId, true, true, false, local.Status));
+3 -2
View File
@@ -16,6 +16,7 @@
<TextBlock Text="连接远程手机,在本机调试与控制" Classes="label"/>
</StackPanel>
<StackPanel Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Spacing="8">
<Border Height="3" HorizontalAlignment="Stretch"><ProgressBar IsIndeterminate="True" IsVisible="{Binding IsRefreshing}" Height="3"/></Border>
<TextBlock x:Name="RelayStatusText" Text="{Binding RelayStatus}" HorizontalAlignment="Right" Foreground="#66D9C0"/>
<StackPanel Orientation="Horizontal"><Button x:Name="RefreshButton" Content="刷新设备" Command="{Binding RefreshCommand}"/><Button Content="保存配置" Command="{Binding SaveCommand}"/></StackPanel>
</StackPanel>
@@ -27,7 +28,7 @@
<Grid RowDefinitions="Auto,*,Auto,Auto,Auto">
<TextBlock Text="在线设备" Classes="section"/>
<ListBox x:Name="DeviceList" Grid.Row="1" ItemsSource="{Binding Devices}" SelectedItem="{Binding SelectedDevice}" Margin="0,14,0,12" Background="Transparent">
<ListBox.ItemTemplate><DataTemplate><StackPanel Margin="5,10" Spacing="6"><TextBlock Text="{Binding Id}" FontSize="17" TextWrapping="Wrap"/><TextBlock Text="{Binding Description}" Classes="label" TextWrapping="Wrap"/></StackPanel></DataTemplate></ListBox.ItemTemplate>
<ListBox.ItemTemplate><DataTemplate><StackPanel Margin="5,10" Spacing="6"><TextBlock Text="{Binding Id}" FontSize="17" TextWrapping="Wrap"/><TextBlock Text="{Binding ComputerLabel}" Classes="label" TextWrapping="Wrap"/><TextBlock Text="{Binding Description}" Classes="label" TextWrapping="Wrap"/></StackPanel></DataTemplate></ListBox.ItemTemplate>
</ListBox>
<TextBlock Grid.Row="2" Text="{Binding ConnectionSummary}" Foreground="#66D9C0" Margin="0,0,0,8" TextWrapping="Wrap"/>
<TextBlock Grid.Row="3" Text="{Binding DeviceSummary}" Classes="label" TextWrapping="Wrap"/>
@@ -146,7 +147,7 @@
</TabItem>
<TabItem Header="运行日志">
<Grid RowDefinitions="Auto,*" Margin="0,16,0,0">
<StackPanel Orientation="Horizontal"><Button Content="清空日志" Command="{Binding ClearLogsCommand}"/><Button Content="导出日志" Command="{Binding ExportLogsCommand}"/></StackPanel>
<StackPanel Orientation="Horizontal"><Button Content="打开日志文件夹" Command="{Binding OpenLogFolderCommand}"/><Button Content="清空日志" Command="{Binding ClearLogsCommand}"/><Button Content="导出日志" Command="{Binding ExportLogsCommand}"/></StackPanel>
<ListBox Grid.Row="1" ItemsSource="{Binding Logs}" Margin="0,8,0,0" FontFamily="Cascadia Mono,Consolas" FontSize="12"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding}" TextWrapping="Wrap"/></DataTemplate></ListBox.ItemTemplate></ListBox>
</Grid>
</TabItem>
+2 -2
View File
@@ -48,7 +48,7 @@ public sealed record RelayOptions(Uri BaseUri, string Origin, string Token)
}.Uri;
}
public sealed record RelayDevice(string Id, bool Busy);
public sealed record RelayDevice(string Id, bool Busy, [property:System.Text.Json.Serialization.JsonPropertyName("agent_id")] string AgentId="", [property:System.Text.Json.Serialization.JsonPropertyName("computer_name")] string ComputerName="");
public sealed record RelayNode(string Id, bool Healthy, int Sessions, int Capacity)
{
@@ -111,6 +111,6 @@ public static class RelayApi
if (devices.Any(d => string.IsNullOrWhiteSpace(d.Id) || d.Id.Length > 128 ||
d.Id.Any(c => !char.IsAsciiLetterOrDigit(c) && c != '-' && c != '_' && c != '.')))
throw new InvalidOperationException("中继返回了无效的设备 ID。");
return devices.OrderBy(d => d.Id, StringComparer.Ordinal).ToArray();
return devices.OrderBy(d => d.ComputerName, StringComparer.OrdinalIgnoreCase).ThenBy(d => d.AgentId, StringComparer.Ordinal).ThenBy(d => d.Id, StringComparer.Ordinal).ToArray();
}
}