clog.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Clog is a channel-based logging package for Go.
  15. package clog
  16. import (
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. )
  23. const (
  24. _VERSION = "1.1.0"
  25. )
  26. // Version returns current version of the package.
  27. func Version() string {
  28. return _VERSION
  29. }
  30. type (
  31. MODE string
  32. LEVEL int
  33. )
  34. const (
  35. TRACE LEVEL = iota
  36. INFO
  37. WARN
  38. ERROR
  39. FATAL
  40. )
  41. var formats = map[LEVEL]string{
  42. TRACE: "[TRACE] ",
  43. INFO: "[ INFO] ",
  44. WARN: "[ WARN] ",
  45. ERROR: "[ERROR] ",
  46. FATAL: "[FATAL] ",
  47. }
  48. // isValidLevel returns true if given level is in the valid range.
  49. func isValidLevel(level LEVEL) bool {
  50. return level >= TRACE && level <= FATAL
  51. }
  52. // Message represents a log message to be processed.
  53. type Message struct {
  54. Level LEVEL
  55. Body string
  56. }
  57. func Write(level LEVEL, skip int, format string, v ...interface{}) {
  58. msg := &Message{
  59. Level: level,
  60. }
  61. // Only error and fatal information needs locate position for debugging.
  62. // But if skip is 0 means caller doesn't care so we can skip.
  63. if msg.Level >= ERROR && skip > 0 {
  64. pc, file, line, ok := runtime.Caller(skip)
  65. if ok {
  66. // Get caller function name.
  67. fn := runtime.FuncForPC(pc)
  68. var fnName string
  69. if fn == nil {
  70. fnName = "?()"
  71. } else {
  72. fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".") + "()"
  73. }
  74. if len(file) > 20 {
  75. file = "..." + file[len(file)-20:]
  76. }
  77. msg.Body = formats[level] + fmt.Sprintf("[%s:%d %s] ", file, line, fnName) + fmt.Sprintf(format, v...)
  78. }
  79. }
  80. if len(msg.Body) == 0 {
  81. msg.Body = formats[level] + fmt.Sprintf(format, v...)
  82. }
  83. for i := range receivers {
  84. if receivers[i].Level() > level {
  85. continue
  86. }
  87. receivers[i].msgChan <- msg
  88. }
  89. }
  90. func Trace(format string, v ...interface{}) {
  91. Write(TRACE, 0, format, v...)
  92. }
  93. func Info(format string, v ...interface{}) {
  94. Write(INFO, 0, format, v...)
  95. }
  96. func Warn(format string, v ...interface{}) {
  97. Write(WARN, 0, format, v...)
  98. }
  99. func Error(skip int, format string, v ...interface{}) {
  100. Write(ERROR, skip, format, v...)
  101. }
  102. func Fatal(skip int, format string, v ...interface{}) {
  103. Write(FATAL, skip, format, v...)
  104. Shutdown()
  105. os.Exit(1)
  106. }
  107. func Shutdown() {
  108. for i := range receivers {
  109. receivers[i].Destroy()
  110. }
  111. // Shutdown the error handling goroutine.
  112. quitChan <- struct{}{}
  113. for {
  114. if len(errorChan) == 0 {
  115. break
  116. }
  117. fmt.Printf("clog: unable to write message: %v\n", <-errorChan)
  118. }
  119. }