Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mumble-voip/grumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOla Bini <ola@autonomia.digital>2020-03-26 17:00:01 +0300
committerOla Bini <ola@autonomia.digital>2020-03-26 17:00:01 +0300
commit8d351aedb06a96bc27093b2d75cc91788bebb067 (patch)
tree12f75c6e8be3cbe87d02bfd3d1093f4173590fbd
parenta6dc45193af2512a7d51919708ad27e2c4f81236 (diff)
Extract a LogTarget interface to make it easier to manage logging
-rw-r--r--cmd/grumble/grumble.go2
-rw-r--r--cmd/grumble/server.go2
-rw-r--r--pkg/logtarget/logtarget.go20
3 files changed, 18 insertions, 6 deletions
diff --git a/cmd/grumble/grumble.go b/cmd/grumble/grumble.go
index 56eaa9a..c47e2de 100644
--- a/cmd/grumble/grumble.go
+++ b/cmd/grumble/grumble.go
@@ -44,7 +44,7 @@ func main() {
}
log.SetPrefix("[G] ")
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
- log.SetOutput(&logtarget.Target)
+ log.SetOutput(logtarget.Target)
log.Printf("Grumble")
log.Printf("Using data directory: %s", Args.DataDir)
diff --git a/cmd/grumble/server.go b/cmd/grumble/server.go
index 64b4dd8..7877830 100644
--- a/cmd/grumble/server.go
+++ b/cmd/grumble/server.go
@@ -156,7 +156,7 @@ func NewServer(id int64) (s *Server, err error) {
s.Channels[0] = NewChannel(0, "Root")
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
}
diff --git a/pkg/logtarget/logtarget.go b/pkg/logtarget/logtarget.go
index 46a2eb2..144654c 100644
--- a/pkg/logtarget/logtarget.go
+++ b/pkg/logtarget/logtarget.go
@@ -7,15 +7,23 @@ package logtarget
import (
"bytes"
+ "io"
"os"
"sync"
)
+type LogTarget interface {
+ io.Writer
+
+ OpenFile(string) error
+ Rotate() error
+}
+
// LogTarget implements the io.Writer interface, allowing
// LogTarget to be registered with the regular Go log package.
// LogTarget multiplexes its incoming writes to multiple optional
// output writers, and one main output writer (the log file).
-type LogTarget struct {
+type FileLogTarget struct {
mu sync.Mutex
logfn string
file *os.File
@@ -24,8 +32,12 @@ type LogTarget struct {
var Target LogTarget
+func init() {
+ Target = &FileLogTarget{}
+}
+
// 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()
defer target.mu.Unlock()
@@ -48,7 +60,7 @@ func (target *LogTarget) Write(in []byte) (int, error) {
// OpenFile opens the main log file for writing.
// 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.file, err = os.OpenFile(target.logfn, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0650)
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,
// and all log writes will be held back until the rotation
// is complete.
-func (target *LogTarget) Rotate() error {
+func (target *FileLogTarget) Rotate() error {
target.mu.Lock()
defer target.mu.Unlock()