aboutsummaryrefslogtreecommitdiff
path: root/vendor/gioui.org/app/internal/log/log_ios.go
blob: e49f425682b691c9b1220ca232f4805d56267bac (plain)
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
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin,ios

package log

/*
#cgo CFLAGS: -Werror -fmodules -fobjc-arc -x objective-c

__attribute__ ((visibility ("hidden"))) void nslog(char *str);
*/
import "C"

import (
	"bufio"
	"io"
	"log"
	"unsafe"

	_ "gioui.org/app/internal/cocoainit"
)

func init() {
	// macOS Console already includes timestamps.
	log.SetFlags(log.Flags() &^ log.LstdFlags)
	log.SetOutput(newNSLogWriter())
}

func newNSLogWriter() io.Writer {
	r, w := io.Pipe()
	go func() {
		// 1024 is an arbitrary truncation limit, taken from Android's
		// log buffer size.
		lineBuf := bufio.NewReaderSize(r, 1024)
		// The buffer to pass to C, including the terminating '\0'.
		buf := make([]byte, lineBuf.Size()+1)
		cbuf := (*C.char)(unsafe.Pointer(&buf[0]))
		for {
			line, _, err := lineBuf.ReadLine()
			if err != nil {
				break
			}
			copy(buf, line)
			buf[len(line)] = 0
			C.nslog(cbuf)
		}
	}()
	return w
}