flexible logging

master
Ted Unangst 2 years ago
parent e2ed63dc43
commit 7eef6ae5d0

@ -23,7 +23,6 @@ import (
"fmt"
"html"
"io"
"log"
notrand "math/rand"
"net/http"
"net/url"
@ -96,7 +95,7 @@ func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) err
default:
return fmt.Errorf("http post status: %d", resp.StatusCode)
}
log.Printf("successful post: %s %d", url, resp.StatusCode)
ilog.Printf("successful post: %s %d", url, resp.StatusCode)
return nil
}
@ -113,13 +112,13 @@ func GetJunkHardMode(url string) (junk.Junk, error) {
if err != nil {
emsg := err.Error()
if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
log.Printf("trying again after error: %s", emsg)
ilog.Printf("trying again after error: %s", emsg)
time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
j, err = GetJunk(url)
if err != nil {
log.Printf("still couldn't get it")
ilog.Printf("still couldn't get it")
} else {
log.Printf("retry success!")
ilog.Printf("retry success!")
}
}
}
@ -158,7 +157,7 @@ func fetchsome(url string) ([]byte, error) {
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("error fetching %s: %s", url, err)
ilog.Printf("error fetching %s: %s", url, err)
return nil, err
}
req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
@ -167,7 +166,7 @@ func fetchsome(url string) ([]byte, error) {
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
log.Printf("error fetching %s: %s", url, err)
ilog.Printf("error fetching %s: %s", url, err)
return nil, err
}
defer resp.Body.Close()
@ -191,7 +190,7 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
if donk := finddonk(url); donk != nil {
return donk
}
log.Printf("saving donk: %s", url)
ilog.Printf("saving donk: %s", url)
data := []byte{}
if localize {
fn := func() (interface{}, error) {
@ -199,19 +198,19 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
}
ii, err := flightdeck.Call(url, fn)
if err != nil {
log.Printf("error fetching donk: %s", err)
ilog.Printf("error fetching donk: %s", err)
localize = false
goto saveit
}
data = ii.([]byte)
if len(data) == 10*1024*1024 {
log.Printf("truncation likely")
ilog.Printf("truncation likely")
}
if strings.HasPrefix(media, "image") {
img, err := shrinkit(data)
if err != nil {
log.Printf("unable to decode image: %s", err)
ilog.Printf("unable to decode image: %s", err)
localize = false
data = []byte{}
goto saveit
@ -220,12 +219,12 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
media = "image/" + img.Format
} else if media == "application/pdf" {
if len(data) > 1000000 {
log.Printf("not saving large pdf")
ilog.Printf("not saving large pdf")
localize = false
data = []byte{}
}
} else if len(data) > 100000 {
log.Printf("not saving large attachment")
ilog.Printf("not saving large attachment")
localize = false
data = []byte{}
}
@ -233,7 +232,7 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
saveit:
fileid, err := savefile(name, desc, url, media, localize, data)
if err != nil {
log.Printf("error saving file %s: %s", url, err)
elog.Printf("error saving file %s: %s", url, err)
return nil
}
donk := new(Donk)
@ -249,7 +248,7 @@ func iszonked(userid int64, xid string) bool {
return true
}
if err != sql.ErrNoRows {
log.Printf("error querying zonk: %s", err)
ilog.Printf("error querying zonk: %s", err)
}
return false
}
@ -274,11 +273,11 @@ func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
return false
}
if rejectorigin(user.ID, xid, isannounce) {
log.Printf("rejecting origin: %s", xid)
ilog.Printf("rejecting origin: %s", xid)
return false
}
if iszonked(user.ID, xid) {
log.Printf("already zonked: %s", xid)
ilog.Printf("already zonked: %s", xid)
return false
}
var id int64
@ -288,7 +287,7 @@ func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
return false
}
if err != sql.ErrNoRows {
log.Printf("error querying xonk: %s", err)
ilog.Printf("error querying xonk: %s", err)
}
return true
}
@ -300,12 +299,12 @@ func eradicatexonk(userid int64, xid string) {
}
_, err := stmtSaveZonker.Exec(userid, xid, "zonk")
if err != nil {
log.Printf("error eradicating: %s", err)
elog.Printf("error eradicating: %s", err)
}
}
func savexonk(x *Honk) {
log.Printf("saving xonk: %s", x.XID)
ilog.Printf("saving xonk: %s", x.XID)
go handles(x.Honker)
go handles(x.Oonker)
savehonk(x)
@ -322,11 +321,11 @@ var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool)
row := stmtGetXonker.QueryRow(ident, "boxes")
err := row.Scan(&info)
if err != nil {
log.Printf("need to get boxes for %s", ident)
dlog.Printf("need to get boxes for %s", ident)
var j junk.Junk
j, err = GetJunk(ident)
if err != nil {
log.Printf("error getting boxes: %s", err)
dlog.Printf("error getting boxes: %s", err)
return nil, false
}
allinjest(originate(ident), j)
@ -342,10 +341,10 @@ var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool)
}})
func gimmexonks(user *WhatAbout, outbox string) {
log.Printf("getting outbox: %s", outbox)
dlog.Printf("getting outbox: %s", outbox)
j, err := GetJunk(outbox)
if err != nil {
log.Printf("error getting outbox: %s", err)
ilog.Printf("error getting outbox: %s", err)
return
}
t, _ := j.GetString("type")
@ -364,7 +363,7 @@ func gimmexonks(user *WhatAbout, outbox string) {
if ok {
j, err = GetJunk(page1)
if err != nil {
log.Printf("error gettings page1: %s", err)
ilog.Printf("error gettings page1: %s", err)
return
}
items, _ = j.GetArray("orderedItems")
@ -390,7 +389,7 @@ func gimmexonks(user *WhatAbout, outbox string) {
}
obj, err = GetJunk(xid)
if err != nil {
log.Printf("error getting item: %s", err)
ilog.Printf("error getting item: %s", err)
continue
}
xonksaver(user, obj, originate(xid))
@ -467,14 +466,14 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk
saveonemore := func(xid string) {
log.Printf("getting onemore: %s", xid)
dlog.Printf("getting onemore: %s", xid)
if depth >= maxdepth {
log.Printf("in too deep")
ilog.Printf("in too deep")
return
}
obj, err := GetJunkHardMode(xid)
if err != nil {
log.Printf("error getting onemore: %s: %s", xid, err)
ilog.Printf("error getting onemore: %s: %s", xid, err)
return
}
depth++
@ -506,16 +505,16 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
return nil
}
if originate(xid) != origin {
log.Printf("forged delete: %s", xid)
ilog.Printf("forged delete: %s", xid)
return nil
}
log.Printf("eradicating %s", xid)
ilog.Printf("eradicating %s", xid)
eradicatexonk(user.ID, xid)
return nil
case "Remove":
xid, _ = item.GetString("object")
targ, _ := obj.GetString("target")
log.Printf("remove %s from %s", obj, targ)
ilog.Printf("remove %s from %s", obj, targ)
return nil
case "Tombstone":
xid, _ = item.GetString("id")
@ -523,10 +522,10 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
return nil
}
if originate(xid) != origin {
log.Printf("forged delete: %s", xid)
ilog.Printf("forged delete: %s", xid)
return nil
}
log.Printf("eradicating %s", xid)
ilog.Printf("eradicating %s", xid)
eradicatexonk(user.ID, xid)
return nil
case "Announce":
@ -539,10 +538,10 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
if !needbonkid(user, xid) {
return nil
}
log.Printf("getting bonk: %s", xid)
dlog.Printf("getting bonk: %s", xid)
obj, err = GetJunkHardMode(xid)
if err != nil {
log.Printf("error getting bonk: %s: %s", xid, err)
ilog.Printf("error getting bonk: %s: %s", xid, err)
}
origin = originate(xid)
what = "bonk"
@ -553,18 +552,18 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
obj, ok = item.GetMap("object")
if !ok {
xid, _ = item.GetString("object")
log.Printf("getting created honk: %s", xid)
dlog.Printf("getting created honk: %s", xid)
if originate(xid) != origin {
log.Printf("out of bounds %s not from %s", xid, origin)
ilog.Printf("out of bounds %s not from %s", xid, origin)
return nil
}
obj, err = GetJunkHardMode(xid)
if err != nil {
log.Printf("error getting creation: %s", err)
ilog.Printf("error getting creation: %s", err)
}
}
if obj == nil {
log.Printf("no object for creation %s", id)
ilog.Printf("no object for creation %s", id)
return nil
}
return xonkxonkfn(obj, origin, isUpdate)
@ -572,12 +571,12 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
xid, ok = item.GetString("object")
if ok {
if !needxonkid(user, xid) {
log.Printf("don't need read obj: %s", xid)
dlog.Printf("don't need read obj: %s", xid)
return nil
}
obj, err = GetJunkHardMode(xid)
if err != nil {
log.Printf("error getting read: %s", err)
ilog.Printf("error getting read: %s", err)
return nil
}
return xonkxonkfn(obj, originate(xid), false)
@ -588,12 +587,12 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
if ok {
// check target...
if !needxonkid(user, xid) {
log.Printf("don't need added obj: %s", xid)
dlog.Printf("don't need added obj: %s", xid)
return nil
}
obj, err = GetJunkHardMode(xid)
if err != nil {
log.Printf("error getting add: %s", err)
ilog.Printf("error getting add: %s", err)
return nil
}
return xonkxonkfn(obj, originate(xid), false)
@ -624,7 +623,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
obj = item
what = "chonk"
default:
log.Printf("unknown activity: %s", what)
ilog.Printf("unknown activity: %s", what)
dumpactivity(item)
return nil
}
@ -634,13 +633,13 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
}
if xid == "" {
log.Printf("don't know what xid is")
item.Write(os.Stdout)
ilog.Printf("don't know what xid is")
item.Write(ilog.Writer())
return nil
}
if originate(xid) != origin {
log.Printf("original sin: %s not from %s", xid, origin)
item.Write(os.Stdout)
ilog.Printf("original sin: %s not from %s", xid, origin)
item.Write(ilog.Writer())
return nil
}
@ -729,14 +728,14 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
what = "tonk"
}
if len(content) > 90001 {
log.Printf("content too long. truncating")
ilog.Printf("content too long. truncating")
content = content[:90001]
}
xonk.Noise = content
xonk.Precis = precis
if rejectxonk(&xonk) {
log.Printf("fast reject: %s", xid)
dlog.Printf("fast reject: %s", xid)
return nil
}
@ -771,16 +770,16 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
}
localize := false
if numatts > 4 {
log.Printf("excessive attachment: %s", at)
ilog.Printf("excessive attachment: %s", at)
} else if at == "Document" || at == "Image" {
mt = strings.ToLower(mt)
log.Printf("attachment: %s %s", mt, u)
dlog.Printf("attachment: %s %s", mt, u)
if mt == "text/plain" || mt == "application/pdf" ||
strings.HasPrefix(mt, "image") {
localize = true
}
} else {
log.Printf("unknown attachment: %s", at)
ilog.Printf("unknown attachment: %s", at)
}
if skipMedia(&xonk) {
localize = false
@ -795,7 +794,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
for _, atti := range atts {
att, ok := atti.(junk.Junk)
if !ok {
log.Printf("attachment that wasn't map?")
ilog.Printf("attachment that wasn't map?")
continue
}
procatt(att)
@ -934,10 +933,10 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
}
if isUpdate {
log.Printf("something has changed! %s", xonk.XID)
dlog.Printf("something has changed! %s", xonk.XID)
prev := getxonk(user.ID, xonk.XID)
if prev == nil {
log.Printf("didn't find old version for update: %s", xonk.XID)
ilog.Printf("didn't find old version for update: %s", xonk.XID)
isUpdate = false
} else {
xonk.ID = prev.ID
@ -971,7 +970,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
if goingup == 0 {
for _, replid := range replies {
if needxonkid(user, replid) {
log.Printf("missing a reply: %s", replid)
dlog.Printf("missing a reply: %s", replid)
saveonemore(replid)
}
}
@ -985,7 +984,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
func dumpactivity(item junk.Junk) {
fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Printf("error opening inbox! %s", err)
elog.Printf("error opening inbox! %s", err)
return
}
defer fd.Close()
@ -1028,7 +1027,7 @@ func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
func subsub(user *WhatAbout, xid string, owner string, folxid string) {
if xid == "" {
log.Printf("can't subscribe to empty")
ilog.Printf("can't subscribe to empty")
return
}
j := junk.New()
@ -1508,7 +1507,7 @@ func asjonker(name string) ([]byte, bool) {
var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
m := strings.Split(name, "@")
if len(m) != 2 {
log.Printf("bad fish name: %s", name)
dlog.Printf("bad fish name: %s", name)
return "", true
}
var href string
@ -1517,10 +1516,10 @@ var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool)
if err == nil {
return href, true
}
log.Printf("fishing for %s", name)
dlog.Printf("fishing for %s", name)
j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
if err != nil {
log.Printf("failed to go fish %s: %s", name, err)
ilog.Printf("failed to go fish %s: %s", name, err)
return "", true
}
links, _ := j.GetArray("links")
@ -1536,7 +1535,7 @@ var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool)
when := time.Now().UTC().Format(dbtimeformat)
_, err := stmtSaveXonker.Exec(name, href, "fishname", when)
if err != nil {
log.Printf("error saving fishname: %s", err)
elog.Printf("error saving fishname: %s", err)
}
return href, true
}
@ -1624,33 +1623,33 @@ func ingestpubkey(origin string, obj junk.Junk) {
return
}
if !ok || origin != originate(keyname) {
log.Printf("bad key origin %s <> %s", origin, keyname)
ilog.Printf("bad key origin %s <> %s", origin, keyname)
return
}
log.Printf("ingesting a needed pubkey: %s", keyname)
dlog.Printf("ingesting a needed pubkey: %s", keyname)
owner, ok := obj.GetString("owner")
if !ok {
log.Printf("error finding %s pubkey owner", keyname)
ilog.Printf("error finding %s pubkey owner", keyname)
return
}
data, ok = obj.GetString("publicKeyPem")
if !ok {
log.Printf("error finding %s pubkey", keyname)
ilog.Printf("error finding %s pubkey", keyname)
return
}
if originate(owner) != origin {
log.Printf("bad key owner: %s <> %s", owner, origin)
ilog.Printf("bad key owner: %s <> %s", owner, origin)
return
}
_, _, err = httpsig.DecodeKey(data)
if err != nil {
log.Printf("error decoding %s pubkey: %s", keyname, err)
ilog.Printf("error decoding %s pubkey: %s", keyname, err)
return
}
when := time.Now().UTC().Format(dbtimeformat)
_, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
if err != nil {
log.Printf("error saving key: %s", err)
elog.Printf("error saving key: %s", err)
}
}
@ -1668,7 +1667,7 @@ func ingestboxes(origin string, obj junk.Junk) {
if err == nil {
return
}
log.Printf("ingesting boxes: %s", ident)
dlog.Printf("ingesting boxes: %s", ident)
inbox, _ := obj.GetString("inbox")
outbox, _ := obj.GetString("outbox")
sbox, _ := obj.GetString("endpoints", "sharedInbox")
@ -1677,7 +1676,7 @@ func ingestboxes(origin string, obj junk.Junk) {
m := strings.Join([]string{inbox, outbox, sbox}, " ")
_, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
if err != nil {
log.Printf("error saving boxes: %s", err)
elog.Printf("error saving boxes: %s", err)
}
}
}
@ -1701,7 +1700,7 @@ func ingesthandle(origin string, obj junk.Junk) {
when := time.Now().UTC().Format(dbtimeformat)
_, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
if err != nil {
log.Printf("error saving handle: %s", err)
elog.Printf("error saving handle: %s", err)
}
}
}
@ -1742,17 +1741,17 @@ func updateMe(username string) {
func followme(user *WhatAbout, who string, name string, j junk.Junk) {
folxid, _ := j.GetString("id")
log.Printf("updating honker follow: %s %s", who, folxid)
ilog.Printf("updating honker follow: %s %s", who, folxid)
var x string
db := opendatabase()
row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
err := row.Scan(&x)
if err != sql.ErrNoRows {
log.Printf("duplicate follow request: %s", who)
ilog.Printf("duplicate follow request: %s", who)
_, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
}
} else {
stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
@ -1770,16 +1769,16 @@ func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
err := row.Scan(&who, &name)
if err != nil {
if err != sql.ErrNoRows {
log.Printf("error scanning honker: %s", err)
elog.Printf("error scanning honker: %s", err)
}
return
}
}
log.Printf("updating honker undo: %s %s", who, folxid)
ilog.Printf("updating honker undo: %s %s", who, folxid)
_, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
return
}
}
@ -1791,14 +1790,14 @@ func followyou(user *WhatAbout, honkerid int64) {
honkerid, user.ID)
err := row.Scan(&url, &owner)
if err != nil {
log.Printf("can't get honker xid: %s", err)
elog.Printf("can't get honker xid: %s", err)
return
}
folxid := xfiltrate()
log.Printf("subscribing to %s", url)
ilog.Printf("subscribing to %s", url)
_, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
return
}
go subsub(user, url, owner, folxid)
@ -1811,13 +1810,13 @@ func unfollowyou(user *WhatAbout, honkerid int64) {
var url, owner, folxid string
err := row.Scan(&url, &owner, &folxid)
if err != nil {
log.Printf("can't get honker xid: %s", err)
elog.Printf("can't get honker xid: %s", err)
return
}
log.Printf("unsubscribing from %s", url)
ilog.Printf("unsubscribing from %s", url)
_, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
return
}
go itakeitallback(user, url, owner, folxid)
@ -1826,19 +1825,19 @@ func unfollowyou(user *WhatAbout, honkerid int64) {
func followyou2(user *WhatAbout, j junk.Junk) {
who, _ := j.GetString("actor")
log.Printf("updating honker accept: %s", who)
ilog.Printf("updating honker accept: %s", who)
db := opendatabase()
row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub')",
user.ID, who)
var name, folxid string
err := row.Scan(&name, &folxid)
if err != nil {
log.Printf("can't get honker name: %s", err)
elog.Printf("can't get honker name: %s", err)
return
}
_, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
return
}
}
@ -1846,20 +1845,20 @@ func followyou2(user *WhatAbout, j junk.Junk) {
func nofollowyou2(user *WhatAbout, j junk.Junk) {
who, _ := j.GetString("actor")
log.Printf("updating honker reject: %s", who)
ilog.Printf("updating honker reject: %s", who)
db := opendatabase()
row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
user.ID, who)
var name, folxid string
err := row.Scan(&name, &folxid)
if err != nil {
log.Printf("can't get honker name: %s", err)
elog.Printf("can't get honker name: %s", err)
return
}
_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "presub")
_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "sub")
if err != nil {
log.Printf("error updating honker: %s", err)
elog.Printf("error updating honker: %s", err)
return
}
}

@ -27,15 +27,13 @@ import "C"
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
)
func adminscreen() {
log.SetOutput(ioutil.Discard)
initLogging("null", "null", "null")
stdout := bufio.NewWriter(os.Stdout)
esc := "\x1b"
smcup := esc + "[?1049h"

@ -22,7 +22,6 @@ import (
"fmt"
"image"
"image/png"
"log"
"net/http"
"regexp"
"strconv"
@ -56,7 +55,7 @@ func loadAvatarColors() {
}
c, err := strconv.ParseUint(l, 16, 32)
if err != nil {
log.Printf("error reading avatar color %d: %s", i, err)
elog.Printf("error reading avatar color %d: %s", i, err)
continue
}
avatarcolors[i][0] = byte(c >> 24 & 0xff)

@ -17,7 +17,6 @@ package main
import (
"bytes"
"log"
"net"
"net/rpc"
"os"
@ -76,27 +75,27 @@ func shrinkit(data []byte) (*image.Image, error) {
var backendhooks []func()
func backendServer() {
log.Printf("backend server running")
dlog.Printf("backend server running")
shrinker := new(Shrinker)
srv := rpc.NewServer()
err := srv.Register(shrinker)
if err != nil {
log.Panicf("unable to register shrinker: %s", err)
elog.Panicf("unable to register shrinker: %s", err)
}
sockname := backendSockname()
err = os.Remove(sockname)
if err != nil && !os.IsNotExist(err) {
log.Panicf("unable to unlink socket: %s", err)
elog.Panicf("unable to unlink socket: %s", err)
}
lis, err := net.Listen("unix", sockname)
if err != nil {
log.Panicf("unable to register shrinker: %s", err)
elog.Panicf("unable to register shrinker: %s", err)
}
err = setLimits()
if err != nil {
log.Printf("error setting backend limits: %s", err)
elog.Printf("error setting backend limits: %s", err)
}
for _, h := range backendhooks {
h()
@ -110,8 +109,8 @@ func runBackendServer() {
proc.Stderr = os.Stderr
err := proc.Start()
if err != nil {
log.Panicf("can't exec backend: %s", err)
elog.Panicf("can't exec backend: %s", err)
}
err = proc.Wait()
log.Printf("lost the backend: %s", err)
elog.Printf("lost the backend: %s", err)
}

@ -3,7 +3,6 @@ package main
import (
"database/sql"
"fmt"
"log"
"os"
"time"
@ -13,7 +12,7 @@ import (
func qordie(db *sql.DB, s string, args ...interface{}) *sql.Rows {
rows, err := db.Query(s, args...)
if err != nil {
log.Fatalf("can't query %s: %s", s, err)
elog.Fatalf("can't query %s: %s", s, err)
}
return rows
}
@ -21,31 +20,31 @@ func qordie(db *sql.DB, s string, args ...interface{}) *sql.Rows {
func scanordie(rows *sql.Rows, args ...interface{}) {
err := rows.Scan(args...)
if err != nil {
log.Fatalf("can't scan: %s", err)
elog.Fatalf("can't scan: %s", err)
}
}
func svalbard(dirname string) {
err := os.Mkdir(dirname, 0700)
if err != nil && !os.IsExist(err) {
log.Fatalf("can't create directory: %s", dirname)
elog.Fatalf("can't create directory: %s", dirname)
}
now := time.Now().Unix()
backupdbname := fmt.Sprintf("%s/honk-%d.db", dirname, now)
backup, err := sql.Open("sqlite3", backupdbname)
if err != nil {
log.Fatalf("can't open backup database")
elog.Fatalf("can't open backup database")
}
for _, line := range strings.Split(sqlSchema, ";") {
_, err = backup.Exec(line)
if err != nil {
log.Fatal(err)
elog.Fatal(err)
return
}
}
tx, err := backup.Begin()
if err != nil {
log.Fatal(err)
elog.Fatal(err)
}
orig := opendatabase()
rows := qordie(orig, "select userid, username, hash, displayname, about, pubkey, seckey, options from users")
@ -161,21 +160,21 @@ func svalbard(dirname string) {
err = tx.Commit()
if err != nil {
log.Fatalf("can't commit backp: %s", err)
elog.Fatalf("can't commit backp: %s", err)
}
backup.Close()
backupblobname := fmt.Sprintf("%s/blob-%d.db", dirname, now)
blob, err := sql.Open("sqlite3", backupblobname)
if err != nil {
log.Fatalf("can't open backup blob database")
elog.Fatalf("can't open backup blob database")
}
doordie(blob, "create table filedata (xid text, media text, hash text, content blob)")
doordie(blob, "create index idx_filexid on filedata(xid)")
doordie(blob, "create index idx_filehash on filedata(hash)")
tx, err = blob.Begin()
if err != nil {
log.Fatalf("can't start transaction: %s", err)
elog.Fatalf("can't start transaction: %s", err)
}
origblob := openblobdb()
for x := range filexids {
@ -191,7 +190,7 @@ func svalbard(dirname string) {
err = tx.Commit()
if err != nil {
log.Fatalf("can't commit blobs: %s", err)
elog.Fatalf("can't commit blobs: %s", err)
}
blob.Close()
}

@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"html/template"
"log"
"sort"
"strconv"
"strings"
@ -49,7 +48,7 @@ func userfromrow(row *sql.Row) (*WhatAbout, error) {
user.URL = fmt.Sprintf("https://%s/%s/%s", serverName, userSep, user.Name)
err = unjsonify(options, &user.Options)
if err != nil {
log.Printf("error processing user options: %s", err)
elog.Printf("error processing user options: %s", err)
}
} else {
user.URL = fmt.Sprintf("https://%s/%s", serverName, user.Name)
@ -88,7 +87,7 @@ func getserveruser() *WhatAbout {
var user *WhatAbout
ok := somenumberedusers.Get(serverUID, &user)
if !ok {
log.Panicf("lost server user")
elog.Panicf("lost server user")
}
return user
}
@ -107,7 +106,7 @@ var honkerinvalidator cache.Invalidator
func gethonkers(userid int64) []*Honker {
rows, err := stmtHonkers.Query(userid)
if err != nil {
log.Printf("error querying honkers: %s", err)
elog.Printf("error querying honkers: %s", err)
return nil
}
defer rows.Close()
@ -120,7 +119,7 @@ func gethonkers(userid int64) []*Honker {
err = unjsonify(meta, &h.Meta)
}
if err != nil {
log.Printf("error scanning honker: %s", err)
elog.Printf("error scanning honker: %s", err)
continue
}
h.Combos = strings.Split(strings.TrimSpace(combos), " ")
@ -141,7 +140,7 @@ func getnameddubs(userid int64, name string) []*Honker {
func dubsfromrows(rows *sql.Rows, err error) []*Honker {
if err != nil {
log.Printf("error querying dubs: %s", err)
elog.Printf("error querying dubs: %s", err)
return nil
}
defer rows.Close()
@ -150,7 +149,7 @@ func dubsfromrows(rows *sql.Rows, err error) []*Honker {
h := new(Honker)
err = rows.Scan(&h.ID, &h.UserID, &h.Name, &h.XID, &h.Flavor)
if err != nil {
log.Printf("error scanning honker: %s", err)
elog.Printf("error scanning honker: %s", err)
return nil
}
honkers = append(honkers, h)
@ -340,7 +339,7 @@ func reversehonks(honks []*Honk) {
func getsomehonks(rows *sql.Rows, err error) []*Honk {
if err != nil {
log.Printf("error querying honks: %s", err)
elog.Printf("error querying honks: %s", err)
return nil
}
defer rows.Close()
@ -367,7 +366,7 @@ func scanhonk(row RowLike) *Honk {
&dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Format, &h.Convoy, &h.Whofore, &h.Flags)
if err != nil {
if err != sql.ErrNoRows {
log.Printf("error scanning honk: %s", err)
elog.Printf("error scanning honk: %s", err)
}
return nil
}
@ -390,7 +389,7 @@ func donksforhonks(honks []*Honk) {
q := fmt.Sprintf("select honkid, donks.fileid, xid, name, description, url, media, local from donks join filemeta on donks.fileid = filemeta.fileid where honkid in (%s)", idset)
rows, err := db.Query(q)
if err != nil {
log.Printf("error querying donks: %s", err)
elog.Printf("error querying donks: %s", err)
return
}
defer rows.Close()
@ -399,7 +398,7 @@ func donksforhonks(honks []*Honk) {
d := new(Donk)
err = rows.Scan(&hid, &d.FileID, &d.XID, &d.Name, &d.Desc, &d.URL, &d.Media, &d.Local)
if err != nil {
log.Printf("error scanning donk: %s", err)
elog.Printf("error scanning donk: %s", err)
continue
}
d.External = !strings.HasPrefix(d.URL, serverPrefix)
@ -412,7 +411,7 @@ func donksforhonks(honks []*Honk) {
q = fmt.Sprintf("select honkid, ontology from onts where honkid in (%s)", idset)
rows, err = db.Query(q)
if err != nil {
log.Printf("error querying onts: %s", err)
elog.Printf("error querying onts: %s", err)
return
}
defer rows.Close()
@ -421,7 +420,7 @@ func donksforhonks(honks []*Honk) {
var o string
err = rows.Scan(&hid, &o)
if err != nil {
log.Printf("error scanning donk: %s", err)
elog.Printf("error scanning donk: %s", err)
continue
}
h := hmap[hid]
@ -433,7 +432,7 @@ func donksforhonks(honks []*Honk) {
q = fmt.Sprintf("select honkid, genus, json from honkmeta where honkid in (%s)", idset)
rows, err = db.Query(q)
if err != nil {
log.Printf("error querying honkmeta: %s", err)
elog.Printf("error querying honkmeta: %s", err)
return
}
defer rows.Close()
@ -442,7 +441,7 @@ func donksforhonks(honks []*Honk) {
var genus, j string
err = rows.Scan(&hid, &genus, &j)
if err != nil {
log.Printf("error scanning honkmeta: %s", err)
elog.Printf("error scanning honkmeta: %s", err)
continue
}
h := hmap[hid]
@ -451,7 +450,7 @@ func donksforhonks(honks []*Honk) {
p := new(Place)
err = unjsonify(j, p)
if err != nil {
log.Printf("error parsing place: %s", err)
elog.Printf("error parsing place: %s", err)
continue
}
h.Place = p
@ -459,25 +458,25 @@ func donksforhonks(honks []*Honk) {
t := new(Time)
err = unjsonify(j, t)
if err != nil {
log.Printf("error parsing time: %s", err)
elog.Printf("error parsing time: %s", err)
continue
}
h.Time = t
case "mentions":
err = unjsonify(j, &h.Mentions)
if err != nil {
log.Printf("error parsing mentions: %s", err)
elog.Printf("error parsing mentions: %s", err)
continue
}
case "badonks":
err = unjsonify(j, &h.Badonks)
if err != nil {
log.Printf("error parsing badonks: %s", err)
elog.Printf("error parsing badonks: %s", err)
continue
}
case "oldrev":
default:
log.Printf("unknown meta genus: %s", genus)
elog.Printf("unknown meta genus: %s", genus)
}
}
rows.Close()
@ -496,7 +495,7 @@ func donksforchonks(chonks []*Chonk) {
q := fmt.Sprintf("select chonkid, donks.fileid, xid, name, description, url, media, local from donks join filemeta on donks.fileid = filemeta.fileid where chonkid in (%s)", idset)
rows, err := db.Query(q)
if err != nil {
log.Printf("error querying donks: %s", err)
elog.Printf("error querying donks: %s", err)
return
}
defer rows.Close()
@ -505,7 +504,7 @@ func donksforchonks(chonks []*Chonk) {
d := new(Donk)
err = rows.Scan(&chid, &d.FileID, &d.XID, &d.Name, &d.Desc, &d.URL, &d.Media, &d.Local)
if err != nil {
log.Printf("error scanning donk: %s", err)
elog.Printf("error scanning donk: %s", err)
continue
}
ch := chmap[chid]
@ -547,7 +546,7 @@ func savefileandxid(name string, desc string, url string, media string, local bo
return 0, "", err
}
} else if err != nil {
log.Printf("error checking file hash: %s", err)
elog.Printf("error checking file hash: %s", err)
return 0, "", err
}
if url == "" {
@ -571,7 +570,7 @@ func finddonk(url string) *Donk {
return donk
}
if err != sql.ErrNoRows {
log.Printf("error finding file: %s", err)
elog.Printf("error finding file: %s", err)
}
return nil
}
@ -581,7 +580,7 @@ func savechonk(ch *Chonk) error {
db := opendatabase()
tx, err := db.Begin()
if err != nil {
log.Printf("can't begin tx: %s", err)
elog.Printf("can't begin tx: %s", err)
return err
}
@ -591,7 +590,7 @@ func savechonk(ch *Chonk) error {
for _, d := range ch.Donks {
_, err := tx.Stmt(stmtSaveDonk).Exec(-1, ch.ID, d.FileID)
if err != nil {
log.Printf("error saving donk: %s", err)
elog.Printf("error saving donk: %s", err)
break
}
}
@ -616,7 +615,7 @@ func chatplusone(tx *sql.Tx, userid int64) {
_, err = tx.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error plussing chat: %s", err)
elog.Printf("error plussing chat: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
@ -636,7 +635,7 @@ func chatnewnone(userid int64) {
_, err = db.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error noneing chat: %s", err)
elog.Printf("error noneing chat: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
@ -655,7 +654,7 @@ func meplusone(tx *sql.Tx, userid int64) {
_, err = tx.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error plussing me: %s", err)
elog.Printf("error plussing me: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
@ -675,7 +674,7 @@ func menewnone(userid int64) {
_, err = db.Exec("update users set options = ? where username = ?", j, user.Name)
}
if err != nil {
log.Printf("error noneing me: %s", err)
elog.Printf("error noneing me: %s", err)
}
somenamedusers.Clear(user.Name)
somenumberedusers.Clear(user.ID)
@ -685,7 +684,7 @@ func loadchatter(userid int64) []*Chatter {
duedt := time.Now().Add(-3 * 24 * time.Hour).UTC().Format(dbtimeformat)
rows, err := stmtLoadChonks.Query(userid, duedt)
if err != nil {
log.Printf("error loading chonks: %s", err)
elog.Printf("error loading chonks: %s", err)
return nil
}
defer rows.Close()
@ -696,7 +695,7 @@ func loadchatter(userid int64) []*Chatter {
var dt string
err = rows.Scan(&ch.ID, &ch.UserID, &ch.XID, &ch.Who, &ch.Target, &dt, &ch.Noise, &ch.Format)
if err != nil {
log.Printf("error scanning chonk: %s", err)
elog.Printf("error scanning chonk: %s", err)
continue
}
ch.Date, _ = time.Parse(dbtimeformat, dt)
@ -707,14 +706,14 @@ func loadchatter(userid int64) []*Chatter {
rows.Close()
rows, err = stmtGetChatters.Query(userid)
if err != nil {
log.Printf("error getting chatters: %s", err)
elog.Printf("error getting chatters: %s", err)
return nil
}
for rows.Next() {
var target string
err = rows.Scan(&target)
if err != nil {
log.Printf("error scanning chatter: %s", target)
elog.Printf("error scanning chatter: %s", target)
continue
}
if _, ok := chonks[target]; !ok {
@ -750,7 +749,7 @@ func savehonk(h *Honk) error {
db := opendatabase()
tx, err := db.Begin()
if err != nil {
log.Printf("can't begin tx: %s", err)
elog.Printf("can't begin tx: %s", err)
return err
}
@ -770,7 +769,7 @@ func savehonk(h *Honk) error {
tx.Rollback()
}
if err != nil {
log.Printf("error saving honk: %s", err)
elog.Printf("error saving honk: %s", err)
}
honkhonkline()
return err
@ -784,7 +783,7 @@ func updatehonk(h *Honk) error {
db := opendatabase()
tx, err := db.Begin()
if err != nil {
log.Printf("can't begin tx: %s", err)
elog.Printf("can't begin tx: %s", err)
return err
}
@ -802,7 +801,7 @@ func updatehonk(h *Honk) error {
_, err = tx.Stmt(stmtSaveMeta).Exec(old.ID, "oldrev", j)
}
if err != nil {
log.Printf("error saving oldrev: %s", err)
elog.Printf("error saving oldrev: %s", err)
}
}
if err == nil {
@ -811,7 +810,7 @@ func updatehonk(h *Honk) error {
tx.Rollback()
}
if err != nil {
log.Printf("error updating honk %d: %s", h.ID, err)
elog.Printf("error updating honk %d: %s", h.ID, err)
}
return err
}
@ -820,7 +819,7 @@ func deletehonk(honkid int64) error {
db := opendatabase()
tx, err := db.Begin()
if err != nil {
log.Printf("can't begin tx: %s", err)
elog.Printf("can't begin tx: %s", err)
return err
}
@ -834,7 +833,7 @@ func deletehonk(honkid int64) error {
tx.Rollback()
}
if err != nil {
log.Printf("error deleting honk %d: %s", honkid, err)
elog.Printf("error deleting honk %d: %s", honkid, err)
}
return err
}
@ -843,14 +842,14 @@ func saveextras(tx *sql.Tx, h *Honk) error {
for _, d := range h.Donks {
_, err := tx.Stmt(stmtSaveDonk).Exec(h.ID, -1, d.FileID)
if err != nil {
log.Printf("error saving donk: %s", err)
elog.Printf("error saving donk: %s", err)
return err
}
}
for _, o := range h.Onts {
_, err := tx.Stmt(stmtSaveOnt).Exec(strings.ToLower(o), h.ID)
if err != nil {
log.Printf("error saving ont: %s", err)
elog.Printf("error saving ont: %s", err)
return err
}
}
@ -860,7 +859,7 @@ func saveextras(tx *sql.Tx, h *Honk) error {
_, err = tx.Stmt(stmtSaveMeta).Exec(h.ID, "place", j)
}
if err != nil {
log.Printf("error saving place: %s", err)
elog.Printf("error saving place: %s", err)
return err
}
}
@ -870,7 +869,7 @@ func saveextras(tx *sql.Tx, h *Honk) error {
_, err = tx.Stmt(stmtSaveMeta).Exec(h.ID, "time", j)
}
if err != nil {
log.Printf("error saving time: %s", err)
elog.Printf("error saving time: %s", err)
return err
}
}
@ -880,7 +879,7 @@ func saveextras(tx *sql.Tx, h *Honk) error {
_, err = tx.Stmt(stmtSaveMeta).Exec(h.ID, "mentions", j)
}
if err != nil {
log.Printf("error saving mentions: %s", err)
elog.Printf("error saving mentions: %s", err)
return err
}
}
@ -970,13 +969,13 @@ func cleanupdb(arg string) {
blobdb := openblobdb()
rows, err := blobdb.Query("select xid from filedata")
if err != nil {
log.Fatal(err)
elog.Fatal(err)
}
for rows.Next() {
var xid string
err = rows.Scan(&xid)
if err != nil {
log.Fatal(err)
elog.Fatal(err)
}
filexids[xid] = true
}
@ -986,24 +985,24 @@ func cle