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.

142 lines
2.3 KiB

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/tmlibs/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. mtx sync.Mutex
  34. file *os.File
  35. }
  36. func OpenAutoFile(path string) (af *AutoFile, err error) {
  37. af = &AutoFile{
  38. ID: cmn.RandStr(12) + ":" + path,
  39. Path: path,
  40. ticker: time.NewTicker(autoFileOpenDuration),
  41. }
  42. if err = af.openFile(); err != nil {
  43. return
  44. }
  45. go af.processTicks()
  46. sighupWatchers.addAutoFile(af)
  47. return
  48. }
  49. func (af *AutoFile) Close() error {
  50. af.ticker.Stop()
  51. err := af.closeFile()
  52. sighupWatchers.removeAutoFile(af)
  53. return err
  54. }
  55. func (af *AutoFile) processTicks() {
  56. for {
  57. _, ok := <-af.ticker.C
  58. if !ok {
  59. return // Done.
  60. }
  61. af.closeFile()
  62. }
  63. }
  64. func (af *AutoFile) closeFile() (err error) {
  65. af.mtx.Lock()
  66. defer af.mtx.Unlock()
  67. file := af.file
  68. if file == nil {
  69. return nil
  70. }
  71. af.file = nil
  72. return file.Close()
  73. }
  74. func (af *AutoFile) Write(b []byte) (n int, err error) {
  75. af.mtx.Lock()
  76. defer af.mtx.Unlock()
  77. if af.file == nil {
  78. if err = af.openFile(); err != nil {
  79. return
  80. }
  81. }
  82. n, err = af.file.Write(b)
  83. return
  84. }
  85. func (af *AutoFile) Sync() error {
  86. af.mtx.Lock()
  87. defer af.mtx.Unlock()
  88. if af.file == nil {
  89. if err := af.openFile(); err != nil {
  90. return err
  91. }
  92. }
  93. return af.file.Sync()
  94. }
  95. func (af *AutoFile) openFile() error {
  96. file, err := os.OpenFile(af.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  97. if err != nil {
  98. return err
  99. }
  100. af.file = file
  101. return nil
  102. }
  103. func (af *AutoFile) Size() (int64, error) {
  104. af.mtx.Lock()
  105. defer af.mtx.Unlock()
  106. if af.file == nil {
  107. err := af.openFile()
  108. if err != nil {
  109. if err == os.ErrNotExist {
  110. return 0, nil
  111. }
  112. return -1, err
  113. }
  114. }
  115. stat, err := af.file.Stat()
  116. if err != nil {
  117. return -1, err
  118. }
  119. return stat.Size(), nil
  120. }