console.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const CONSOLE MODE = "console"
  20. // Console color set for different levels.
  21. var consoleColors = []func(a ...interface{}) string{
  22. color.New(color.FgBlue).SprintFunc(), // Trace
  23. color.New(color.FgGreen).SprintFunc(), // Info
  24. color.New(color.FgYellow).SprintFunc(), // Warn
  25. color.New(color.FgRed).SprintFunc(), // Error
  26. color.New(color.FgHiRed).SprintFunc(), // Fatal
  27. }
  28. type ConsoleConfig struct {
  29. // Minimum level of messages to be processed.
  30. Level LEVEL
  31. // Buffer size defines how many messages can be queued before hangs.
  32. BufferSize int64
  33. }
  34. type console struct {
  35. *log.Logger
  36. Adapter
  37. }
  38. func newConsole() Logger {
  39. return &console{
  40. Logger: log.New(color.Output, "", log.Ldate|log.Ltime),
  41. Adapter: Adapter{
  42. quitChan: make(chan struct{}),
  43. },
  44. }
  45. }
  46. func (c *console) Level() LEVEL { return c.level }
  47. func (c *console) Init(v interface{}) error {
  48. cfg, ok := v.(ConsoleConfig)
  49. if !ok {
  50. return ErrConfigObject{"ConsoleConfig", v}
  51. }
  52. if !isValidLevel(cfg.Level) {
  53. return ErrInvalidLevel{}
  54. }
  55. c.level = cfg.Level
  56. c.msgChan = make(chan *Message, cfg.BufferSize)
  57. return nil
  58. }
  59. func (c *console) ExchangeChans(errorChan chan<- error) chan *Message {
  60. c.errorChan = errorChan
  61. return c.msgChan
  62. }
  63. func (c *console) write(msg *Message) {
  64. c.Logger.Print(consoleColors[msg.Level](msg.Body))
  65. }
  66. func (c *console) Start() {
  67. LOOP:
  68. for {
  69. select {
  70. case msg := <-c.msgChan:
  71. c.write(msg)
  72. case <-c.quitChan:
  73. break LOOP
  74. }
  75. }
  76. for {
  77. if len(c.msgChan) == 0 {
  78. break
  79. }
  80. c.write(<-c.msgChan)
  81. }
  82. c.quitChan <- struct{}{} // Notify the cleanup is done.
  83. }
  84. func (c *console) Destroy() {
  85. c.quitChan <- struct{}{}
  86. <-c.quitChan
  87. close(c.msgChan)
  88. close(c.quitChan)
  89. }
  90. func init() {
  91. Register(CONSOLE, newConsole)
  92. }