-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add 'mobilecli device logs' support for streaming logs from mobile devices #195
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
gmegidish
wants to merge
13
commits into
main
Choose a base branch
from
feat/device-logs
base: main
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 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
01a66a3
feat: add "device logs" command for streaming device logs
gmegidish 92de82f
feat: add --process and --pid filters to device logs
gmegidish c5fdce5
fix: use -1 as default for --pid flag since pid 0 is valid
gmegidish 10ddb96
feat: add Android logcat support to device logs
gmegidish 96c026c
fix: use exact match for --process filter instead of substring
gmegidish 7506449
refactor: clean code improvements for device logs
gmegidish cec0580
feat: replace --process/--pid/--tag with repeatable --filter flag
gmegidish 561a210
docs: add device logs section to README
gmegidish b0b9d40
feat: switch iOS real-device logs to os_trace_relay
gmegidish c9fc86a
Merge remote-tracking branch 'origin/main' into feat/device-logs
gmegidish b53ca63
fix: harden device log streaming on iOS and simulator
gmegidish 5c04467
fix: add context propagation to StreamLogs for cancellation on dead c…
gmegidish 183d83a
fix: replace stale ps snapshot with lazy per-pid cmdline cache on And…
gmegidish 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/mobile-next/mobilecli/commands" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var logsLimit int | ||
| var logsProcess string | ||
| var logsPID int | ||
|
|
||
| var deviceLogsCmd = &cobra.Command{ | ||
| Use: "logs", | ||
| Short: "Stream device logs", | ||
| Long: `Streams real-time logs from a device. Press Ctrl+C to stop.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| response := commands.LogsCommand(commands.LogsRequest{ | ||
| DeviceID: deviceId, | ||
| Limit: logsLimit, | ||
| Process: logsProcess, | ||
| PID: logsPID, | ||
| }) | ||
| if response.Status == "error" { | ||
| printJson(response) | ||
| return fmt.Errorf("%s", response.Error) | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| deviceCmd.AddCommand(deviceLogsCmd) | ||
| deviceLogsCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to stream logs from") | ||
| deviceLogsCmd.Flags().IntVar(&logsLimit, "limit", 0, "Stop after N log entries (0 = unlimited)") | ||
| deviceLogsCmd.Flags().StringVar(&logsProcess, "process", "", "Filter by process name (substring match)") | ||
| deviceLogsCmd.Flags().IntVar(&logsPID, "pid", -1, "Filter by process ID") | ||
| } |
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,49 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/mobile-next/mobilecli/devices" | ||
| ) | ||
|
|
||
| type LogsRequest struct { | ||
| DeviceID string | ||
| Limit int | ||
| Process string | ||
| PID int | ||
| } | ||
|
|
||
| func LogsCommand(req LogsRequest) *CommandResponse { | ||
| device, err := FindDeviceOrAutoSelect(req.DeviceID) | ||
| if err != nil { | ||
| return NewErrorResponse(fmt.Errorf("error finding device: %w", err)) | ||
| } | ||
|
|
||
| encoder := json.NewEncoder(os.Stdout) | ||
| count := 0 | ||
| err = device.StreamLogs(func(entry devices.LogEntry) bool { | ||
| if req.Process != "" && !strings.Contains(entry.Process, req.Process) { | ||
| return true | ||
| } | ||
| if req.PID >= 0 && entry.PID != req.PID { | ||
| return true | ||
| } | ||
|
|
||
| if err := encoder.Encode(entry); err != nil { | ||
| return false | ||
| } | ||
| count++ | ||
| if req.Limit > 0 && count >= req.Limit { | ||
| return false | ||
| } | ||
| return true | ||
| }) | ||
| if err != nil { | ||
| return NewErrorResponse(fmt.Errorf("error streaming logs: %w", err)) | ||
| } | ||
|
|
||
| return NewSuccessResponse("done") | ||
| } | ||
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
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
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.
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.