goray /log/log.go

Language Go Lines 126
MD5 Hash e9a33a1c60ad11557fc14d7212fb375a
Repository https://bitbucket.org/zombiezen/goray View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
	Copyright (c) 2011 Ross Light.
	Copyright (c) 2005 Mathias Wein, Alejandro Conty, and Alfredo de Greef.

	This file is part of goray.

	goray is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	goray is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with goray.  If not, see <http://www.gnu.org/licenses/>.
*/

// Package log provides an interface for multi-level logging.
package log

import (
	"fmt"
	"io"
	"os"
	"sync"
)

// A type that implements Logger can output multi-level log messages.
type Logger interface {
	// Debugf formats its arguments according to the format, analogous to fmt.Printf,
	// and records the text as a log message at Debug level.
	Debugf(format string, args ...interface{})

	// Infof is like Debugf, but at Info level.
	Infof(format string, args ...interface{})

	// Warningf is like Debugf, but at Warning level.
	Warningf(format string, args ...interface{})

	// Errorf is like Debugf, but at Error level.
	Errorf(format string, args ...interface{})

	// Criticalf is like Debugf, but at Critical level.
	Criticalf(format string, args ...interface{})
}

// A writerLog is a logger that sends its messages to a writer.
type writerLog struct {
	w   io.Writer
	buf []byte
	sync.Mutex
}

// New creates a logger that serializes writes to w.
func New(w io.Writer) Logger {
	return &writerLog{w: w}
}

func (l *writerLog) output(s string) error {
	l.Lock()
	defer l.Unlock()
	l.buf = l.buf[:0]
	l.buf = append(l.buf, s...)
	if len(s) > 0 && s[len(s)-1] != '\n' {
		l.buf = append(l.buf, '\n')
	}
	_, err := l.w.Write(l.buf)
	return err
}

func (l *writerLog) Debugf(format string, args ...interface{}) {
	l.output("DEBUG: " + fmt.Sprintf(format, args...))
}

func (l *writerLog) Infof(format string, args ...interface{}) {
	l.output("INFO: " + fmt.Sprintf(format, args...))
}

func (l *writerLog) Warningf(format string, args ...interface{}) {
	l.output("WARNING: " + fmt.Sprintf(format, args...))
}

func (l *writerLog) Errorf(format string, args ...interface{}) {
	l.output("ERROR: " + fmt.Sprintf(format, args...))
}

func (l *writerLog) Criticalf(format string, args ...interface{}) {
	l.output("CRITICAL: " + fmt.Sprintf(format, args...))
}

// Default logger
var Default Logger = New(os.Stderr)

// Debugf calls Debugf on the default logger. Arguments are handled in the
// manner of fmt.Printf.
func Debugf(format string, args ...interface{}) {
	Default.Debugf(format, args...)
}

// Infof calls Infof on the default logger. Arguments are handled in the
// manner of fmt.Printf.
func Infof(format string, args ...interface{}) {
	Default.Infof(format, args...)
}

// Warningf calls Warningf on the default logger. Arguments are handled in the
// manner of fmt.Printf.
func Warningf(format string, args ...interface{}) {
	Default.Warningf(format, args...)
}

// Errorf calls Errorf on the default logger. Arguments are handled in the
// manner of fmt.Printf.
func Errorf(format string, args ...interface{}) {
	Default.Errorf(format, args...)
}

// Criticalf calls Criticalf on the default logger. Arguments are handled in the
// manner of fmt.Printf.
func Criticalf(format string, args ...interface{}) {
	Default.Criticalf(format, args...)
}
Back to Top