forked from dryark/stf_ios_support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_log.go
215 lines (182 loc) · 6.15 KB
/
view_log.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"encoding/json"
"os"
"strings"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)
type Config struct {
Log LogConfig
//LogFile string `json:"log_file"`
//LinesLogFile string `json:"lines_log_file"`
}
type LogConfig struct {
Main string `json:"main"`
ProcLines string `json:"proc_lines"`
WDAWrapperStdout string `json:"wda_wrapper_stdout"`
WDAWrapperStderr string `json:"wda_wrapper_stderr"`
}
func main() {
var configFile = flag.String( "config", "config.json", "Config file path" )
var findProc = flag.String( "proc" , "" , "Process to view log of" )
flag.Parse()
config := read_config( *configFile )
fileName := config.Log.ProcLines
if *findProc == "" {
fmt.Println("specify a log to view / tail ( view_log -proc [proc] ):\n wdaproxy\n stf_device_ios\n device_trigger\n video_enabler\n stf_provider\n ffmpeg\n wda\n device_trigger\n")
os.Exit( 0 )
}
if *findProc == "wda" {
fileName = "bin/wda/req_log.json"
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
fh, err := os.Open( fileName )
if err != nil {
panic(err)
}
defer fh.Close()
size := fileSize( fh )
//fh.Seek( size, io.SeekStart )
scanner := bufio.NewScanner( fh )
for scanner.Scan() {
checkLine( []byte( scanner.Text() ), *findProc )
}
err = watcher.Add( fileName )
if err != nil {
log.Fatal(err)
}
for {
select {
case event := <-watcher.Events:
if event.Op & fsnotify.Write == fsnotify.Write {
//fmt.Println("modify")
newSize := fileSize( fh )
newBytes := newSize - size
if newBytes > 0 {
//fmt.Printf(" dif: %d\n", newBytes )
//f.Seek(pos, io.SeekStart)
buf := make( []byte, newBytes )
fh.Read( buf )
//fmt.Printf(" \"%s\"\n", string( buf ) )
checkLine( buf, *findProc )
size = newSize
}
}
}
}
}
func read_config( configPath string ) *Config {
fh, serr := os.Stat( configPath )
if serr != nil {
log.WithFields( log.Fields{
"type": "err_read_config",
"error": serr,
"config_path": configPath,
} ).Fatal("Could not read specified config path")
}
var configFile string
switch mode := fh.Mode(); {
case mode.IsDir():
configFile = fmt.Sprintf("%s/config.json", configPath)
case mode.IsRegular():
configFile = configPath
}
configFh, err := os.Open( configFile )
if err != nil {
log.WithFields( log.Fields{
"type": "err_read_config",
"config_file": configFile,
"error": err,
} ).Fatal("failed reading config file")
}
defer configFh.Close()
jsonBytes, _ := ioutil.ReadAll( configFh )
config := Config{}
defaultJson := `{
"log":{
"main": "logs/coordinator",
"proc_lines": "logs/procs",
"wda_wrapper_stdout": "./logs/wda_wrapper_stdout",
"wda_wrapper_stderr": "./logs/wda_wrapper_stderr"
}
}`
err = json.Unmarshal( []byte( defaultJson ), &config )
if err != nil {
log.Fatal( "1 ", err )
}
json.Unmarshal( jsonBytes, &config )
return &config
}
func checkLine( data []byte, findProc string ) {
var dat map[string]interface{}
startJ := strings.Index( string(data), "{" )
endJ := strings.LastIndex( string(data), "}" )
part := string(data)[ startJ : (endJ + 1) ]
decoder := json.NewDecoder( strings.NewReader( part ) )
for {
err := decoder.Decode( &dat )
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
if findProc == "wda" {
//fmt.Println( part )
typeif := dat["type"]
if typeif != nil {
typ := typeif.(string)
//fmt.Println( typ )
if typ == "req.start" {
if dat["body_in"] != nil {
inStr := dat["body_in"].(string)
fmt.Printf("Req URI: %s\n", dat["uri"].(string) )
if inStr[:1] == "{" {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte( inStr ), "", " ")
if error != nil {
fmt.Println( inStr )
} else {
fmt.Println( prettyJSON.String() )
}
//dec2 :=- json.NewDecoder( strings.NewReader( dat["body_in"].(string) ) )
//err = dec2.Decode( &dat )
} else {
fmt.Println( inStr )
}
}
} else if typ == "req.done" {
if dat["body_out"] != nil {
outStr := dat["body_out"].(string)
fmt.Printf("Response to URI: %s\n", dat["uri"].(string) )
fmt.Println( outStr )
}
}
}
} else {
proc := dat["proc"].(string)
if proc == findProc {
//fmt.Println(dat)
line := dat["line"].(string)
fmt.Println( line )
}
}
}
}
func fileSize( fh *os.File ) (int64) {
newinfo, err := fh.Stat()
if err != nil {
panic(err)
}
return newinfo.Size()
}