Skip to content

Commit b8db264

Browse files
author
borney
committed
add lib support
1 parent b0d76e8 commit b8db264

File tree

22 files changed

+187
-59
lines changed

22 files changed

+187
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pidcat"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Borney"]
55
edition = "2021"
66
license = "MIT"
@@ -16,6 +16,14 @@ keywords = [
1616
readme = "README.md"
1717
repository = "https://github.com/borneygit/pidcat"
1818

19+
[[bin]]
20+
name="pidcat"
21+
path="src/main.rs"
22+
23+
[lib]
24+
name="pidcat"
25+
path="src/lib.rs"
26+
1927
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2028

2129
[dependencies]

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pidcat toor
1212

1313
![ScreenShot](/asset/screen.png)
1414

15-
# Install
15+
# Install terminal
1616
First install the rust environment
1717
```
1818
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
@@ -22,7 +22,7 @@ Then use cargo to install pidcat
2222
cargo install --path .
2323
```
2424

25-
# How to use
25+
# How to use terminal
2626

2727
```rust
2828
pidcat --help
@@ -86,6 +86,32 @@ Options:
8686
Print version
8787
```
8888

89+
# Use by crate
90+
add dep for Cargo.toml
91+
```
92+
[dependencies]
93+
pidcat="0.2.0"
94+
```
95+
You can use the following code to capture the adb logcat logs and process them twice according to your needs
96+
```rust
97+
use futures::StreamExt;
98+
use pidcat::LogStream;
99+
use pidcat::source::*;
100+
101+
#[tokio::main]
102+
async fn main() {
103+
let source = ADBSource::new(None);
104+
105+
let mut logs: LogStream = source.source().await;
106+
107+
while let Some(r) = logs.next().await {
108+
if let Ok(log) = r {
109+
println!("{}", log);
110+
}
111+
}
112+
}
113+
```
114+
89115
# Thanks
90116
https://github.com/JakeWharton/pidcat </br>
91117
https://github.com/flxo/rogcat

examples/colored.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@ lazy_static! {
99
u8::from_str_radix(&"#ff2600"[3..5], 16).unwrap(),
1010
u8::from_str_radix(&"#ff2600"[5..7], 16).unwrap()
1111
);
12-
1312
static ref VERBOSE: Color = Color(
1413
u8::from_str_radix(&"#ffffff"[1..3], 16).unwrap(),
1514
u8::from_str_radix(&"#ffffff"[3..5], 16).unwrap(),
1615
u8::from_str_radix(&"#ffffff"[5..7], 16).unwrap()
1716
);
18-
1917
static ref INFO: Color = Color(
2018
u8::from_str_radix(&"#05d702"[1..3], 16).unwrap(),
2119
u8::from_str_radix(&"#05d702"[3..5], 16).unwrap(),
2220
u8::from_str_radix(&"#05d702"[5..7], 16).unwrap()
2321
);
24-
2522
static ref WARNING: Color = Color(
2623
u8::from_str_radix(&"#d75f02"[1..3], 16).unwrap(),
2724
u8::from_str_radix(&"#d75f02"[3..5], 16).unwrap(),
2825
u8::from_str_radix(&"#d75f02"[5..7], 16).unwrap()
2926
);
30-
3127
static ref DEBUG: Color = Color(
3228
u8::from_str_radix(&"#5fafff"[1..3], 16).unwrap(),
3329
u8::from_str_radix(&"#5fafff"[3..5], 16).unwrap(),
@@ -71,4 +67,4 @@ fn main() {
7167
debug!("hello {} {}", 123, "abc");
7268
warn!("hello {} {}", 123, "abc");
7369
verbose!("hello {} {}", 123, "abc");
74-
}
70+
}

examples/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use futures::StreamExt;
2+
use pidcat::source::*;
3+
use pidcat::LogStream;
4+
5+
#[tokio::main]
6+
async fn main() {
7+
let source = ADBSource::new(None);
8+
9+
let mut logs: LogStream = source.source().await;
10+
11+
while let Some(r) = logs.next().await {
12+
if let Ok(log) = r {
13+
println!("{}", log);
14+
}
15+
}
16+
}

examples/logcat.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::fmt::{Display, Formatter};
2-
use std::pin::Pin;
3-
use futures::{Sink, Stream, StreamExt};
41
use anyhow::Result;
52
use async_stream::stream;
6-
use std::collections::HashMap;
73
use async_trait::async_trait;
4+
use futures::{Sink, Stream, StreamExt};
85
use regex::Regex;
9-
use tokio::io::AsyncBufReadExt;
6+
use std::collections::HashMap;
107
use std::error::Error;
8+
use std::fmt::{Display, Formatter};
9+
use std::pin::Pin;
10+
use tokio::io::AsyncBufReadExt;
1111
use tokio::process::{Child, Command};
1212

1313
#[derive(Debug, Clone)]
@@ -24,11 +24,15 @@ pub(crate) struct Log {
2424

2525
impl Display for Log {
2626
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27-
write!(f, "{} {} {} {} {} {} {}", self.date, self.time, self.pid, self.tid, self.level, self.tag, self.message)
27+
write!(
28+
f,
29+
"{} {} {} {} {} {} {}",
30+
self.date, self.time, self.pid, self.tid, self.level, self.tag, self.message
31+
)
2832
}
2933
}
3034

31-
type LogStream = Pin<Box<dyn Stream<Item=Result<Log, Box<dyn Error + Send>>> + Send>>;
35+
type LogStream = Pin<Box<dyn Stream<Item = Result<Log, Box<dyn Error + Send>>> + Send>>;
3236

3337
#[async_trait]
3438
trait Source {
@@ -45,8 +49,7 @@ impl AdbSource {
4549
command.arg("-D");
4650
command.arg("-v").arg("long");
4751
command.arg("-b").arg("all");
48-
command.spawn()
49-
.expect("Failed to execute adb logcat")
52+
command.spawn().expect("Failed to execute adb logcat")
5053
}
5154
}
5255

@@ -55,7 +58,9 @@ impl Source for AdbSource {
5558
async fn source(&self) -> LogStream {
5659
let mut logcat = self.spawn_adb_logcat().await;
5760
let mut reader = tokio::io::BufReader::new(logcat.stdout.take().unwrap());
58-
let re = Regex::new(r"\[ (\d{2}-\d{2})\s(\d{2}:\d{2}:\d{2}\.\d{3})\s+(\d+):\s+(\d+)\s+(.*) ]").unwrap();
61+
let re =
62+
Regex::new(r"\[ (\d{2}-\d{2})\s(\d{2}:\d{2}:\d{2}\.\d{3})\s+(\d+):\s+(\d+)\s+(.*) ]")
63+
.unwrap();
5964

6065
let s = stream! {
6166
let mut line = String::new();
@@ -125,6 +130,7 @@ async fn main() {
125130
Err(_) => {}
126131
}
127132
}
128-
}).await.expect("TODO: panic message");
129-
130-
}
133+
})
134+
.await
135+
.expect("TODO: panic message");
136+
}

examples/ps.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ use tokio::process::{Child, Command};
44
#[tokio::main]
55
async fn main() {
66
let mut ps = spawn_ps().await;
7-
let pids = Vec::from([
8-
"launcher",
9-
"com.android.phone",
10-
"genymotion"
11-
]);
7+
let pids = Vec::from(["launcher", "com.android.phone", "genymotion"]);
128

139
let reader_task = tokio::spawn(async move {
1410
let mut reader = tokio::io::BufReader::new(ps.stdout.take().unwrap());
@@ -20,7 +16,7 @@ async fn main() {
2016
}
2117
let spl = line.trim().split_whitespace().collect::<Vec<&str>>();
2218
let name = spl[8];
23-
let pid= spl[1];
19+
let pid = spl[1];
2420
for p in pids.iter() {
2521
if name.contains(p) {
2622
println!("{}:{}", name, pid);
@@ -38,6 +34,5 @@ async fn spawn_ps() -> Child {
3834
command.stdout(std::process::Stdio::piped());
3935
command.arg("shell");
4036
command.arg("ps");
41-
command.spawn()
42-
.expect("Failed to execute adb shell ps")
37+
command.spawn().expect("Failed to execute adb shell ps")
4338
}

examples/re.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
"[ 05-24 05:54:17.377 181: 181 I/vold ]",
1616
"[546,1000,system,89723904,77209600,0]",
1717
"[3849]> SIGNAL_STRENGTH [SUB0]",
18-
"[ 05-26 05:45:32.733 1440: 1440 W/System.err ]"
18+
"[ 05-26 05:45:32.733 1440: 1440 W/System.err ]",
1919
];
2020
let re = Regex::new(r"\[ (\d{2}-\d{2})\s(\d{2}:\d{2}:\d{2}\.\d{3})\s+(\d+):(.*) ]").unwrap();
2121

@@ -36,4 +36,4 @@ fn main() {
3636
println!("{}---->{}", s, re.is_match(s));
3737
}
3838
}
39-
}
39+
}

src/filter/buffer_filter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ pub(crate) struct BufferFilter {
1010
}
1111

1212
impl BufferFilter {
13-
pub fn new(buffers: Vec<String>, filter: Option<Arc<dyn Filter>>) -> Self {
13+
#[allow(dead_code)]
14+
pub(crate) fn new(buffers: Vec<String>, filter: Option<Arc<dyn Filter>>) -> Self {
1415
let mut s = Self {
1516
buffers,
1617
filter,

src/filter/level_filter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ pub(crate) struct LevelFilter {
99
}
1010

1111
impl LevelFilter {
12-
pub fn new(level: Level, filter: Option<Arc<dyn Filter>>) -> Self {
12+
#[allow(dead_code)]
13+
pub(crate) fn new(level: Level, filter: Option<Arc<dyn Filter>>) -> Self {
1314
Self { level, filter }
1415
}
1516
}

0 commit comments

Comments
 (0)