github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/log/log_ios.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 // +build darwin,ios 4 5 package log 6 7 /* 8 #cgo CFLAGS: -Werror -fmodules -fobjc-arc -x objective-c 9 10 #include "log_ios.h" 11 */ 12 import "C" 13 14 import ( 15 "bufio" 16 "io" 17 "log" 18 "unsafe" 19 ) 20 21 func init() { 22 // macOS Console already includes timstamps. 23 log.SetFlags(log.Flags() &^ log.LstdFlags) 24 log.SetOutput(newNSLogWriter()) 25 } 26 27 func newNSLogWriter() io.Writer { 28 r, w := io.Pipe() 29 go func() { 30 // 1024 is an arbitrary truncation limit, taken from Android's 31 // log buffer size. 32 lineBuf := bufio.NewReaderSize(r, 1024) 33 // The buffer to pass to C, including the terminating '\0'. 34 buf := make([]byte, lineBuf.Size()+1) 35 cbuf := (*C.char)(unsafe.Pointer(&buf[0])) 36 for { 37 line, _, err := lineBuf.ReadLine() 38 if err != nil { 39 break 40 } 41 copy(buf, line) 42 buf[len(line)] = 0 43 C.nslog(cbuf) 44 } 45 }() 46 return w 47 }