aboutsummaryrefslogtreecommitdiffstats
path: root/lib/socket.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-07-19 22:31:51 +0200
committerRobin Jarry <robin@jarry.cc>2022-07-23 22:52:15 +0200
commitcd1999555714fb886493d2d04b6c472be55cebef (patch)
tree1df3bcf5f687752db671d8bc9c7eab8a5c0fde71 /lib/socket.go
parenta1f779ccc9b16b22ad6cb2e0bf73c290fd0cc756 (diff)
downloadaerc-cd1999555714fb886493d2d04b6c472be55cebef.tar.gz
logging: use level-based logger functions
Do not pass logger objects around anymore. Shuffle some messages to make them consistent with the new logging API. Avoid using %v when a more specific verb exists for the argument types. The loggers are completely disabled (i.e. Sprintf is not even called) by default. They are only enabled when redirecting stdout to a file. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'lib/socket.go')
-rw-r--r--lib/socket.go18
1 files changed, 7 insertions, 11 deletions
diff --git a/lib/socket.go b/lib/socket.go
index 902a7be2..fa36172e 100644
--- a/lib/socket.go
+++ b/lib/socket.go
@@ -4,7 +4,6 @@ import (
"bufio"
"errors"
"fmt"
- "log"
"net"
"net/url"
"os"
@@ -18,24 +17,21 @@ import (
)
type AercServer struct {
- logger *log.Logger
listener net.Listener
OnMailto func(addr *url.URL) error
OnMbox func(source string) error
}
-func StartServer(logger *log.Logger) (*AercServer, error) {
+func StartServer() (*AercServer, error) {
sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
// remove the socket if it already exists
os.Remove(sockpath)
+ logging.Infof("Starting Unix server: %s", sockpath)
l, err := net.Listen("unix", sockpath)
if err != nil {
return nil, err
}
- as := &AercServer{
- logger: logger,
- listener: l,
- }
+ as := &AercServer{listener: l}
// TODO: stash clients and close them on exit... bleh racey
go func() {
defer logging.PanicHandler()
@@ -45,7 +41,7 @@ func StartServer(logger *log.Logger) (*AercServer, error) {
if err != nil {
// TODO: Something more useful, in some cases, on wednesdays,
// after 2 PM, I guess?
- as.logger.Printf("Closing Unix server: %v", err)
+ logging.Errorf("Closing Unix server: %v", err)
return
}
go func() {
@@ -66,13 +62,13 @@ var lastId int64 = 0 // access via atomic
func (as *AercServer) handleClient(conn net.Conn) {
clientId := atomic.AddInt64(&lastId, 1)
- as.logger.Printf("Accepted Unix connection %d", clientId)
+ logging.Debugf("unix:%d accepted connection", clientId)
scanner := bufio.NewScanner(conn)
conn.SetDeadline(time.Now().Add(1 * time.Minute))
for scanner.Scan() {
conn.SetDeadline(time.Now().Add(1 * time.Minute))
msg := scanner.Text()
- as.logger.Printf("unix:%d: got message %s", clientId, msg)
+ logging.Debugf("unix:%d got message %s", clientId, msg)
if !strings.ContainsRune(msg, ':') {
conn.Write([]byte("error: invalid command\n"))
continue
@@ -105,7 +101,7 @@ func (as *AercServer) handleClient(conn net.Conn) {
}
}
}
- as.logger.Printf("Closed Unix connection %d", clientId)
+ logging.Debugf("unix:%d closed connection", clientId)
}
func ConnectAndExec(msg string) error {