diff --git a/fedilogue/config.go b/fedilogue/config.go index 78537c9..53d5538 100644 --- a/fedilogue/config.go +++ b/fedilogue/config.go @@ -57,15 +57,15 @@ func stringexists(needle string, haystack []string) bool { func getSettings() { c, err := ioutil.ReadFile("config.jsonc") if err != nil { - logFatal.Fatal("Unable to open config.jsonc, exiting: ", err) + logFatal("Unable to open config.jsonc, exiting: ", err) } jsoncbin := jsonc.ToJSON(c) // Calling jsonc.ToJSON() to convert JSONC to JSON if jsonc.Valid(jsoncbin) == false { - logFatal.Fatal("Invalid jsonc, exiting.") + logFatal("Invalid jsonc, exiting.") } err = json.Unmarshal(jsoncbin, &settings) if err != nil { - logFatal.Fatal("Unable to parse config.jsonc, exiting: ", err) + logFatal("Unable to parse config.jsonc, exiting: ", err) } } diff --git a/fedilogue/db.go b/fedilogue/db.go index b8e8b38..21f03ec 100644 --- a/fedilogue/db.go +++ b/fedilogue/db.go @@ -1,8 +1,9 @@ package main import ( - "os" "context" + "os" + "github.com/jackc/pgx/v4/pgxpool" ) @@ -13,7 +14,7 @@ func getDbPool() *pgxpool.Pool { dburl := os.Getenv("DATABASE_URL") pool, err := pgxpool.Connect(context.Background(), dburl) if err != nil { - logFatal.Fatal("Unable to connect to database:", err) + logFatal("Unable to connect to database:", err) } return pool } diff --git a/fedilogue/fedilogue.go b/fedilogue/fedilogue.go index 18ef088..4b31cc7 100644 --- a/fedilogue/fedilogue.go +++ b/fedilogue/fedilogue.go @@ -19,7 +19,7 @@ var ri_mutex = &sync.Mutex{} func startpprof() { logInfo("Starting http/pprof on :7777") - logFatal.Fatal(http.ListenAndServe("127.0.0.1:7777", nil)) + logFatal(http.ListenAndServe("127.0.0.1:7777", nil)) } func statusReportHandler() { diff --git a/fedilogue/instance.go b/fedilogue/instance.go index 572883a..abe2ca5 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -54,7 +54,7 @@ func BuildClient(endpoint string) http.Client { proxyuri := fmt.Sprintf("socks5://%s:%d", settings.Proxies[ridx].Host, settings.Proxies[ridx].Port) proxy, err := url.Parse(proxyuri) if err != nil { - logFatal.Fatal("Error: ", err) + logFatal("Error: ", err) } tr.Proxy = http.ProxyURL(proxy) } @@ -175,7 +175,7 @@ func LogInstance(endpoint string, o shared.RunningInstance) bool { _, err = pool.Exec(context.Background(), "INSERT INTO instances (endpoint, state, software) VALUES($1, $2, $3)", endpoint, "", o.Software) if err != nil { - logWarn("Error inserting %s into `instances`: "+endpoint, err) + logWarn("Error inserting ", endpoint+" into `instances`: ", err) return true } diff --git a/fedilogue/log.go b/fedilogue/log.go index e99e0d0..19eac15 100644 --- a/fedilogue/log.go +++ b/fedilogue/log.go @@ -6,49 +6,53 @@ import ( ) var ( - _logInfo *log.Logger + _logInfo *log.Logger _logErr *log.Logger _logWarn *log.Logger _logConn *log.Logger _logDebug *log.Logger - logFatal *log.Logger + _logFatal *log.Logger ) func logInit() { - _logInfo = log.New(os.Stdout, "INFO: ", log.Lmsgprefix|log.Ldate|log.Ltime) - _logErr = log.New(os.Stdout, "ERR: ", log.Lmsgprefix|log.Ldate|log.Ltime) - _logWarn = log.New(os.Stdout, "WARN: ", log.Lmsgprefix|log.Ldate|log.Ltime) - _logConn = log.New(os.Stdout, "CONN: ", log.Lmsgprefix|log.Ldate|log.Ltime) + _logInfo = log.New(os.Stdout, "INFO: ", log.Lmsgprefix|log.Ldate|log.Ltime) + _logErr = log.New(os.Stdout, "ERR: ", log.Lmsgprefix|log.Ldate|log.Ltime) + _logWarn = log.New(os.Stdout, "WARN: ", log.Lmsgprefix|log.Ldate|log.Ltime) + _logConn = log.New(os.Stdout, "CONN: ", log.Lmsgprefix|log.Ldate|log.Ltime) _logDebug = log.New(os.Stdout, "DEBUG: ", log.Lmsgprefix|log.Ldate|log.Ltime) - logFatal = log.New(os.Stdout, "FATAL: ", log.Lmsgprefix|log.Ldate|log.Ltime) + _logFatal = log.New(os.Stdout, "FATAL: ", log.Lmsgprefix|log.Ldate|log.Ltime) } func logInfo(s ...interface{}) { - if (settings.LogLevel >= 1) { + if settings.LogLevel >= 1 { _logInfo.Print(s...) } } func logErr(s ...interface{}) { - if (settings.LogLevel >= 2) { + if settings.LogLevel >= 2 { _logErr.Print(s...) } } func logWarn(s ...interface{}) { - if (settings.LogLevel >= 3) { + if settings.LogLevel >= 3 { _logWarn.Print(s...) } } func logConn(s ...interface{}) { - if (settings.LogLevel >= 4) { + if settings.LogLevel >= 4 { _logConn.Print(s...) } } func logDebug(s ...interface{}) { - if (settings.LogLevel >= 5) { - _logConn.Print(s...) + if settings.LogLevel >= 5 { + _logDebug.Print(s...) } } + +func logFatal(s ...interface{}) { + _logFatal.Fatal(s...) +} diff --git a/fedilogue/poll.go b/fedilogue/poll.go index 18a585b..57a7750 100644 --- a/fedilogue/poll.go +++ b/fedilogue/poll.go @@ -94,7 +94,7 @@ func PollMastodonPleroma(endpoint string, o *shared.RunningInstance) { req, err := http.NewRequest("GET", api_timeline, nil) req.Header.Set("User-Agent", "Tusky") if err != nil { - logFatal.Fatal("Unable to create new request for "+endpoint+": ", err) + logFatal("Unable to create new request for "+endpoint+": ", err) return } diff --git a/fedilogue/retrieve.go b/fedilogue/retrieve.go index 25c24c8..152feeb 100644 --- a/fedilogue/retrieve.go +++ b/fedilogue/retrieve.go @@ -162,7 +162,7 @@ func check_activity(uri string) { } _, err = pool.Exec(context.Background(), "INSERT INTO activities (document, normalized, instance, hashtags, bot) VALUES($1, $2, $3, $4, $5)", jsondocument, activityjson.normalized, activityjson.instance, hashtags, activityjson.bot) if err != nil { - logWarn("Error inserting %s into `activities`: "+uri, err) + logWarn("Error inserting ", uri, " into `activities`: ", err) return } @@ -200,6 +200,7 @@ func check_actor(uri string) *ActorJson { o.Recentactors.Mu.Lock() i, cachedactorjson := o.Recentactors.Contains(uri) if i != -1 { + o.Recentactors.Mu.Unlock() cachedactorjson := cachedactorjson.(*ActorJson) return cachedactorjson } @@ -208,7 +209,6 @@ func check_actor(uri string) *ActorJson { selectRet := pool.QueryRow(context.Background(), "SELECT document FROM actors WHERE document->>'id' = $1", uri) err := selectRet.Scan(&actorjson) if err == nil { - logWarn(actorjson) return actorjson // Actor already in database, good! } @@ -255,7 +255,7 @@ func check_actor(uri string) *ActorJson { _, err = pool.Exec(context.Background(), "INSERT INTO actors (document, instance, bot) VALUES($1, $2, $3)", jsondocument, actorjson.instance, bot) if err != nil { - logWarn("Error inserting %s into `actors`: "+uri, err) + logWarn("Error inserting ", uri, " into `actors`: ", err) return nil // Unable to insert actor } diff --git a/fedilogue/stream.go b/fedilogue/stream.go index 0e258bb..d44fc3d 100644 --- a/fedilogue/stream.go +++ b/fedilogue/stream.go @@ -22,7 +22,7 @@ func StreamMastodon(endpoint string, o *shared.RunningInstance) { req, err := http.NewRequest("GET", api_timeline, nil) req.Header.Set("User-Agent", "Tusky") if err != nil { - logFatal.Fatal("Unable to create new request for " + endpoint + ", exiting.") + logFatal("Unable to create new request for " + endpoint + ", exiting.") return }