mobileinit_ios.go 732 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mobileinit
  5. import (
  6. "io"
  7. "log"
  8. "os"
  9. "unsafe"
  10. )
  11. /*
  12. #include <stdlib.h>
  13. #include <os/log.h>
  14. os_log_t create_os_log() {
  15. return os_log_create("org.golang.mobile", "os_log");
  16. }
  17. void os_log_wrap(os_log_t log, const char *str) {
  18. os_log(log, "%s", str);
  19. }
  20. */
  21. import "C"
  22. type osWriter struct {
  23. w C.os_log_t
  24. }
  25. func (o osWriter) Write(p []byte) (n int, err error) {
  26. cstr := C.CString(string(p))
  27. C.os_log_wrap(o.w, cstr)
  28. C.free(unsafe.Pointer(cstr))
  29. return len(p), nil
  30. }
  31. func init() {
  32. log.SetOutput(io.MultiWriter(os.Stderr, osWriter{C.create_os_log()}))
  33. }