Created
January 9, 2019 22:44
-
-
Save neilalexander/9a3e1425d854fb517ba2b953b17f780b to your computer and use it in GitHub Desktop.
macOS/iOS: Writing to NSLog using Golang (using Cgo)
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 myapp | |
import "log" | |
... | |
mobilelog := MobileLogger{} | |
logger := log.New(mobilelog, "", 0) |
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 myapp | |
/* | |
#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 "unsafe" | |
type MobileLogger struct { | |
} | |
func (nsl MobileLogger) Write(p []byte) (n int, err error) { | |
p = append(p, 0) | |
cstr := (*C.char)(unsafe.Pointer(&p[0])) | |
C.Log(cstr) | |
return len(p), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment