-
Notifications
You must be signed in to change notification settings - Fork 74
[Feat] [SDK-399] Capture logcat output as telemetry events #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
buongarzoni
wants to merge
9
commits into
master
Choose a base branch
from
feat/SDK-399/add-android-logs-telemetry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4fdb86b
feat(android): capture logcat output as telemetry events
buongarzoni b5f0407
fix: include verbosity logs for debug level
buongarzoni 858a713
fix(android): reset logcat capture state on unexpected process death
buongarzoni 0f13b1a
fix(android): skip logcat ring buffer replay on startup
buongarzoni e63b0af
fix(android): classify captured logcat entries as log telemetry type
buongarzoni b47bc62
fix(android): resolve test failure caused by missing Android stub def…
buongarzoni 41da663
fix(android): use Rollbar.TAG in ConnectivityDetector to suppress SDK…
buongarzoni ea3a83c
docs(android): correct captureLogsAsTelemetry javadoc to reference lo…
buongarzoni e8355f2
docs: fix stale dump() references in telemetry javadoc
buongarzoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
202 changes: 202 additions & 0 deletions
202
rollbar-android/src/main/java/com/rollbar/android/LogcatTelemetryCapture.java
This file contains hidden or 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,202 @@ | ||
| package com.rollbar.android; | ||
|
|
||
| import android.util.Log; | ||
|
|
||
| import com.rollbar.api.payload.data.Level; | ||
| import com.rollbar.api.payload.data.Source; | ||
| import com.rollbar.notifier.telemetry.TelemetryEventTracker; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.charset.Charset; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class LogcatTelemetryCapture { | ||
|
|
||
| // threadtime format: "MM-dd HH:mm:ss.SSS PID TID L Tag: message" | ||
| private static final Pattern LOGCAT_LINE_PATTERN = Pattern.compile( | ||
| "^\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\s+\\d+\\s+\\d+\\s+([VDIWEF])\\s+(.+?):\\s(.*)$" | ||
| ); | ||
|
|
||
| private final TelemetryEventTracker tracker; | ||
| private final Level minimumLevel; | ||
| private final String selfTag; | ||
| private final ProcessFactory processFactory; | ||
|
|
||
| private Thread thread; | ||
| private Process process; | ||
| private volatile boolean running; | ||
|
|
||
| LogcatTelemetryCapture( | ||
| TelemetryEventTracker tracker, | ||
| Level minimumLevel, | ||
| String selfTag | ||
| ) { | ||
| this(tracker, minimumLevel, selfTag, defaultProcessFactory()); | ||
| } | ||
|
|
||
| LogcatTelemetryCapture( | ||
| TelemetryEventTracker tracker, | ||
| Level minimumLevel, | ||
| String selfTag, | ||
| ProcessFactory processFactory | ||
| ) { | ||
| this.tracker = tracker; | ||
| this.minimumLevel = minimumLevel != null ? minimumLevel : Level.WARNING; | ||
| this.selfTag = selfTag; | ||
| this.processFactory = processFactory; | ||
| } | ||
|
|
||
| synchronized void start() { | ||
| if (running) { | ||
| return; | ||
| } | ||
| try { | ||
| this.process = processFactory.start(logcatPriorityFor(this.minimumLevel)); | ||
| } catch (IOException e) { | ||
| Log.w(Rollbar.TAG, "Failed to start logcat telemetry capture", e); | ||
| return; | ||
| } | ||
| running = true; | ||
| thread = new Thread(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| readLoop(); | ||
| } | ||
| }, "rollbar-logcat-telemetry"); | ||
| thread.setDaemon(true); | ||
| thread.start(); | ||
| } | ||
|
|
||
| synchronized void stop() { | ||
| if (!running) { | ||
| return; | ||
| } | ||
| running = false; | ||
| if (process != null) { | ||
| process.destroy(); | ||
| process = null; | ||
| } | ||
| if (thread != null) { | ||
| thread.interrupt(); | ||
| thread = null; | ||
| } | ||
| } | ||
|
|
||
| private void readLoop() { | ||
| Process currentProcess = this.process; | ||
| if (currentProcess == null) { | ||
| return; | ||
| } | ||
| BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(currentProcess.getInputStream(), Charset.forName("UTF-8"))); | ||
| try { | ||
| String line; | ||
| while (running && (line = reader.readLine()) != null) { | ||
| processLine(line); | ||
| } | ||
| } catch (IOException e) { | ||
| // Process died or was destroyed — expected on stop(). | ||
| } finally { | ||
| try { | ||
| reader.close(); | ||
| } catch (IOException ignored) { | ||
| } | ||
| if (running) { | ||
| Log.w(Rollbar.TAG, "logcat process exited unexpectedly; resetting capture state"); | ||
| stop(); | ||
| } | ||
| } | ||
| } | ||
|
buongarzoni marked this conversation as resolved.
|
||
|
|
||
| void processLine(String line) { | ||
| if (line == null) { | ||
| return; | ||
| } | ||
| Matcher matcher = LOGCAT_LINE_PATTERN.matcher(line); | ||
| if (!matcher.matches()) { | ||
| return; | ||
| } | ||
|
|
||
| String priority = matcher.group(1); | ||
| String tag = matcher.group(2).trim(); | ||
| String message = matcher.group(3); | ||
|
|
||
| if (selfTag != null && selfTag.equals(tag)) { | ||
| return; | ||
| } | ||
|
buongarzoni marked this conversation as resolved.
|
||
|
|
||
| Level level = mapPriorityToLevel(priority); | ||
| if (level == null) { | ||
| return; | ||
| } | ||
| if (level.level() < minimumLevel.level()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| tracker.recordLogEventFor(level, Source.CLIENT, message); | ||
| } catch (Exception e) { | ||
|
buongarzoni marked this conversation as resolved.
|
||
| // Never let a broken tracker kill the reader thread. | ||
| } | ||
| } | ||
|
|
||
| static Level mapPriorityToLevel(String priority) { | ||
| if (priority == null || priority.isEmpty()) { | ||
| return null; | ||
| } | ||
| switch (priority.charAt(0)) { | ||
| case 'V': | ||
| case 'D': | ||
| return Level.DEBUG; | ||
| case 'I': | ||
| return Level.INFO; | ||
| case 'W': | ||
| return Level.WARNING; | ||
| case 'E': | ||
| return Level.ERROR; | ||
| case 'F': | ||
| return Level.CRITICAL; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| static String logcatPriorityFor(Level level) { | ||
| if (level == null) { | ||
| return "W"; | ||
| } | ||
| switch (level) { | ||
| case DEBUG: | ||
| return "V"; | ||
| case INFO: | ||
| return "I"; | ||
| case WARNING: | ||
| return "W"; | ||
| case ERROR: | ||
| return "E"; | ||
| case CRITICAL: | ||
| return "F"; | ||
| default: | ||
| return "W"; | ||
| } | ||
| } | ||
|
buongarzoni marked this conversation as resolved.
|
||
|
|
||
| interface ProcessFactory { | ||
| Process start(String priorityFilter) throws IOException; | ||
| } | ||
|
|
||
| private static ProcessFactory defaultProcessFactory() { | ||
| return new ProcessFactory() { | ||
| @Override | ||
| public Process start(String priorityFilter) throws IOException { | ||
| return new ProcessBuilder( | ||
| "logcat", "-v", "threadtime", "-T", "1", "*:" + priorityFilter) | ||
| .redirectErrorStream(true) | ||
| .start(); | ||
| } | ||
| }; | ||
|
buongarzoni marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.