writer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2013, Cong Ding. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // author: Cong Ding <dinggnu@gmail.com>
  16. package logging
  17. import (
  18. "bytes"
  19. "fmt"
  20. "sync/atomic"
  21. "time"
  22. )
  23. // watcher watches the logger.queue channel, and writes the logs to output
  24. func (logger *Logger) watcher() {
  25. var buf bytes.Buffer
  26. for {
  27. timeout := time.After(time.Second / 10)
  28. for i := 0; i < bufSize; i++ {
  29. select {
  30. case msg := <-logger.queue:
  31. fmt.Fprintln(&buf, msg)
  32. case req := <-logger.request:
  33. logger.flushReq(&buf, &req)
  34. case <-timeout:
  35. i = bufSize
  36. case <-logger.flush:
  37. logger.flushBuf(&buf)
  38. logger.flush <- true
  39. i = bufSize
  40. case <-logger.quit:
  41. // If quit signal received, cleans the channel
  42. // and writes all of them to io.Writer.
  43. for {
  44. select {
  45. case msg := <-logger.queue:
  46. fmt.Fprintln(&buf, msg)
  47. case req := <-logger.request:
  48. logger.flushReq(&buf, &req)
  49. case <-logger.flush:
  50. // do nothing
  51. default:
  52. logger.flushBuf(&buf)
  53. logger.quit <- true
  54. return
  55. }
  56. }
  57. }
  58. }
  59. logger.flushBuf(&buf)
  60. }
  61. }
  62. // flushBuf flushes the content of buffer to out and reset the buffer
  63. func (logger *Logger) flushBuf(b *bytes.Buffer) {
  64. if len(b.Bytes()) > 0 {
  65. logger.out.Write(b.Bytes())
  66. b.Reset()
  67. }
  68. }
  69. // flushReq handles the request and writes the result to writer
  70. func (logger *Logger) flushReq(b *bytes.Buffer, req *request) {
  71. if req.format == "" {
  72. msg := fmt.Sprint(req.v...)
  73. msg = logger.genLog(req.level, msg)
  74. fmt.Fprintln(b, msg)
  75. } else {
  76. msg := fmt.Sprintf(req.format, req.v...)
  77. msg = logger.genLog(req.level, msg)
  78. fmt.Fprintln(b, msg)
  79. }
  80. }
  81. // flushMsg is to print log to file, stdout, or others.
  82. func (logger *Logger) flushMsg(message string) {
  83. if logger.sync {
  84. logger.wlock.Lock()
  85. defer logger.wlock.Unlock()
  86. fmt.Fprintln(logger.out, message)
  87. } else {
  88. logger.queue <- message
  89. }
  90. }
  91. // log records log v... with level `level'.
  92. func (logger *Logger) log(level Level, v ...interface{}) {
  93. if int32(level) >= atomic.LoadInt32((*int32)(&logger.level)) {
  94. if logger.runtime || logger.sync {
  95. message := fmt.Sprint(v...)
  96. message = logger.genLog(level, message)
  97. logger.flushMsg(message)
  98. } else {
  99. r := new(request)
  100. r.level = level
  101. r.v = v
  102. logger.request <- *r
  103. }
  104. }
  105. }
  106. // logf records log v... with level `level'.
  107. func (logger *Logger) logf(level Level, format string, v ...interface{}) {
  108. if int32(level) >= atomic.LoadInt32((*int32)(&logger.level)) {
  109. if logger.runtime || logger.sync {
  110. message := fmt.Sprintf(format, v...)
  111. message = logger.genLog(level, message)
  112. logger.flushMsg(message)
  113. } else {
  114. r := new(request)
  115. r.level = level
  116. r.format = format
  117. r.v = v
  118. logger.request <- *r
  119. }
  120. }
  121. }