start making log shared

This commit is contained in:
Ted Unangst 2022-02-27 15:38:19 -05:00
parent caa1cbdd42
commit 24f78ccd65
6 changed files with 17 additions and 94 deletions

View File

@ -30,10 +30,12 @@ import (
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
"humungus.tedunangst.com/r/webs/log"
) )
func adminscreen() { func adminscreen() {
initLogging("null", "null", "null") log.InitX("honk", "null", "null", "null")
stdout := bufio.NewWriter(os.Stdout) stdout := bufio.NewWriter(os.Stdout)
esc := "\x1b" esc := "\x1b"
smcup := esc + "[?1049h" smcup := esc + "[?1049h"

4
fun.go
View File

@ -653,10 +653,10 @@ var zaggies = cache.New(cache.Options{Filler: func(keyname string) (httpsig.Publ
return key, true return key, true
}, Limit: 512}) }, Limit: 512})
func zaggy(keyname string) httpsig.PublicKey { func zaggy(keyname string) (httpsig.PublicKey, error) {
var key httpsig.PublicKey var key httpsig.PublicKey
zaggies.Get(keyname, &key) zaggies.Get(keyname, &key)
return key return key, nil
} }
func savingthrow(keyname string) { func savingthrow(keyname string) {

2
go.mod
View File

@ -10,5 +10,5 @@ require (
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 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
) )

4
go.sum
View File

@ -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= 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 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= 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.49 h1:Tv3Fx2xnv+TINW5gepCpaDP+xhkOWlWp3rYE9FAkP50=
humungus.tedunangst.com/r/webs v0.6.48/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps= humungus.tedunangst.com/r/webs v0.6.49/go.mod h1:S9sXpVSbgAIa24yYhnMN0C94LKHG+2rioS+NsiDimps=

16
honk.go
View File

@ -19,6 +19,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"html/template" "html/template"
golog "log"
notrand "math/rand" notrand "math/rand"
"os" "os"
"strconv" "strconv"
@ -26,6 +27,7 @@ import (
"time" "time"
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/log"
) )
var softwareVersion = "develop" var softwareVersion = "develop"
@ -268,22 +270,22 @@ func unplugserver(hostname string) {
func reexecArgs(cmd string) []string { func reexecArgs(cmd string) []string {
args := []string{"-datadir", dataDir} args := []string{"-datadir", dataDir}
args = append(args, loggingArgs()...) args = append(args, log.Args()...)
args = append(args, cmd) args = append(args, cmd)
return args return args
} }
var elog, ilog, dlog *golog.Logger
func main() { func main() {
flag.StringVar(&dataDir, "datadir", dataDir, "data directory") flag.StringVar(&dataDir, "datadir", dataDir, "data directory")
flag.StringVar(&viewDir, "viewdir", viewDir, "view directory") flag.StringVar(&viewDir, "viewdir", viewDir, "view directory")
flag.Parse() flag.Parse()
if alllogname != "stderr" { log.Init("honk")
elogname = alllogname elog = log.E
ilogname = alllogname ilog = log.I
dlogname = alllogname dlog = log.D
}
initLogging(elogname, ilogname, dlogname)
args := flag.Args() args := flag.Args()
cmd := "run" cmd := "run"

81
log.go
View File

@ -1,81 +0,0 @@
//
// Copyright (c) 2022 Ted Unangst <tedu@tedunangst.com>
//
// 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
}