forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlog.go
More file actions
93 lines (76 loc) · 2.4 KB
/
log.go
File metadata and controls
93 lines (76 loc) · 2.4 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
/*
Copyright (C) 2021 by nekohasekai <contact-sagernet@sekai.icu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package libexclavecore
/*
#cgo LDFLAGS: -landroid -llog
#include <android/log.h>
#include <string.h>
#include <stdlib.h>
*/
import "C"
import (
"log"
"strings"
"unsafe"
appLog "github.com/exclavenetwork/exclave-core/v5/app/log"
commonLog "github.com/exclavenetwork/exclave-core/v5/common/log"
)
var (
tag = C.CString("libexclavecore")
tagV2Ray = C.CString("exclave-core")
)
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, "[Warning]") {
s = strings.Replace(s, "[Warning]", "", 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)
_ = appLog.RegisterHandlerCreator(appLog.LogType_Console, func(lt appLog.LogType,
options appLog.HandlerCreatorOptions,
) (commonLog.Handler, error) {
return commonLog.NewLogger(func() commonLog.Writer {
return &v2rayLogWriter{}
}), nil
})
}