Skip to content

Instantly share code, notes, and snippets.

@Redth
Created June 25, 2013 20:35
Show Gist options
  • Save Redth/5862119 to your computer and use it in GitHub Desktop.
Save Redth/5862119 to your computer and use it in GitHub Desktop.
Collect Android Logs and Version info in Xamarin.Android
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.IO;
using System.Threading.Tasks;
using Java.Util.Regex;
using SharpRaven;
namespace HyperLocal.Demo.Xamdroid
{
public class LogCollector
{
public string GetAndroidVersionNumber()
{
var codeName = string.Empty;
var sdkName = string.Empty;
var sdkNum = string.Empty;
try { codeName = Android.OS.Build.VERSION.Codename; }
catch { }
try { sdkName = Android.OS.Build.VERSION.Sdk; }
catch { }
try { sdkNum = Android.OS.Build.VERSION.SdkInt.ToString(); }
catch { }
return string.Format("{0} ({1}) [{2}]", sdkName, sdkNum, codeName);
}
public string GetVersionNumber(Context context)
{
var version = "?";
try {
var pkgInfo = context.PackageManager.GetPackageInfo (context.PackageName, (Android.Content.PM.PackageInfoFlags)0);
version = pkgInfo.VersionName + " (" + pkgInfo.VersionCode + ")";
}
catch { }
return version;
}
public string CollectLogs()
{
var log = new StringBuilder ();
var cmdLine = new List<string> ();
cmdLine.Add ("logcat");
cmdLine.Add ("-d");
var p = Java.Lang.Runtime.GetRuntime ().Exec (cmdLine.ToArray ());
var reader = new BufferedReader (new InputStreamReader (p.InputStream));
string line;
while ((line = reader.ReadLine()) != null)
{
log.AppendLine (line.Trim ());
}
return log.ToString();
}
public string GetKernelVersion()
{
string procVersionStr;
try {
BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
try {
procVersionStr = reader.ReadLine();
} finally {
reader.Close();
}
var PROC_VERSION_REGEX =
"\\w+\\s+" + /* ignore: Linux */
"\\w+\\s+" + /* ignore: version */
"([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
"\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
"\\([^)]+\\)\\s+" + /* ignore: (gcc ..) */
"([^\\s]+)\\s+" + /* group 3: #26 */
"(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
"(.+)"; /* group 4: date */
var p = Java.Util.Regex.Pattern.Compile(PROC_VERSION_REGEX);
Matcher m = p.Matcher(procVersionStr);
if (!m.Matches()) {
//Log.e(TAG, "Regex did not match on /proc/version: " + procVersionStr);
return "Unavailable";
} else if (m.GroupCount() < 4) {
//Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
// + " groups");
return "Unavailable";
} else {
return (new StringBuilder(m.Group(1)).Append("\n").Append(
m.Group(2)).Append(" ").Append(m.Group(3)).Append("\n")
.Append(m.Group(4))).ToString();
}
} catch {
return "Unavailable";
}
}
string GetBody(Context context)
{
var body = new StringBuilder ();
body.AppendLine ("Log Report");
body.AppendLine ("App Version: " + GetVersionNumber (context));
body.AppendLine ("Android Version: " + GetAndroidVersionNumber ());
body.AppendLine ("Kernel Version: " + GetKernelVersion ());
body.AppendLine ();
body.AppendLine (CollectLogs ());
return body.ToString ();
}
public void SendEmailIntent(Context context, string emailTo, string introMsg)
{
var body = new StringBuilder ();
body.AppendLine(introMsg);
body.AppendLine ();
body.AppendLine ("Log Report:");
body.AppendLine ("=====================================");
body.AppendLine(GetBody(context));
var emailIntent = new Intent (Android.Content.Intent.ActionSend);
emailIntent.PutExtra (Android.Content.Intent.ExtraEmail, emailTo);
emailIntent.PutExtra(Android.Content.Intent.ExtraSubject, "HyperLocal Log Report");
emailIntent.SetType("plain/text");
emailIntent.PutExtra(Android.Content.Intent.ExtraText, body.ToString());
context.StartActivity(emailIntent);
}
public void LogToRaven(Context context)
{
try
{
var ravenClient = new RavenClient("YOUR_RAVEN_CREDENTIALS");
ravenClient.CaptureException(new Exception(GetBody(context)));
}
catch (Exception ex)
{
System.Console.WriteLine ("LogToRaven Failed: " + ex.ToString ());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment