log.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package log
  2. import (
  3. "fmt"
  4. "github.com/davecgh/go-spew/spew"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "time"
  10. )
  11. var level = 0
  12. const (
  13. // TRACE level
  14. TRACE = 0
  15. // DEBUG level
  16. DEBUG = 1
  17. // INFO level
  18. INFO = 2
  19. // WARNING level
  20. WARNING = 3
  21. // ERROR level
  22. ERROR = 4
  23. // FATAL level
  24. FATAL = 5
  25. )
  26. // Stringer interface
  27. type Stringer interface {
  28. LogString() string
  29. }
  30. // Init logger
  31. func Init(plevel int) {
  32. level = plevel
  33. spew.Config.DisableMethods = true
  34. spew.Config.Indent = " "
  35. }
  36. // Dumpf object
  37. func Dumpf(format string, a ...interface{}) string {
  38. return spew.Sprintf(format, a...)
  39. }
  40. // Dump object
  41. func Dump(a ...interface{}) string {
  42. return spew.Sdump(a...)
  43. }
  44. func dump(a ...interface{}) []interface{} {
  45. for ai, av := range a {
  46. if avf, ok := av.(func() string); ok {
  47. a[ai] = avf()
  48. } else if avf, ok := av.(Stringer); ok {
  49. a[ai] = avf.LogString()
  50. } else if avf, ok := av.(fmt.GoStringer); ok {
  51. a[ai] = avf.GoString()
  52. } else if avf, ok := av.(fmt.Stringer); ok {
  53. a[ai] = avf.String()
  54. }
  55. }
  56. return a
  57. }
  58. // Trace log
  59. func Trace(a ...interface{}) {
  60. if level <= TRACE {
  61. for i := 1; i > 0 && i < 100; i++ {
  62. _, path, _, _ := runtime.Caller(i)
  63. if path != "" {
  64. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  65. _, path, line, _ := runtime.Caller(i + 1)
  66. _, file := filepath.Split(path)
  67. fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  68. return
  69. }
  70. } else {
  71. i = -999
  72. }
  73. }
  74. _, path, line, _ := runtime.Caller(1)
  75. _, file := filepath.Split(path)
  76. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  77. }
  78. }
  79. // Tracef log
  80. func Tracef(format string, a ...interface{}) {
  81. if level <= TRACE {
  82. for i := 1; i > 0 && i < 100; i++ {
  83. _, path, _, _ := runtime.Caller(i)
  84. if path != "" {
  85. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  86. _, path, line, _ := runtime.Caller(i + 1)
  87. _, file := filepath.Split(path)
  88. fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  89. return
  90. }
  91. } else {
  92. i = -999
  93. }
  94. }
  95. _, path, line, _ := runtime.Caller(1)
  96. _, file := filepath.Split(path)
  97. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  98. }
  99. }
  100. // Debug log
  101. func Debug(a ...interface{}) {
  102. if level <= DEBUG {
  103. for i := 1; i > 0 && i < 100; i++ {
  104. _, path, _, _ := runtime.Caller(i)
  105. if path != "" {
  106. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  107. _, path, line, _ := runtime.Caller(i + 1)
  108. _, file := filepath.Split(path)
  109. fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  110. return
  111. }
  112. } else {
  113. i = -999
  114. }
  115. }
  116. _, path, line, _ := runtime.Caller(1)
  117. _, file := filepath.Split(path)
  118. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  119. }
  120. }
  121. // Debugf log
  122. func Debugf(format string, a ...interface{}) {
  123. if level <= DEBUG {
  124. for i := 1; i > 0 && i < 100; i++ {
  125. _, path, _, _ := runtime.Caller(i)
  126. if path != "" {
  127. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  128. _, path, line, _ := runtime.Caller(i + 1)
  129. _, file := filepath.Split(path)
  130. fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  131. return
  132. }
  133. } else {
  134. i = -999
  135. }
  136. }
  137. _, path, line, _ := runtime.Caller(1)
  138. _, file := filepath.Split(path)
  139. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  140. }
  141. }
  142. // Info log
  143. func Info(a ...interface{}) {
  144. if level <= INFO {
  145. for i := 1; i > 0 && i < 100; i++ {
  146. _, path, _, _ := runtime.Caller(i)
  147. if path != "" {
  148. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  149. _, path, line, _ := runtime.Caller(i + 1)
  150. _, file := filepath.Split(path)
  151. fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  152. return
  153. }
  154. } else {
  155. i = -999
  156. }
  157. }
  158. _, path, line, _ := runtime.Caller(1)
  159. _, file := filepath.Split(path)
  160. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  161. }
  162. }
  163. // Infof log
  164. func Infof(format string, a ...interface{}) {
  165. if level <= INFO {
  166. for i := 1; i > 0 && i < 100; i++ {
  167. _, path, _, _ := runtime.Caller(i)
  168. if path != "" {
  169. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  170. _, path, line, _ := runtime.Caller(i + 1)
  171. _, file := filepath.Split(path)
  172. fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  173. return
  174. }
  175. } else {
  176. i = -999
  177. }
  178. }
  179. _, path, line, _ := runtime.Caller(1)
  180. _, file := filepath.Split(path)
  181. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  182. }
  183. }
  184. // Warning log
  185. func Warning(a ...interface{}) {
  186. if level <= WARNING {
  187. for i := 1; i > 0 && i < 100; i++ {
  188. _, path, _, _ := runtime.Caller(i)
  189. if path != "" {
  190. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  191. _, path, line, _ := runtime.Caller(i + 1)
  192. _, file := filepath.Split(path)
  193. fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  194. return
  195. }
  196. } else {
  197. i = -999
  198. }
  199. }
  200. _, path, line, _ := runtime.Caller(1)
  201. _, file := filepath.Split(path)
  202. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  203. }
  204. }
  205. // Warningf log
  206. func Warningf(format string, a ...interface{}) {
  207. if level <= WARNING {
  208. for i := 1; i > 0 && i < 100; i++ {
  209. _, path, _, _ := runtime.Caller(i)
  210. if path != "" {
  211. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  212. _, path, line, _ := runtime.Caller(i + 1)
  213. _, file := filepath.Split(path)
  214. fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  215. return
  216. }
  217. } else {
  218. i = -999
  219. }
  220. }
  221. _, path, line, _ := runtime.Caller(1)
  222. _, file := filepath.Split(path)
  223. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  224. }
  225. }
  226. // Error log
  227. func Error(a ...interface{}) {
  228. if level <= ERROR {
  229. for i := 1; i > 0 && i < 100; i++ {
  230. _, path, _, _ := runtime.Caller(i)
  231. if path != "" {
  232. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  233. _, path, line, _ := runtime.Caller(i + 1)
  234. _, file := filepath.Split(path)
  235. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  236. return
  237. }
  238. } else {
  239. i = -999
  240. }
  241. }
  242. _, path, line, _ := runtime.Caller(1)
  243. _, file := filepath.Split(path)
  244. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  245. }
  246. }
  247. // Errorf log
  248. func Errorf(format string, a ...interface{}) {
  249. if level <= ERROR {
  250. for i := 1; i > 0 && i < 100; i++ {
  251. _, path, _, _ := runtime.Caller(i)
  252. if path != "" {
  253. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  254. _, path, line, _ := runtime.Caller(i + 1)
  255. _, file := filepath.Split(path)
  256. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  257. return
  258. }
  259. } else {
  260. i = -999
  261. }
  262. }
  263. _, path, line, _ := runtime.Caller(1)
  264. _, file := filepath.Split(path)
  265. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  266. }
  267. }