Skip to content

Commit

Permalink
[chore] ensure consistent caller name fetching regardless of compiler…
Browse files Browse the repository at this point in the history
… inlining (#3323)

* move logging levels into log package itself

* ensure inconsistent inlining doesn't mess with log calling function name

* remove unused global variable

* fix log level
  • Loading branch information
NyaaaWhatsUpDoc authored Sep 20, 2024
1 parent 747c251 commit 77b095a
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 205 deletions.
3 changes: 1 addition & 2 deletions cmd/process-emoji/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os/signal"
"syscall"

"codeberg.org/gruf/go-logger/v2/level"
"codeberg.org/gruf/go-storage/memory"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
Expand All @@ -41,7 +40,7 @@ func main() {
ctx, cncl := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
defer cncl()

log.SetLevel(level.INFO)
log.SetLevel(log.INFO)

if len(os.Args) != 3 {
log.Panic(ctx, "Usage: go run ./cmd/process-emoji <input-file> <output-static>")
Expand Down
3 changes: 1 addition & 2 deletions cmd/process-media/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os/signal"
"syscall"

"codeberg.org/gruf/go-logger/v2/level"
"codeberg.org/gruf/go-storage/memory"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
Expand All @@ -40,7 +39,7 @@ func main() {
ctx, cncl := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
defer cncl()

log.SetLevel(level.INFO)
log.SetLevel(log.INFO)

if len(os.Args) != 4 {
log.Panic(ctx, "Usage: go run ./cmd/process-media <input-file> <output-processed> <output-thumbnail>")
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
codeberg.org/gruf/go-iotools v0.0.0-20240710125620-934ae9c654cf
codeberg.org/gruf/go-kv v1.6.4
codeberg.org/gruf/go-list v0.0.0-20240425093752-494db03d641f
codeberg.org/gruf/go-logger/v2 v2.2.1
codeberg.org/gruf/go-mempool v0.0.0-20240507125005-cef10d64a760
codeberg.org/gruf/go-mimetypes v1.2.0
codeberg.org/gruf/go-mutexes v1.5.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ codeberg.org/gruf/go-kv v1.6.4 h1:3NZiW8HVdBM3kpOiLb7XfRiihnzZWMAixdCznguhILk=
codeberg.org/gruf/go-kv v1.6.4/go.mod h1:O/YkSvKiS9XsRolM3rqCd9YJmND7dAXu9z+PrlYO4bc=
codeberg.org/gruf/go-list v0.0.0-20240425093752-494db03d641f h1:Ss6Z+vygy+jOGhj96d/GwsYYDd22QmIcH74zM7/nQkw=
codeberg.org/gruf/go-list v0.0.0-20240425093752-494db03d641f/go.mod h1:F9pl4h34iuVN7kucKam9fLwsItTc+9mmaKt7pNXRd/4=
codeberg.org/gruf/go-logger/v2 v2.2.1 h1:RP2u059EQKTBFV3cN8X6xDxNk2RkzqdgXGKflKqB7Oc=
codeberg.org/gruf/go-logger/v2 v2.2.1/go.mod h1:m/vBfG5jNUmYXI8Hg9aVSk7Pn8YgEBITQB/B/CzdRss=
codeberg.org/gruf/go-loosy v0.0.0-20231007123304-bb910d1ab5c4 h1:IXwfoU7f2whT6+JKIKskNl/hBlmWmnF1vZd84Eb3cyA=
codeberg.org/gruf/go-loosy v0.0.0-20231007123304-bb910d1ab5c4/go.mod h1:fiO8HE1wjZCephcYmRRsVnNI/i0+mhy44Z5dQalS0rM=
codeberg.org/gruf/go-mangler v1.4.1 h1:Dv58jFfy9On49L11ji6tpADUknwoJA46iaiZvnNXecs=
Expand Down
3 changes: 1 addition & 2 deletions internal/db/bundb/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"codeberg.org/gruf/go-kv"
"codeberg.org/gruf/go-logger/v2/level"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func (queryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {

// On trace, we log query information,
// manually crafting so DB query not escaped.
case log.Level() >= level.TRACE:
case log.Level() >= log.TRACE:
log.Printf("level=TRACE duration=%s query=%s", dur, event.Query)
}
}
12 changes: 7 additions & 5 deletions internal/log/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
)

// Caller fetches the calling function name, skipping 'depth'.
//
//go:noinline
func Caller(depth int) string {
var pcs [1]uintptr
pcs := make([]uintptr, 1)

// Fetch calling function using calldepth
_ = runtime.Callers(depth, pcs[:])
// Fetch calling func using depth.
_ = runtime.Callers(depth, pcs)
fn := runtime.FuncForPC(pcs[0])

if fn == nil {
Expand All @@ -37,14 +39,14 @@ func Caller(depth int) string {
// Get func name.
name := fn.Name()

// Drop all but the package name and function name, no mod path
// Drop all but package and function name, no path.
if idx := strings.LastIndex(name, "/"); idx >= 0 {
name = name[idx+1:]
}

const params = `[...]`

// Drop any generic type parameter markers
// Drop any function generic type parameter markers.
if idx := strings.Index(name, params); idx >= 0 {
name = name[:idx] + name[idx+len(params):]
}
Expand Down
93 changes: 67 additions & 26 deletions internal/log/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,104 +20,145 @@ package log
import (
"context"
"fmt"
"syscall"

"codeberg.org/gruf/go-kv"
"codeberg.org/gruf/go-logger/v2/level"
)

type Entry struct {
ctx context.Context
kvs []kv.Field
}

// WithContext updates Entry{} value context.
func (e Entry) WithContext(ctx context.Context) Entry {
e.ctx = ctx
return e
}

// WithField appends key-value field to Entry{}.
func (e Entry) WithField(key string, value interface{}) Entry {
e.kvs = append(e.kvs, kv.Field{K: key, V: value})
return e
}

// WithFields appends key-value fields to Entry{}.
func (e Entry) WithFields(kvs ...kv.Field) Entry {
e.kvs = append(e.kvs, kvs...)
return e
}

// Trace will log formatted args as 'msg' field to the log at TRACE level.
//
//go:noinline
func (e Entry) Trace(a ...interface{}) {
logf(e.ctx, 3, level.TRACE, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, TRACE, e.kvs, args(len(a)), a...)
}

// Tracef will log format string as 'msg' field to the log at TRACE level.
//
//go:noinline
func (e Entry) Tracef(s string, a ...interface{}) {
logf(e.ctx, 3, level.TRACE, e.kvs, s, a...)
logf(e.ctx, 3, TRACE, e.kvs, s, a...)
}

// Debug will log formatted args as 'msg' field to the log at DEBUG level.
//
//go:noinline
func (e Entry) Debug(a ...interface{}) {
logf(e.ctx, 3, level.DEBUG, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, DEBUG, e.kvs, args(len(a)), a...)
}

// Debugf will log format string as 'msg' field to the log at DEBUG level.
//
//go:noinline
func (e Entry) Debugf(s string, a ...interface{}) {
logf(e.ctx, 3, level.DEBUG, e.kvs, s, a...)
logf(e.ctx, 3, DEBUG, e.kvs, s, a...)
}

// Info will log formatted args as 'msg' field to the log at INFO level.
//
//go:noinline
func (e Entry) Info(a ...interface{}) {
logf(e.ctx, 3, level.INFO, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, INFO, e.kvs, args(len(a)), a...)
}

// Infof will log format string as 'msg' field to the log at INFO level.
//
//go:noinline
func (e Entry) Infof(s string, a ...interface{}) {
logf(e.ctx, 3, level.INFO, e.kvs, s, a...)
logf(e.ctx, 3, INFO, e.kvs, s, a...)
}

// Warn will log formatted args as 'msg' field to the log at WARN level.
//
//go:noinline
func (e Entry) Warn(a ...interface{}) {
logf(e.ctx, 3, level.WARN, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, WARN, e.kvs, args(len(a)), a...)
}

// Warnf will log format string as 'msg' field to the log at WARN level.
//
//go:noinline
func (e Entry) Warnf(s string, a ...interface{}) {
logf(e.ctx, 3, level.WARN, e.kvs, s, a...)
logf(e.ctx, 3, WARN, e.kvs, s, a...)
}

// Error will log formatted args as 'msg' field to the log at ERROR level.
//
//go:noinline
func (e Entry) Error(a ...interface{}) {
logf(e.ctx, 3, level.ERROR, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, ERROR, e.kvs, args(len(a)), a...)
}

// Errorf will log format string as 'msg' field to the log at ERROR level.
//
//go:noinline
func (e Entry) Errorf(s string, a ...interface{}) {
logf(e.ctx, 3, level.ERROR, e.kvs, s, a...)
}

func (e Entry) Fatal(a ...interface{}) {
defer syscall.Exit(1)
logf(e.ctx, 3, level.FATAL, e.kvs, args(len(a)), a...)
}

func (e Entry) Fatalf(s string, a ...interface{}) {
defer syscall.Exit(1)
logf(e.ctx, 3, level.FATAL, e.kvs, s, a...)
logf(e.ctx, 3, ERROR, e.kvs, s, a...)
}

// Panic will log formatted args as 'msg' field to the log at PANIC level.
// This will then call panic causing the application to crash.
//
//go:noinline
func (e Entry) Panic(a ...interface{}) {
defer panic(fmt.Sprint(a...))
logf(e.ctx, 3, level.PANIC, e.kvs, args(len(a)), a...)
logf(e.ctx, 3, PANIC, e.kvs, args(len(a)), a...)
}

// Panicf will log format string as 'msg' field to the log at PANIC level.
// This will then call panic causing the application to crash.
//
//go:noinline
func (e Entry) Panicf(s string, a ...interface{}) {
defer panic(fmt.Sprintf(s, a...))
logf(e.ctx, 3, level.PANIC, e.kvs, s, a...)
logf(e.ctx, 3, PANIC, e.kvs, s, a...)
}

func (e Entry) Log(lvl level.LEVEL, a ...interface{}) {
// Log will log formatted args as 'msg' field to the log at given level.
//
//go:noinline
func (e Entry) Log(lvl LEVEL, a ...interface{}) {
logf(e.ctx, 3, lvl, e.kvs, args(len(a)), a...)
}

func (e Entry) Logf(lvl level.LEVEL, s string, a ...interface{}) {
// Logf will log format string as 'msg' field to the log at given level.
//
//go:noinline
func (e Entry) Logf(lvl LEVEL, s string, a ...interface{}) {
logf(e.ctx, 3, lvl, e.kvs, s, a...)
}

// Print will log formatted args to the stdout log output.
//
//go:noinline
func (e Entry) Print(a ...interface{}) {
printf(3, e.kvs, args(len(a)), a...)
}

// Printf will log format string to the stdout log output.
//
//go:noinline
func (e Entry) Printf(s string, a ...interface{}) {
printf(3, e.kvs, s, a...)
}
18 changes: 8 additions & 10 deletions internal/log/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ import (
"fmt"
"log/syslog"
"strings"

"codeberg.org/gruf/go-logger/v2/level"
)

// ParseLevel will parse the log level from given string and set to appropriate level.
// ParseLevel will parse the log level from given string and set to appropriate LEVEL.
func ParseLevel(str string) error {
switch strings.ToLower(str) {
case "trace":
SetLevel(level.TRACE)
SetLevel(TRACE)
case "debug":
SetLevel(level.DEBUG)
SetLevel(DEBUG)
case "", "info":
SetLevel(level.INFO)
SetLevel(INFO)
case "warn":
SetLevel(level.WARN)
SetLevel(WARN)
case "error":
SetLevel(level.ERROR)
case "fatal":
SetLevel(level.FATAL)
SetLevel(ERROR)
case "fatal", "panic":
SetLevel(PANIC)
default:
return fmt.Errorf("unknown log level: %q", str)
}
Expand Down
38 changes: 38 additions & 0 deletions internal/log/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// GoToSocial
// Copyright (C) GoToSocial Authors [email protected]
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package log

// LEVEL defines a level of logging.
type LEVEL uint8

// Default levels of logging.
const (
UNSET LEVEL = 0
PANIC LEVEL = 1
ERROR LEVEL = 100
WARN LEVEL = 150
INFO LEVEL = 200
DEBUG LEVEL = 250
TRACE LEVEL = 254
ALL LEVEL = ^LEVEL(0)
)

// CanLog returns whether an incoming log of 'lvl' can be logged against receiving level.
func (loglvl LEVEL) CanLog(lvl LEVEL) bool {
return loglvl > lvl
}
Loading

0 comments on commit 77b095a

Please sign in to comment.