Beispiel #1
0
func init() {
	// Redirect the standard output and error to logcat
	oldStdout, oldStderr := os.Stdout, os.Stderr

	outRead, outWrite, _ := os.Pipe()
	errRead, errWrite, _ := os.Pipe()

	os.Stdout = outWrite
	os.Stderr = errWrite

	go func() {
		scanner := bufio.NewScanner(outRead)
		for scanner.Scan() {
			line := scanner.Text()
			C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stdout"), C.CString(line))
			oldStdout.WriteString(line + "\n")
		}
	}()

	go func() {
		scanner := bufio.NewScanner(errRead)
		for scanner.Scan() {
			line := scanner.Text()
			C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stderr"), C.CString(line))
			oldStderr.WriteString(line + "\n")
		}
	}()
}
Beispiel #2
0
func run(cb Callbacks) {
	// We want to keep the event loop on a consistent OS thread.
	runtime.LockOSThread()

	ctag := C.CString("Go")
	cstr := C.CString("app.Run")
	C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
	C.free(unsafe.Pointer(ctag))
	C.free(unsafe.Pointer(cstr))

	if JavaInit != nil {
		JavaInit(uintptr(unsafe.Pointer(C.current_vm)))
	}

	// Inform Java that the program is initialized.
	C.pthread_mutex_lock(&C.go_started_mu)
	C.go_started = 1
	C.pthread_cond_signal(&C.go_started_cond)
	C.pthread_mutex_unlock(&C.go_started_mu)

	for {
		select {
		case w := <-windowCreated:
			windowDrawLoop(cb, w, queue)
		}
	}
}
Beispiel #3
0
//export LogHello
func LogHello(name string) {
	fmt.Printf("Hello, %s!\n", name)

	ctag := C.CString("Go")
	cstr := C.CString(fmt.Sprintf("Printing hello message for %q", name))
	C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
	C.free(unsafe.Pointer(ctag))
	C.free(unsafe.Pointer(cstr))
}
Beispiel #4
0
func (aw *androidWriter) Write(p []byte) (n int, err error) {
	n = len(p)
	err = nil
	for nlidx := bytes.IndexByte(p, '\n'); nlidx != -1; nlidx = bytes.IndexByte(p, '\n') {
		aw.buf = append(aw.buf, p[:nlidx]...)
		p = p[nlidx+1:]
		aw.buf = append(aw.buf, 0)
		cstr := (*C.char)(unsafe.Pointer(&aw.buf[0]))
		C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
		aw.buf = aw.buf[:0]
	}
	aw.buf = append(aw.buf, p...)
	return
}
Beispiel #5
0
func (w AndroidWriter) Write(p []byte) (n int, err error) {
	ctag := C.CString(w.Tag)
	n = len(p)
	err = nil
	for nlidx := bytes.IndexByte(p, '\n'); nlidx != -1; nlidx = bytes.IndexByte(p, '\n') {
		w.buf = append(w.buf, p[:nlidx]...)
		p = p[nlidx+1:]
		w.buf = append(w.buf, 0)
		cstr := (*C.char)(unsafe.Pointer(&w.buf[0]))
		C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
		w.buf = w.buf[:0]
	}
	w.buf = append(w.buf, p...)
	return
}
func lineLog(f *os.File, priority C.int) {
	const logSize = 1024 // matches android/log.h.
	r := bufio.NewReaderSize(f, logSize)
	for {
		line, _, err := r.ReadLine()
		str := string(line)
		if err != nil {
			str += " " + err.Error()
		}
		cstr := C.CString(str)
		C.__android_log_write(priority, ctag, cstr)
		C.free(unsafe.Pointer(cstr))
		if err != nil {
			break
		}
	}
}
Beispiel #7
0
func run(cb Callbacks) {
	// We want to keep the event loop on a consistent OS thread.
	runtime.LockOSThread()

	ctag := C.CString("Go")
	cstr := C.CString("app.Run")
	C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
	C.free(unsafe.Pointer(ctag))
	C.free(unsafe.Pointer(cstr))

	if C.current_native_activity == nil {
		runStart(cb)
		notifyInitDone()
		select {}
	} else {
		notifyInitDone()
		windowDrawLoop(cb, <-windowCreated, queue)
	}
}
func (infoWriter) Write(p []byte) (n int, err error) {
	cstr := C.CString(string(p))
	C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
	C.free(unsafe.Pointer(cstr))
	return len(p), nil
}