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. //
  17. package logging
  18. import (
  19. "bytes"
  20. "fmt"
  21. "sync/atomic"
  22. "time"
  23. )
  24. // watcher watches the logger.queue channel, and writes the logs to output
  25. func (logger *Logger) watcher() {
  26. var buf bytes.Buffer
  27. for {
  28. timeout := time.After(time.Second / 10)
  29. for i := 0; i < bufSize; i++ {
  30. select {
  31. case msg := <-logger.queue:
  32. fmt.Fprintln(&buf, msg)
  33. case req := <-logger.request:
  34. logger.flushReq(&buf, &req)
  35. case <-timeout:
  36. i = bufSize
  37. case <-logger.flush:
  38. logger.flushBuf(&buf)
  39. logger.flush <- true
  40. i = bufSize
  41. case <-logger.quit:
  42. // If quit signal received, cleans the channel
  43. // and writes all of them to io.Writer.
  44. for {
  45. select {
  46. case msg := <-logger.queue:
  47. fmt.Fprintln(&buf, msg)
  48. case req := <-logger.request:
  49. logger.flushReq(&buf, &req)
  50. case <-logger.flush:
  51. // do nothing
  52. default:
  53. logger.flushBuf(&buf)
  54. logger.quit <- true
  55. return
  56. }
  57. }
  58. }
  59. }
  60. logger.flushBuf(&buf)
  61. }
  62. }
  63. // flushBuf flushes the content of buffer to out and reset the buffer
  64. func (logger *Logger) flushBuf(b *bytes.Buffer) {
  65. if len(b.Bytes()) > 0 {
  66. logger.out.Write(b.Bytes())
  67. b.Reset()
  68. }
  69. }
  70. // flushReq handles the request and writes the result to writer
  71. func (logger *Logger) flushReq(b *bytes.Buffer, req *request) {
  72. if req.format == "" {
  73. msg := fmt.Sprint(req.v...)
  74. msg = logger.genLog(req.level, msg)
  75. fmt.Fprintln(b, msg)
  76. } else {
  77. msg := fmt.Sprintf(req.format, req.v...)
  78. msg = logger.genLog(req.level, msg)
  79. fmt.Fprintln(b, msg)
  80. }
  81. }
  82. // flushMsg is to print log to file, stdout, or others.
  83. func (logger *Logger) flushMsg(message string) {
  84. if logger.sync {
  85. logger.wlock.Lock()
  86. defer logger.wlock.Unlock()
  87. fmt.Fprintln(logger.out, message)
  88. } else {
  89. logger.queue <- message
  90. }
  91. }
  92. // log records log v... with level `level'.
  93. func (logger *Logger) log(level Level, v ...interface{}) {
  94. if int32(level) >= atomic.LoadInt32((*int32)(&logger.level)) {
  95. if logger.runtime || logger.sync {
  96. message := fmt.Sprint(v...)
  97. message = logger.genLog(level, message)
  98. logger.flushMsg(message)
  99. } else {
  100. r := new(request)
  101. r.level = level
  102. r.v = v
  103. logger.request <- *r
  104. }
  105. }
  106. }
  107. // logf records log v... with level `level'.
  108. func (logger *Logger) logf(level Level, format string, v ...interface{}) {
  109. if int32(level) >= atomic.LoadInt32((*int32)(&logger.level)) {
  110. if logger.runtime || logger.sync {
  111. message := fmt.Sprintf(format, v...)
  112. message = logger.genLog(level, message)
  113. logger.flushMsg(message)
  114. } else {
  115. r := new(request)
  116. r.level = level
  117. r.format = format
  118. r.v = v
  119. logger.request <- *r
  120. }
  121. }
  122. }