forked from External/grumble
Extract a LogTarget interface to make it easier to manage logging
This commit is contained in:
parent
a6dc45193a
commit
8d351aedb0
3 changed files with 18 additions and 6 deletions
|
|
@ -44,7 +44,7 @@ func main() {
|
||||||
}
|
}
|
||||||
log.SetPrefix("[G] ")
|
log.SetPrefix("[G] ")
|
||||||
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
||||||
log.SetOutput(&logtarget.Target)
|
log.SetOutput(logtarget.Target)
|
||||||
log.Printf("Grumble")
|
log.Printf("Grumble")
|
||||||
log.Printf("Using data directory: %s", Args.DataDir)
|
log.Printf("Using data directory: %s", Args.DataDir)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ func NewServer(id int64) (s *Server, err error) {
|
||||||
s.Channels[0] = NewChannel(0, "Root")
|
s.Channels[0] = NewChannel(0, "Root")
|
||||||
s.nextChanId = 1
|
s.nextChanId = 1
|
||||||
|
|
||||||
s.Logger = log.New(&logtarget.Target, fmt.Sprintf("[%v] ", s.Id), log.LstdFlags|log.Lmicroseconds)
|
s.Logger = log.New(logtarget.Target, fmt.Sprintf("[%v] ", s.Id), log.LstdFlags|log.Lmicroseconds)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,23 @@ package logtarget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type LogTarget interface {
|
||||||
|
io.Writer
|
||||||
|
|
||||||
|
OpenFile(string) error
|
||||||
|
Rotate() error
|
||||||
|
}
|
||||||
|
|
||||||
// LogTarget implements the io.Writer interface, allowing
|
// LogTarget implements the io.Writer interface, allowing
|
||||||
// LogTarget to be registered with the regular Go log package.
|
// LogTarget to be registered with the regular Go log package.
|
||||||
// LogTarget multiplexes its incoming writes to multiple optional
|
// LogTarget multiplexes its incoming writes to multiple optional
|
||||||
// output writers, and one main output writer (the log file).
|
// output writers, and one main output writer (the log file).
|
||||||
type LogTarget struct {
|
type FileLogTarget struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
logfn string
|
logfn string
|
||||||
file *os.File
|
file *os.File
|
||||||
|
|
@ -24,8 +32,12 @@ type LogTarget struct {
|
||||||
|
|
||||||
var Target LogTarget
|
var Target LogTarget
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Target = &FileLogTarget{}
|
||||||
|
}
|
||||||
|
|
||||||
// Write writes a log message to all registered io.Writers
|
// Write writes a log message to all registered io.Writers
|
||||||
func (target *LogTarget) Write(in []byte) (int, error) {
|
func (target *FileLogTarget) Write(in []byte) (int, error) {
|
||||||
target.mu.Lock()
|
target.mu.Lock()
|
||||||
defer target.mu.Unlock()
|
defer target.mu.Unlock()
|
||||||
|
|
||||||
|
|
@ -48,7 +60,7 @@ func (target *LogTarget) Write(in []byte) (int, error) {
|
||||||
|
|
||||||
// OpenFile opens the main log file for writing.
|
// OpenFile opens the main log file for writing.
|
||||||
// This method will open the file in append-only mode.
|
// This method will open the file in append-only mode.
|
||||||
func (target *LogTarget) OpenFile(fn string) (err error) {
|
func (target *FileLogTarget) OpenFile(fn string) (err error) {
|
||||||
target.logfn = fn
|
target.logfn = fn
|
||||||
target.file, err = os.OpenFile(target.logfn, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0650)
|
target.file, err = os.OpenFile(target.logfn, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0650)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -61,7 +73,7 @@ func (target *LogTarget) OpenFile(fn string) (err error) {
|
||||||
// This method holds a lock while rotating the log file,
|
// This method holds a lock while rotating the log file,
|
||||||
// and all log writes will be held back until the rotation
|
// and all log writes will be held back until the rotation
|
||||||
// is complete.
|
// is complete.
|
||||||
func (target *LogTarget) Rotate() error {
|
func (target *FileLogTarget) Rotate() error {
|
||||||
target.mu.Lock()
|
target.mu.Lock()
|
||||||
defer target.mu.Unlock()
|
defer target.mu.Unlock()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue