forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlog.go
More file actions
134 lines (112 loc) · 2.94 KB
/
log.go
File metadata and controls
134 lines (112 loc) · 2.94 KB
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
//go:build android
// +build android
package libcore
/*
#cgo LDFLAGS: -landroid -llog
#include <android/log.h>
#include <string.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"github.com/sirupsen/logrus"
"log"
"strings"
"unsafe"
appLog "github.com/xtls/xray-core/app/log"
commonLog "github.com/xtls/xray-core/common/log"
)
var (
tag = C.CString("libcore")
tagV2Ray = C.CString("xray-core")
)
var levels = []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
type androidHook struct {
}
type androidFormatter struct{}
func (f *androidFormatter) Format(entry *logrus.Entry) ([]byte, error) {
msgWithLevel := fmt.Sprint("[", strings.Title(entry.Level.String()), "] ", entry.Message)
return []byte(msgWithLevel), nil
}
func (hook *androidHook) Levels() []logrus.Level {
return levels
}
func (hook *androidHook) Fire(e *logrus.Entry) error {
formatted, err := logrus.StandardLogger().Formatter.Format(e)
if err != nil {
return err
}
str := C.CString(string(formatted))
var priority C.int
switch e.Level {
case logrus.PanicLevel:
priority = C.ANDROID_LOG_FATAL
case logrus.FatalLevel:
priority = C.ANDROID_LOG_FATAL
case logrus.ErrorLevel:
priority = C.ANDROID_LOG_ERROR
case logrus.WarnLevel:
priority = C.ANDROID_LOG_WARN
case logrus.InfoLevel:
priority = C.ANDROID_LOG_INFO
case logrus.DebugLevel:
priority = C.ANDROID_LOG_DEBUG
}
C.__android_log_write(priority, tag, str)
C.free(unsafe.Pointer(str))
return nil
}
type v2rayLogWriter struct {
}
func (w *v2rayLogWriter) Write(s string) error {
var priority C.int
if strings.Contains(s, "[Debug]") {
s = strings.Replace(s, "[Debug]", "", 1)
priority = C.ANDROID_LOG_DEBUG
} else if strings.Contains(s, "[Info]") {
s = strings.Replace(s, "[Info]", "", 1)
priority = C.ANDROID_LOG_INFO
} else if strings.Contains(s, "[Warn]") {
s = strings.Replace(s, "[Warn]", "", 1)
priority = C.ANDROID_LOG_WARN
} else if strings.Contains(s, "[Error]") {
s = strings.Replace(s, "[Error]", "", 1)
priority = C.ANDROID_LOG_ERROR
} else {
priority = C.ANDROID_LOG_DEBUG
}
str := C.CString(strings.TrimSpace(s))
C.__android_log_write(priority, tagV2Ray, str)
C.free(unsafe.Pointer(str))
return nil
}
func (w *v2rayLogWriter) Close() error {
return nil
}
type stdLogWriter struct{}
func (stdLogWriter) Write(p []byte) (n int, err error) {
str := C.CString(string(p))
C.__android_log_write(C.ANDROID_LOG_INFO, tag, str)
C.free(unsafe.Pointer(str))
return len(p), nil
}
func init() {
log.SetOutput(stdLogWriter{})
log.SetFlags(log.Flags() &^ log.LstdFlags)
logrus.SetFormatter(&androidFormatter{})
logrus.AddHook(&androidHook{})
_ = appLog.RegisterHandlerCreator(appLog.LogType_Console, func(lt appLog.LogType,
options appLog.HandlerCreatorOptions) (commonLog.Handler, error) {
return commonLog.NewLogger(func() commonLog.Writer {
return &v2rayLogWriter{}
}), nil
})
}