Skip to content

Instantly share code, notes, and snippets.

@neilalexander
Created January 9, 2019 22:44
Show Gist options
  • Save neilalexander/9a3e1425d854fb517ba2b953b17f780b to your computer and use it in GitHub Desktop.
Save neilalexander/9a3e1425d854fb517ba2b953b17f780b to your computer and use it in GitHub Desktop.
macOS/iOS: Writing to NSLog using Golang (using Cgo)
package myapp
import "log"
...
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
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