-
Notifications
You must be signed in to change notification settings - Fork 579
/
log_android.go
73 lines (60 loc) · 1.71 KB
/
log_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
71
72
73
// +build android
package main
/*
#cgo LDFLAGS: -landroid -llog
#include <android/log.h>
#include <stdlib.h>
#include <string.h>
*/
import "C"
import (
"fmt"
"unsafe"
alog "github.com/v2fly/v2ray-core/v5/app/log"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/log"
"github.com/v2fly/v2ray-core/v5/common/serial"
)
var (
ctag = C.CString("v2ray")
)
type androidLogger struct{}
func (l *androidLogger) Handle(msg log.Message) {
var priority = C.ANDROID_LOG_FATAL // this value should never be used in client mode
var message string
switch msg := msg.(type) {
case *log.GeneralMessage:
switch msg.Severity {
case log.Severity_Error:
priority = C.ANDROID_LOG_ERROR
case log.Severity_Warning:
priority = C.ANDROID_LOG_WARN
case log.Severity_Info:
priority = C.ANDROID_LOG_INFO
case log.Severity_Debug:
priority = C.ANDROID_LOG_DEBUG
}
message = serial.ToString(msg.Content)
default:
message = msg.String()
}
cstr := C.CString(message)
defer C.free(unsafe.Pointer(cstr))
C.__android_log_write(C.int(priority), ctag, cstr)
}
func logInit() {
common.Must(alog.RegisterHandlerCreator(alog.LogType_Console, func(_ alog.LogType, _ alog.HandlerCreatorOptions) (log.Handler, error) {
return &androidLogger{}, nil
}))
}
func logFatal(v ...interface{}) {
cstr := C.CString(fmt.Sprintln(v...))
defer C.free(unsafe.Pointer(cstr))
C.__android_log_write(C.ANDROID_LOG_FATAL, ctag, cstr)
}
func logWarn(v ...interface{}) {
(&androidLogger{}).Handle(&log.GeneralMessage{Severity: log.Severity_Warning, Content: fmt.Sprintln(v...)})
}
func logInfo(v ...interface{}) {
(&androidLogger{}).Handle(&log.GeneralMessage{Severity: log.Severity_Info, Content: fmt.Sprintln(v...)})
}