PageRenderTime 81ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/log/log.go

https://bitbucket.org/zombiezen/goray
Go | 125 lines | 64 code | 21 blank | 40 comment | 4 complexity | e9a33a1c60ad11557fc14d7212fb375a MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright (c) 2011 Ross Light.
  3. Copyright (c) 2005 Mathias Wein, Alejandro Conty, and Alfredo de Greef.
  4. This file is part of goray.
  5. goray is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. goray is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with goray. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // Package log provides an interface for multi-level logging.
  17. package log
  18. import (
  19. "fmt"
  20. "io"
  21. "os"
  22. "sync"
  23. )
  24. // A type that implements Logger can output multi-level log messages.
  25. type Logger interface {
  26. // Debugf formats its arguments according to the format, analogous to fmt.Printf,
  27. // and records the text as a log message at Debug level.
  28. Debugf(format string, args ...interface{})
  29. // Infof is like Debugf, but at Info level.
  30. Infof(format string, args ...interface{})
  31. // Warningf is like Debugf, but at Warning level.
  32. Warningf(format string, args ...interface{})
  33. // Errorf is like Debugf, but at Error level.
  34. Errorf(format string, args ...interface{})
  35. // Criticalf is like Debugf, but at Critical level.
  36. Criticalf(format string, args ...interface{})
  37. }
  38. // A writerLog is a logger that sends its messages to a writer.
  39. type writerLog struct {
  40. w io.Writer
  41. buf []byte
  42. sync.Mutex
  43. }
  44. // New creates a logger that serializes writes to w.
  45. func New(w io.Writer) Logger {
  46. return &writerLog{w: w}
  47. }
  48. func (l *writerLog) output(s string) error {
  49. l.Lock()
  50. defer l.Unlock()
  51. l.buf = l.buf[:0]
  52. l.buf = append(l.buf, s...)
  53. if len(s) > 0 && s[len(s)-1] != '\n' {
  54. l.buf = append(l.buf, '\n')
  55. }
  56. _, err := l.w.Write(l.buf)
  57. return err
  58. }
  59. func (l *writerLog) Debugf(format string, args ...interface{}) {
  60. l.output("DEBUG: " + fmt.Sprintf(format, args...))
  61. }
  62. func (l *writerLog) Infof(format string, args ...interface{}) {
  63. l.output("INFO: " + fmt.Sprintf(format, args...))
  64. }
  65. func (l *writerLog) Warningf(format string, args ...interface{}) {
  66. l.output("WARNING: " + fmt.Sprintf(format, args...))
  67. }
  68. func (l *writerLog) Errorf(format string, args ...interface{}) {
  69. l.output("ERROR: " + fmt.Sprintf(format, args...))
  70. }
  71. func (l *writerLog) Criticalf(format string, args ...interface{}) {
  72. l.output("CRITICAL: " + fmt.Sprintf(format, args...))
  73. }
  74. // Default logger
  75. var Default Logger = New(os.Stderr)
  76. // Debugf calls Debugf on the default logger. Arguments are handled in the
  77. // manner of fmt.Printf.
  78. func Debugf(format string, args ...interface{}) {
  79. Default.Debugf(format, args...)
  80. }
  81. // Infof calls Infof on the default logger. Arguments are handled in the
  82. // manner of fmt.Printf.
  83. func Infof(format string, args ...interface{}) {
  84. Default.Infof(format, args...)
  85. }
  86. // Warningf calls Warningf on the default logger. Arguments are handled in the
  87. // manner of fmt.Printf.
  88. func Warningf(format string, args ...interface{}) {
  89. Default.Warningf(format, args...)
  90. }
  91. // Errorf calls Errorf on the default logger. Arguments are handled in the
  92. // manner of fmt.Printf.
  93. func Errorf(format string, args ...interface{}) {
  94. Default.Errorf(format, args...)
  95. }
  96. // Criticalf calls Criticalf on the default logger. Arguments are handled in the
  97. // manner of fmt.Printf.
  98. func Criticalf(format string, args ...interface{}) {
  99. Default.Criticalf(format, args...)
  100. }