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