From 24f78ccd658a5d5bf259f346a3daefcd72f164ed Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 27 Feb 2022 15:38:19 -0500 Subject: [PATCH] start making log shared --- admin.go | 4 ++- fun.go | 4 +-- go.mod | 2 +- go.sum | 4 +-- honk.go | 16 ++++++----- log.go | 81 -------------------------------------------------------- 6 files changed, 17 insertions(+), 94 deletions(-) delete mode 100644 log.go diff --git a/admin.go b/admin.go index 54a611b..1b9d9f0 100644 --- a/admin.go +++ b/admin.go @@ -30,10 +30,12 @@ import ( "os" "os/signal" "strings" + + "humungus.tedunangst.com/r/webs/log" ) func adminscreen() { - initLogging("null", "null", "null") + log.InitX("honk", "null", "null", "null") stdout := bufio.NewWriter(os.Stdout) esc := "\x1b" smcup := esc + "[?1049h" diff --git a/fun.go b/fun.go index a7eaba0..e1351ba 100644 --- a/fun.go +++ b/fun.go @@ -653,10 +653,10 @@ var zaggies = cache.New(cache.Options{Filler: func(keyname string) (httpsig.Publ return key, true }, Limit: 512}) -func zaggy(keyname string) httpsig.PublicKey { +func zaggy(keyname string) (httpsig.PublicKey, error) { var key httpsig.PublicKey zaggies.Get(keyname, &key) - return key + return key, nil } func savingthrow(keyname string) { diff --git a/go.mod b/go.mod index e91b022..8a77da0 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,5 @@ require ( golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect golang.org/x/net v0.0.0-20201110031124-69a78807bb2b humungus.tedunangst.com/r/go-sqlite3 v1.1.3 - humungus.tedunangst.com/r/webs v0.6.48 + humungus.tedunangst.com/r/webs v0.6.49 ) diff --git a/go.sum b/go.sum index b3d95fb..9b290ef 100644 --- a/go.sum +++ b/go.sum @@ -24,5 +24,5 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI= humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= -humungus.tedunangst.com/r/webs v0.6.48 h1:s4TadyjAZRWOi8I1v7bNXHbvPwQMKgGLGr/d/M2ZoM8= -humungus.tedunangst.com/r/webs v0.6.48/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= +humungus.tedunangst.com/r/webs v0.6.49 h1:Tv3Fx2xnv+TINW5gepCpaDP+xhkOWlWp3rYE9FAkP50= +humungus.tedunangst.com/r/webs v0.6.49/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= diff --git a/honk.go b/honk.go index a0dcbb6..abea138 100644 --- a/honk.go +++ b/honk.go @@ -19,6 +19,7 @@ import ( "flag" "fmt" "html/template" + golog "log" notrand "math/rand" "os" "strconv" @@ -26,6 +27,7 @@ import ( "time" "humungus.tedunangst.com/r/webs/httpsig" + "humungus.tedunangst.com/r/webs/log" ) var softwareVersion = "develop" @@ -268,22 +270,22 @@ func unplugserver(hostname string) { func reexecArgs(cmd string) []string { args := []string{"-datadir", dataDir} - args = append(args, loggingArgs()...) + args = append(args, log.Args()...) args = append(args, cmd) return args } +var elog, ilog, dlog *golog.Logger + func main() { flag.StringVar(&dataDir, "datadir", dataDir, "data directory") flag.StringVar(&viewDir, "viewdir", viewDir, "view directory") flag.Parse() - if alllogname != "stderr" { - elogname = alllogname - ilogname = alllogname - dlogname = alllogname - } - initLogging(elogname, ilogname, dlogname) + log.Init("honk") + elog = log.E + ilog = log.I + dlog = log.D args := flag.Args() cmd := "run" diff --git a/log.go b/log.go deleted file mode 100644 index bb441fc..0000000 --- a/log.go +++ /dev/null @@ -1,81 +0,0 @@ -// -// Copyright (c) 2022 Ted Unangst -// -// Permission to use, copy, modify, and distribute this software for any -// purpose with or without fee is hereby granted, provided that the above -// copyright notice and this permission notice appear in all copies. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -package main - -import ( - "flag" - "io/ioutil" - "log" - "log/syslog" - "os" -) - -// log.Default() not added until go 1.16 -func logdefault() *log.Logger { - return log.New(os.Stderr, "", log.LstdFlags) -} - -var elog = logdefault() -var ilog = logdefault() -var dlog = logdefault() - -var elogname, ilogname, dlogname, alllogname string - -func init() { - flag.StringVar(&elogname, "errorlog", "stderr", "error log file (or stderr, null, syslog)") - flag.StringVar(&ilogname, "infolog", "stderr", "info log file (or stderr, null, syslog)") - flag.StringVar(&dlogname, "debuglog", "stderr", "debug log file (or stderr, null, syslog)") - flag.StringVar(&alllogname, "log", "stderr", "combined log file (or stderr, null, syslog)") - -} - -func loggingArgs() []string { - return []string{"-errorlog", elogname, "-infolog", ilogname, "-debuglog", dlogname} -} - -func initLogging(elogname, ilogname, dlogname string) { - elog = openlog(elogname, syslog.LOG_ERR) - ilog = openlog(ilogname, syslog.LOG_INFO) - dlog = openlog(dlogname, syslog.LOG_DEBUG) -} - -func openlog(name string, prio syslog.Priority) *log.Logger { - if name == "stderr" { - return log.New(os.Stderr, "", log.LstdFlags) - } - if name == "stdout" { - return log.New(os.Stdout, "", log.LstdFlags) - } - if name == "null" { - return log.New(ioutil.Discard, "", log.LstdFlags) - } - if name == "syslog" { - w, err := syslog.New(syslog.LOG_UUCP|prio, "honk") - if err != nil { - elog.Printf("can't create syslog: %s", err) - return logdefault() - } - return log.New(w, "", log.LstdFlags) - } - fd, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) - if err != nil { - elog.Printf("can't open log file %s: %s", name, err) - return logdefault() - } - logger := log.New(fd, "", log.LstdFlags) - logger.Printf("new log started") - return logger -}