From e15f47c7663eceb63233aff94d46d0b2ee378f86 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 8 Mar 2014 15:01:15 -0800 Subject: [PATCH] move to channel-based logging --- ergonomadic.conf | 8 ++---- ergonomadic.go | 7 +---- irc/client.go | 5 +--- irc/client_lookup_set.go | 24 ++++------------ irc/config.go | 8 +----- irc/constants.go | 6 ---- irc/logging.go | 60 ++++++++++++++++++++++++++++++++++++++++ irc/server.go | 16 +++-------- irc/socket.go | 17 ++++-------- 9 files changed, 80 insertions(+), 71 deletions(-) create mode 100644 irc/logging.go diff --git a/ergonomadic.conf b/ergonomadic.conf index 2f360024..528d4211 100644 --- a/ergonomadic.conf +++ b/ergonomadic.conf @@ -3,14 +3,10 @@ name = "irc.example.com" ; required, usually a hostname database = "ergonomadic.db" ; path relative to this file listen = "localhost:6667" ; see `net.Listen` for examples listen = "[::1]:6667" ; multiple `listen`s are allowed. +log = "debug" ; error, warn, info, debug motd = "motd.txt" ; path relative to this file password = "JDJhJDA0JHJzVFFlNXdOUXNhLmtkSGRUQVVEVHVYWXRKUmdNQ3FKVTRrczRSMTlSWGRPZHRSMVRzQmtt" ; 'test' +log = "debug" [operator "root"] password = "JDJhJDA0JEhkcm10UlNFRkRXb25iOHZuSDVLZXVBWlpyY0xyNkQ4dlBVc1VMWVk1LlFjWFpQbGxZNUtl" ; 'toor' - -[debug] -net = true -client = false -channel = false -server = false diff --git a/ergonomadic.go b/ergonomadic.go index 3d107a7c..96bd1b09 100644 --- a/ergonomadic.go +++ b/ergonomadic.go @@ -46,12 +46,7 @@ func main() { return } - // TODO move to data structures - irc.DEBUG_NET = config.Debug.Net - irc.DEBUG_CLIENT = config.Debug.Client - irc.DEBUG_CHANNEL = config.Debug.Channel - irc.DEBUG_SERVER = config.Debug.Server - + irc.Log.SetLevel(config.Server.Log) server := irc.NewServer(config) log.Println(irc.SEM_VER, "running") defer log.Println(irc.SEM_VER, "exiting") diff --git a/irc/client.go b/irc/client.go index 7545840d..03d9b481 100644 --- a/irc/client.go +++ b/irc/client.go @@ -2,7 +2,6 @@ package irc import ( "fmt" - "log" "net" "time" ) @@ -149,9 +148,7 @@ func (client *Client) destroy() { client.socket.Close() - if DEBUG_CLIENT { - log.Printf("%s: destroyed", client) - } + Log.debug.Printf("%s: destroyed", client) } func (client *Client) IdleTime() time.Duration { diff --git a/irc/client_lookup_set.go b/irc/client_lookup_set.go index 261461d9..1190b06d 100644 --- a/irc/client_lookup_set.go +++ b/irc/client_lookup_set.go @@ -88,25 +88,19 @@ func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) { `SELECT nickname FROM client WHERE userhost LIKE ? ESCAPE '\'`, QuoteLike(userhost)) if err != nil { - if DEBUG_SERVER { - log.Println("ClientLookupSet.FindAll.Query:", err) - } + Log.error.Println("ClientLookupSet.FindAll.Query:", err) return } for rows.Next() { var nickname string err := rows.Scan(&nickname) if err != nil { - if DEBUG_SERVER { - log.Println("ClientLookupSet.FindAll.Scan:", err) - } + Log.error.Println("ClientLookupSet.FindAll.Scan:", err) return } client := clients.Get(nickname) if client == nil { - if DEBUG_SERVER { - log.Println("ClientLookupSet.FindAll: missing client:", nickname) - } + Log.error.Println("ClientLookupSet.FindAll: missing client:", nickname) continue } set.Add(client) @@ -122,9 +116,7 @@ func (clients *ClientLookupSet) Find(userhost string) *Client { var nickname string err := row.Scan(&nickname) if err != nil { - if DEBUG_SERVER { - log.Println("ClientLookupSet.Find:", err) - } + Log.error.Println("ClientLookupSet.Find:", err) return nil } return clients.Get(nickname) @@ -163,9 +155,7 @@ func (db *ClientDB) Add(client *Client) { _, err := db.db.Exec(`INSERT INTO client (nickname, userhost) VALUES (?, ?)`, client.Nick(), client.UserHost()) if err != nil { - if DEBUG_SERVER { - log.Println("ClientDB.Add:", err) - } + Log.error.Println("ClientDB.Add:", err) } } @@ -173,9 +163,7 @@ func (db *ClientDB) Remove(client *Client) { _, err := db.db.Exec(`DELETE FROM client WHERE nickname = ?`, client.Nick()) if err != nil { - if DEBUG_SERVER { - log.Println("ClientDB.Remove:", err) - } + Log.error.Println("ClientDB.Remove:", err) } } diff --git a/irc/config.go b/irc/config.go index 6c5aa14d..201624e6 100644 --- a/irc/config.go +++ b/irc/config.go @@ -23,18 +23,12 @@ type Config struct { PassConfig Database string Listen []string + Log string MOTD string Name string } Operator map[string]*PassConfig - - Debug struct { - Net bool - Client bool - Channel bool - Server bool - } } func (conf *Config) Operators() map[string][]byte { diff --git a/irc/constants.go b/irc/constants.go index d2709468..cb0bde98 100644 --- a/irc/constants.go +++ b/irc/constants.go @@ -7,12 +7,6 @@ import ( ) var ( - // debugging flags - DEBUG_NET = false - DEBUG_CLIENT = false - DEBUG_CHANNEL = false - DEBUG_SERVER = false - // errors ErrAlreadyDestroyed = errors.New("already destroyed") diff --git a/irc/logging.go b/irc/logging.go new file mode 100644 index 00000000..6390aab2 --- /dev/null +++ b/irc/logging.go @@ -0,0 +1,60 @@ +package irc + +import ( + "io" + "log" + "os" +) + +type Logging struct { + debug *log.Logger + info *log.Logger + warn *log.Logger + error *log.Logger +} + +var ( + levels = map[string]uint8{ + "debug": 4, + "info": 3, + "warn": 2, + "error": 1, + } + devNull io.Writer +) + +func init() { + var err error + devNull, err = os.Open(os.DevNull) + if err != nil { + log.Fatal(err) + } +} + +func NewLogger(on bool) *log.Logger { + return log.New(output(on), "", log.LstdFlags) +} + +func output(on bool) io.Writer { + if on { + return os.Stdout + } + return devNull +} + +func (logging *Logging) SetLevel(level string) { + logging.debug = NewLogger(levels[level] >= levels["debug"]) + logging.info = NewLogger(levels[level] >= levels["info"]) + logging.warn = NewLogger(levels[level] >= levels["warn"]) + logging.error = NewLogger(levels[level] >= levels["error"]) +} + +func NewLogging(level string) *Logging { + logging := &Logging{} + logging.SetLevel(level) + return logging +} + +var ( + Log = NewLogging("warn") +) diff --git a/irc/server.go b/irc/server.go index 8da5f1ba..66576743 100644 --- a/irc/server.go +++ b/irc/server.go @@ -109,9 +109,7 @@ func (server *Server) loadChannels() { func (server *Server) processCommand(cmd Command) { client := cmd.Client() - if DEBUG_SERVER { - log.Printf("%s → %s %s", client, server, cmd) - } + Log.debug.Printf("%s → %s %s", client, server, cmd) switch client.phase { case Registration: @@ -180,21 +178,15 @@ func (s *Server) listen(addr string) { log.Fatal(s, "listen error: ", err) } - if DEBUG_SERVER { - log.Printf("%s listening on %s", s, addr) - } + Log.info.Printf("%s listening on %s", s, addr) for { conn, err := listener.Accept() if err != nil { - if DEBUG_SERVER { - log.Printf("%s accept error: %s", s, err) - } + Log.error.Printf("%s accept error: %s", s, err) continue } - if DEBUG_SERVER { - log.Printf("%s accept: %s", s, conn.RemoteAddr()) - } + Log.debug.Printf("%s accept: %s", s, conn.RemoteAddr()) s.newConns <- conn } diff --git a/irc/socket.go b/irc/socket.go index 1710cbf7..84e90313 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -3,7 +3,6 @@ package irc import ( "bufio" "io" - "log" "net" "strings" ) @@ -38,9 +37,7 @@ func (socket *Socket) String() string { func (socket *Socket) Close() { socket.conn.Close() - if DEBUG_NET { - log.Printf("%s closed", socket) - } + Log.debug.Printf("%s closed", socket) } func (socket *Socket) readLines(commands chan<- Command) { @@ -57,9 +54,7 @@ func (socket *Socket) readLines(commands chan<- Command) { if len(line) == 0 { continue } - if DEBUG_NET { - log.Printf("%s → %s", socket, line) - } + Log.debug.Printf("%s → %s", socket, line) msg, err := ParseCommand(line) if err != nil { @@ -87,16 +82,14 @@ func (socket *Socket) Write(line string) (err error) { return } - if DEBUG_NET { - log.Printf("%s ← %s", socket, line) - } + Log.debug.Printf("%s ← %s", socket, line) return } func (socket *Socket) isError(err error, dir rune) bool { if err != nil { - if DEBUG_NET && (err != io.EOF) { - log.Printf("%s %c error: %s", socket, dir, err) + if err != io.EOF { + Log.debug.Printf("%s %c error: %s", socket, dir, err) } return true }