-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LogCat integration to Android via an EventProcessor (#2926)
Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
- Loading branch information
1 parent
610f5fa
commit 88bfc29
Showing
7 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/Sentry/Platforms/Android/LogCatAttachmentEventProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using Sentry.Extensibility; | ||
using Runtime = Java.Lang.Runtime; | ||
|
||
namespace Sentry.Android; | ||
|
||
internal class LogCatAttachmentEventProcessor : ISentryEventProcessorWithHint | ||
{ | ||
private static bool SendLogcatLogs = true; | ||
private readonly LogCatIntegrationType _logCatIntegrationType; | ||
private readonly IDiagnosticLogger? _diagnosticLogger; | ||
private readonly int _maxLines; | ||
|
||
public LogCatAttachmentEventProcessor(IDiagnosticLogger? diagnosticLogger, LogCatIntegrationType logCatIntegrationType, int maxLines = 1000) | ||
{ | ||
if (maxLines <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(maxLines), "LogCatMaxLines must be greater than 0"); | ||
} | ||
|
||
_diagnosticLogger = diagnosticLogger; | ||
_logCatIntegrationType = logCatIntegrationType; | ||
_maxLines = maxLines; | ||
} | ||
|
||
public SentryEvent Process(SentryEvent @event, Hint hint) | ||
{ | ||
// If sending has failed once, we have to disable this feature to prevent infinite loops and to allow the SDK to work otherwise | ||
if (!SendLogcatLogs) | ||
{ | ||
return @event; | ||
} | ||
|
||
// The logcat command only works on Android API 23 and above | ||
if (!OperatingSystem.IsAndroidVersionAtLeast(23)) | ||
{ | ||
SendLogcatLogs = false; | ||
return @event; | ||
} | ||
|
||
// Just in case the setting got overridden at the scope level | ||
if (_logCatIntegrationType == LogCatIntegrationType.None) | ||
{ | ||
return @event; | ||
} | ||
|
||
try | ||
{ | ||
if (_logCatIntegrationType != LogCatIntegrationType.All && !@event.HasException()) | ||
{ | ||
return @event; | ||
} | ||
|
||
// Only send logcat logs if the event is unhandled if the integration is set to Unhandled | ||
if (_logCatIntegrationType == LogCatIntegrationType.Unhandled) | ||
{ | ||
if (!@event.HasTerminalException()) | ||
{ | ||
return @event; | ||
} | ||
} | ||
|
||
// We run the logcat command via the runtime | ||
var process = Runtime.GetRuntime()?.Exec($"logcat -t {_maxLines} *:I"); | ||
|
||
// Strangely enough, process.InputStream is the *output* of the command | ||
if (process?.InputStream is null) | ||
{ | ||
return @event; | ||
} | ||
|
||
// We write the logcat logs to a memory stream so we can attach it | ||
using var output = new MemoryStream(); | ||
|
||
process.InputStream.CopyTo(output); | ||
process.WaitFor(); | ||
|
||
// Flush the stream to make sure all data is written | ||
process.InputStream.Flush(); | ||
|
||
// Reset the position of the stream to the beginning so we can read it | ||
output.Seek(0, SeekOrigin.Begin); | ||
var bytes = output.ToArray(); | ||
|
||
hint.Attachments.Add(new Attachment(AttachmentType.Default, new ByteAttachmentContent(bytes), "logcat.log", "text/logcat")); | ||
|
||
//hint.AddAttachment($"{filesDir.Path}/{fileName}", AttachmentType.Default, "text/logcat"); | ||
|
||
return @event; | ||
} | ||
catch (Exception e) // Catch all exceptions to prevent crashing the app during logging | ||
{ | ||
// Disable the feature if it fails once | ||
SendLogcatLogs = false; | ||
|
||
// Log the failure to Sentry | ||
_diagnosticLogger?.LogError(e, "Failed to send logcat logs"); | ||
return @event; | ||
} | ||
} | ||
|
||
public SentryEvent Process(SentryEvent @event) | ||
{ | ||
return Process(@event, new Hint()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace Sentry.Android; | ||
|
||
/// <summary> | ||
/// Configures when to attach LogCat logs to events. | ||
/// </summary> | ||
public enum LogCatIntegrationType | ||
{ | ||
/// <summary> | ||
/// The LogCat integration is disabled. | ||
/// </summary> | ||
None, | ||
/// <summary> | ||
/// LogCat logs are attached to events only when the event is unhandled. | ||
/// </summary> | ||
Unhandled, | ||
|
||
/// <summary> | ||
/// LogCat logs are attached to events with an exception. | ||
/// </summary> | ||
Errors, | ||
|
||
/// <summary> | ||
/// LogCat logs are attached to all events. | ||
/// Use caution when enabling, as this may result in a lot of data being sent to Sentry | ||
/// and performance issues if the SDK generates a lot of events. | ||
/// </summary> | ||
All | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters