forked from XTLS/libXray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_android.go
70 lines (60 loc) · 1.28 KB
/
logger_android.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
package libXray
import (
"bufio"
"io"
"log"
"os"
"syscall"
"github.com/amnezia-vpn/amnezia-libxray/nodep"
)
type Logger interface {
io.Writer
Warning(s string)
Error(s string)
}
var (
// Store the writer end of the redirected stderr and stdout
// so that they are not garbage collected and closed.
stderr, stdout *os.File
)
func writeLog(stream *os.File, log func(s string), logError func(s string)) {
const logSize = 1024
r := bufio.NewReaderSize(stream, logSize)
for {
line, _, err := r.ReadLine()
str := string(line)
if err != nil {
str += " " + err.Error()
logError(str)
break
} else {
log(str)
}
}
}
func logStream(stream uintptr, log func(s string), logError func(s string)) (*os.File, error) {
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
if err := syscall.Dup3(int(w.Fd()), int(stream), 0); err != nil {
return nil, err
}
go writeLog(r, log, logError)
return w, err
}
func InitLogger(logger Logger) string {
log.SetOutput(logger)
log.SetFlags(0)
stream, err := logStream(os.Stderr.Fd(), logger.Error, logger.Error)
if err != nil {
return nodep.WrapError(err)
}
stderr = stream
stream, err = logStream(os.Stdout.Fd(), logger.Warning, logger.Error)
if err != nil {
return nodep.WrapError(err)
}
stdout = stream
return ""
}