Writing to the Android Log

So I just figured out how to get my log messages from a system Golang program into the android log without the need for logwrapper.

It is a side effect of importing the “app” mobile package. so…

import (
	_ "golang.org/x/mobile/app"

The downside is that *everything* written via stdout will go to the system log – even flag.Usage()

If you want to go a little more serious, here is a nice chunk of code

package main

#cgo LDFLAGS: -llog
import "C"
import "unsafe"

func writeAndroidLogInfo(tag, message string) {
	ctag := C.CString(tag)
	cstr := C.CString(message)
	C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)

You can use it like so

writeAndroidLogInfo("MyIdentifier", "My Message")

and the output in logcat will be

I/MyIdentifier( 4444): My Message