mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-26 05:49:25 +01:00
move to channel-based logging
This commit is contained in:
parent
77d053ccac
commit
e15f47c766
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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")
|
||||
|
||||
|
60
irc/logging.go
Normal file
60
irc/logging.go
Normal 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")
|
||||
)
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user