forked from himananiito/livedl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
livedl-logger.go
81 lines (74 loc) · 1.24 KB
/
livedl-logger.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"regexp"
"sync"
)
func main() {
args := os.Args[1:]
var vid string
for _, s := range args {
if ma := regexp.MustCompile(`(lv\d{9,})`).FindStringSubmatch(s); len(ma) > 0 {
vid = ma[1]
}
}
args = append(args, "-nicoDebug")
cmd := exec.Command("livedl", args...)
if vid == "" {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
} else {
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println(err)
return
}
name := fmt.Sprintf("log/%s.txt", vid)
os.MkdirAll("log", os.ModePerm)
f, err := os.Create(name)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
var mtx sync.Mutex
append := func(s string) {
mtx.Lock()
defer mtx.Unlock()
f.WriteString(s)
}
go func() {
rdr := bufio.NewReader(stdout)
for {
s, err := rdr.ReadString('\n')
if err != nil {
return
}
fmt.Print(s)
append(s)
}
defer stdout.Close()
}()
go func() {
rdr := bufio.NewReader(stderr)
for {
s, err := rdr.ReadString('\n')
if err != nil {
return
}
append(s)
}
defer stderr.Close()
}()
cmd.Run()
}
}