Commit f16baf1d authored by Lei Li's avatar Lei Li Committed by “李磊”
Browse files

feat: 增加logcat日志输出

parent 04fd4500
......@@ -2,7 +2,7 @@
mkdir -p lib/
SRC=./*.go
DST=lib/libagent.so
DST2=lib/libagent.a
x86:
export GOARCH=amd64 GOOS=linux GO111MODULE=on CGO_ENABLED=1 && go build -buildmode=c-shared -o $(DST) $(SRC)
......@@ -11,7 +11,16 @@ arm:
android:
export GOARCH=arm GOOS=android GO111MODULE=on CGO_ENABLED=1 CC=/usr/local/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi19-clang && go build -buildmode=c-shared -o $(DST) $(SRC)
android-64:
export GOARCH=arm64 GOOS=android GO111MODULE=on CGO_ENABLED=1 CC=/usr/local/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang && go build -buildmode=c-shared -o $(DST) $(SRC)
android-static:
export GOARCH=arm GOOS=android GO111MODULE=on CGO_ENABLED=1 CC=/usr/local/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi19-clang && go build -buildmode=c-shared -o $(DST2) $(SRC)
android-static-64:
export GOARCH=arm64 GOOS=android GO111MODULE=on CGO_ENABLED=1 CC=/usr/local/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang && go build -buildmode=c-shared -o $(DST2) $(SRC)
.PHONY: clean
P.HONY: clean
clean:
rm -f lib/*
......@@ -17,6 +17,7 @@ import (
//export start
func start() {
RootDir = GetCurrentDirectory()
msg := ""
for {
if file.Exists(filepath.Join(RootDir, agentFileName)) {
go startAgent()
......@@ -25,20 +26,26 @@ func start() {
err := DownloadFile(agentVersionReportFileName, agentVersionURL)
if err != nil {
l.Errorf("DownloadFile err: %v", err)
msg = fmt.Sprintf("DownloadFile err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName))
if err != nil {
l.Errorf("ReadFile err: %v", err)
msg = fmt.Sprintf("ReadFile err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
data := make(map[string]string)
err = json.Unmarshal(content, &data)
if err != nil {
time.Sleep(5 * time.Second)
l.Error("unmarshal VersionReport content failed...")
msg = fmt.Sprintf("unmarshal VersionReport content failed... err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
return
}
......@@ -47,30 +54,40 @@ func start() {
newExecFile := fmt.Sprintf("%s-new", agentFileName)
err = DownloadFile(newExecFile, newFileURL)
if err != nil {
time.Sleep(5 * time.Second)
l.Error("download Agent failed...")
msg = fmt.Sprintf("download Agent failed.... err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName))
if err != nil {
l.Errorf("copy Agent failed...err:%v", err)
msg = fmt.Sprintf("copy Agent failed.... err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
err = os.Chmod(filepath.Join(RootDir, agentFileName), 0755)
if err != nil {
l.Errorf("chmod Agent failed...err:%v", err)
msg = fmt.Sprintf("chmod Agent failed.... err:%s", err.Error())
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName))
if err != nil {
l.Warnf("GetFileMD5 err: %v", err)
msg = fmt.Sprintf("GetFileMD5 err:%s", err.Error())
LogWarn("MainActivity", msg)
}
if md5 != data["md5"] {
time.Sleep(5 * time.Second)
l.Error("update new agent failed, check md5sum err!")
msg = "update new agent failed, check md5sum err!"
LogError("MainActivity", msg)
time.Sleep(5 * time.Second)
continue
}
......@@ -82,6 +99,7 @@ func start() {
}
}
l.Info("start")
LogInfo("MainActivity", "start")
}
//export stop
......@@ -91,6 +109,7 @@ func stop() {
l.Error(err)
}
l.Info("stop")
LogInfo("MainActivity", "stop")
}
func main() {}
......@@ -57,12 +57,15 @@ func HttpGet(url string, body []byte) (*http.Response, error) {
}
func startAgent() {
msg := ""
for {
agent := exec.Command(filepath.Join(RootDir, agentFileName), "-vendor=test")
// 启动子进程
err := agent.Start()
if err != nil {
l.Errorf("agent start failed: %v", err)
msg = fmt.Sprintf("agent start failed: %s", err.Error())
LogError("MainActivity", msg)
time.Sleep(10 * time.Second)
continue
}
......@@ -72,7 +75,10 @@ func startAgent() {
err = agent.Wait()
if err != nil {
l.Errorf("agent wait failed: %v", err)
msg = fmt.Sprintf("agent wait failed: %s", err.Error())
LogError("MainActivity", msg)
break
}
time.Sleep(10 * time.Second)
//time.Sleep(10 * time.Second)
}
}
package main
/*
#include <android/log.h>
#include <stdlib.h>
void logDebug(const char *tag, const char *msg) {
__android_log_print(ANDROID_LOG_DEBUG, tag, "%s", msg);
}
void logInfo(const char *tag, const char *msg) {
__android_log_print(ANDROID_LOG_INFO, tag, "%s", msg);
}
void logWarn(const char *tag, const char *msg) {
__android_log_print(ANDROID_LOG_WARN, tag, "%s", msg);
}
void logError(const char *tag, const char *msg) {
__android_log_print(ANDROID_LOG_ERROR, tag, "%s", msg);
}
*/
import "C"
import (
"unsafe"
)
// LogInfo 使用 Android logcat 输出信息级别的日志
func LogInfo(tag, msg string) {
ctag := C.CString(tag)
cmsg := C.CString(msg)
defer C.free(unsafe.Pointer(ctag))
defer C.free(unsafe.Pointer(cmsg))
C.logInfo(ctag, cmsg)
}
func LogDebug(tag, msg string) {
ctag := C.CString(tag)
cmsg := C.CString(msg)
defer C.free(unsafe.Pointer(ctag))
defer C.free(unsafe.Pointer(cmsg))
C.logDebug(ctag, cmsg)
}
func LogWarn(tag, msg string) {
ctag := C.CString(tag)
cmsg := C.CString(msg)
defer C.free(unsafe.Pointer(ctag))
defer C.free(unsafe.Pointer(cmsg))
C.logWarn(ctag, cmsg)
}
func LogError(tag, msg string) {
ctag := C.CString(tag)
cmsg := C.CString(msg)
defer C.free(unsafe.Pointer(ctag))
defer C.free(unsafe.Pointer(cmsg))
C.logError(ctag, cmsg)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment