You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package autofile
  2. import (
  3. "os"
  4. "sync"
  5. "time"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. )
  8. /* AutoFile usage
  9. // Create/Append to ./autofile_test
  10. af, err := OpenAutoFile("autofile_test")
  11. if err != nil {
  12. panic(err)
  13. }
  14. // Stream of writes.
  15. // During this time, the file may be moved e.g. by logRotate.
  16. for i := 0; i < 60; i++ {
  17. af.Write([]byte(Fmt("LOOP(%v)", i)))
  18. time.Sleep(time.Second)
  19. }
  20. // Close the AutoFile
  21. err = af.Close()
  22. if err != nil {
  23. panic(err)
  24. }
  25. */
  26. const autoFileOpenDuration = 1000 * time.Millisecond
  27. // Automatically closes and re-opens file for writing.
  28. // This is useful for using a log file with the logrotate tool.
  29. type AutoFile struct {
  30. ID string
  31. Path string
  32. ticker *time.Ticker
  33. tickerStopped chan struct{} // closed when ticker is stopped
  34. mtx sync.Mutex
  35. file *os.File
  36. }
  37. func OpenAutoFile(path string) (af *AutoFile, err error) {
  38. af = &AutoFile{
  39. ID: cmn.RandStr(12) + ":" + path,
  40. Path: path,
  41. ticker: time.NewTicker(autoFileOpenDuration),
  42. tickerStopped: make(chan struct{}),
  43. }
  44. if err = af.openFile(); err != nil {
  45. return
  46. }
  47. go af.processTicks()
  48. sighupWatchers.addAutoFile(af)
  49. return
  50. }
  51. func (af *AutoFile) Close() error {
  52. af.ticker.Stop()
  53. close(af.tickerStopped)
  54. err := af.closeFile()
  55. sighupWatchers.removeAutoFile(af)
  56. return err
  57. }
  58. func (af *AutoFile) processTicks() {
  59. select {
  60. case <-af.ticker.C:
  61. af.closeFile()
  62. case <-af.tickerStopped:
  63. return
  64. }
  65. }
  66. func (af *AutoFile) closeFile() (err error) {
  67. af.mtx.Lock()
  68. defer af.mtx.Unlock()
  69. file := af.file
  70. if file == nil {
  71. return nil
  72. }
  73. af.file = nil
  74. return file.Close()
  75. }
  76. func (af *AutoFile) Write(b []byte) (n int, err error) {
  77. af.mtx.Lock()
  78. defer af.mtx.Unlock()
  79. if af.file == nil {
  80. if err = af.openFile(); err != nil {
  81. return
  82. }
  83. }
  84. n, err = af.file.Write(b)
  85. return
  86. }
  87. func (af *AutoFile) Sync() error {
  88. af.mtx.Lock()
  89. defer af.mtx.Unlock()
  90. if af.file == nil {
  91. if err := af.openFile(); err != nil {
  92. return err
  93. }
  94. }
  95. return af.file.Sync()
  96. }
  97. func (af *AutoFile) openFile() error {
  98. file, err := os.OpenFile(af.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  99. if err != nil {
  100. return err
  101. }
  102. af.file = file
  103. return nil
  104. }
  105. func (af *AutoFile) Size() (int64, error) {
  106. af.mtx.Lock()
  107. defer af.mtx.Unlock()
  108. if af.file == nil {
  109. err := af.openFile()
  110. if err != nil {
  111. if err == os.ErrNotExist {
  112. return 0, nil
  113. }
  114. return -1, err
  115. }
  116. }
  117. stat, err := af.file.Stat()
  118. if err != nil {
  119. return -1, err
  120. }
  121. return stat.Size(), nil
  122. }