Size: 1.8 KB Modified: 27/06/2026 11:00 AM
param(
    [int]$Seconds = 60,
    [string]$PackageName = "com.raymond.kaichat",
    [string]$OutputDirectory = (Join-Path $PSScriptRoot "..\logs")
)

$ErrorActionPreference = "Stop"

$androidHome = $env:ANDROID_HOME
if ([string]::IsNullOrWhiteSpace($androidHome)) {
    $androidHome = [Environment]::GetEnvironmentVariable("ANDROID_HOME", "User")
}

if ([string]::IsNullOrWhiteSpace($androidHome)) {
    throw "ANDROID_HOME is not set. Install the Android SDK or set ANDROID_HOME first."
}

$adb = Join-Path $androidHome "platform-tools\adb.exe"
if (-not (Test-Path -LiteralPath $adb)) {
    throw "adb.exe was not found at $adb."
}

New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null

$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$fullLog = Join-Path $OutputDirectory "kaichat-android-$stamp.log"
$filteredLog = Join-Path $OutputDirectory "kaichat-android-$stamp.crash.log"
$errorLog = Join-Path $OutputDirectory "kaichat-android-$stamp.adb.err.log"

& $adb devices -l
& $adb logcat -c

Write-Host "Logcat cleared. Open KaiChat on the Android device now."
Write-Host "Capturing for $Seconds seconds..."

$process = Start-Process `
    -FilePath $adb `
    -ArgumentList @("logcat", "-v", "time") `
    -RedirectStandardOutput $fullLog `
    -RedirectStandardError $errorLog `
    -WindowStyle Hidden `
    -PassThru

try {
    Start-Sleep -Seconds $Seconds
}
finally {
    if (-not $process.HasExited) {
        Stop-Process -Id $process.Id -Force
    }
}

$patterns = @(
    $PackageName,
    "AndroidRuntime",
    "FATAL EXCEPTION",
    "mono",
    "DOTNET",
    "Microsoft.Maui",
    "System.",
    "KaiChat"
)

Select-String -Path $fullLog -Pattern $patterns -SimpleMatch |
    ForEach-Object { $_.Line } |
    Set-Content -Path $filteredLog -Encoding UTF8

Write-Host "Full log: $fullLog"
Write-Host "Filtered crash log: $filteredLog"
Offline