library_android.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains specialized code for running Geth on Android.
  17. package main
  18. // #include <android/log.h>
  19. // #cgo LDFLAGS: -llog
  20. import "C"
  21. import (
  22. "bufio"
  23. "os"
  24. )
  25. func init() {
  26. // Redirect the standard output and error to logcat
  27. oldStdout, oldStderr := os.Stdout, os.Stderr
  28. outRead, outWrite, _ := os.Pipe()
  29. errRead, errWrite, _ := os.Pipe()
  30. os.Stdout = outWrite
  31. os.Stderr = errWrite
  32. go func() {
  33. scanner := bufio.NewScanner(outRead)
  34. for scanner.Scan() {
  35. line := scanner.Text()
  36. C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stdout"), C.CString(line))
  37. oldStdout.WriteString(line + "\n")
  38. }
  39. }()
  40. go func() {
  41. scanner := bufio.NewScanner(errRead)
  42. for scanner.Scan() {
  43. line := scanner.Text()
  44. C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stderr"), C.CString(line))
  45. oldStderr.WriteString(line + "\n")
  46. }
  47. }()
  48. }