console.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // 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, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package clog
  15. import (
  16. "log"
  17. "github.com/fatih/color"
  18. )
  19. // Console color set for different levels.
  20. var consoleColors = []func(a ...interface{}) string{
  21. color.New(color.FgBlue).SprintFunc(), // Trace
  22. color.New(color.FgGreen).SprintFunc(), // Info
  23. color.New(color.FgYellow).SprintFunc(), // Warn
  24. color.New(color.FgRed).SprintFunc(), // Error
  25. color.New(color.FgHiRed).SprintFunc(), // Fatal
  26. }
  27. type ConsoleConfig struct {
  28. // Minimum level of messages to be processed.
  29. Level LEVEL
  30. // Buffer size defines how many messages can be queued before hangs.
  31. BufferSize int64
  32. }
  33. type console struct {
  34. *log.Logger
  35. Adapter
  36. }
  37. func newConsole() Logger {
  38. return &console{
  39. Logger: log.New(color.Output, "", log.Ldate|log.Ltime),
  40. Adapter: Adapter{
  41. quitChan: make(chan struct{}),
  42. },
  43. }
  44. }
  45. func (c *console) Level() LEVEL { return c.level }
  46. func (c *console) Init(v interface{}) error {
  47. cfg, ok := v.(ConsoleConfig)
  48. if !ok {
  49. return ErrConfigObject{"ConsoleConfig", v}
  50. }
  51. if !isValidLevel(cfg.Level) {
  52. return ErrInvalidLevel{}
  53. }
  54. c.level = cfg.Level
  55. c.msgChan = make(chan *Message, cfg.BufferSize)
  56. return nil
  57. }
  58. func (c *console) ExchangeChans(errorChan chan<- error) chan *Message {
  59. c.errorChan = errorChan
  60. return c.msgChan
  61. }
  62. func (c *console) write(msg *Message) {
  63. c.Logger.Print(consoleColors[msg.Level](msg.Body))
  64. }
  65. func (c *console) Start() {
  66. LOOP:
  67. for {
  68. select {
  69. case msg := <-c.msgChan:
  70. c.write(msg)
  71. case <-c.quitChan:
  72. break LOOP
  73. }
  74. }
  75. for {
  76. if len(c.msgChan) == 0 {
  77. break
  78. }
  79. c.write(<-c.msgChan)
  80. }
  81. c.quitChan <- struct{}{} // Notify the cleanup is done.
  82. }
  83. func (c *console) Destroy() {
  84. c.quitChan <- struct{}{}
  85. <-c.quitChan
  86. close(c.msgChan)
  87. close(c.quitChan)
  88. }
  89. func init() {
  90. Register(CONSOLE, newConsole)
  91. }