diff --git a/ui/app/log_ios.go b/ui/app/log_ios.go new file mode 100644 index 00000000..4bbd6d30 --- /dev/null +++ b/ui/app/log_ios.go @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +// +build ios + +package app + +/* +#include "log_ios.h" +*/ +import "C" + +import ( + "bufio" + "io" + "log" + "unsafe" +) + +func init() { + // macOS Console already includes timstamps. + 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 +} diff --git a/ui/app/log_ios.h b/ui/app/log_ios.h new file mode 100644 index 00000000..dc096260 --- /dev/null +++ b/ui/app/log_ios.h @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +__attribute__ ((visibility ("hidden"))) void nslog(char *str); diff --git a/ui/app/log_ios.m b/ui/app/log_ios.m new file mode 100644 index 00000000..bb5efa8e --- /dev/null +++ b/ui/app/log_ios.m @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +// +build ios + +@import Foundation; + +#include "log_ios.h" + +void nslog(char *str) { + NSLog(@"%@", @(str)); +}