3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

move to channel-based logging

This commit is contained in:
Jeremy Latt 2014-03-08 15:01:15 -08:00
parent 77d053ccac
commit e15f47c766
9 changed files with 80 additions and 71 deletions

View File

@ -3,14 +3,10 @@ name = "irc.example.com" ; required, usually a hostname
database = "ergonomadic.db" ; path relative to this file database = "ergonomadic.db" ; path relative to this file
listen = "localhost:6667" ; see `net.Listen` for examples listen = "localhost:6667" ; see `net.Listen` for examples
listen = "[::1]:6667" ; multiple `listen`s are allowed. listen = "[::1]:6667" ; multiple `listen`s are allowed.
log = "debug" ; error, warn, info, debug
motd = "motd.txt" ; path relative to this file motd = "motd.txt" ; path relative to this file
password = "JDJhJDA0JHJzVFFlNXdOUXNhLmtkSGRUQVVEVHVYWXRKUmdNQ3FKVTRrczRSMTlSWGRPZHRSMVRzQmtt" ; 'test' password = "JDJhJDA0JHJzVFFlNXdOUXNhLmtkSGRUQVVEVHVYWXRKUmdNQ3FKVTRrczRSMTlSWGRPZHRSMVRzQmtt" ; 'test'
log = "debug"
[operator "root"] [operator "root"]
password = "JDJhJDA0JEhkcm10UlNFRkRXb25iOHZuSDVLZXVBWlpyY0xyNkQ4dlBVc1VMWVk1LlFjWFpQbGxZNUtl" ; 'toor' password = "JDJhJDA0JEhkcm10UlNFRkRXb25iOHZuSDVLZXVBWlpyY0xyNkQ4dlBVc1VMWVk1LlFjWFpQbGxZNUtl" ; 'toor'
[debug]
net = true
client = false
channel = false
server = false

View File

@ -46,12 +46,7 @@ func main() {
return return
} }
// TODO move to data structures irc.Log.SetLevel(config.Server.Log)
irc.DEBUG_NET = config.Debug.Net
irc.DEBUG_CLIENT = config.Debug.Client
irc.DEBUG_CHANNEL = config.Debug.Channel
irc.DEBUG_SERVER = config.Debug.Server
server := irc.NewServer(config) server := irc.NewServer(config)
log.Println(irc.SEM_VER, "running") log.Println(irc.SEM_VER, "running")
defer log.Println(irc.SEM_VER, "exiting") defer log.Println(irc.SEM_VER, "exiting")

View File

@ -2,7 +2,6 @@ package irc
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"time" "time"
) )
@ -149,9 +148,7 @@ func (client *Client) destroy() {
client.socket.Close() client.socket.Close()
if DEBUG_CLIENT { Log.debug.Printf("%s: destroyed", client)
log.Printf("%s: destroyed", client)
}
} }
func (client *Client) IdleTime() time.Duration { func (client *Client) IdleTime() time.Duration {

View File

@ -88,25 +88,19 @@ func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
`SELECT nickname FROM client WHERE userhost LIKE ? ESCAPE '\'`, `SELECT nickname FROM client WHERE userhost LIKE ? ESCAPE '\'`,
QuoteLike(userhost)) QuoteLike(userhost))
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Println("ClientLookupSet.FindAll.Query:", err)
log.Println("ClientLookupSet.FindAll.Query:", err)
}
return return
} }
for rows.Next() { for rows.Next() {
var nickname string var nickname string
err := rows.Scan(&nickname) err := rows.Scan(&nickname)
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Println("ClientLookupSet.FindAll.Scan:", err)
log.Println("ClientLookupSet.FindAll.Scan:", err)
}
return return
} }
client := clients.Get(nickname) client := clients.Get(nickname)
if client == nil { if client == nil {
if DEBUG_SERVER { Log.error.Println("ClientLookupSet.FindAll: missing client:", nickname)
log.Println("ClientLookupSet.FindAll: missing client:", nickname)
}
continue continue
} }
set.Add(client) set.Add(client)
@ -122,9 +116,7 @@ func (clients *ClientLookupSet) Find(userhost string) *Client {
var nickname string var nickname string
err := row.Scan(&nickname) err := row.Scan(&nickname)
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Println("ClientLookupSet.Find:", err)
log.Println("ClientLookupSet.Find:", err)
}
return nil return nil
} }
return clients.Get(nickname) return clients.Get(nickname)
@ -163,9 +155,7 @@ func (db *ClientDB) Add(client *Client) {
_, err := db.db.Exec(`INSERT INTO client (nickname, userhost) VALUES (?, ?)`, _, err := db.db.Exec(`INSERT INTO client (nickname, userhost) VALUES (?, ?)`,
client.Nick(), client.UserHost()) client.Nick(), client.UserHost())
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Println("ClientDB.Add:", err)
log.Println("ClientDB.Add:", err)
}
} }
} }
@ -173,9 +163,7 @@ func (db *ClientDB) Remove(client *Client) {
_, err := db.db.Exec(`DELETE FROM client WHERE nickname = ?`, _, err := db.db.Exec(`DELETE FROM client WHERE nickname = ?`,
client.Nick()) client.Nick())
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Println("ClientDB.Remove:", err)
log.Println("ClientDB.Remove:", err)
}
} }
} }

View File

@ -23,18 +23,12 @@ type Config struct {
PassConfig PassConfig
Database string Database string
Listen []string Listen []string
Log string
MOTD string MOTD string
Name string Name string
} }
Operator map[string]*PassConfig Operator map[string]*PassConfig
Debug struct {
Net bool
Client bool
Channel bool
Server bool
}
} }
func (conf *Config) Operators() map[string][]byte { func (conf *Config) Operators() map[string][]byte {

View File

@ -7,12 +7,6 @@ import (
) )
var ( var (
// debugging flags
DEBUG_NET = false
DEBUG_CLIENT = false
DEBUG_CHANNEL = false
DEBUG_SERVER = false
// errors // errors
ErrAlreadyDestroyed = errors.New("already destroyed") ErrAlreadyDestroyed = errors.New("already destroyed")

60
irc/logging.go Normal file
View File

@ -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")
)

View File

@ -109,9 +109,7 @@ func (server *Server) loadChannels() {
func (server *Server) processCommand(cmd Command) { func (server *Server) processCommand(cmd Command) {
client := cmd.Client() client := cmd.Client()
if DEBUG_SERVER { Log.debug.Printf("%s → %s %s", client, server, cmd)
log.Printf("%s → %s %s", client, server, cmd)
}
switch client.phase { switch client.phase {
case Registration: case Registration:
@ -180,21 +178,15 @@ func (s *Server) listen(addr string) {
log.Fatal(s, "listen error: ", err) log.Fatal(s, "listen error: ", err)
} }
if DEBUG_SERVER { Log.info.Printf("%s listening on %s", s, addr)
log.Printf("%s listening on %s", s, addr)
}
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
if DEBUG_SERVER { Log.error.Printf("%s accept error: %s", s, err)
log.Printf("%s accept error: %s", s, err)
}
continue continue
} }
if DEBUG_SERVER { Log.debug.Printf("%s accept: %s", s, conn.RemoteAddr())
log.Printf("%s accept: %s", s, conn.RemoteAddr())
}
s.newConns <- conn s.newConns <- conn
} }

View File

@ -3,7 +3,6 @@ package irc
import ( import (
"bufio" "bufio"
"io" "io"
"log"
"net" "net"
"strings" "strings"
) )
@ -38,9 +37,7 @@ func (socket *Socket) String() string {
func (socket *Socket) Close() { func (socket *Socket) Close() {
socket.conn.Close() socket.conn.Close()
if DEBUG_NET { Log.debug.Printf("%s closed", socket)
log.Printf("%s closed", socket)
}
} }
func (socket *Socket) readLines(commands chan<- Command) { func (socket *Socket) readLines(commands chan<- Command) {
@ -57,9 +54,7 @@ func (socket *Socket) readLines(commands chan<- Command) {
if len(line) == 0 { if len(line) == 0 {
continue continue
} }
if DEBUG_NET { Log.debug.Printf("%s → %s", socket, line)
log.Printf("%s → %s", socket, line)
}
msg, err := ParseCommand(line) msg, err := ParseCommand(line)
if err != nil { if err != nil {
@ -87,16 +82,14 @@ func (socket *Socket) Write(line string) (err error) {
return return
} }
if DEBUG_NET { Log.debug.Printf("%s ← %s", socket, line)
log.Printf("%s ← %s", socket, line)
}
return return
} }
func (socket *Socket) isError(err error, dir rune) bool { func (socket *Socket) isError(err error, dir rune) bool {
if err != nil { if err != nil {
if DEBUG_NET && (err != io.EOF) { if err != io.EOF {
log.Printf("%s %c error: %s", socket, dir, err) Log.debug.Printf("%s %c error: %s", socket, dir, err)
} }
return true return true
} }