Created
October 9, 2020 21:56
-
-
Save abemedia/def29d42c80cef8921e2db497d000dae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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