Skip to content

Instantly share code, notes, and snippets.

@abemedia
Created October 9, 2020 21:56
Show Gist options
  • Save abemedia/def29d42c80cef8921e2db497d000dae to your computer and use it in GitHub Desktop.
Save abemedia/def29d42c80cef8921e2db497d000dae to your computer and use it in GitHub Desktop.
package nslog
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#import <Foundation/Foundation.h>
void Log(const char *text) {
NSString *nss = [NSString stringWithUTF8String:text];
NSLog(@"%@", nss);
}
*/
import "C"
import (
"io"
"unsafe"
)
type nslog struct{}
func New() io.Writer {
return new(nslog)
}
func (l *nslog) Write(p []byte) (n int, err error) {
msg := append(p[:], 0)
C.Log((*C.char)(unsafe.Pointer(&msg[0])))
return len(p), nil
}
func (l *nslog) WriteString(s string) (n int, err error) {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
C.Log(cs)
return len(s), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment