Skip to content

Instantly share code, notes, and snippets.

@aaronoe
Last active April 2, 2020 19:52
Show Gist options
  • Select an option

  • Save aaronoe/428f9f554e51086fec1e839580139c8f to your computer and use it in GitHub Desktop.

Select an option

Save aaronoe/428f9f554e51086fec1e839580139c8f to your computer and use it in GitHub Desktop.
private static readonly Regex AppIdPattern = "fg=([0-z]{1,10}):\"com.example.yourpackagename\"".ToRegex();
private static readonly Regex TimeOnBatteryPattern = @"(Time on battery: .+?) \(".ToRegex();
private static BatteryStatistics ExtractBatteryStatsFromLog(string batteryLog)
{
var appId = AppIdPattern.Match(batteryLog).Groups[1].Value;
var wifiUsagePattern = $@"{appId}:[\s]+?(Wi-Fi network: .*?packets \d+ received, \d+ sent\))".ToRegex();
var wifiUsage = wifiUsagePattern.Match(batteryLog).Groups[1].Value;
var wifiUsageBreakdown = NetworkUsageBreakdown.CreateFromLogLine(wifiUsage);
var batteryUsagePattern = $@"Uid {appId}: (([0-9]*[.]?[0-9]+)* \(.*?\))".ToRegex();
var batteryUsage = batteryUsagePattern.Match(batteryLog).Groups[1].Value;
var batteryUsageBreakdown = BatteryUsageBreakdown.CreateFromLogLine(batteryUsage);
var batteryCapacityPattern = $@"Estimated power use \(mAh\):[\s\S\n\r]+?Capacity: (\d+)".ToRegex();
var batteryCapacity = batteryCapacityPattern.Match(batteryLog).Groups[1].Value;
var timeOnBattery = TimeOnBatteryPattern.Match(batteryLog).Groups[1].Value;
return new BatteryStatistics(appId, wifiUsageBreakdown, batteryUsageBreakdown,
GetTimeSpanFromLog(timeOnBattery), Int32.Parse(batteryCapacity));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment