-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (171 loc) · 3.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
type Process struct {
Tag string
Pid uint64
}
type LogcatLine struct {
Type string
Tag string
Pid uint64
}
const (
MATCH_V1 = `(\d\d.\d\d\s\d\d:\d\d:\d\d.\d\d\d)\s*(\d*)\s*(\d*)\s([VDIWEFS])\s(.*?)(:\s)(.*)`
MATCH_V2 = `([VDIWEFS])/(.*?)\((\s*|)(\d*)\):.`
MATCH_PROCESS_V1 = `\d\d.\d\d\s\d\d:\d\d:\d\d.\d\d\d\s*\d*\s*\d*\s[VDIWEFS]\sActivityManager:\sStart\sproc\s(\d*):(.*?)/.*`
MATCH_PROCESS_V2 = `.*Start\sproc\s(.*)\sfor\sactivity\s.*:\spid=(\d*)\s.*`
)
func matchProcessV1(inputLine string) *Process {
exp := regexp.MustCompile(MATCH_PROCESS_V1)
m := exp.FindStringSubmatch(inputLine)
if len(m) > 0 {
var i uint64
i, _ = strconv.ParseUint(m[1], 10, 64)
return &Process{
Tag: m[2],
Pid: i,
}
}
return nil
}
func matchProcessV2(inputLine string) *Process {
exp := regexp.MustCompile(MATCH_PROCESS_V2)
m := exp.FindStringSubmatch(inputLine)
if len(m) > 0 {
var i uint64
i, _ = strconv.ParseUint(m[2], 10, 64)
return &Process{
Tag: m[1],
Pid: i,
}
}
return nil
}
func process(inputLine string) *Process {
if proc := matchProcessV1(inputLine); proc != nil {
return proc
}
if proc := matchProcessV2(inputLine); proc != nil {
return proc
}
return nil
}
func matchV1(inputLine string) *LogcatLine {
exp := regexp.MustCompile(MATCH_V1)
m := exp.FindStringSubmatch(inputLine)
if len(m) > 0 {
var i uint64
i, _ = strconv.ParseUint(m[2], 10, 64)
return &LogcatLine{
Type: m[4],
Tag: m[5],
Pid: i,
}
}
return nil
}
func matchV2(inputLine string) *LogcatLine {
exp := regexp.MustCompile(MATCH_V2)
m := exp.FindStringSubmatch(inputLine)
if len(m) > 0 {
var i uint64
i, _ = strconv.ParseUint(m[4], 10, 64)
return &LogcatLine{
Type: m[1],
Tag: m[2],
Pid: i,
}
}
return nil
}
func detectFormat(inputLine string) *LogcatLine {
if line := matchV1(inputLine); line != nil {
return line
}
if line := matchV2(inputLine); line != nil {
return line
}
return &LogcatLine{
Type: "",
Tag: "",
Pid: 0,
}
}
var (
tag string
filter string
)
func init() {
flag.StringVar(&tag, "tag", "", "-tag com.yourcompany.yourapp")
flag.StringVar(&filter, "filter", "", "-filter Networking")
}
func main() {
flag.Parse()
reader := bufio.NewReader(os.Stdin)
var currentPid uint64
currentPid = 0
filterProcess := false
if tag != "" {
filterProcess = true
}
filterText := false
if len(tag) > 0 {
fmt.Println("tag:" + tag)
}
if len(filter) > 0 {
filterText = true
filter = strings.ToLower(filter)
fmt.Println("filter:" + filter)
}
for {
text, err := reader.ReadString('\n')
if err == io.EOF {
return
}
if err != nil {
fmt.Println(err.Error())
}
kind := detectFormat(text)
if filterProcess {
process := process(text)
if process != nil && process.Tag == tag {
currentPid = process.Pid
}
if kind.Pid != currentPid {
continue
}
}
if filterText {
if !strings.Contains(strings.ToLower(text), filter) {
continue
}
}
text = time.Now().Format(time.StampMilli) + "| " + text
switch kind.Type {
case "D":
color.Green(text)
break
case "W":
color.Yellow(text)
break
case "E":
color.Red(text)
break
case "I":
color.Cyan(text)
case "":
fmt.Print(text)
}
}
}