gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/app/log_ios.go (about)

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