mirror of https://github.com/fluffle/goirc
Minimally invasive change to put logging behind a user-replaceable interface.
This could probably be done better, and there are probably awful caveats and hidden gotchas with this approach. I REGRET NOTHING.
This commit is contained in:
parent
06a9cb5d0f
commit
5bb0c8278d
|
@ -4,8 +4,8 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fluffle/goirc/logging"
|
||||||
"github.com/fluffle/goirc/state"
|
"github.com/fluffle/goirc/state"
|
||||||
"github.com/fluffle/golog/logging"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -107,9 +107,13 @@ func SimpleClient(nick string, args ...string) *Conn {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Client(cfg *Config) *Conn {
|
func Client(cfg *Config) *Conn {
|
||||||
logging.InitFromFlags()
|
if cfg == nil {
|
||||||
if cfg == nil || cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
|
cfg = NewConfig("__idiot__")
|
||||||
logging.Fatal("irc.Client(): Both cfg.Nick and cfg.Ident must be non-empty.")
|
}
|
||||||
|
if cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
|
||||||
|
cfg.Me = state.NewNick("__idiot__")
|
||||||
|
cfg.Me.Ident = "goirc"
|
||||||
|
cfg.Me.Name = "Powered by GoIRC"
|
||||||
}
|
}
|
||||||
conn := &Conn{
|
conn := &Conn{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
@ -309,7 +313,7 @@ func (conn *Conn) write(line string) {
|
||||||
if !conn.cfg.Flood {
|
if !conn.cfg.Flood {
|
||||||
if t := conn.rateLimit(len(line)); t != 0 {
|
if t := conn.rateLimit(len(line)); t != 0 {
|
||||||
// sleep for the current line's time value before sending it
|
// sleep for the current line's time value before sending it
|
||||||
logging.Debug("irc.rateLimit(): Flood! Sleeping for %.2f secs.",
|
logging.Info("irc.rateLimit(): Flood! Sleeping for %.2f secs.",
|
||||||
t.Seconds())
|
t.Seconds())
|
||||||
<-time.After(t)
|
<-time.After(t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package client
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/gomock/gomock"
|
"code.google.com/p/gomock/gomock"
|
||||||
"github.com/fluffle/goirc/state"
|
"github.com/fluffle/goirc/state"
|
||||||
"github.com/fluffle/golog/logging"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -22,7 +21,6 @@ func setUp(t *testing.T, start ...bool) (*Conn, *testState) {
|
||||||
st := state.NewMockTracker(ctrl)
|
st := state.NewMockTracker(ctrl)
|
||||||
nc := MockNetConn(t)
|
nc := MockNetConn(t)
|
||||||
c := SimpleClient("test", "test", "Testing IRC")
|
c := SimpleClient("test", "test", "Testing IRC")
|
||||||
logging.SetLogLevel(logging.LogFatal)
|
|
||||||
|
|
||||||
c.st = st
|
c.st = st
|
||||||
c.sock = nc
|
c.sock = nc
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
|
@ -136,11 +136,11 @@ func (m *mockNetConn) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNetConn) LocalAddr() net.Addr {
|
func (m *mockNetConn) LocalAddr() net.Addr {
|
||||||
return &net.IPAddr{net.IPv4(127, 0, 0, 1)}
|
return &net.IPAddr{net.IPv4(127, 0, 0, 1), ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNetConn) RemoteAddr() net.Addr {
|
func (m *mockNetConn) RemoteAddr() net.Addr {
|
||||||
return &net.IPAddr{net.IPv4(127, 0, 0, 1)}
|
return &net.IPAddr{net.IPv4(127, 0, 0, 1), ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNetConn) SetDeadline(t time.Time) error {
|
func (m *mockNetConn) SetDeadline(t time.Time) error {
|
||||||
|
|
|
@ -4,7 +4,7 @@ package client
|
||||||
// to manage tracking state for an IRC connection
|
// to manage tracking state for an IRC connection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package logging
|
||||||
|
|
||||||
|
// The IRC client will log things using these methods
|
||||||
|
type Logger interface {
|
||||||
|
// Debug logging of raw socket comms to/from server.
|
||||||
|
Debug(format string, args ...interface{})
|
||||||
|
// Informational logging about client behaviour.
|
||||||
|
Info(format string, args ...interface{})
|
||||||
|
// Warnings of inconsistent or unexpected data, mostly
|
||||||
|
// related to state tracking of IRC nicks/chans.
|
||||||
|
Warn(format string, args ...interface{})
|
||||||
|
// Errors, mostly to do with network communication.
|
||||||
|
Error(format string, args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// By default we do no logging. Logging is enabled or disabled
|
||||||
|
// at the package level, since I'm lazy and re-re-reorganising
|
||||||
|
// my code to pass a per-client-struct Logger around to all the
|
||||||
|
// state objects is a pain in the arse.
|
||||||
|
var logger Logger = nullLogger{}
|
||||||
|
|
||||||
|
// SetLogger sets the internal goirc Logger to l. If l is nil,
|
||||||
|
// a dummy logger that does nothing is installed instead.
|
||||||
|
func SetLogger(l Logger) {
|
||||||
|
if l == nil {
|
||||||
|
logger = nullLogger{}
|
||||||
|
} else {
|
||||||
|
logger = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A nullLogger does nothing while fulfilling Logger.
|
||||||
|
type nullLogger struct{}
|
||||||
|
func (nl nullLogger) Debug(f string, a ...interface{}) {}
|
||||||
|
func (nl nullLogger) Info(f string, a ...interface{}) {}
|
||||||
|
func (nl nullLogger) Warn(f string, a ...interface{}) {}
|
||||||
|
func (nl nullLogger) Error(f string, a ...interface{}) {}
|
||||||
|
|
||||||
|
// Shim functions so that the package can be used directly
|
||||||
|
func Debug(f string, a ...interface{}) { logger.Debug(f, a...) }
|
||||||
|
func Info(f string, a ...interface{}) { logger.Info(f, a...) }
|
||||||
|
func Warn(f string, a ...interface{}) { logger.Warn(f, a...) }
|
||||||
|
func Error(f string, a ...interface{}) { logger.Error(f, a...) }
|
|
@ -2,7 +2,7 @@ package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The state manager interface
|
// The state manager interface
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fluffle/golog/logging"
|
"github.com/fluffle/goirc/logging"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue